Skip to content

Commit f39d8f3

Browse files
committed
[IMP] estate: chapter 15
1 parent dc28c90 commit f39d8f3

8 files changed

+38
-42
lines changed

estate/models/estate_property.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dateutil import relativedelta
22

3-
from odoo import api, fields, models
3+
from odoo import _, api, fields, models
44
from odoo.exceptions import UserError, ValidationError
55
from odoo.tools import float_compare
66

@@ -27,7 +27,7 @@ class EstateProperty(models.Model):
2727
("offer_received", "Offer Received"),
2828
("offer_accepted", "Offer Accepted"),
2929
("sold", "Sold"),
30-
("cancelled", "Cancelled"),
30+
("canceled", "canceled"),
3131
],
3232
required=True,
3333
copy=False,
@@ -80,17 +80,17 @@ class EstateProperty(models.Model):
8080
]
8181

8282
@api.depends("garden_area", "living_area")
83-
def _compute_total_area(self) -> None:
83+
def _compute_total_area(self):
8484
for record in self:
8585
record.total_area = record.garden_area + record.living_area
8686

8787
@api.depends("offer_ids")
88-
def _compute_best_price(self) -> None:
88+
def _compute_best_price(self):
8989
for record in self:
9090
record.best_price = max(record.offer_ids.mapped("price")) if record.offer_ids else 0.0
9191

9292
@api.onchange("garden")
93-
def _onchange_change(self) -> None:
93+
def _onchange_change(self):
9494
self.write(
9595
{
9696
"garden_area": 10 if self.garden else 0,
@@ -99,27 +99,27 @@ def _onchange_change(self) -> None:
9999
)
100100

101101
@api.constrains("selling_price")
102-
def _check_selling_price(self) -> None:
102+
def _check_selling_price(self):
103103
for record in self:
104104
if (
105-
record.offer_ids.filtered(lambda x: x.status == "accepted")
105+
record.offer_ids
106106
and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=3) == -1
107107
):
108-
raise ValidationError("The selling price should be atleast 90% of the expected price")
108+
raise ValidationError(_("The selling price should be atleast 90% of the expected price"))
109109

110110
@api.ondelete(at_uninstall=False)
111-
def _unlink_if_new_or_cancelled(self) -> None:
112-
if any(record.state not in ("new", "cancelled") for record in self):
113-
raise UserError("Can't delete an active property")
111+
def _unlink_if_new_or_canceled(self):
112+
if any(record.state not in ("new", "canceled") for record in self):
113+
raise UserError(_("Can't delete an active property"))
114114

115115
def action_cancel_property(self) -> bool:
116116
if "sold" in self.mapped("state"):
117-
raise UserError("Cannot cancel a sold property")
118-
self.write({"state": "cancelled"})
117+
raise UserError(_("Cannot cancel a sold property"))
118+
self.write({"state": "canceled"})
119119
return True
120120

121121
def action_sold_property(self) -> bool:
122-
if "cancelled" in self.mapped("state"):
123-
raise UserError("Cannot sell a cancel property")
122+
if "canceled" in self.mapped("state"):
123+
raise UserError(_("Cannot sell a cancel property"))
124124
self.write({"state": "sold"})
125125
return True

estate/models/estate_property_offer.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EstatePropertyOffer(models.Model):
1010
_description = "Real Estate: Offer"
1111
_order = "price desc"
1212

13-
price = fields.Float("Price")
13+
price = fields.Float("Price", required=True)
1414
status = fields.Selection(
1515
selection=[
1616
("accepted", "Accepted"),
@@ -32,35 +32,36 @@ class EstatePropertyOffer(models.Model):
3232
]
3333

3434
@api.depends("validity")
35-
def _compute_deadline(self) -> None:
35+
def _compute_deadline(self):
3636
for record in self:
3737
creation_date = record.create_date or fields.Date.today()
3838
record.date_deadline = creation_date + relativedelta.relativedelta(days=record.validity)
3939

40-
def _inverse_compute_deadline(self) -> None:
40+
def _inverse_compute_deadline(self):
4141
for record in self:
4242
record.validity = (record.date_deadline - record.create_date.date()).days
4343

4444
@api.model_create_multi
45-
def create(self, vals_list) -> None:
45+
def create(self, vals_list):
4646
for val in vals_list:
4747
property_id = self.env["estate.property"].browse(val["property_id"])
48+
max_price = max(
49+
property_id.offer_ids.mapped("price"),
50+
default=0.0,
51+
)
4852
if (
4953
float_compare(
50-
max(
51-
property_id.offer_ids.mapped("price"),
52-
default=0.0,
53-
),
54+
max_price,
5455
val["price"],
5556
precision_digits=2,
5657
)
57-
> -1
58+
>= 0
5859
):
59-
raise UserError(_("Can't make an offer with a lower price than the other"))
60+
raise UserError(_("The offer must be higher than %.2f") % max_price)
6061
property_id.write({"state": "offer_received"})
6162
return super().create(vals_list)
6263

63-
def action_refuse_offer(self) -> bool:
64+
def action_refuse_offer(self):
6465
for record in self:
6566
record.status = "refused"
6667
record.property_id.write(
@@ -71,14 +72,15 @@ def action_refuse_offer(self) -> bool:
7172
)
7273
return True
7374

74-
def action_accept_offer(self) -> bool:
75+
def action_accept_offer(self):
7576
for record in self:
76-
record.property_id.offer_ids.filtered(lambda x: x.status == "accepted").write({"status": "refused"})
77+
record.property_id.offer_ids.write({"status": "refused"})
7778
record.status = "accepted"
7879
record.property_id.write(
7980
{
8081
"selling_price": record.price,
8182
"buyer_id": record.partner_id,
83+
"state": "offer_accepted",
8284
}
8385
)
8486

estate/models/estate_property_type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class EstatePropertyType(models.Model):
1313
offer_count = fields.Integer(compute="_compute_offer_count")
1414

1515
@api.depends("offer_ids")
16-
def _compute_offer_count(self) -> None:
16+
def _compute_offer_count(self):
1717
for record in self:
1818
record.offer_count = len(record.offer_ids.filtered(lambda x: x.property_type_id.name == record.name))
1919

estate/models/res_users.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import fields, models
22

33

4-
class ResUsrs(models.Model):
4+
class ResUsers(models.Model):
55
_inherit = "res.users"
66

77
property_ids = fields.One2many(

estate/views/estate_property_views.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
<form string="Properties">
3535
<header>
3636
<button name="action_cancel_property" type="object" string="Cancel"
37-
invisible="state in ('cancelled', 'sold',)"/>
37+
invisible="state in ('canceled', 'sold',)"/>
3838
<button name="action_sold_property" type="object" string="Sold"
39-
invisible="state in ('cancelled', 'sold',)"/>
39+
invisible="state in ('canceled', 'sold',)"/>
4040
<field name="state" widget="statusbar"
4141
statusbar_visible="new,offer_received,offer_accepted,sold"
4242
options="{'clickable': '1'}"/>
@@ -82,7 +82,7 @@
8282
</page>
8383
<page string="Offers">
8484
<field name="offer_ids"
85-
readonly="state in ('offer_accepted', 'sold', 'cancelled')"/>
85+
readonly="state in ('offer_accepted', 'sold', 'canceled')"/>
8686
</page>
8787
<page string="Other Info">
8888
<group>
@@ -123,7 +123,7 @@
123123
<field name="name">estate.property.kanban</field>
124124
<field name="model">estate.property</field>
125125
<field name="arch" type="xml">
126-
<kanban default_group_by="property_type_id" records_draggable="False" groups_draggable="False">
126+
<kanban default_group_by="property_type_id" records_draggable="0" groups_draggable="0">
127127
<field name="state"/>
128128
<templates>
129129
<t t-name="card">

estate/views/res_users_views.xml

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
<field name="model">res.users</field>
66
<field name="inherit_id" ref="base.view_users_form"/>
77
<field name="arch" type="xml">
8-
<!-- <xpath expr="//page[@name='account_security']" position="after">
9-
<page string="Properties" name="estate_properties">
10-
<field name="property_ids"/>
11-
</page>
12-
</xpath> -->
138
<notebook position="inside">
149
<page string="Properties" name="estate_properties">
1510
<field name="property_ids"/>

estate_account/__manifest__.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
"installable": True,
44
"category": "Tutorials/RealEstate",
55
"depends": ["account", "estate"],
6-
# "data": [
7-
# ],
86
"license": "LGPL-3",
97
}

estate_account/models/estate_property.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class EstateProperty(models.Model):
55
_inherit = "estate.property"
66

77
def action_sold_property(self):
8+
res = super().action_sold_property()
89
invoice_vals_list = []
910
purchase_journal = self.env["account.journal"].search([("type", "=", "sale")], limit=1)
1011
for record in self:
@@ -31,4 +32,4 @@ def action_sold_property(self):
3132
}
3233
invoice_vals_list.append(invoice_vals)
3334
self.env["account.move"].create(invoice_vals_list)
34-
return super().action_sold_property()
35+
return res

0 commit comments

Comments
 (0)