|
1 | 1 | from odoo import fields, models, api |
| 2 | +from odoo.exceptions import UserError, ValidationError |
| 3 | +from odoo.tools.float_utils import float_compare, float_is_zero |
2 | 4 | from datetime import timedelta |
3 | 5 |
|
4 | 6 |
|
5 | 7 | class EstateProperty(models.Model): |
6 | 8 | _name = "estate.property" |
7 | 9 | _description = "Estate property" |
8 | 10 |
|
| 11 | + _check_expected_price = models.Constraint( |
| 12 | + "CHECK(expected_price > 0)", |
| 13 | + "The expected price must be strictly positive.", |
| 14 | + ) |
| 15 | + |
| 16 | + _check_selling_price = models.Constraint( |
| 17 | + "CHECK(selling_price >= 0)", |
| 18 | + "The selling price must be positive.", |
| 19 | + ) |
| 20 | + |
9 | 21 | name = fields.Char('Name', required=True, default='My new house') |
10 | 22 | description = fields.Text('Description') |
11 | 23 | active = fields.Boolean(default=True) |
@@ -115,3 +127,34 @@ def _inverse_date_deadline(self): |
115 | 127 | record.validity = ( |
116 | 128 | record.date_deadline - record.create_date.date() |
117 | 129 | ).days |
| 130 | + |
| 131 | + def action_cancel(self): |
| 132 | + for record in self: |
| 133 | + if record.state == "sold": |
| 134 | + raise UserError("A sold property cannot be cancelled.") |
| 135 | + record.state = "cancelled" |
| 136 | + return True |
| 137 | + |
| 138 | + def action_sold(self): |
| 139 | + for record in self: |
| 140 | + if record.state == "cancelled": |
| 141 | + raise UserError("A cancelled property cannot be sold.") |
| 142 | + record.state = "sold" |
| 143 | + return True |
| 144 | + |
| 145 | + @api.constrains("selling_price", "expected_price") |
| 146 | + def _check_selling_price(self): |
| 147 | + for record in self: |
| 148 | + if float_is_zero(record.selling_price, precision_rounding=0.01): |
| 149 | + continue |
| 150 | + |
| 151 | + min_price = record.expected_price * 0.9 |
| 152 | + |
| 153 | + if float_compare( |
| 154 | + record.selling_price, |
| 155 | + min_price, |
| 156 | + precision_rounding=0.01 |
| 157 | + ) < 0: |
| 158 | + raise ValidationError( |
| 159 | + "The selling price cannot be lower than 90% of the expected price." |
| 160 | + ) |
0 commit comments