@@ -33,32 +33,18 @@ class EstateProperty(models.Model):
3333 ('east' , 'East' ), ('west' , 'West' )])
3434 active = fields .Boolean (default = True )
3535 state = fields .Selection (
36- copy = False ,
37- readonly = True ,
38- default = 'new' ,
39- string = 'States ' ,
40- selection = [
41- ('new' , 'New' ),
42- ('offer_received' , 'Offer Received' ),
43- ('offer_accepted' , 'Offer Accepted' ),
44- ('sold' , 'Sold' ),
45- ('cancelled' , 'Cancelled' )]
36+ copy = False ,
37+ readonly = True ,
38+ default = 'new' ,
39+ string = 'Property_States ' ,
40+ selection = [
41+ ('new' , 'New' ),
42+ ('offer_received' , 'Offer Received' ),
43+ ('offer_accepted' , 'Offer Accepted' ),
44+ ('sold' , 'Sold' ),
45+ ('cancelled' , 'Cancelled' )]
4646 )
4747
48- def action_set_sold (self ):
49- if self .state != "cancelled" :
50- self .state = "sold"
51- else :
52- raise UserError ("A cancelled property can not be sold" )
53- return True
54-
55- def action_set_cancelled (self ):
56- if (self .state != "sold" ):
57- self .state = "cancelled"
58- else :
59- raise UserError ("A sold property can not be cancelled" )
60- return True
61-
6248 property_type_id = fields .Many2one ('estate.property.type' , string = 'Type' )
6349 buyer_id = fields .Many2one ('res.partner' , string = 'Buyer' , copy = False )
6450 salesman_id = fields .Many2one ('res.users' , string = 'Salesman' , default = lambda self : self .env .user )
@@ -67,15 +53,28 @@ def action_set_cancelled(self):
6753 total_area = fields .Integer (string = 'Total Area(m2)' , compute = '_compute_total_area' , store = True )
6854 best_price = fields .Float (string = 'Best Offer' , compute = '_compute_best_price' , store = True )
6955
56+ _check_expected_price = models .Constraint (
57+ 'CHECK(expected_price >= 0)' , 'The expected price must be strictly positive.' )
58+
59+ _check_selling_price = models .Constraint (
60+ 'CHECK(selling_price > 0)' , 'The selling price must be positive.' )
61+
62+ @api .constrains ('selling_price' , 'expected_price' )
63+ def _check_selling_price_expected_price (self ):
64+ for record in self :
65+ if record .selling_price == 0 :
66+ continue
67+ if float_compare (record .selling_price , 0.9 * record .expected_price , precision_digits = 2 ) == - 1 :
68+ raise UserError ("The selling price must be at least 90% of the expected price." )
69+
7070 @api .depends ('living_area' , 'garden_area' )
7171 def _compute_total_area (self ):
7272 for property in self :
73- property .total_area = property .living_area + ( property .garden_area or 0 )
73+ property .total_area = property .living_area + property .garden_area
7474
7575 @api .depends ('offer_ids.price' )
7676 def _compute_best_price (self ):
7777 for record in self :
78- if record .offer_ids :
7978 record .best_price = max (record .offer_ids .mapped ('price' )) if record .offer_ids else 0
8079
8180 @api .onchange ('garden' )
@@ -87,16 +86,25 @@ def _onchange_garden(self):
8786 self .garden_area = 0
8887 self .garden_orientation = False
8988
90- _check_expected_price = models .Constraint (
91- 'CHECK(expected_price >= 0)' , 'The expected price must be strictly positive.' )
89+ @api .onchange ('offer_ids' )
90+ def _onchange_offers (self ):
91+ for record in self :
92+ has_offers = any (offer .status not in ('refused' , 'accepted' ) for offer in record .offer_ids )
93+ if record .state == 'new' and has_offers :
94+ record .state = 'offer_received'
9295
93- _check_selling_price = models . Constraint (
94- 'CHECK(selling_price > 0)' , 'The selling price must be positive.' )
96+ if not has_offers and not any ( offer . status == 'accepted' for offer in record . offer_ids ) and record . state in ( 'offer_received' ,):
97+ record . state = 'offer_receive'
9598
96- @api .constrains ('selling_price' , 'expected_price' )
97- def _check_selling_price_expected_price (self ):
98- for record in self :
99- if record .selling_price == 0 :
100- continue
101- if float_compare (record .selling_price , 0.9 * record .expected_price , precision_digits = 2 ) == - 1 :
102- raise UserError ("The selling price must be at least 90% of the expected price." )
99+ def action_set_sold (self ):
100+ print (self .state )
101+ if self .state == "cancelled" :
102+ raise UserError ("A cancelled property can not be sold" )
103+ self .state = "sold"
104+ return True
105+
106+ def action_set_cancelled (self ):
107+ if self .state == "sold" :
108+ raise UserError ("A sold property can not be cancelled" )
109+ self .state = "cancelled"
110+ return True
0 commit comments