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') taxes_system_id = fields.Many2one('account.tax', string='Taxes System', readonly=True) include_price = fields.Float(string='Include 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') 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') # def _contrains_include_price(self): # taxes = self.taxes_system_id or self.taxes_product_id # tax_include = taxes.price_include # price_unit = self.system_price or self.product_price # 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 @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)