11from odoo import models , fields , api
22from datetime import timedelta
3+ from odoo .exceptions import ValidationError
34
45
56class EstateProperty (models .Model ):
@@ -39,11 +40,9 @@ class EstateProperty(models.Model):
3940 salesperson_id = fields .Many2one ("res.users" , string = "Salesperson" )
4041 tag_ids = fields .Many2many ("estate.property.tag" , string = "Tags" )
4142 offer_ids = fields .One2many ("estate.property.offer" , "property_id" , string = "Offers" )
42-
4343 total_area = fields .Float (compute = "_compute_total_area" , string = "Total Area" , store = True )
4444 best_price = fields .Float (compute = "_compute_best_price" , string = "Best Offer" , store = True )
45-
46- validity = fields .Integer (default = 7 )
45+ validity_days = fields .Integer (default = 7 )
4746 date_deadline = fields .Date (
4847 compute = "_compute_date_deadline" ,
4948 inverse = "_inverse_date_deadline" ,
@@ -60,21 +59,21 @@ def _compute_best_price(self):
6059 for record in self :
6160 record .best_price = max (record .offer_ids .mapped ("price" )) if record .offer_ids else 0.0
6261
63- @api .depends ("create_date" , "validity " )
62+ @api .depends ("create_date" , "validity_days " )
6463 def _compute_date_deadline (self ):
6564 for record in self :
6665 create_date = record .create_date or fields .Date .today ()
6766 if hasattr (create_date , "date" ):
6867 create_date = create_date .date ()
69- record .date_deadline = create_date + timedelta (days = record .validity )
68+ record .date_deadline = create_date + timedelta (days = record .validity_days )
7069
7170 def _inverse_date_deadline (self ):
7271 for record in self :
7372 create_date = record .create_date or fields .Date .today ()
7473 if hasattr (create_date , "date" ):
7574 create_date = create_date .date ()
7675 delta = (record .date_deadline - create_date ).days if record .date_deadline else 0
77- record .validity = delta
76+ record .validity_days = delta
7877
7978 @api .onchange ("garden" )
8079 def _onchange_garden (self ):
@@ -106,3 +105,23 @@ def action_set_next_status(self):
106105 def action_back_to_new (self ):
107106 for record in self :
108107 record .state = "new"
108+
109+ @api .constrains ("selling_price" ,"expected_price" )
110+ def _check_selling_price_ratio (self ):
111+ for record in self :
112+ if record .selling_price <= 0 :
113+ raise ValidationError ("Selling price must be greater than or equal to 0" )
114+ if record .selling_price < 0.9 * record .expected_price :
115+ raise ValidationError ("Selling price must be at least 90% of the expected price" )
116+ if record .expected_price < 0 :
117+ raise ValidationError ("Expected price must be greater than 0" )
118+
119+ _check_expected_price = models .Constraint (
120+ 'CHECK(expected_price < 0)' ,
121+ 'The expected price of a property must be strictly positive.'
122+ )
123+
124+ _check_selling_price = models .Constraint (
125+ 'CHECK(selling_price < 0)' ,
126+ 'The selling price of a property must be positive.'
127+ )
0 commit comments