|
1 | 1 | from odoo import fields, models, api
|
2 | 2 | from dateutil.relativedelta import relativedelta
|
3 |
| -from odoo.exceptions import UserError |
| 3 | +from odoo.exceptions import UserError, ValidationError |
| 4 | +from odoo.tools.float_utils import float_compare, float_is_zero |
4 | 5 |
|
5 | 6 |
|
6 | 7 | class EstateProperty(models.Model):
|
7 | 8 |
|
8 | 9 | _name = "estate.property"
|
9 | 10 | _description = "estate properties"
|
10 | 11 | _order = "id"
|
| 12 | + _sql_constraints = [ |
| 13 | + ('check_expected_price', 'CHECK(expected_price > 0 )', 'expected price must be strictly positive'), |
| 14 | + ('check_selling_price', 'CHECK(selling_price >= 0 )', 'selling price must be positive'), |
| 15 | + ] |
11 | 16 |
|
12 | 17 | name = fields.Char("Title", required=True)
|
13 | 18 | description = fields.Text("Description")
|
@@ -84,3 +89,9 @@ def action_cancel(self):
|
84 | 89 | raise UserError("Sold properties cannot be canceled.")
|
85 | 90 | else:
|
86 | 91 | record.state = 'canceled'
|
| 92 | + |
| 93 | + @api.constrains('expected_price', 'selling_price') |
| 94 | + def _check_selling_price(self): |
| 95 | + for record in self: |
| 96 | + if not float_is_zero(record.selling_price, precision_rounding=0.01) and float_compare(record.selling_price, record.expected_price * 0.9, precision_rounding=0.01) < 0: |
| 97 | + raise ValidationError("the selling price cannot be lower than 90% of the expected price") |
0 commit comments