|
| 1 | +from dateutil.relativedelta import relativedelta |
| 2 | +from odoo import api, fields, models |
| 3 | +from odoo.exceptions import UserError, ValidationError |
| 4 | +from odoo.tools.float_utils import float_compare, float_is_zero |
| 5 | + |
| 6 | + |
| 7 | +class EstateProperty(models.Model): |
| 8 | + _name = "estate.property" |
| 9 | + _description = "Estate property" |
| 10 | + _order = "id desc" |
| 11 | + |
| 12 | + name = fields.Char(string="Title", required=True) |
| 13 | + description = fields.Text() |
| 14 | + postcode = fields.Char() |
| 15 | + date_availability = fields.Date(string="Available From", default=lambda self: fields.Date.context_today(self) + relativedelta(months=3), copy=False) |
| 16 | + expected_price = fields.Float(required=True) |
| 17 | + selling_price = fields.Float(readonly=True, copy=False) |
| 18 | + bedrooms = fields.Integer(default=2) |
| 19 | + living_area = fields.Integer(string="Living Area (sqm)") |
| 20 | + facades = fields.Integer() |
| 21 | + garage = fields.Boolean() |
| 22 | + garden = fields.Boolean() |
| 23 | + garden_area = fields.Integer(string="Garden Area (sqm)") |
| 24 | + garden_orientation = fields.Selection( |
| 25 | + selection=[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")], |
| 26 | + ) |
| 27 | + |
| 28 | + # Reserved fields |
| 29 | + active = fields.Boolean(default=True) |
| 30 | + state = fields.Selection( |
| 31 | + required=True, |
| 32 | + selection=[("new", "New"), ("offer_received", "Offer Received"), ("offer_accepted", "Offer Accepted"), ("sold", "Sold"), ("cancelled", "Cancelled")], |
| 33 | + default="new", |
| 34 | + copy=False, |
| 35 | + string="Status", |
| 36 | + ) |
| 37 | + |
| 38 | + # Relations |
| 39 | + property_type_id = fields.Many2one("estate.property.type", string="Property Type") |
| 40 | + salesman_id = fields.Many2one("res.users", default=lambda self: self.env.user) |
| 41 | + buyer_id = fields.Many2one("res.partner", copy=False) |
| 42 | + tag_ids = fields.Many2many("estate.property.tag") |
| 43 | + offer_ids = fields.One2many("estate.property.offer", "property_id") |
| 44 | + |
| 45 | + # Computed |
| 46 | + total_area = fields.Integer(compute="_compute_total_area", string="Total Area (sqm)") |
| 47 | + best_price = fields.Float(compute="_compute_best_price", string="Best Offer") |
| 48 | + |
| 49 | + @api.depends("living_area", "garden_area") |
| 50 | + def _compute_total_area(self) -> None: |
| 51 | + for record in self: |
| 52 | + record.total_area = record.living_area + record.garden_area |
| 53 | + |
| 54 | + @api.depends("offer_ids.price") |
| 55 | + def _compute_best_price(self) -> None: |
| 56 | + for record in self: |
| 57 | + record.best_price = max(record.offer_ids.mapped("price"), default=0) |
| 58 | + |
| 59 | + # Methods that trigger on changes |
| 60 | + @api.onchange("garden") |
| 61 | + def _onchange_garden_defaults(self) -> None: |
| 62 | + if self.garden: |
| 63 | + self.garden_area = 10 |
| 64 | + self.garden_orientation = "north" |
| 65 | + else: |
| 66 | + self.garden_area = None |
| 67 | + self.garden_orientation = None |
| 68 | + |
| 69 | + @api.ondelete(at_uninstall=False) |
| 70 | + def _unlink_except_new_or_cancelled(self) -> None: |
| 71 | + if any(record.state not in {"new", "cancelled"} for record in self): |
| 72 | + raise UserError(self.env._("Cannot delete properties unless they are new or cancelled.")) |
| 73 | + |
| 74 | + # Public methods |
| 75 | + def action_set_sold(self) -> bool: |
| 76 | + for record in self: |
| 77 | + if record.state == "sold": |
| 78 | + continue |
| 79 | + |
| 80 | + if record.state == "cancelled": |
| 81 | + raise UserError(record.env._("Cancelled properties cannot be sold.")) |
| 82 | + |
| 83 | + record.state = "sold" |
| 84 | + return True |
| 85 | + |
| 86 | + def action_set_cancelled(self) -> bool: |
| 87 | + for record in self: |
| 88 | + if record.state == "cancelled": |
| 89 | + continue |
| 90 | + |
| 91 | + if record.state == "sold": |
| 92 | + raise UserError(record.env._("Sold properties cannot be cancelled.")) |
| 93 | + |
| 94 | + record.state = "cancelled" |
| 95 | + return True |
| 96 | + |
| 97 | + # Constraints |
| 98 | + _check_expected_price_strict_positive = models.Constraint( |
| 99 | + "CHECK(expected_price > 0)", |
| 100 | + "A property's expected price must be strictly greater than 0.", |
| 101 | + ) |
| 102 | + |
| 103 | + _check_selling_price_positive = models.Constraint( |
| 104 | + "CHECK(selling_price >= 0)", |
| 105 | + "A property's selling price must be equal to or greater than 0.", |
| 106 | + ) |
| 107 | + |
| 108 | + @api.constrains("expected_price", "selling_price") |
| 109 | + def _check_selling_price_percentage(self) -> None: |
| 110 | + for record in self: |
| 111 | + # Selling price is zero when no offer has been accepted |
| 112 | + if not float_is_zero(record.selling_price, precision_digits=2) and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0: |
| 113 | + raise ValidationError(record.env._("A property's selling price cannot be lower than 90 percent of its expected price.")) |
0 commit comments