|
| 1 | +from dateutil.relativedelta import relativedelta |
| 2 | + |
| 3 | +from odoo import api, fields, models |
| 4 | +from odoo.exceptions import UserError, ValidationError |
| 5 | +from odoo.tools import float_compare, float_is_zero |
| 6 | + |
| 7 | + |
| 8 | +class Property(models.Model): |
| 9 | + _name = 'estate.property' |
| 10 | + _description = "Estate property" |
| 11 | + _order = 'id desc' |
| 12 | + |
| 13 | + name = fields.Char(string="Title", required=True) |
| 14 | + description = fields.Text() |
| 15 | + postcode = fields.Char() |
| 16 | + date_availability = fields.Date( |
| 17 | + string="Available From", |
| 18 | + default=lambda self: fields.Date.today() + relativedelta(months=3), |
| 19 | + copy=False, |
| 20 | + ) |
| 21 | + expected_price = fields.Float(required=True) |
| 22 | + selling_price = fields.Float(readonly=True, copy=False) |
| 23 | + bedrooms = fields.Integer(default=2) |
| 24 | + living_area = fields.Integer(string="Living Area (sqm)") |
| 25 | + facades = fields.Integer() |
| 26 | + garage = fields.Boolean() |
| 27 | + garden = fields.Boolean() |
| 28 | + garden_area = fields.Integer(string="Garden Area (sqm)") |
| 29 | + garden_orientation = fields.Selection( |
| 30 | + selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')] |
| 31 | + ) |
| 32 | + state = fields.Selection( |
| 33 | + default="new", |
| 34 | + string="Status", |
| 35 | + selection=[ |
| 36 | + ('new', 'New'), |
| 37 | + ('offer_received', 'Offer Received'), |
| 38 | + ('offer_accepted', 'Offer Accepted'), |
| 39 | + ('sold', 'Sold'), |
| 40 | + ('cancelled', 'Cancelled'), |
| 41 | + ], |
| 42 | + required=True, |
| 43 | + copy=False, |
| 44 | + ) |
| 45 | + active = fields.Boolean(default=True) |
| 46 | + property_type_id = fields.Many2one('estate.property.type', string="Property Type") |
| 47 | + partner_id = fields.Many2one('res.partner', string="Buyer", readonly=True) |
| 48 | + user_id = fields.Many2one('res.users', string="Salesman", default=lambda self: self.env.user) |
| 49 | + tag_ids = fields.Many2many('estate.property.tag', string="Property Tags") |
| 50 | + offer_ids = fields.One2many('estate.property.offer', 'property_id', string="Offers") |
| 51 | + total_area = fields.Integer(string="Total Area (sqm)", compute='_compute_total_area') |
| 52 | + best_offer = fields.Float(default=0.0, compute="_compute_best_offer") |
| 53 | + |
| 54 | + _expected_price_strictly_pos = models.Constraint( |
| 55 | + "CHECK(expected_price > 0)", "The expected price must be strictly positive." |
| 56 | + ) |
| 57 | + _selling_price_pos = models.Constraint( |
| 58 | + "CHECK(selling_price >= 0)", "The selling price must be positive." |
| 59 | + ) |
| 60 | + |
| 61 | + @api.depends('living_area', 'garden_area') |
| 62 | + def _compute_total_area(self): |
| 63 | + for record in self: |
| 64 | + record.total_area = record.living_area + record.garden_area |
| 65 | + |
| 66 | + @api.depends('offer_ids') |
| 67 | + def _compute_best_offer(self): |
| 68 | + for record in self: |
| 69 | + record.best_offer = max(record.offer_ids.mapped('price')) if record.offer_ids else 0.0 |
| 70 | + |
| 71 | + @api.onchange('garden') |
| 72 | + def _onchange_garden(self): |
| 73 | + self.garden_area = 10 if self.garden else 0 |
| 74 | + self.garden_orientation = 'north' if self.garden else '' |
| 75 | + |
| 76 | + @api.constrains('selling_price', 'expected_price') |
| 77 | + def _check_selling_price(self): |
| 78 | + for record in self: |
| 79 | + if ( |
| 80 | + not float_is_zero(record.selling_price, precision_digits=2) and |
| 81 | + float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1 |
| 82 | + ): |
| 83 | + raise ValidationError( |
| 84 | + self.env._( |
| 85 | + "The selling price must be at least 90% of the expected price! " |
| 86 | + "You must reduce the expected price if you want to accept this offer." |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + def action_set_sold(self): |
| 91 | + if self.state == 'cancelled': |
| 92 | + raise UserError(self.env._("Cancelled properties cannot be sold.")) |
| 93 | + |
| 94 | + self.state = 'sold' |
| 95 | + return True |
| 96 | + |
| 97 | + def action_set_cancelled(self): |
| 98 | + if self.state == 'sold': |
| 99 | + raise UserError(self.env._("Sold properties cannot be cancelled.")) |
| 100 | + |
| 101 | + self.state = 'cancelled' |
| 102 | + return True |
| 103 | + |
| 104 | + @api.ondelete(at_uninstall=False) |
| 105 | + def _unlink_except_new_or_cancelled(self): |
| 106 | + for record in self: |
| 107 | + if record.state not in ['new', 'cancelled']: |
| 108 | + raise UserError( |
| 109 | + self.env._("Only new and cancelled properties can be deleted.") |
| 110 | + ) |
0 commit comments