1- from odoo import models , fields , api , exceptions
1+ from odoo import models , fields , api , exceptions , _
22from odoo .tools .float_utils import float_compare
33
44
55class EstateProperty (models .Model ):
66 _name = 'estate_property'
77 _description = 'Estate Property details'
8+ _order = 'id desc'
89
910 name = fields .Char (required = True )
1011 description = fields .Text ()
@@ -23,11 +24,22 @@ class EstateProperty(models.Model):
2324 garden_area = fields .Integer ()
2425 garden_orientation = fields .Selection (
2526 string = 'Garden Orientation' ,
26- selection = [('north' , 'North' ), ('south' , 'South' ), ('east' , 'East' ), ('west' , 'West' )]
27+ selection = [
28+ ('north' , 'North' ),
29+ ('south' , 'South' ),
30+ ('east' , 'East' ),
31+ ('west' , 'West' )
32+ ]
2733 )
2834 state = fields .Selection (
2935 string = 'Status' ,
30- selection = [('new' , 'New' ), ('offer_received' , 'Offer Received' ), ('offer_accepted' , 'Offer Accepted' ), ('sold' , 'Sold' ), ('cancelled' , 'Cancelled' )],
36+ selection = [
37+ ('new' , 'New' ),
38+ ('offer_received' , 'Offer Received' ),
39+ ('offer_accepted' , 'Offer Accepted' ),
40+ ('sold' , 'Sold' ),
41+ ('cancelled' , 'Cancelled' )
42+ ],
3143 default = 'new' ,
3244 required = True ,
3345 copy = False
@@ -41,6 +53,24 @@ class EstateProperty(models.Model):
4153 total_area = fields .Integer (compute = '_compute_total_area' )
4254 best_price = fields .Float (compute = '_compute_best_price' )
4355
56+ _check_expected_price = models .Constraint (
57+ 'CHECK(expected_price > 0)' ,
58+ 'The Expected price cannot be 0 or less then 0'
59+ )
60+
61+ _check_selling_price = models .Constraint (
62+ 'CHECK(selling_price >= 0)' ,
63+ 'The Selling price cannot be less then 0'
64+ )
65+
66+ @api .constrains ('selling_price' , 'buyer' , 'expected_price' )
67+ def _check_selling_price_90p (self ):
68+ for record in self :
69+ if record .selling_price == 0 :
70+ return False
71+ if float_compare ((record .selling_price / record .expected_price ) * 100 , 90 , precision_digits = 2 ) < 0 :
72+ raise exceptions .ValidationError (_ ('Selling Price should be 90% or more of expected price.' ))
73+
4474 @api .depends ('living_area' , 'garden_area' )
4575 def _compute_total_area (self ):
4676 for record in self :
@@ -60,36 +90,16 @@ def _onchange_garden(self):
6090 self .garden_orientation = None
6191 self .garden_area = 0
6292
63- def cancel_property_sale (self ):
64- for record in self :
65- if record .state == 'sold' :
66- raise exceptions .UserError ('A sold property cannot be cancelled' )
67- else :
68- record .state = 'cancelled'
69- return True
93+ def action_cancel_property (self ):
94+ if self .filtered (lambda x : x .state == 'sold' ):
95+ raise exceptions .UserError (_ ('A sold property cannot be cancelled' ))
7096
71- def set_property_sold (self ):
72- for record in self :
73- if record .state == 'cancelled' :
74- raise exceptions .UserError ('A cancelled property cannot be Sold' )
75- else :
76- record .state = 'sold'
97+ self .state = 'cancelled'
7798 return True
7899
79- _check_expected_price = models .Constraint (
80- 'CHECK(expected_price > 0)' ,
81- 'The Expected price cannot be 0 or less then 0'
82- )
83-
84- _check_selling_price = models .Constraint (
85- 'CHECK(selling_price >= 0)' ,
86- 'The Selling price cannot be less then 0'
87- )
100+ def action_property_sold (self ):
101+ if self .filtered (lambda x : x .state == 'cancelled' ):
102+ raise exceptions .UserError (_ ('A cancelled property cannot be sold' ))
88103
89- @api .constrains ('selling_price' , 'buyer' , 'expected_price' )
90- def _check_selling_price_90p (self ):
91- for record in self :
92- if record .selling_price == 0 :
93- return False
94- if float_compare ((record .selling_price / record .expected_price ) * 100 , 90 , precision_digits = 2 ) < 0 :
95- raise exceptions .ValidationError ('Selling Price should be 90% or more of expected price.' )
104+ self .state = 'sold'
105+ return True
0 commit comments