summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
blob: c7da0e248fa82741f81d304230b8a4160f2f0394 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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')
    price_vendor = fields.Float(string='Price Vendor', compute='compute_price_vendor')
    so_line_id = fields.Many2one('sale.order.line', string='ID SO Line')
    indent = fields.Boolean(string='Indent', help='centang ini jika barang indent')
    is_ltc = fields.Boolean(string='Sudah di LTC', default=False, help='centang ini jika barang sudah di LTC')
    note = fields.Char(string='Note')
    qty_reserved = fields.Float(string='Qty Reserved', compute='_compute_qty_reserved')
    delete_line = fields.Boolean(string='Delete', default=False, help='centang ini jika anda ingin menghapus line ini')

    def _compute_qty_reserved(self):
        for line in self:
            sale_line = self.env['sale.order.line'].search([
                ('product_id', '=', line.product_id.id),
                ('order_id', '=', line.order_id.sale_order_id.id)
            ])

            reserved_qty = sum(line.qty_reserved for line in sale_line)
            line.qty_reserved = reserved_qty
    
    def suggest_purchasing(self):
        for line in self: 
            if line.product_id.qty_available_bandengan + line.qty_reserved < line.product_qty:
                line.suggest = 'harus beli'
            else:
                line.suggest = 'masih cukup'

    def compute_price_vendor(self):
        for line in self:
            purchase_pricelist = self.env['purchase.pricelist'].search([
                ('product_id', '=', line.product_id.id),
                ('vendor_id', '=', line.order_id.partner_id.id)
            ], limit=1)

            if purchase_pricelist:
                price_vendor = format(purchase_pricelist.product_price, ".2f")
                price_vendor = float(price_vendor)
                line.price_vendor = price_vendor
            else:
                line.price_vendor = 0

    def _compute_qty_stock(self):
        for line in self:
            line.qty_available = line.product_id.qty_available_bandengan
            line.qty_onhand = line.product_id.qty_onhand_bandengan
            line.qty_incoming = line.product_id.qty_incoming_bandengan
            line.qty_outgoing = line.product_id.qty_outgoing_bandengan

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

    @api.onchange('product_id','product_qty', 'product_uom')
    def _onchange_quantity(self):
        res = super(PurchaseOrderLine, self)._onchange_quantity()
        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

        price_unit, taxes = self._get_valid_purchase_price(purchase_pricelist)

        self.price_unit = price_unit
        if purchase_pricelist.taxes_product_id or purchase_pricelist.taxes_system_id:
            self.taxes_id = taxes

        return res
    
    def _get_valid_purchase_price(self, purchase_price):
        p_price = 0
        taxes = False

        if purchase_price.system_price > 0 and purchase_price.product_price > 0:
            if purchase_price.human_last_update > purchase_price.system_last_update:
                p_price = purchase_price.product_price  
                taxes = purchase_price.taxes_product_id  
            else:
                p_price = purchase_price.system_price
                taxes = purchase_price.taxes_system_id
        elif purchase_price.system_price > 0 and purchase_price.product_price == 0:
            p_price = purchase_price.system_price
            taxes = purchase_price.taxes_system_id
        elif purchase_price.system_price == 0 and purchase_price.product_price > 0:
            p_price = purchase_price.product_price
            taxes = purchase_price.taxes_product_id

        return p_price, taxes

    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