Skip to content

Commit 7a6c138

Browse files
committed
[IMP] estate: Implemented constraints for price validation
-Refactored validity field to validity_days -added constraints for price validation
1 parent 87dad3d commit 7a6c138

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

Estate/models/estate_property.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from odoo import models, fields, api
22
from datetime import timedelta
3+
from odoo.exceptions import ValidationError
34

45

56
class EstateProperty(models.Model):
@@ -39,11 +40,9 @@ class EstateProperty(models.Model):
3940
salesperson_id = fields.Many2one("res.users", string="Salesperson")
4041
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
4142
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
42-
4343
total_area = fields.Float(compute="_compute_total_area", string="Total Area", store=True)
4444
best_price = fields.Float(compute="_compute_best_price", string="Best Offer", store=True)
45-
46-
validity = fields.Integer(default=7)
45+
validity_days = fields.Integer(default=7)
4746
date_deadline = fields.Date(
4847
compute="_compute_date_deadline",
4948
inverse="_inverse_date_deadline",
@@ -60,21 +59,21 @@ def _compute_best_price(self):
6059
for record in self:
6160
record.best_price = max(record.offer_ids.mapped("price")) if record.offer_ids else 0.0
6261

63-
@api.depends("create_date", "validity")
62+
@api.depends("create_date", "validity_days")
6463
def _compute_date_deadline(self):
6564
for record in self:
6665
create_date = record.create_date or fields.Date.today()
6766
if hasattr(create_date, "date"):
6867
create_date = create_date.date()
69-
record.date_deadline = create_date + timedelta(days=record.validity)
68+
record.date_deadline = create_date + timedelta(days=record.validity_days)
7069

7170
def _inverse_date_deadline(self):
7271
for record in self:
7372
create_date = record.create_date or fields.Date.today()
7473
if hasattr(create_date, "date"):
7574
create_date = create_date.date()
7675
delta = (record.date_deadline - create_date).days if record.date_deadline else 0
77-
record.validity = delta
76+
record.validity_days = delta
7877

7978
@api.onchange("garden")
8079
def _onchange_garden(self):
@@ -106,3 +105,23 @@ def action_set_next_status(self):
106105
def action_back_to_new(self):
107106
for record in self:
108107
record.state = "new"
108+
109+
@api.constrains("selling_price","expected_price")
110+
def _check_selling_price_ratio(self):
111+
for record in self:
112+
if record.selling_price <= 0:
113+
raise ValidationError("Selling price must be greater than or equal to 0")
114+
if record.selling_price < 0.9 * record.expected_price:
115+
raise ValidationError("Selling price must be at least 90% of the expected price")
116+
if record.expected_price < 0:
117+
raise ValidationError("Expected price must be greater than 0")
118+
119+
_check_expected_price = models.Constraint(
120+
'CHECK(expected_price < 0)',
121+
'The expected price of a property must be strictly positive.'
122+
)
123+
124+
_check_selling_price = models.Constraint(
125+
'CHECK(selling_price < 0)',
126+
'The selling price of a property must be positive.'
127+
)

Estate/models/estate_property_offer.py

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

34

45
class EstatePropertyOffer(models.Model):
@@ -23,3 +24,13 @@ def action_accept_offer(self):
2324
def action_refuse_offer(self):
2425
for record in self:
2526
record.status = 'refused'
27+
@api.constrains("price")
28+
def _check_price_ratio(self):
29+
for record in self:
30+
if record.price <= 0.0:
31+
raise ValidationError("Price must be greater than 0")
32+
33+
_check_price = models.Constraint(
34+
'CHECK(price < 0)',
35+
'The price must be strictly positive.'
36+
)

Estate/views/estate_property_views.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<field name="best_price" readonly="1"/>
7272
<field name="state"/>
7373
<field name="validity_days"/>
74-
<field name= "deadline_date"/>
74+
<field name= "date_deadline"/>
7575
</group>
7676

7777
</group>
@@ -127,4 +127,4 @@
127127
</record>
128128

129129

130-
</odoo>
130+
</odoo>

0 commit comments

Comments
 (0)