|
| 1 | +from dateutil.relativedelta import relativedelta |
| 2 | + |
| 3 | +from odoo import models, fields, api |
| 4 | +from odoo.exceptions import UserError, ValidationError |
| 5 | +from odoo.tools import float_is_zero, float_compare |
| 6 | + |
| 7 | + |
| 8 | +class Property(models.Model): |
| 9 | + _name = 'estate.property' |
| 10 | + _description = 'Property' |
| 11 | + _sql_constraints = [ |
| 12 | + ('check_expected_price', 'CHECK(expected_price > 0)', 'The Expected Price must be positive.'), |
| 13 | + ('check_selling_price', 'CHECK(selling_price >= 0)', 'The Selling Price must be positive.'), |
| 14 | + ('check_bedrooms', 'CHECK(bedrooms >= 0)', 'The number of bedrooms must be positive.'), |
| 15 | + ('check_living_area', 'CHECK(living_area > 0)', 'The living area must be positive.'), |
| 16 | + ('check_facades', 'CHECK(facades > 0)', 'The number of facades must be positive.'), |
| 17 | + ('check_name_unique', 'UNIQUE(name)', 'The Property name must be unique.') |
| 18 | + ] |
| 19 | + _order = 'id desc' |
| 20 | + |
| 21 | + name = fields.Char(string='Title', required=True) |
| 22 | + description = fields.Text() |
| 23 | + postcode = fields.Char() |
| 24 | + date_availability = fields.Date(string='Available From', copy=False, |
| 25 | + default=lambda self: fields.Datetime.today() + relativedelta(months=3)) |
| 26 | + expected_price = fields.Float(required=True) |
| 27 | + selling_price = fields.Float(readonly=True, copy=False) |
| 28 | + bedrooms = fields.Integer(default=2) |
| 29 | + living_area = fields.Integer(string='Living Area (m²)') |
| 30 | + facades = fields.Integer() |
| 31 | + garage = fields.Boolean() |
| 32 | + garden = fields.Boolean() |
| 33 | + garden_area = fields.Integer(string='Garden Area (m²)') |
| 34 | + garden_orientation = fields.Selection([ |
| 35 | + ('north', 'North'), |
| 36 | + ('east', 'East'), |
| 37 | + ('south', 'South'), |
| 38 | + ('west', 'West') |
| 39 | + ]) |
| 40 | + active = fields.Boolean(default=True) |
| 41 | + state = fields.Selection([ |
| 42 | + ('new', 'New'), |
| 43 | + ('offer_received', 'Offer Received'), |
| 44 | + ('offer_accepted', 'Offer Accepted'), |
| 45 | + ('sold', 'Sold'), |
| 46 | + ('canceled', 'Canceled') |
| 47 | + ], default='new', required=True) |
| 48 | + property_type_id = fields.Many2one('estate.property.type', string='Property Type', required=True) |
| 49 | + buyer_id = fields.Many2one('res.partner', string='Buyer', copy=False) |
| 50 | + salesperson_id = fields.Many2one('res.users', string='Sales Person', default=lambda self: self.env.user) |
| 51 | + tag_ids = fields.Many2many('estate.property.tag', string='Tags') |
| 52 | + offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers') |
| 53 | + total_area = fields.Integer(string='Total Area (m²)', compute='_compute_total_area') |
| 54 | + best_price = fields.Float(compute='_compute_best_price') |
| 55 | + |
| 56 | + @api.depends('living_area', 'garden_area') |
| 57 | + def _compute_total_area(self): |
| 58 | + for record in self: |
| 59 | + record.total_area = record.living_area + record.garden_area |
| 60 | + |
| 61 | + @api.depends('offer_ids.price') |
| 62 | + def _compute_best_price(self): |
| 63 | + for record in self: |
| 64 | + if len(record.offer_ids) == 0: |
| 65 | + record.best_price = 0 |
| 66 | + else: |
| 67 | + record.best_price = max(record.offer_ids.mapped('price')) |
| 68 | + |
| 69 | + @api.onchange('garden') |
| 70 | + def _onchange_garden(self): |
| 71 | + if self.garden: |
| 72 | + self.garden_orientation = 'north' |
| 73 | + self.garden_area = 10 |
| 74 | + else: |
| 75 | + self.garden_orientation = None |
| 76 | + self.garden_area = 0 |
| 77 | + |
| 78 | + @api.constrains('selling_price', 'expected_price') |
| 79 | + def _check_date_end(self): |
| 80 | + for record in self: |
| 81 | + if not float_is_zero(record.selling_price, precision_digits=2) and float_compare(record.selling_price, |
| 82 | + 0.9 * record.expected_price, |
| 83 | + precision_digits=2) >= 0: |
| 84 | + raise ValidationError("The selling price must not be below 90% of the expected price.") |
| 85 | + |
| 86 | + def action_sold(self): |
| 87 | + for record in self: |
| 88 | + if record.state in ['sold', 'canceled']: |
| 89 | + raise UserError('Cannot mark as sold.') |
| 90 | + record.state = 'sold' |
| 91 | + return True |
| 92 | + |
| 93 | + def action_canceled(self): |
| 94 | + for record in self: |
| 95 | + if record.state in ['sold', 'canceled']: |
| 96 | + raise UserError('Cannot mark as canceled.') |
| 97 | + record.state = 'canceled' |
| 98 | + return True |
| 99 | + |
| 100 | + def action_reset(self): |
| 101 | + for record in self: |
| 102 | + if record.state not in ['sold', 'canceled']: |
| 103 | + raise UserError('Cannot reset.') |
| 104 | + record.state = 'new' |
| 105 | + return True |
| 106 | + |
| 107 | + # Make sure that only one offer is accepted |
| 108 | + def set_accepted_offer(self, offer): |
| 109 | + for record in self: |
| 110 | + if offer is not None: |
| 111 | + record.state = 'offer_accepted' |
| 112 | + record.selling_price = offer.price |
| 113 | + record.buyer_id = offer.partner_id |
| 114 | + else: |
| 115 | + record.state = 'offer_received' |
| 116 | + record.selling_price = 0 |
| 117 | + record.buyer_id = None |
| 118 | + for o in record.offer_ids: |
| 119 | + if o.state == 'accepted' and (offer is None or o.id != offer.id): |
| 120 | + o.action_reset(propagate=False) |
0 commit comments