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', readonly=True) human_last_update = fields.Datetime(string='Human Update') system_last_update = fields.Datetime(string='System Update') count_trx_po = fields.Integer(string='Count Trx Product') count_trx_po_vendor = fields.Integer(string='Count Trx Vendor') taxes_product_id = fields.Many2one('account.tax', string='Taxes Human', domain=[('type_tax_use', '=', 'purchase')]) taxes_system_id = fields.Many2one('account.tax', string='Taxes System') include_price = fields.Float(string='Final Price', readonly=True) @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','taxes_system_id','taxes_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('system_last_update','system_price','product_price','human_last_update','taxes_system_id','taxes_product_id') def _contrains_include_price(self): price_unit, taxes = self._get_valid_price() if price_unit == 0: self.include_price = 0 return tax_include = taxes.price_include if taxes: if tax_include: price_unit = price_unit else: price_unit = price_unit + (price_unit * 11 / 100) else: price_unit = price_unit + (price_unit * 11 / 100) self.include_price = price_unit def _get_valid_price(self): purchase_price = self price = 0 taxes = None human_last_update = purchase_price.human_last_update or datetime.min system_last_update = purchase_price.system_last_update or datetime.min if system_last_update > human_last_update: price = purchase_price.system_price taxes = purchase_price.taxes_system_id else: price = purchase_price.product_price taxes = purchase_price.taxes_product_id return price, taxes @api.constrains('vendor_id', 'product_id') def _check_duplicate_purchase_pricelist(self): for price in self: domain = [ ('product_id', '=', price.product_id.id), ('vendor_id', '=', price.vendor_id.id) ] domain.append(('id', '!=', price.id)) existing_purchase = self.search(domain, limit=1) massage="Ada duplikat product dan vendor, berikut data yang anda duplikat : \n" + str(existing_purchase.product_id.name) + " - " + str(existing_purchase.vendor_id.name) + " - " + str(existing_purchase.product_price) if existing_purchase: raise UserError(massage)