1- from odoo import api , fields , models , exceptions
2- from odoo .tools import float_compare
3-
41from dateutil .relativedelta import relativedelta
52
3+ from odoo import api , fields , models
4+ from odoo .tools import float_compare
5+ from odoo .exceptions import ValidationError , UserError
6+
67
7- class Property (models .Model ):
8+ class EstateProperty (models .Model ):
89 _name = 'estate.property'
9- _description = "when duplicated status and date are not copied "
10+ _description = "All property created "
1011 _order = 'id desc'
11-
12- name = fields .Char (required = True , string = 'title' )
13- description = fields .Text (string = "description " )
12+
13+ name = fields .Char (required = True , string = "Title" )
14+ description = fields .Text (string = "Description " )
1415 postcode = fields .Char ()
15- date_availability = fields .Date (default = ( fields .Date .today () + relativedelta (months = 3 ) ), copy = False ,
16- string = 'available date' )
17- expected_price = fields .Float (required = True , string = 'expected price' )
18- selling_price = fields .Float (readonly = True , copy = False , string = 'selling price' )
16+ date_availability = fields .Date (default = lambda p : fields .Date .today () + relativedelta (months = 3 ), copy = False ,
17+ string = "Available Date" )
18+ expected_price = fields .Float (required = True , string = "Expected Price" )
19+ selling_price = fields .Float (readonly = True , copy = False , string = "Selling price" )
1920 bedrooms = fields .Integer (default = 2 )
20- living_area = fields .Integer (string = 'living area' )
21- facades = fields .Integer (string = "facades " )
22- garage = fields .Boolean (string = "garage " )
23- garden = fields .Boolean (string = "garden " )
21+ living_area = fields .Integer (string = "Living Area" )
22+ facades = fields .Integer (string = "Facades " )
23+ garage = fields .Boolean (string = "Garage " )
24+ garden = fields .Boolean (string = "Garden " )
2425 active = fields .Boolean (default = True )
25- garden_area = fields .Integer (string = "garden area " , default = 0 )
26+ garden_area = fields .Integer (string = "Garden Area " , default = 0 )
2627
27- property_type_id = fields .Many2one (" estate.property.type" , string = 'property type' )
28- buyer_id = fields .Many2one (" res.partner" , string = 'buyer' , copy = False )
29- salesperson_id = fields .Many2one (" res.users" , string = 'salesperson' , default = lambda self : self .env .user )
28+ property_type_id = fields .Many2one (' estate.property.type' , string = "Property Type" )
29+ buyer_id = fields .Many2one (' res.partner' , string = "Buyer" , copy = False )
30+ salesperson_id = fields .Many2one (' res.users' , string = "Salesperson" , default = lambda self : self .env .user )
3031
31- tag_ids = fields .Many2many (" estate.property.tag" )
32+ tag_ids = fields .Many2many (' estate.property.tag' )
3233
33- offer_ids = fields .One2many (" estate.property.offer" , " property_id" , string = "offer " )
34+ offer_ids = fields .One2many (comodel_name = ' estate.property.offer' , inverse_name = ' property_id' , string = "Offer " )
3435
3536 garden_orientation = fields .Selection (
36- string = "garden orientation " ,
37- selection = [('north' , "North" ), ('south' , "South" ), ('east' , "East" ), ('west' , "West" )]
38- )
37+ string = "Garden Orientation " ,
38+ selection = [('north' , "North" ), ('south' , "South" ), ('east' , "East" ), ('west' , "West" )]
39+ )
3940
4041 state = fields .Selection (
4142 string = "Status" ,
4243 required = True ,
4344 default = 'new' ,
44- selection = [('new' , "New" ), ('offer' , "Offer" ), ('received' , "Offer Received" ),
45- ('accepted' , "Offer Accepted" ), ('sold' , "Sold" ), ('cancelled' , "Cancelled" )]
46- )
47-
48- total_area = fields .Float (compute = "_compute_total_area" )
49-
50- best_price = fields .Float (compute = "_compute_best_price" )
51-
45+ selection = [
46+ ('new' , "New" ),
47+ ('offer' , "Offer" ),
48+ ('received' , "Offer Received" ),
49+ ('accepted' , "Offer Accepted" ),
50+ ('sold' , "Sold" ),
51+ ('cancelled' , "Cancelled" ),
52+ ]
53+ )
54+
55+ total_area = fields .Float (compute = '_compute_total_area' )
56+
57+ best_price = fields .Float (compute = '_compute_best_price' )
58+
59+ _check_expected_price = models .Constraint (
60+ 'CHECK(expected_price > 0)' ,
61+ "The expected price must be strictly positive." ,
62+ )
63+
64+ _check_selling_price = models .Constraint (
65+ 'CHECK(selling_price >= 0)' ,
66+ "The selling price must be positive." ,
67+ )
68+
69+ @api .constrains ('selling_price' , 'expected_price' )
70+ def _check_sell_price (self ):
71+ for estate in self :
72+ if len (estate .offer_ids ) > 0 and float_compare (estate .selling_price , estate .expected_price * 0.9 , 2 ) == - 1 :
73+ raise ValidationError (self .env ._ ("Put a higher price" ))
74+ return True
75+
5276 @api .depends ('garden_area' , 'living_area' )
5377 def _compute_total_area (self ):
5478 for estate in self :
@@ -57,61 +81,46 @@ def _compute_total_area(self):
5781 @api .depends ('offer_ids.price' )
5882 def _compute_best_price (self ):
5983 for estate in self :
60- prices = estate .offer_ids .filtered (lambda o :o .status != 'refused' ).mapped ('price' )
84+ prices = estate .offer_ids .filtered (lambda o : o .status != 'refused' ).mapped ('price' )
6185 estate .best_price = max (prices ) if len (prices ) > 0 else 0
6286
6387 @api .onchange ('garden' )
6488 def _onchange_garden (self ):
65- self .garden_area = 10 * self .garden
66- self .garden_orientation = 'north' if self .garden else None
89+ self .garden_area = 10 * self .garden
90+ self .garden_orientation = 'north' if self .garden else None
6791
68- def cancel_sell (self ):
92+ def action_cancel_sell (self ):
6993 for estate in self :
70- if estate .state == 'sold' :
71- raise exceptions . UserError ("Sold properties can not be canceled" )
94+ if estate .state == 'sold' :
95+ raise UserError (self . env . _ ( "Sold properties can not be canceled" ) )
7296
7397 estate .state = 'cancelled'
74- return True
75-
76- def set_sell (self ):
98+ return True
99+
100+ def action_sell (self ):
77101 for estate in self :
78- if estate .state == 'cancelled' :
79- raise exceptions . UserError ("Cancelled properties can not be sell" )
102+ if estate .state == 'cancelled' :
103+ raise UserError (self . env . _ ( "Cancelled properties can not be sell" ) )
80104
81105 estate .state = 'sold'
82- return True
83-
106+ return True
107+
84108 def set_received (self ):
85109 for estate in self :
86110 estate .state = 'received'
87- return True
88-
89- def accepted_offer (self ,offer ):
111+ return True
112+
113+ def accepted_offer (self , offer ):
90114 for estate in self :
91115 if offer .status == 'accepted' :
92116 estate .selling_price = offer .price
93117 estate .buyer_id = offer .partner_id
94- estate .state = 'accepted'
118+ estate .state = 'accepted'
119+ return True
95120
96- _check_expected_price = models .Constraint (
97- 'CHECK(expected_price > 0)' ,
98- "The expected price must be strictly positive." ,
99- )
100-
101- _check_selling_price = models .Constraint (
102- 'CHECK(selling_price >= 0)' ,
103- "The selling price must be positive." ,
104- )
105-
106- @api .constrains ('selling_price' ,'expected_price' )
107- def _check_sell_price (self ):
108- for estate in self :
109- if len (estate .offer_ids ) > 0 and float_compare (estate .selling_price , estate .expected_price * 0.9 ,2 ) == - 1 :
110- raise exceptions .ValidationError ("Put a higher price" )
111-
112121 @api .model
113122 def ondelete (self ):
114- for property in self :
115- if property .state != 'new' or property .state != 'cancelled' :
116- raise exceptions . ValidationError ("Can only delete new or cancelled properties" )
117- return super ().ondelete ()
123+ for property in self :
124+ if property .state != 'new' or property .state != 'cancelled' :
125+ raise ValidationError (self . env . _ ( "Can only delete new or cancelled properties" ) )
126+ return super ().ondelete ()
0 commit comments