11# Part of Odoo. See LICENSE file for full copyright and licensing details.
22
33from odoo import api , fields , models
4+ from odoo .exceptions import ValidationError
5+ from odoo .tools .float_utils import float_compare
46from dateutil .relativedelta import relativedelta
57
68
@@ -54,6 +56,19 @@ class Estate(models.Model):
5456 total_area = fields .Float (compute = "_compute_total_area" )
5557 best_price = fields .Float (compute = "_compute_best_price" )
5658
59+ _sql_constraints = [
60+ (
61+ "check_expected_price" ,
62+ "CHECK(expected_price > 0)" ,
63+ "Expected price must be positive." ,
64+ ),
65+ (
66+ "check_selling_price" ,
67+ "CHECK(selling_price > -1)" ,
68+ "Selling price must be positive." ,
69+ ),
70+ ]
71+
5772 @api .depends ("garden_area" , "living_area" )
5873 def _compute_total_area (self ):
5974 for record in self :
@@ -62,6 +77,9 @@ def _compute_total_area(self):
6277 @api .depends ("property_offer_ids" )
6378 def _compute_best_price (self ):
6479 for record in self :
80+ if not record .property_offer_ids :
81+ record .best_price = 0
82+ continue
6583 record .best_price = max (record .mapped ("property_offer_ids.price" ))
6684
6785 @api .onchange ("garden" )
@@ -70,8 +88,8 @@ def _onchange_garden(self):
7088 self .garden_area = 10
7189 self .garden_orientation = "north"
7290 else :
73- self .garden_area = 0 # Or None if you prefer
74- self .garden_orientation = False # Or None if you prefer
91+ self .garden_area = 0
92+ self .garden_orientation = False
7593
7694 def action_sell_property (self ):
7795 for record in self :
@@ -80,3 +98,14 @@ def action_sell_property(self):
8098 def action_cancel_property (self ):
8199 for record in self :
82100 record .state = "cancelled"
101+
102+ @api .constrains ("selling_price" )
103+ def _check_selling_price (self ):
104+ for record in self :
105+ if not record .selling_price :
106+ continue
107+
108+ if float_compare (record .selling_price , 0.9 * record .expected_price , precision_digits = 2 ) == - 1 :
109+ raise ValidationError (
110+ "The selling price can not be less than 90 percent of the expected price"
111+ )
0 commit comments