Skip to content

Commit 7d0d446

Browse files
committed
[IMP] estate: ch10 Validations for property prices added
1 parent 2e71ec2 commit 7d0d446

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

estate/models/estate_property.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
from odoo import api, fields, models, _
2-
from odoo.exceptions import UserError
2+
from odoo.tools import float_compare
3+
from odoo.exceptions import UserError, ValidationError
34

45

56
class EstateProperty(models.Model):
67
_name = "estate.property"
78
_description = "Estate property model"
89

10+
_check_expected_price = models.Constraint(
11+
'CHECK(expected_price > 0)',
12+
'Expected price of the property must be strictly positive.',
13+
)
14+
15+
_check_selling_price = models.Constraint(
16+
'CHECK(selling_price >= 0)',
17+
'Selling price of the property must be strictly positive.',
18+
)
19+
920
name = fields.Char("Title", required=True, default="Unknown")
1021
description = fields.Text("Description")
1122
postcode = fields.Char("Postcode")
@@ -93,4 +104,13 @@ def action_mark_as_cancelled(self):
93104
if record.state == "sold":
94105
raise UserError(_("Sold properties cannot be cancelled."))
95106
record.state = "cancelled"
107+
#TODO: Reject all offers
96108
return True
109+
110+
@api.constrains('expected_price')
111+
def _check_expected_price(self):
112+
for record in self:
113+
for offer in record.offer_ids:
114+
if offer.status == "yes" and float_compare(offer.price, 0.9 * record.expected_price, 2) < 0:
115+
raise ValidationError("There is an accepted offer for an amount less than 90 percent of the new selling price, action required!")
116+

estate/models/estate_property_offer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from odoo import api, fields, models, _
2-
from odoo.exceptions import UserError
2+
from odoo.tools import float_compare
3+
from odoo.exceptions import UserError, ValidationError
34

45

56
class EstatePropertyOffer(models.Model):
67
_name = "estate.property.offer"
78
_description = "Estate property offer"
89

10+
_check_offer_price = models.Constraint(
11+
'CHECK(price > 0)', 'Offer price must be strictly positive.'
12+
)
13+
914
@api.model_create_multi
1015
def create(self, vals_list):
1116
records = super().create(vals_list)
@@ -51,6 +56,8 @@ def action_accept_offer(self):
5156
for offer in record.property_id.offer_ids:
5257
if offer.status == "yes":
5358
raise UserError(_("Already accepted an offer for this property."))
59+
if float_compare(record.price, 0.9 * record.property_id.expected_price, 2) < 0:
60+
raise ValidationError("Accepted offer price cannot be less than 90 percent of property price")
5461
record.status = "yes"
5562
record.property_id.buyer_id = record.partner_id
5663
record.property_id.selling_price = record.price
@@ -59,7 +66,13 @@ def action_accept_offer(self):
5966

6067
def action_reject_offer(self):
6168
for record in self:
69+
record.status = "no"
6270
record.property_id.buyer_id = None
6371
record.property_id.selling_price = 0
64-
record.status = "no"
6572
return True
73+
74+
@api.constrains('price')
75+
def _check_offered_price(self):
76+
for record in self:
77+
if float_compare(record.price, 0.9 * record.property_id.expected_price, 2) < 0:
78+
raise ValidationError("Offer price cannot be less than 90 percent of property price")

estate/models/estate_property_tag.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
class EstatePropertyTag(models.Model):
55
_name = "estate.property.tag"
66
_description = "Estate property tag"
7+
_name_unique = models.Constraint(
8+
'unique (name)',
9+
'A property tag of that name already exists',
10+
)
711

812
name = fields.Char('Property Tag', required=True)

estate/models/estate_property_type.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
class EstatePropertyType(models.Model):
55
_name = "estate.property.type"
66
_description = "Estate property type"
7+
_name_unique = models.Constraint(
8+
'unique (name)',
9+
'A property type of that name already exists',
10+
)
711

812
name = fields.Char('Property Type', required=True)

0 commit comments

Comments
 (0)