1- from odoo import fields , models
1+ from odoo import api , fields , models
2+ from odoo .exceptions import UserError , ValidationError
3+ from odoo .tools .float_utils import float_compare
4+
25
36class EstateProperty (models .Model ):
4- _name = " estate.property"
7+ _name = ' estate.property'
58 _description = "Real-estate property"
9+ _order = 'id desc'
610
711 name = fields .Char (required = True )
812 description = fields .Text ()
913 postcode = fields .Char ()
10- date_availability = fields .Date (copy = False , default = fields .Date .add (fields .Date .today (), months = 3 ))
14+ date_availability = fields .Date (
15+ copy = False , default = fields .Date .add (fields .Date .today (), months = 3 )
16+ )
1117 expected_price = fields .Float (required = True )
12- selling_price = fields .Float (readonly = True , copy = False )
18+ selling_price = fields .Float (copy = False , readonly = True )
1319 bedrooms = fields .Integer (default = 2 )
1420 living_area = fields .Integer ()
1521 facades = fields .Integer ()
@@ -18,11 +24,89 @@ class EstateProperty(models.Model):
1824 garden_area = fields .Integer ()
1925 active = fields .Boolean (default = True )
2026 state = fields .Selection (
27+ [
28+ ('new' , "New" ),
29+ ('offer_received' , "Offer Received" ),
30+ ('offer_accepted' , "Offer Accepted" ),
31+ ('sold' , "Sold" ),
32+ ('cancelled' , "Cancelled" ),
33+ ],
2134 required = True ,
22- default = 'New' ,
23- copy = False ,
24- selection = [( 'New' , 'new' ), ( 'Offer Received' , 'offer received' ), ( 'Offer Accepted' , 'offer accepted' ), ( 'Sold' , 'sold' ), ( 'Cancelled' , 'cancelled' )] )
35+ default = 'new' ,
36+ copy = False ,
37+ )
2538 garden_orientation = fields .Selection (
26- string = 'Type' ,
27- selection = [('North' , 'north' ), ('South' , 'south' ), ('East' , 'east' ), ('West' , 'west' )])
39+ string = "Type" ,
40+ selection = [
41+ ('north' , "North" ),
42+ ('south' , "South" ),
43+ ('east' , "East" ),
44+ ('west' , "West" ),
45+ ],
46+ )
47+ property_type = fields .Many2one ('estate.property.type' , string = "property type" )
48+ salesman_id = fields .Many2one (
49+ 'res.users' , string = "Salesman" , default = lambda self : self .env .user
50+ )
51+ buyer_id = fields .Many2one ('res.partner' , string = "Buyer" , copy = False )
52+ tag_ids = fields .Many2many ('estate.property.tag' )
53+ offer_ids = fields .One2many ('estate.property.offer' , 'property_id' )
54+ total_area = fields .Float (compute = '_compute_total_area' , store = True )
55+ best_price = fields .Float (compute = '_compute_best_price' , store = True )
56+ _chek_expected_price = models .Constraint (
57+ "CHECK(expected_price > 0)" , "Expected price of property should be positive"
58+ )
59+ _chek_selling_price = models .Constraint (
60+ "CHECK(selling_price >= 0)" , "selling price of property should be positive"
61+ )
62+
63+ @api .depends ('living_area' , 'garden_area' )
64+ def _compute_total_area (self ):
65+ for record in self :
66+ record .total_area = record .living_area + record .garden_area
67+
68+ @api .depends ('offer_ids.price' )
69+ def _compute_best_price (self ):
70+ for record in self :
71+ record .best_price = max (record .offer_ids .mapped ('price' ), default = 0.0 )
72+
73+ @api .onchange ('garden' )
74+ def _onchnage_garden (self ):
75+ if self .garden :
76+ self .garden_area = 10
77+ self .garden_orientation = 'north'
78+ else :
79+ self .garden_area = 0
80+ self .garden_orientation = False
81+
82+ def action_property_sold (self ):
83+ for record in self :
84+ if record .state == 'cancelled' :
85+ raise UserError ("cancelled property cannot be sold" )
86+ else :
87+ record .state = 'sold'
88+ return True
89+
90+ def action_property_cancel (self ):
91+ for record in self :
92+ if record .state == 'sold' :
93+ raise UserError ("sold property cannot be cancelled" )
94+ else :
95+ record .state = 'cancelled'
96+ return True
2897
98+ @api .constrains ('selling_price' )
99+ def _check_selling_price (self ):
100+ for record in self :
101+ if (
102+ record .selling_price
103+ and float_compare (
104+ record .selling_price ,
105+ record .expected_price * 0.9 ,
106+ precision_digits = 2 ,
107+ )
108+ == - 1
109+ ):
110+ raise ValidationError (
111+ "Selling price should not be less than 90% of expected price"
112+ )
0 commit comments