Skip to content

Commit f93dec0

Browse files
committed
[IMP] estate: add computed fields,inverse funcs, onchange & Sold/Cancel buttons
Added computed fields with inverse functions and onchange methods, along with Sold and Cancel buttons to enhance the Real Estate module functionality.
1 parent ed301d8 commit f93dec0

File tree

7 files changed

+83
-10
lines changed

7 files changed

+83
-10
lines changed

estate/__manifest__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@
1212
'application': True,
1313
'installable':True
1414
}
15-

estate/models/estate_property.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from odoo import models, fields
1+
from odoo import models, fields, api
22
from dateutil.relativedelta import relativedelta
3+
from datetime import date
4+
from odoo.exceptions import UserError
35

46

57
class EstateProperty(models.Model):
@@ -35,15 +37,53 @@ class EstateProperty(models.Model):
3537
("sold", "Sold"),
3638
("cancelled", "Cancelled"),
3739
],
38-
string="Status",
40+
"Status",
3941
required=True,
4042
copy=False,
4143
default="new",
4244
)
4345
active = fields.Boolean(default=True)
44-
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
46+
property_type_id = fields.Many2one("estate.property.type", "Property Type")
4547
buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False)
4648
salesperson_id = fields.Many2one(
4749
"res.users", string="Salesperson")
4850
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
49-
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
51+
offer_ids = fields.One2many("estate.property.offer", "property_id", "Offers")
52+
total_area = fields.Integer("Total Area(sqm)", compute="_compute_total_area")
53+
best_price = fields.Float("Best Offer", compute="_compute_best_price")
54+
55+
@api.depends("living_area", "garden_area")
56+
def _compute_total_area(self):
57+
for record in self:
58+
record.total_area = record.living_area + record.garden_area
59+
60+
@api.depends("offer_ids.price")
61+
def _compute_best_price(self):
62+
for record in self:
63+
if record.offer_ids:
64+
record.best_price = max(record.offer_ids.mapped("price"))
65+
else:
66+
record.best_price = 0
67+
68+
@api.onchange('garden')
69+
def _onchange_garden(self):
70+
if self.garden:
71+
self.garden_area = 10
72+
self.garden_orientation = 'north'
73+
else:
74+
self.garden_area = 0
75+
self.garden_orientation = False
76+
77+
def action_cancel(self):
78+
for record in self:
79+
if record.state == 'sold':
80+
raise UserError("A sold property cannot be cancelled")
81+
else:
82+
self.state = 'cancelled'
83+
84+
def action_sold(self):
85+
for record in self:
86+
if record.state == 'cancelled':
87+
raise UserError("A cancelled property cannot be set as sold")
88+
else:
89+
self.state = 'sold'

estate/models/estate_property_offer.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from odoo import fields, models
1+
from odoo import fields, models, api
2+
from dateutil.relativedelta import relativedelta
23

34

45
class EstatePropertyOffer(models.Model):
@@ -12,4 +13,20 @@ class EstatePropertyOffer(models.Model):
1213
)
1314
partner_id = fields.Many2one("res.partner", string="Partner", required=True)
1415
property_id = fields.Many2one("estate.property", string="Property", required=True)
15-
16+
validity = fields.Integer(string="Validity(days)", default=7)
17+
date_deadline = fields.Date(string="Deadline Date", compute="_compute_date_deadline", inverse="_inverse_date_deadline")
18+
19+
@api.depends('validity')
20+
def _compute_date_deadline(self):
21+
for record in self:
22+
if record.create_date:
23+
record.date_deadline = fields.Date.to_date(record.create_date) + relativedelta(days=record.validity)
24+
else:
25+
record.date_deadline = False
26+
27+
def _inverse_date_deadline(self):
28+
for record in self:
29+
if record.create_date:
30+
record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days
31+
else:
32+
record.validity = False

estate/models/estate_property_tag.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ class EstatePropertyTag(models.Model):
66
_description = "Real Estate Property Tag"
77

88
name = fields.Char(required=True)
9+

estate/models/estate_property_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ class EstatePropertyType(models.Model):
66
_description = "Real Estate Property Type"
77

88
name = fields.Char(required=True)
9+

estate/views/estate_property_offer_views.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<field name="price" />
88
<field name="partner_id" />
99
<field name="status" />
10+
<field name="validity" />
11+
<field name="date_deadline" />
1012
</list>
1113
</field>
1214
</record>
@@ -18,9 +20,11 @@
1820
<form>
1921
<sheet>
2022
<group>
21-
<field name="price"/>
22-
<field name="partner_id"/>
23-
<field name="status"/>
23+
<field name="price" />
24+
<field name="partner_id" />
25+
<field name="status" />
26+
<field name="validity" />
27+
<field name="date_deadline" />
2428
</group>
2529
</sheet>
2630
</form>

estate/views/estate_property_views.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@
3333
<field name="model">estate.property</field>
3434
<field name="arch" type="xml">
3535
<form string="form">
36+
<header>
37+
<button name="action_sold" type="object" string="Sold" />
38+
<button name="action_cancel" type="object" string="Cancel" />
39+
</header>
3640
<sheet>
3741
<group>
3842
<group>
3943
<field name="name" />
4044
<field name="postcode" />
4145
<field name="date_availability" />
4246
<field name="property_type_id" />
47+
<field name="state" />
4348
<field name="tag_ids" widget="many2many_tags" />
49+
<field name="best_price" />
4450
</group>
4551
<group>
4652
<field name="expected_price" />
@@ -52,6 +58,7 @@
5258
<field name="garden" />
5359
<field name="garden_area" />
5460
<field name="garden_orientation" />
61+
<field name="total_area" />
5562
</group>
5663
</group>
5764
<notebook>
@@ -65,12 +72,16 @@
6572
<field name="price" />
6673
<field name="partner_id" />
6774
<field name="status" />
75+
<field name="validity" />
76+
<field name="date_deadline" />
6877
</list>
6978
<form>
7079
<group>
7180
<field name="price" />
7281
<field name="partner_id" />
7382
<field name="status" />
83+
<field name="validity" />
84+
<field name="date_deadline" />
7485
</group>
7586
</form>
7687
</field>

0 commit comments

Comments
 (0)