Skip to content

Commit 1a772bd

Browse files
[IMP] Added Accept and Refuse buttons to offers. Added Sell and Cancel buttons to Buildings Model in Estate Module.
1 parent cb0c70a commit 1a772bd

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

estate/models/buildings_model.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from odoo import models, fields, api
2+
from odoo.exceptions import UserError
23
from datetime import timedelta
34

45

@@ -39,9 +40,7 @@ class buildings_model(models.Model):
3940
tag_ids = fields.Many2many("estate.building_tags", string="Tags")
4041
offer_ids = fields.One2many("estate.offers", "building_id", string="Offers")
4142

42-
total_area = fields.Integer(
43-
string="Total Area", compute="_compute_total_area"
44-
)
43+
total_area = fields.Integer(string="Total Area", compute="_compute_total_area")
4544

4645
best_price = fields.Integer(
4746
string="Best Offer Price",
@@ -61,11 +60,24 @@ def _compute_best_price(self):
6160
record.best_price = max(record.offer_ids.mapped("price"))
6261
else:
6362
record.best_price = 0
63+
6464
@api.onchange("has_garden")
6565
def _onchange_garden_area(self):
6666
if self.has_garden:
6767
self.garden_area = 10
6868
self.garden_orientation = "north"
6969
else:
7070
self.garden_area = 0
71-
self.garden_orientation = False
71+
self.garden_orientation = False
72+
73+
def action_set_sold(self):
74+
for record in self:
75+
if record.state == "canceled":
76+
raise UserError("Canceled buildings cannot be sold.")
77+
record.state = "sold"
78+
79+
def action_set_canceled(self):
80+
for record in self:
81+
if record.state == "sold":
82+
raise UserError("Sold buildings cannot be canceled.")
83+
record.state = "canceled"

estate/models/offers_model.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from odoo import models, fields, api
2+
from odoo.exceptions import UserError
23
from datetime import timedelta
34

45

@@ -10,22 +11,53 @@ class offers_model(models.Model):
1011
status = fields.Selection(
1112
[("accepted", "Accepted"), ("refused", "Refused")],
1213
string="Status",
13-
required=True,
14+
required=False,
1415
)
1516
building_id = fields.Many2one("estate.buildings", string="Building")
1617
partner_id = fields.Many2one("res.partner", string="Partner")
17-
validity = fields.Integer(
18-
string="Validity (days)", default=7
19-
)
18+
validity = fields.Integer(string="Validity (days)", default=7)
2019
date_deadline = fields.Date(
21-
string="Deadline", compute="_compute_date_deadline", inverse="_inverse_date_deadline"
20+
string="Deadline",
21+
compute="_compute_date_deadline",
22+
inverse="_inverse_date_deadline",
2223
)
2324

2425
@api.depends("validity")
2526
def _compute_date_deadline(self):
2627
for record in self:
2728
record.date_deadline = fields.Date.today() + timedelta(days=record.validity)
28-
29+
2930
def _inverse_date_deadline(self):
3031
for record in self:
3132
record.validity = (record.date_deadline - fields.Date.today()).days
33+
34+
def action_accept_offer(self):
35+
for record in self:
36+
if record.status != "accepted" and record.building_id.state not in [
37+
"sold",
38+
"canceled",
39+
]:
40+
record.status = "accepted"
41+
record.building_id.state = "offer accepted"
42+
record.building_id.buyer_id = record.partner_id
43+
record.building_id.value = record.price
44+
other_offers = self.search(
45+
[
46+
("building_id", "=", record.building_id.id),
47+
("id", "!=", record.id),
48+
]
49+
)
50+
other_offers.write({"status": "refused"})
51+
elif record.building_id.state in ["sold", "canceled"]:
52+
raise UserError("Cannot accept offers for sold or canceled buildings.")
53+
else:
54+
raise UserError("Offer is already accepted.")
55+
56+
def action_refuse_offer(self):
57+
for record in self:
58+
if record.status != "refused":
59+
record.status = "refused"
60+
record.building_id.state = "offer received"
61+
record.building_id.buyer_id = False
62+
else:
63+
raise UserError("Offer is already refused.")

estate/views/views.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<field name="model">estate.buildings</field>
2525
<field name="arch" type="xml">
2626
<form string="Test">
27+
<header>
28+
<button name="action_set_sold" type="object" string="Sell Building"/>
29+
<button name="action_set_canceled" type="object" string="Cancel Building"/>
30+
</header>
2731
<sheet>
2832
<group>
2933
<group>
@@ -64,6 +68,8 @@
6468
<field name="partner_id"/>
6569
<field name="date_deadline"/>
6670
<field name="validity"/>
71+
<button name="action_accept_offer" type="object" string="" icon="fa-check"/>
72+
<button name="action_refuse_offer" type="object" string="" icon="fa-times"/>
6773
</list>
6874
</field>
6975
<group>
@@ -91,4 +97,4 @@
9197
</field>
9298
</record>
9399

94-
</odoo>
100+
</odoo>

0 commit comments

Comments
 (0)