summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_pricelist.py
blob: 5d077e0484f5aa9bbd20135d412dca8627641b96 (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
from odoo import fields, models, api, _
from odoo.exceptions import AccessError, UserError, ValidationError
from datetime import datetime, timedelta
from pytz import timezone

class PurchasePricelist(models.Model):
    _name = 'purchase.pricelist'
    _rec_name = 'product_id'

    name = fields.Char(string='Name', compute="_compute_name")
    product_id = fields.Many2one('product.product', string="Product", required=True)
    vendor_id = fields.Many2one('res.partner', string="Vendor", required=True)
    product_price = fields.Float(string='Human Price', required=True)
    system_price = fields.Float(string='System Price', required=True)
    human_last_update = fields.Datetime(string='Human Update')
    system_last_update = fields.Datetime(string='System Update')

    @api.depends('product_id', 'vendor_id')
    def _compute_name(self):
        self.name = self.vendor_id.name + ', ' + self.product_id.name

    @api.constrains('product_price','system_price','vendor_id','product_id')
    def _contrains_product_price(self):
        current_time = fields.Datetime.now(timezone('Asia/Jakarta')).strftime('%Y-%m-%d %H:%M:%S')
        update_by = self._context.get('update_by')
        if update_by == 'system':
            self.system_last_update = current_time
        else:
            self.human_last_update = current_time

    @api.constrains('vendor_id', 'product_id','human_last_update','write_date')
    def _check_duplicate_purchase_pricelist(self):
        for price in self:
            if price.write_date == price.create_date:
                domain = [
                    ('product_id', '=', price.product_id.id),
                    ('vendor_id', '=', price.vendor_id.id)
                    ]
                
                domain.append(('id', '!=', price.id))
                massage="Product dan vendor yang anda gunakan sudah ada di purchase pricelist" 
                existing_purchase = self.search(domain, limit=1)
                if existing_purchase:
                    raise UserError(massage)
            else:
                domain = [
                    ('id', '=', price.id)
                    ]
                massage="Tidak Dapat Merubah Product"
                existing_purchase = self.search(domain, limit=1)
                if existing_purchase:
                    raise UserError(massage)