|
| 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 = 'An individual estate property listing' |
| 10 | + _order = 'id desc' |
| 11 | + _sql_constraints = [ |
| 12 | + ( |
| 13 | + 'estate_property_expected_price_positive', |
| 14 | + 'CHECK(expected_price > 0)', |
| 15 | + 'The expected price must be strictly positive.', |
| 16 | + ), |
| 17 | + ( |
| 18 | + 'estate_property_selling_price_non_negative', |
| 19 | + 'CHECK(selling_price >= 0)', |
| 20 | + 'The selling price must be non negative.', |
| 21 | + ), |
| 22 | + ] |
| 23 | + |
| 24 | + @api.constrains('selling_price', 'expected_price') |
| 25 | + def _check_selling_price(self): |
| 26 | + for record in self: |
| 27 | + if not float_is_zero(record.selling_price, 2) and ( |
| 28 | + float_compare(record.selling_price, (record.expected_price * 0.9), 2) |
| 29 | + == -1 |
| 30 | + ): |
| 31 | + raise ValidationError( |
| 32 | + 'The selling price must be greater than 90% of the expected price.' |
| 33 | + 'You must reduce the expected price if you want to accept this offer' |
| 34 | + ) |
| 35 | + |
| 36 | + name = fields.Char('Title', required=True, default='Unknown') |
| 37 | + description = fields.Text('Description') |
| 38 | + postcode = fields.Char('Postcode') |
| 39 | + date_availability = fields.Date( |
| 40 | + 'Available From', |
| 41 | + default=lambda self: fields.Date.today() + relativedelta(months=3), |
| 42 | + copy=False, |
| 43 | + ) |
| 44 | + expected_price = fields.Float('Expected Price', required=True) |
| 45 | + selling_price = fields.Float('Selling Price', copy=False, readonly=True) |
| 46 | + bedrooms = fields.Integer('Bedrooms', default=2) |
| 47 | + living_area = fields.Integer('Living Area (sqm)') |
| 48 | + facades = fields.Integer('Facades') |
| 49 | + garage = fields.Boolean('Garage') |
| 50 | + garden = fields.Boolean('Garden') |
| 51 | + garden_area = fields.Integer('Garden Area (sqm)') |
| 52 | + garden_orientation = fields.Selection( |
| 53 | + [('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')], |
| 54 | + 'Garden Orientation', |
| 55 | + ) |
| 56 | + last_seen = fields.Datetime('Last Seen', default=fields.Datetime.now) |
| 57 | + active = fields.Boolean('Active', default=True) |
| 58 | + state = fields.Selection( |
| 59 | + [ |
| 60 | + ('new', 'New'), |
| 61 | + ('offer_received', 'Offer Received'), |
| 62 | + ('offer_accepted', 'Offer Accepted'), |
| 63 | + ('sold', 'Sold'), |
| 64 | + ('cancelled', 'Cancelled'), |
| 65 | + ], |
| 66 | + 'Status', |
| 67 | + default='new', |
| 68 | + copy=False, |
| 69 | + required=True, |
| 70 | + ) |
| 71 | + company_id = fields.Many2one( |
| 72 | + 'res.company', required=True, default=lambda self: self.env.company |
| 73 | + ) |
| 74 | + property_type_id = fields.Many2one('estate.property.type', 'Property Type') |
| 75 | + salesperson_id = fields.Many2one( |
| 76 | + 'res.users', 'Salesperson', default=lambda self: self.env.uid |
| 77 | + ) |
| 78 | + buyer_id = fields.Many2one('res.partner', 'Buyer', copy=False) |
| 79 | + tag_ids = fields.Many2many('estate.property.tag', string='Tags') |
| 80 | + offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers') |
| 81 | + best_price = fields.Float( |
| 82 | + 'Best Price', copy=False, readonly=True, compute='_compute_best_price' |
| 83 | + ) |
| 84 | + total_area = fields.Integer( |
| 85 | + compute='_compute_total_area', readonly=True, string='Total Area (sqm)' |
| 86 | + ) |
| 87 | + |
| 88 | + @api.depends('garden_area', 'living_area') |
| 89 | + def _compute_total_area(self): |
| 90 | + for record in self: |
| 91 | + record.total_area = record.living_area + record.garden_area |
| 92 | + |
| 93 | + @api.depends('offer_ids.price') |
| 94 | + def _compute_best_price(self): |
| 95 | + for record in self: |
| 96 | + record.best_price = ( |
| 97 | + max(record.offer_ids.mapped('price')) if record.offer_ids else 0 |
| 98 | + ) |
| 99 | + |
| 100 | + @api.onchange('garden') |
| 101 | + def _onchange_garden(self): |
| 102 | + """Set default garden area and orientation when garden is checked.""" |
| 103 | + if self.garden: |
| 104 | + self.garden_area = 10 |
| 105 | + self.garden_orientation = 'north' |
| 106 | + else: |
| 107 | + self.garden_area = 0 |
| 108 | + self.garden_orientation = False |
| 109 | + |
| 110 | + @api.onchange('offer_ids') |
| 111 | + def _onchange_offer_ids(self): |
| 112 | + if self.state == 'offer_received': |
| 113 | + if not self.offer_ids: |
| 114 | + self.state = 'new' |
| 115 | + return |
| 116 | + |
| 117 | + if self.state != 'new': |
| 118 | + raise UserError( |
| 119 | + 'You cannot modify offers when the property is not in New or Offer Received state.' |
| 120 | + ) |
| 121 | + |
| 122 | + def action_cancel(self): |
| 123 | + for record in self: |
| 124 | + if record.state == 'sold': |
| 125 | + raise UserError('You cannot cancel a sold property.') |
| 126 | + |
| 127 | + record.state = 'cancelled' |
| 128 | + |
| 129 | + return True |
| 130 | + |
| 131 | + def action_sold(self): |
| 132 | + for record in self: |
| 133 | + if record.state == 'cancelled': |
| 134 | + raise UserError('You cannot mark a cancelled property as sold.') |
| 135 | + |
| 136 | + if record.state != 'offer_accepted': |
| 137 | + raise UserError( |
| 138 | + 'You cannot mark a property as sold without accepting an offer.' |
| 139 | + ) |
| 140 | + |
| 141 | + record.state = 'sold' |
| 142 | + |
| 143 | + return True |
| 144 | + |
| 145 | + @api.ondelete(at_uninstall=False) |
| 146 | + def _unlink_if_property_new_cancelled(self): |
| 147 | + """Only allow New or Cancelled properties to be deleted.""" |
| 148 | + for record in self: |
| 149 | + if record.state not in ['new', 'cancelled']: |
| 150 | + raise UserError( |
| 151 | + 'You cannot delete a property that is not New or Cancelled.' |
| 152 | + ) |
0 commit comments