summaryrefslogtreecommitdiff
path: root/fixco_custom/models/purchase_order_line.py
blob: bdd729c7ccafd2790161d3b45f3fbdbd29ec7b6e (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
from odoo import models, fields, api, _
from odoo.exceptions import UserError

class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'
    
    automatic_purchase_line_id = fields.Many2one(
        'automatic.purchase.line',
        string='Automatic Purchase Line Reference',
        ondelete='set null',
        index=True
    )
    discount = fields.Float(
        string='Discount (%)',
        digits='Discount',  
        default=0.0
    )
    altama_ordered = fields.Float(
        string='Altama Ordered',  
        default=0.0,
        copy=False
    )
    altama_delivered = fields.Float(
        string='Altama Delivered', 
        default=0.0,
        copy=False
    )
    altama_invoiced = fields.Float(
        string='Altama Invoiced',
        default=0.0,
        copy=False
    )
    discount_amount = fields.Float(
        string='Discount Amount',
        compute='_compute_discount_amount'
    )
    original_price_unit = fields.Float(string='Original Unit Price', readonly=True)
    original_price_subtotal = fields.Float(string='Original Subtotal', readonly=True)
    description = fields.Text(string='Description', readonly=True, copy=False)
    docstatus_altama = fields.Text(string='Status Altama', readonly=True, copy=False)
    bill_remain = fields.Float(string='Bill Remain', readonly=True, copy=False, tracking=True, compute='_compute_bill_remain')

    @api.depends('qty_invoiced', 'product_qty')
    def _compute_bill_remain(self):
        for line in self:
            line.bill_remain = line.product_qty - line.qty_invoiced
            

    @api.constrains('product_id', 'price_unit', 'product_qty')
    def _store_original_price(self):
        for line in self:
            # Simpen harga awal cuma sekali
            if not line.original_price_unit or line.price_unit != line.original_price_unit:
                line.original_price_unit = line.price_unit
            line.original_price_subtotal = line.original_price_unit * line.product_qty

    # @api.constrains('product_qty', 'product_id')
    # def constrains_product_qty(self):
    #     for line in self:
    #         if line.product_id.check_multiple_qty(line.product_qty) == True:
    #             raise UserError(f'Qty Product {line.product_id.display_name} tidak sesuai dengan kelipatan {line.product_id.qty_multiple}')
        

    @api.depends('price_unit', 'product_qty', 'discount')
    def _compute_discount_amount(self):
        for line in self:
            discount = line.discount or 0.0
            line.discount_amount = (line.price_unit * line.product_qty * discount) / 100.0

    def _prepare_compute_all_values(self):
        res = super(PurchaseOrderLine, self)._prepare_compute_all_values()
        price_unit = res['price_unit'] * (1 - (self.discount or 0.0) / 100.0)
        res.update({
            'price_unit': price_unit,
        })
        return res
    
    @api.depends('product_qty', 'price_unit', 'taxes_id', 'discount')
    def _compute_amount(self):
        return super(PurchaseOrderLine, self)._compute_amount()
    
    def write(self, values):
        res = super().write(values)
        if 'discount' in values:
            self._compute_amount()
        return res