11from odoo import models , fields , api
22from dateutil .relativedelta import relativedelta
3- from odoo .exceptions import UserError
3+ from odoo .exceptions import UserError , ValidationError
4+ from odoo .tools .float_utils import float_compare
45
56
67class EstateProperty (models .Model ):
@@ -22,8 +23,8 @@ class EstateProperty(models.Model):
2223 garden_orientation = fields .Selection (
2324 string = "Garden Orientation" ,
2425 selection = [('north' , 'North' ),
25- ('south' , 'South' ),
26- ('east' , 'East' ),
26+ ('south' , 'South' ),
27+ ('east' , 'East' ),
2728 ('west' , 'West' )
2829 ]
2930 )
@@ -42,14 +43,14 @@ class EstateProperty(models.Model):
4243 salesperson = fields .Many2one ("res.users" , default = lambda self : self .env .user )
4344 tag_ids = fields .Many2many ("estate.property.tag" )
4445 offer_ids = fields .One2many ("estate.property.offer" , "property_id" , string = "Offers" )
45-
4646 total_area = fields .Float (compute = "_compute_total_area" )
47+ best_offer = fields .Float (compute = "_compute_best_offer" )
48+
4749 @api .depends ('living_area' , 'garden_area' )
4850 def _compute_total_area (self ):
4951 for record in self :
5052 record .total_area = record .living_area + record .garden_area
5153
52- best_offer = fields .Float (compute = "_compute_best_offer" )
5354 @api .depends ('offer_ids.price' )
5455 def _compute_best_offer (self ):
5556 for record in self :
@@ -79,5 +80,16 @@ def sold_property(self):
7980 if record .state == 'cancelled' :
8081 raise UserError ("cancelled property cannot be sold." )
8182 else :
82- record .state = 'sold'
83-
83+ record .state = 'sold'
84+
85+ _sql_constraints = [
86+ ('check_expected_price' , 'CHECK(expected_price>=0)' , 'expected price must be positive' ),
87+ ('check_selling_price' , 'CHECK(selling_price>=0)' , 'selling price must be positive' )
88+ ]
89+
90+ @api .constrains ('selling_price' , 'expected_price' , 'state' )
91+ def _check_selling_price (self ):
92+ for record in self :
93+ if float_compare (record .selling_price , 0.9 * record .expected_price , precision_digits = 2 ) < 0 and (record .state == 'sold' or record .state == 'offer_accepted' ):
94+ raise ValidationError ("the selling price is lower" )
95+
0 commit comments