-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathestate_property_offer.py
More file actions
97 lines (79 loc) · 3.43 KB
/
Copy pathestate_property_offer.py
File metadata and controls
97 lines (79 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare
class EstatePropertyOffer(models.Model):
# ---------------------------------------- Private Attributes ---------------------------------
_name = "estate.property.offer"
_description = "Real Estate Property Offer"
_order = "price desc"
_sql_constraints = [
("check_price", "CHECK(price > 0)", "The price must be strictly positive"),
]
# --------------------------------------- Fields Declaration ----------------------------------
# Basic
price = fields.Float("Price", required=True)
validity = fields.Integer(string="Validity (days)", default=7)
# Special
state = fields.Selection(
selection=[
("accepted", "Accepted"),
("refused", "Refused"),
],
string="Status",
copy=False,
default=False,
)
# Relational
partner_id = fields.Many2one("res.partner", string="Partner", required=True)
property_id = fields.Many2one("estate.property", string="Property", required=True)
# For stat button:
property_type_id = fields.Many2one(
"estate.property.type", related="property_id.property_type_id", string="Property Type", store=True
)
# Computed
date_deadline = fields.Date(string="Deadline", compute="_compute_date_deadline", inverse="_inverse_date_deadline")
# ---------------------------------------- Compute methods ------------------------------------
@api.depends("create_date", "validity")
def _compute_date_deadline(self):
for offer in self:
date = offer.create_date.date() if offer.create_date else fields.Date.today()
offer.date_deadline = date + relativedelta(days=offer.validity)
def _inverse_date_deadline(self):
for offer in self:
date = offer.create_date.date() if offer.create_date else fields.Date.today()
offer.validity = (offer.date_deadline - date).days
# ------------------------------------------ CRUD Methods -------------------------------------
@api.model
def create(self, vals):
if vals.get("property_id") and vals.get("price"):
prop = self.env["estate.property"].browse(vals["property_id"])
# We check if the offer is higher than the existing offers
if prop.offer_ids:
max_offer = max(prop.mapped("offer_ids.price"))
if float_compare(vals["price"], max_offer, precision_rounding=0.01) <= 0:
raise UserError("The offer must be higher than %.2f" % max_offer)
prop.state = "offer_received"
return super().create(vals)
# ---------------------------------------- Action Methods -------------------------------------
def action_accept(self):
if "accepted" in self.mapped("property_id.offer_ids.state"):
raise UserError("An offer as already been accepted.")
return self.write(
{
"state": "accepted",
}
) and self.mapped("property_id").write(
{
"state": "offer_accepted",
"selling_price": self.price,
"buyer_id": self.partner_id.id,
}
)
def action_refuse(self):
return self.write(
{
"state": "refused",
}
)