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
|
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))
|