|
1 | 1 | from odoo import api, fields, models |
2 | 2 | from dateutil.relativedelta import relativedelta |
3 | 3 | from datetime import date |
4 | | -from odoo.exceptions import UserError |
| 4 | +from odoo.exceptions import UserError, ValidationError |
| 5 | +from odoo.tools import float_compare, float_is_zero |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class EstatePropertyModel(models.Model): |
@@ -56,6 +57,9 @@ class EstatePropertyModel(models.Model): |
56 | 57 | total_area = fields.Integer(compute='_compute_total_area', string="Total Area (sqm)") |
57 | 58 | best_price = fields.Float(compute='_compute_best_price', string="Best Offer") |
58 | 59 |
|
| 60 | + _expected_price_check = models.Constraint('CHECK(expected_price > 0)', "The expected price must be strictly positive.") |
| 61 | + _selling_price_check = models.Constraint('CHECK(selling_price >= 0)', "The selling price must be positive.") |
| 62 | + |
59 | 63 | @api.depends('living_area', 'garden_area') |
60 | 64 | def _compute_total_area(self): |
61 | 65 | for record in self: |
@@ -92,3 +96,10 @@ def action_cancel(self): |
92 | 96 | raise UserError("Sold properties cannot be canceled.") |
93 | 97 | record.state = "cancel" |
94 | 98 | return True |
| 99 | + |
| 100 | + @api.constrains('selling_price', 'expected_price') |
| 101 | + def _check_selling_price(self): |
| 102 | + for record in self: |
| 103 | + if not float_is_zero(record.selling_price, precision_digits=2): |
| 104 | + if float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1: |
| 105 | + raise ValidationError("The selling price cannot be lower than 90% of the expected price!") |
0 commit comments