1
1
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2
2
3
3
from odoo import api , fields , models
4
+ from odoo .exceptions import ValidationError
5
+ from odoo .tools .float_utils import float_compare
4
6
from dateutil .relativedelta import relativedelta
5
7
6
8
@@ -54,6 +56,19 @@ class Estate(models.Model):
54
56
total_area = fields .Float (compute = "_compute_total_area" )
55
57
best_price = fields .Float (compute = "_compute_best_price" )
56
58
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
+
57
72
@api .depends ("garden_area" , "living_area" )
58
73
def _compute_total_area (self ):
59
74
for record in self :
@@ -62,6 +77,9 @@ def _compute_total_area(self):
62
77
@api .depends ("property_offer_ids" )
63
78
def _compute_best_price (self ):
64
79
for record in self :
80
+ if not record .property_offer_ids :
81
+ record .best_price = 0
82
+ continue
65
83
record .best_price = max (record .mapped ("property_offer_ids.price" ))
66
84
67
85
@api .onchange ("garden" )
@@ -70,8 +88,8 @@ def _onchange_garden(self):
70
88
self .garden_area = 10
71
89
self .garden_orientation = "north"
72
90
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
75
93
76
94
def action_sell_property (self ):
77
95
for record in self :
@@ -80,3 +98,14 @@ def action_sell_property(self):
80
98
def action_cancel_property (self ):
81
99
for record in self :
82
100
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