Skip to content

Commit 073a19d

Browse files
author
SurryaT10
committed
[IMP] Chapter 9: Ready for Some Action
Added Sold and Cancel buttons: When a property is sold it cannot be canceled When a property is canceled it cannot be sold Added offer accept and rejected icon button: When an offer is accepted: 1. The selling price is automatically updated by button actions 2. Status changes to 'Offer Accepted' 3. Rejects all other offers Added an onchange, which updates the status to 'Offer Received' when there is an offer.
1 parent e512458 commit 073a19d

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

estate/models/estate.py

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

34

45
class Estate(models.Model):
@@ -43,7 +44,15 @@ def _compute_total_area(self):
4344
@api.depends('offer_ids.price')
4445
def _compute_best_price(self):
4546
for record in self:
46-
record.best_price = max(record.offer_ids.mapped('price'))
47+
record.best_price = max(record.offer_ids.mapped('price')) if record.offer_ids else 0.0
48+
49+
@api.onchange('offer_ids')
50+
def _onchange_property_status(self):
51+
for record in self:
52+
if record.status == 'new' and len(record.offer_ids) > 0:
53+
record.status = 'offer_received'
54+
else:
55+
record.status = record.status
4756

4857
@api.onchange("garden")
4958
def _onchange_garden(self):
@@ -53,3 +62,15 @@ def _onchange_garden(self):
5362
else:
5463
self.garden_area = 0
5564
self.garden_orientation = False
65+
66+
def action_mark_sold(self):
67+
for record in self:
68+
if record.status == 'canceled':
69+
raise UserError("Canceled properties cannot be sold.")
70+
record.status = 'sold'
71+
72+
def action_mark_cancel(self):
73+
for record in self:
74+
if record.status == 'sold':
75+
raise UserError("Sold properties cannot be canceled.")
76+
record.status = 'canceled'

estate/models/offer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,23 @@ def _inverse_date_deadline(self):
2525
for record in self:
2626
create_date = record.create_date if record.create_date else fields.Date.today()
2727
record.validity = (record.date_deadline - create_date.date()).days
28+
29+
def action_accept_offer(self):
30+
for record in self:
31+
record.status = 'accepted'
32+
record.property_id.selling_price = record.price
33+
record.property_id.buyer_id = record.partner_id
34+
record.property_id.status = 'offer_accepted'
35+
36+
# Reject other offers
37+
other_offers = record.property_id.offer_ids.filtered(lambda o: o.id != record.id)
38+
other_offers.action_reject_offer()
39+
40+
def action_reject_offer(self):
41+
for record in self:
42+
record.status = 'refused'
43+
if record.property_id.status == 'offer_accepted' and record.property_id.buyer_id == record.partner_id:
44+
record.property_id.selling_price = 0.0
45+
record.property_id.buyer_id = False
46+
record.property_id.status = 'offer_received'
47+

estate/views/estate_property_search_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<field name="bedrooms"/>
1111
<field name="living_area"/>
1212
<field name="facades"/>
13+
<field name="status"/>
1314
<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
1415
<filter string="Status" name="availability" domain="[('status', 'in', ['new', 'offered'])]"/>
1516
<group>

estate/views/estate_property_views.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<field name="model">estate.property</field>
2828
<field name="arch" type="xml">
2929
<form string="My new house">
30+
<header>
31+
<button name="action_mark_sold" type="object" string="Sold" class="oe_highlight"/>
32+
<button name="action_mark_cancel" type="object" string="Cancel"/>
33+
</header>
3034
<sheet>
3135
<group>
3236
<h1>
@@ -38,6 +42,7 @@
3842
</group>
3943
<group>
4044
<group>
45+
<field name="status"/>
4146
<field name="property_type_id"/>
4247
<field name="postcode"/>
4348
<field name="date_availability"/>
@@ -69,6 +74,8 @@
6974
<field name="partner_id"/>
7075
<field name="validity"/>
7176
<field name="date_deadline"/>
77+
<button name="action_accept_offer" type="object" icon="fa-check"/>
78+
<button name="action_reject_offer" type="object" icon="fa-remove"/>
7279
<field name="status"/>
7380
</list>
7481
</field>

0 commit comments

Comments
 (0)