|
| 1 | +# Python Imports |
| 2 | +from datetime import date |
| 3 | +from dateutil.relativedelta import relativedelta |
| 4 | + |
| 5 | +# Odoo Imports |
| 6 | +from odoo import _, api, fields, models |
| 7 | +from odoo.exceptions import UserError, ValidationError |
| 8 | +from odoo.tools.float_utils import float_compare, float_is_zero |
| 9 | + |
| 10 | + |
| 11 | +class EstateProperty(models.Model): |
| 12 | + _name = 'estate.property' |
| 13 | + _description = 'Estate Property' |
| 14 | + _order = 'id desc' |
| 15 | + |
| 16 | + # ----------------------------- |
| 17 | + # Field Declarations |
| 18 | + # ----------------------------- |
| 19 | + name = fields.Char(string='Title', required=True, help='Title or name of the property.') |
| 20 | + description = fields.Text(string='Description', help='Detailed description of the property.') |
| 21 | + postcode = fields.Char(string='Postcode', help='Postal code of the property location.') |
| 22 | + date_availability = fields.Date( |
| 23 | + string='Availability From', |
| 24 | + copy=False, |
| 25 | + default=(date.today() + relativedelta(months=3)), |
| 26 | + help='Date from which the property will be available.' |
| 27 | + ) |
| 28 | + expected_price = fields.Float(string='Expected Price', required=True, help='Price expected by the seller for this property.') |
| 29 | + selling_price = fields.Float(string='Selling Price', readonly=True, copy=False, help='Final selling price once the property is sold.') |
| 30 | + bedrooms = fields.Integer(string='Bedrooms', default=2, help='Number of bedrooms in the property.') |
| 31 | + living_area = fields.Integer(string='Living Area (sqm)', help='Living area size in square meters.') |
| 32 | + facades = fields.Integer(string='Facades', help='Number of facades of the property.') |
| 33 | + garage = fields.Integer(string='Garage', help='Number of garage spaces.') |
| 34 | + garden = fields.Boolean(string='Garden', help='Whether the property has a garden.') |
| 35 | + garden_area = fields.Integer(string='Garden Area (sqm)', help='Size of the garden area in square meters.') |
| 36 | + garden_orientation = fields.Selection( |
| 37 | + string='Garden Orientation', |
| 38 | + selection=[ |
| 39 | + ('north', 'North'), |
| 40 | + ('south', 'South'), |
| 41 | + ('east', 'East'), |
| 42 | + ('west', 'West'), |
| 43 | + ], |
| 44 | + default='north', help='Direction the garden faces.') |
| 45 | + state = fields.Selection( |
| 46 | + string='Status', |
| 47 | + selection=[ |
| 48 | + ('new', 'New'), |
| 49 | + ('offer_received', 'Offer Received'), |
| 50 | + ('offer_accepted', 'Offer Accepted'), |
| 51 | + ('sold', 'Sold'), |
| 52 | + ('cancelled', 'Cancelled'), |
| 53 | + ], |
| 54 | + required=True, copy=False, default='new', help='Current status of the property.' |
| 55 | + ) |
| 56 | + active = fields.Boolean(string='Active', default=True, help='Whether the property is active and visible.') |
| 57 | + property_type_id = fields.Many2one('estate.property.type', string='Property Type', help='Type or category of the property.') |
| 58 | + buyer_id = fields.Many2one('res.partner', string='Buyer', copy=False, help='Partner who bought the property.') |
| 59 | + sales_id = fields.Many2one('res.users', string='Salesman', default=lambda self: self.env.user, help='Salesperson responsible for the property.') |
| 60 | + tag_ids = fields.Many2many('estate.property.tag', string='Tags', help='Tags to classify the property.') |
| 61 | + offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers', help='Offers made on this property.') |
| 62 | + |
| 63 | + # ----------------------------- |
| 64 | + # SQL Constraints |
| 65 | + # ----------------------------- |
| 66 | + _sql_constraints = [ |
| 67 | + ('check_expected_price', 'CHECK(expected_price > 0)', 'Expected price cannot be negative.') |
| 68 | + ] |
| 69 | + |
| 70 | + # ----------------------------- |
| 71 | + # Computed Fields |
| 72 | + # ----------------------------- |
| 73 | + total = fields.Float( |
| 74 | + string='Total (sqm)', |
| 75 | + compute='_compute_total_area', |
| 76 | + help='Total area of the property including living and garden areas.' |
| 77 | + ) |
| 78 | + best_price = fields.Float( |
| 79 | + string='Best Offer', |
| 80 | + compute='_compute_best_price', |
| 81 | + help='Highest offer price received for the property.' |
| 82 | + ) |
| 83 | + |
| 84 | + @api.depends('living_area', 'garden_area') |
| 85 | + def _compute_total_area(self): |
| 86 | + """Compute total area as sum of living area and garden area.""" |
| 87 | + for record in self: |
| 88 | + record.total = (record.living_area or 0) + (record.garden_area or 0) |
| 89 | + |
| 90 | + @api.depends('offer_ids.price') |
| 91 | + def _compute_best_price(self): |
| 92 | + """Compute highest offer price or 0 if no offers.""" |
| 93 | + for record in self: |
| 94 | + offer_prices = record.offer_ids.mapped('price') |
| 95 | + record.best_price = max(offer_prices) if offer_prices else 0.0 |
| 96 | + |
| 97 | + # ----------------------------- |
| 98 | + # Action Methods |
| 99 | + # ----------------------------- |
| 100 | + def action_sold(self): |
| 101 | + """Set property state to 'sold', with validation against invalid states.""" |
| 102 | + for record in self: |
| 103 | + if record.state == 'cancelled': |
| 104 | + raise UserError('A cancelled property cannot be set as sold.') |
| 105 | + elif record.state == 'sold': |
| 106 | + raise UserError('Property is already sold.') |
| 107 | + else: |
| 108 | + record.state = 'sold' |
| 109 | + |
| 110 | + def action_cancel(self): |
| 111 | + """Set property state to 'cancelled', with validation against invalid states.""" |
| 112 | + for record in self: |
| 113 | + if record.state == 'sold': |
| 114 | + raise UserError('A sold property cannot be cancelled.') |
| 115 | + elif record.state == 'cancelled': |
| 116 | + raise UserError('Property is already cancelled.') |
| 117 | + else: |
| 118 | + record.state = 'cancelled' |
| 119 | + |
| 120 | + # ----------------------------- |
| 121 | + # Constraints |
| 122 | + # ----------------------------- |
| 123 | + @api.constrains('selling_price', 'expected_price') |
| 124 | + def _check_selling_price_above_90_percent(self): |
| 125 | + """ |
| 126 | + Validate selling price with float precision. |
| 127 | + Ignores zero selling price, otherwise enforces minimum 90% threshold. |
| 128 | + """ |
| 129 | + for record in self: |
| 130 | + if float_is_zero(record.selling_price, precision_digits=2): |
| 131 | + continue |
| 132 | + min_acceptable_price = 0.9 * record.expected_price |
| 133 | + if float_compare(record.selling_price, min_acceptable_price, precision_digits=2) < 0: |
| 134 | + raise ValidationError(_( |
| 135 | + "The selling price must be at least 90%% of the expected price.\n" |
| 136 | + "Expected Price: %(expected_price).2f\nSelling Price: %(selling_price).2f", |
| 137 | + { |
| 138 | + 'expected_price': record.expected_price, |
| 139 | + 'selling_price': record.selling_price |
| 140 | + } |
| 141 | + )) |
| 142 | + |
| 143 | + @api.ondelete(at_uninstall=False) |
| 144 | + def _check_can_be_deleted(self): |
| 145 | + """ |
| 146 | + Restrict deletion to properties in 'new' or 'cancelled' state. |
| 147 | + Raises UserError otherwise. |
| 148 | + """ |
| 149 | + for record in self: |
| 150 | + if record.state not in ['new', 'cancelled']: |
| 151 | + raise UserError('You can only delete properties that are New or Cancelled.') |
0 commit comments