55
66class EstateProperty (models .Model ):
77 _name = 'estate.property'
8- _description = 'All properties '
8+ _description = 'Estate Properties '
99 _order = 'id desc'
1010
1111 name = fields .Char (required = True )
@@ -69,37 +69,36 @@ class EstateProperty(models.Model):
6969 'The selling price of a property should be positive.' ,
7070 )
7171
72+ @api .depends ('offer_ids' )
73+ def _compute_best_price (self ):
74+ for record in self :
75+ record .best_price = max (record .offer_ids .mapped ('price' ), default = 0.0 )
76+
7277 @api .depends ('living_area' , 'garden_area' )
7378 def _compute_total_area (self ):
7479 for record in self :
7580 living_area = record .living_area or 0
7681 garden_area = record .garden_area or 0
7782 record .total_area = living_area + garden_area
7883
79- @api .depends ( 'offer_ids ' )
80- def _compute_best_price (self ):
84+ @api .constrains ( 'selling_price' , 'expected_price ' )
85+ def _check_selling_price (self ):
8186 for record in self :
82- offers = record .offer_ids or []
83- record .best_price = max (offers .mapped ('price' )) if len (offers ) else 0
87+ if (
88+ not float_is_zero (record .selling_price , 2 )
89+ and float_compare (record .selling_price , record .expected_price * 0.90 , 2 ) < 0
90+ ):
91+ raise ValidationError ("Selling price cannot be lower than 90% of expected price" )
8492
8593 @api .onchange ('garden' )
8694 def _onchange_garden (self ):
8795 if self .garden :
8896 self .garden_area = 10
8997 self .garden_orientation = 'north'
9098 else :
91- self .garden_area = None
99+ self .garden_area = 0
92100 self .garden_orientation = None
93101
94- @api .constrains ('selling_price' , 'expected_price' )
95- def _check_selling_price (self ):
96- for record in self :
97- if (
98- not float_is_zero (record .selling_price , 2 )
99- and float_compare (record .selling_price , record .expected_price * 0.90 , 2 ) < 0
100- ):
101- raise ValidationError ("Selling price cannot be lower than 90% of expected price" )
102-
103102 @api .ondelete (at_uninstall = False )
104103 def _unlink_if_new_or_cancelled (self ):
105104 for record in self :
@@ -109,13 +108,13 @@ def _unlink_if_new_or_cancelled(self):
109108 def action_mark_as_sold (self ):
110109 for record in self :
111110 if record .state == 'cancelled' :
112- raise UserError ('Property is already cancelled' )
111+ raise UserError ('Cannot sell cancelled properties ' )
113112
114113 record .state = 'sold'
115114
116115 def action_mark_as_cancelled (self ):
117116 for record in self :
118117 if record .state == 'sold' :
119- raise UserError ('Property is already sold' )
118+ raise UserError ('Cannot cancel sold properties ' )
120119
121120 record .state = 'cancelled'
0 commit comments