summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
blob: f255095fb6187aedb01a5e7aa57020d1963d4ebf (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from odoo import fields, models, api, _
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
import logging

_logger = logging.getLogger(__name__)


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")
    delivery_amt_line = fields.Float('DeliveryAmtLine', compute='compute_delivery_amt_line')
    line_no = fields.Integer('No', default=0)
    qty_available = fields.Float('Qty Available', compute='compute_qty_stock')
    qty_onhand = fields.Float('Qty On Hand', compute='compute_qty_stock')
    qty_incoming = fields.Float('Qty Incoming', compute='compute_qty_stock')
    qty_outgoing = fields.Float('Qty Outgoing', compute='compute_qty_stock')
    qty_available_store = fields.Float(string='Available')
    suggest = fields.Char(string='Suggest')

    def compute_qty_stock(self):
        for line in self:
            line.qty_available = line.product_id.virtual_available
            line.qty_onhand = line.product_id.qty_available
            line.qty_incoming = line.product_id.incoming_qty
            line.qty_outgoing = line.product_id.outgoing_qty

    @api.onchange('product_id')
    def _onchange_product_custom(self):
        self.compute_qty_stock()

    # 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')
            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
            if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
                sales_price -= sale_order_line.delivery_amt_line
            if sale_order_line.order_id.fee_third_party > 0:
                sales_price -= sale_order_line.fee_third_party_line
            sum_sales_price += sales_price
            purchase_price = line.price_subtotal
            if line.order_id.delivery_amount > 0:
                purchase_price += line.delivery_amt_line
            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

    def compute_delivery_amt_line(self):
        for line in self:
            if line.product_id.type == 'product':
                contribution = round((line.price_total / line.order_id.amount_total_without_service), 2)
                delivery_amt = line.order_id.delivery_amount
                line.delivery_amt_line = delivery_amt * contribution
            else:
                line.delivery_amt_line = 0