1
1
from dateutil .relativedelta import relativedelta
2
2
from odoo import api , fields , models
3
- from odoo .exceptions import UserError
3
+ from odoo .exceptions import UserError , ValidationError
4
+ from odoo .tools .float_utils import float_compare , float_is_zero
4
5
5
6
6
7
class EstateProperty (models .Model ):
7
8
_name = "estate.property"
8
9
_description = "Real Estate"
10
+ _sql_constraints = [
11
+ ('check_expected_price' , 'CHECK(expected_price > 0)' , 'Expected price must be strictly positve' ),
12
+ ('check_selling_price' , 'CHECK(selling_price >= 0)' , 'Selling price must be positive' )
13
+ ]
9
14
10
15
name = fields .Char ('Name' , required = True )
11
16
description = fields .Char ('Description' )
@@ -41,7 +46,8 @@ class EstateProperty(models.Model):
41
46
tag_ids = fields .Many2many ('estate.property.tag' , string = 'Tags' )
42
47
offer_ids = fields .One2many ('estate.property.offer' , 'property_id' , string = 'Offers' )
43
48
44
- @api .depends ('living_area' , 'garden_area' )
49
+
50
+ @api .depends ('living_area' ,'garden_area' )
45
51
def _compute_total_area (self ):
46
52
for record in self :
47
53
record .total_area = record .living_area + record .garden_area
@@ -57,12 +63,20 @@ def _onchange_garden_checkbox(self):
57
63
self .garden_area = 10 if self .garden else 0
58
64
self .garden_orientation = 'north' if self .garden else None
59
65
66
+ @api .constrains ('selling_price' ,'expected_price' )
67
+ def _check_selling_price (self ):
68
+ for record in self :
69
+ if float_is_zero (record .selling_price , precision_digits = 2 ):
70
+ continue
71
+
72
+ if float_compare (record .selling_price , record .expected_price * 0.9 , precision_digits = 2 ) < 0 :
73
+ raise ValidationError ("Selling price cannot be lower than 90% of the expected price." )
74
+
60
75
def action_set_sold (self ):
61
76
for record in self :
62
77
if (record .state == 'sold' ):
63
78
raise UserError ('Property Already Sold' )
64
79
if record .state != 'canceled' :
65
- record .active = False
66
80
record .state = 'sold'
67
81
else :
68
82
raise UserError ("Cannot sell a canceled property" )
@@ -74,6 +88,5 @@ def action_set_cancel(self):
74
88
raise UserError ('Cannot cancel a sold property' )
75
89
if (record .state == 'canceled' ):
76
90
raise UserError ("Property Already canceled" )
77
- record .active = False
78
91
record .state = 'canceled'
79
92
return True
0 commit comments