summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
blob: 4965b507d6917278bab3e1ba17ea58f310fa316b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from odoo import fields, models, api, _
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT


class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'
    item_margin = fields.Float(
        'Margin', compute='compute_item_margin',
        help="Total Margin in Sales Order Header")
    item_percent_margin = fields.Float(
        'Margin%', compute='compute_item_margin',
        help="Total % Margin in Sales Order Header")
    so_item_margin = fields.Float(
        'SO Margin', compute='compute_item_margin',
        help="Total Margin in Sales Order Header")
    so_item_percent_margin = fields.Float(
        'SO Margin%', compute='compute_item_margin',
        help="Total % Margin in Sales Order Header")

    # Override method from addons/purchase/models/purchase.py
    @api.onchange('product_qty', 'product_uom')
    def _onchange_quantity(self):
        res = super(PurchaseOrderLine, self)._onchange_quantity()
        # Custom script
        purchase_pricelist = self.env['purchase.pricelist'].search([
            ('product_id', '=', self.product_id.id),
            ('vendor_id', '=', self.partner_id.id)
        ], limit=1)
        price_unit = purchase_pricelist.product_price

        if not price_unit:
            product_supplierinfo = self.env['product.supplierinfo'].search([
                ('product_tmpl_id', '=', self.product_id.product_tmpl_id.id),
                ('name', '=', self.partner_id.id)
            ], limit=1)
            price_unit = product_supplierinfo.price

        self.price_unit = price_unit
        return res

    def compute_item_margin(self):
        sum_so_margin = sum_sales_price = sum_margin = 0
        for line in self:
            if not line.product_id or line.product_id.type == 'service' or not self.order_id.sale_order_id:
                line.so_item_margin = 0
                line.so_item_percent_margin = 0
                line.item_margin = 0
                line.item_percent_margin = 0
                continue
            sale_order_line = self.env['sale.order.line'].search(
                        [('product_id', '=', line.product_id.id),
                         ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
            # calculate margin so and real margin
            # margin so compare est purchase price vs sales price
            # real margin compare purchase price vs sales price
            line.so_item_margin = sale_order_line.item_margin
            line.so_item_percent_margin = sale_order_line.item_percent_margin
            sum_so_margin += sale_order_line.item_margin

            sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
            # minus with delivery if covered by indoteknik
            if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
                sales_price -= round((sale_order_line.order_id.delivery_amt / sale_order_line.order_id.count_line_product), 2)
            sum_sales_price += sales_price

            purchase_price = line.price_subtotal
            if line.order_id.delivery_amount > 0:
                purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
            # add with delivery amount in sales price or purchase price if any
            real_item_margin = sales_price - purchase_price
            real_item_percent_margin = round((real_item_margin/sales_price), 2) * 100
            line.item_margin = real_item_margin
            line.item_percent_margin = real_item_percent_margin
            sum_margin += real_item_margin

        if sum_so_margin > 0 and sum_sales_price > 0 and sum_margin > 0:
            self.order_id.total_so_margin = sum_so_margin
            self.order_id.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100
            self.order_id.total_margin = sum_margin
            self.order_id.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100