Dalam mengembangkan aplikasi dengan Odoo ERP kita mungkin membutuhkan validasi yang cukup komplek. Untuk validasi yang cukup komplek maka kita memerlukan Python Constraint.

Python Contraint pada pengembangan aplikasi Odoo ERP adalah seperti pada penjelasan-penjelasan berikut ini

  1. Update model

    Kita masuk ke Virtualmin dari Webmin, menuju menu File Manager dan mengedit bagian awal model.

  2. Restart Odoo

  3. Upgrade modul

  4. Mencoba program

    Kita kemudian mengedit suatu data dan kemudian Save manually seperti pada gambar di atas.


    Tampak ada warning yang menunjukkan validasi yang kita buat pada Python berjalan dengan baik.
  5. Source code  estate_property.py
    from odoo import api, fields, models
    from odoo.exceptions import UserError, ValidationError
    from odoo.tools import float_compare, float_is_zero
    
    class EstateProperty(models.Model):
    	_name = "estate.property"
    	_description = "Real Estate Property"
    
    	_sql_constraints = [
    		("check_expected_price", "CHECK(expected_price > 0)", "The expected price must be strictly positive"),
    		("check_selling_price", "CHECK(selling_price >= 0)", "The offer price must be positive"),
    	]
    
    	active = fields.Boolean(default=True)
    	state = fields.Selection(
        	[ 
            	('n', 'New'),
            	('o', 'Offer Received'),
            	('a', 'Offer Accepted'),
            	('s', 'Sold'),('c', 'Cancelled'),
            ],
        	'State', default='n'
        )
    
    	name = fields.Char(required=True,default='Rumah Baru')
    	description = fields.Text()
    
    	property_type_id = fields.Many2one("estate.property.type", string="Property Type")
    	tags_id = fields.Many2many("estate.property.tag", string="Property Tags")
    
    	postcode = fields.Char()
    	date_availability = fields.Date(copy=False)
    	expected_price = fields.Float()
    	selling_price = fields.Float(readonly=True,copy=False)
    	bedrooms = fields.Integer(default=2)
    	living_area = fields.Integer()
    	facades = fields.Integer()
    	garage = fields.Boolean()
    	garden = fields.Boolean()
    	garden_area = fields.Integer()
    	garden_orientation = fields.Selection(
        	[ 
            	('n', 'North'),
            	('s', 'South'),
            	('e', 'East'),
            	('w', 'West'),
            ],
        	'Garden Orientation', default='e'
    	)
    
    	user_id = fields.Many2one("res.users", string="Salesman",default=lambda self: self.env.user)
    	buyer_id = fields.Many2one("res.partner", string="Buyer",copy=False)
    
    	offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offer")
    
    	best_price = fields.Float("Best Offer", compute="_compute_best_price", help="Best offer received")
    
    	@api.depends("offer_ids.price")
    	def _compute_best_price(self):
    		for prop in self:
    			prop.best_price = max(prop.offer_ids.mapped("price")) if prop.offer_ids else 0.0
    
    	@api.onchange("garden")
    	def _onchange_garden(self):
    		if self.garden:
    			self.garden_area = 10
    			self.garden_orientation = "n"
    		else:
    			self.garden_area = 0
    			self.garden_orientation = False
    
    	def action_sold(self):
    		if "c" in self.mapped("state"):
    			raise UserError("Canceled properties cannot be sold.")
    		return self.write({"state": "s"})
    
    	def action_cancel(self):
    		if "s" in self.mapped("state"):
    			raise UserError("Sold properties cannot be canceled.")
    		return self.write({"state": "c"})
    
    	@api.constrains("expected_price", "selling_price")
    	def _check_price_difference(self):
    		for prop in self:
    			if (
    				not float_is_zero(prop.selling_price, precision_rounding=0.01)
    				and float_compare(prop.selling_price, prop.expected_price * 90.0 / 100.0, precision_rounding=0.01) < 0
    			):
    				raise ValidationError(
    					"The selling price must be at least 90% of the expected price! "
    					+ "You must reduce the expected price if you want to accept this offer."
    				)
    

Informasi lebih lanjut silahkan menghubungi
1. https://www.odoo.com/documentation/16.0/developer/howtos/rdtraining/11_constraints.html .
2. https://github.com/odoo/technical-training-solutions/tree/16.0-core/estate .

Kunjungi www.proweb.co.id untuk menambah wawasan anda.

Jika anda tertarik mengenai aplikasi Odoo ERP silahkan mengisi form di bawah ini

Python Constraint pada Pengembangan Aplikasi Odoo ERP
× Ada yang dapat saya bantu ? Available on SundayMondayTuesdayWednesdayThursdayFridaySaturday