22
33from odoo import models , fields , api
44from odoo .exceptions import UserError , ValidationError
5- from odoo .tools .float_utils import float_compare
5+ from odoo .tools .float_utils import float_compare , float_is_zero
66
77
88class EstateProperty (models .Model ):
@@ -14,7 +14,7 @@ class EstateProperty(models.Model):
1414 description = fields .Text ()
1515 postcode = fields .Char ()
1616 date_availability = fields .Date (copy = False , default = fields .Date .today () + relativedelta (months = 3 ))
17- expected_price = fields .Float (required = True )
17+ expected_price = fields .Float (required = True , default = 0 )
1818 selling_price = fields .Float (readonly = True , copy = False )
1919 bedrooms = fields .Integer (default = 2 )
2020 living_area = fields .Float (string = "Living Area (sqm)" )
@@ -24,7 +24,8 @@ class EstateProperty(models.Model):
2424 garden_area = fields .Float (string = "Garden Area (sqm)" )
2525 garden_orientation = fields .Selection (
2626 string = "Garden Orientation" ,
27- selection = [('north' , 'North' ),
27+ selection = [
28+ ('north' , 'North' ),
2829 ('south' , 'South' ),
2930 ('east' , 'East' ),
3031 ('west' , 'West' )
@@ -41,9 +42,9 @@ class EstateProperty(models.Model):
4142 ],
4243 default = "new"
4344 )
44- type_id = fields .Many2one ("estate.property.type" )
45- buyer = fields .Many2one ("res.partner" , copy = False )
46- salesperson = fields .Many2one ("res.users" , default = lambda self : self .env .user )
45+ property_type_id = fields .Many2one ("estate.property.type" )
46+ buyer_id = fields .Many2one ("res.partner" , copy = False )
47+ salesperson_id = fields .Many2one ("res.users" , default = lambda self : self .env .user )
4748 tag_ids = fields .Many2many ("estate.property.tag" )
4849 offer_ids = fields .One2many ("estate.property.offer" , "property_id" , string = "Offers" )
4950 total_area = fields .Float (compute = "_compute_total_area" )
@@ -85,22 +86,17 @@ def sold_property(self):
8586 else :
8687 record .state = 'sold'
8788
88- _check_expected_price = models .Constraint (
89- 'CHECK(expected_price >= 0)' ,
90- 'expected price must be positive'
91- )
92-
93- _check_selling_price = models .Constraint (
94- 'CHECK(selling_price >= 0)' ,
95- 'selling price must be positive'
89+ _check_price = models .Constraint (
90+ 'CHECK(selling_price >= 0 AND expected_price>=0)' ,
91+ 'selling price and expected_price must be positive'
9692 )
9793
9894 @api .constrains ('selling_price' , 'expected_price' , 'state' )
9995 def _check_selling_price (self ):
10096 for record in self :
101- if record .state in [ 'sold' , 'offer_accepted' ] :
97+ if not float_is_zero ( record .selling_price , precision_digits = 2 ) :
10298 if float_compare (record .selling_price , 0.9 * record .expected_price , precision_digits = 2 ) < 0 :
103- raise ValidationError ("the selling price is lower" )
99+ raise ValidationError ("the selling price lower" )
104100
105101 @api .ondelete (at_uninstall = True )
106102 def _unlink_check_state_of_property (self ):
0 commit comments