summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
blob: 9406f85f7779655b00403ccd0da6f348badc97c4 (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
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')

    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()

    # 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