1- from odoo import models , fields , api
21from dateutil .relativedelta import relativedelta
2+
3+ from odoo import models , fields , api
34from odoo .exceptions import UserError , ValidationError
45from odoo .tools .float_utils import float_compare
56
67
78class EstateProperty (models .Model ):
89 _name = 'estate.property'
910 _description = "Real Estate Property"
11+ _order = "id desc"
1012
1113 name = fields .Char (required = True )
1214 description = fields .Text ()
@@ -45,7 +47,7 @@ class EstateProperty(models.Model):
4547 offer_ids = fields .One2many ("estate.property.offer" , "property_id" , string = "Offers" )
4648 total_area = fields .Float (compute = "_compute_total_area" )
4749 best_offer = fields .Float (compute = "_compute_best_offer" )
48-
50+
4951 @api .depends ('living_area' , 'garden_area' )
5052 def _compute_total_area (self ):
5153 for record in self :
@@ -58,7 +60,7 @@ def _compute_best_offer(self):
5860 record .best_offer = 0
5961 else :
6062 record .best_offer = max (record .mapped ('offer_ids.price' ))
61-
63+
6264 @api .onchange ('garden' )
6365 def _onchange_garden (self ):
6466 if self .garden :
@@ -80,16 +82,21 @@ def sold_property(self):
8082 if record .state == 'cancelled' :
8183 raise UserError ("cancelled property cannot be sold." )
8284 else :
83- record .state = 'sold'
85+ record .state = 'sold'
86+
87+ _check_expected_price = models .Constraint (
88+ 'CHECK(expected_price >= 0)' ,
89+ 'expected price must be positive'
90+ )
8491
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- ]
92+ _check_selling_price = models . Constraint (
93+ ' CHECK(selling_price >= 0)' ,
94+ ' selling price must be positive'
95+ )
8996
90- @api .constrains ('selling_price' , 'expected_price' , 'state' )
97+ @api .constrains ('selling_price' , 'expected_price' , 'state' )
9198 def _check_selling_price (self ):
9299 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-
100+ if record .state in [ 'sold' , 'offer_accepted' ] :
101+ if float_compare ( record . selling_price , 0.9 * record . expected_price , precision_digits = 2 ) < 0 :
102+ raise ValidationError ( "the selling price is lower" )
0 commit comments