diff options
Diffstat (limited to 'fixco_custom/models/sale_pricelist.py')
| -rw-r--r-- | fixco_custom/models/sale_pricelist.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/fixco_custom/models/sale_pricelist.py b/fixco_custom/models/sale_pricelist.py new file mode 100644 index 0000000..6af81fe --- /dev/null +++ b/fixco_custom/models/sale_pricelist.py @@ -0,0 +1,31 @@ +from odoo import models, fields, api, _ +from odoo.exceptions import ValidationError + +class SalePricelist(models.Model): + _name = 'sale.pricelist' + _rec_name = 'product_id' + _inherit = ['mail.thread', 'mail.activity.mixin'] + + name = fields.Char(string='Name', compute="_compute_name") + product_id = fields.Many2one('product.product', string="Product", required=True) + price = fields.Float(string='Price', required=True) + + _sql_constraints = [ + ('product_unique', 'unique(product_id)', 'Product sudah ada dalam daftar harga! Pilih product lain.'), + ] + + @api.depends('product_id') + def _compute_name(self): + for record in self: + record.name = record.product_id.display_name + + @api.constrains('product_id') + def _check_product_duplicate(self): + for record in self: + existing = self.search([ + ('product_id', '=', record.product_id.id), + ('id', '!=', record.id) + ], limit=1) + if existing: + raise ValidationError(_('Product %s sudah ada dalam daftar harga (ID: %s).') % + (record.product_id.display_name, existing.id))
\ No newline at end of file |
