|
1 |
| -from odoo import models, fields, api, exceptions |
2 |
| -from odoo.tools import float_utils # type: ignore |
3 | 1 | from datetime import date
|
| 2 | + |
4 | 3 | from dateutil.relativedelta import relativedelta
|
5 | 4 |
|
| 5 | +from odoo import _, api, exceptions, fields, models |
| 6 | +from odoo.tools import float_utils |
| 7 | + |
| 8 | + |
6 | 9 | class EstateProperty(models.Model):
|
7 |
| - _name = "estate.property" |
| 10 | + # ---------------------------------------- |
| 11 | + # Private attributes |
| 12 | + # ---------------------------------------- |
| 13 | + _name = 'estate.property' |
8 | 14 | _description = "Comprehensive platform for managing properties, sales, rentals, and client relationships throughout their entire lifecycle."
|
9 | 15 | _sql_constraints = [
|
10 |
| - ('check_expected_price_positive', 'CHECK (expected_price > 0)', 'Expected price must be positive.'), |
11 |
| - ('check_selling_price_positive', 'CHECK (selling_price >= 0)', 'Selling price must be positive.') |
| 16 | + ( |
| 17 | + 'check_expected_price_positive', |
| 18 | + 'CHECK (expected_price > 0)', |
| 19 | + "Expected price must be positive.", |
| 20 | + ), |
| 21 | + ( |
| 22 | + 'check_selling_price_positive', |
| 23 | + 'CHECK (selling_price >= 0)', |
| 24 | + "Selling price must be positive.", |
| 25 | + ), |
12 | 26 | ]
|
13 |
| - _order = "id desc" |
| 27 | + _order = 'id desc' |
14 | 28 |
|
| 29 | + # ---------------------------------------- |
| 30 | + # Field declarations |
| 31 | + # ---------------------------------------- |
15 | 32 | name = fields.Char(required=True)
|
16 | 33 | description = fields.Text()
|
17 | 34 | postcode = fields.Char()
|
18 |
| - date_availability = fields.Date(copy=False, default=lambda self: date.today() + relativedelta(months=3)) |
| 35 | + date_availability = fields.Date( |
| 36 | + copy=False, default=lambda self: date.today() + relativedelta(months=3) |
| 37 | + ) |
19 | 38 | expected_price = fields.Float(required=True)
|
20 | 39 | selling_price = fields.Float(readonly=True, copy=False)
|
21 |
| - @api.constrains('selling_price', 'expected_price') |
22 |
| - def _check_selling_price(self): |
23 |
| - for record in self: |
24 |
| - if record.selling_price !=0 and float_utils.float_compare( |
25 |
| - record.selling_price, |
26 |
| - record.expected_price * 0.9, |
27 |
| - precision_digits=2 |
28 |
| - ) < 0: |
29 |
| - raise exceptions.ValidationError("Selling price must be greater than or equal to 90% of expected price.") |
30 | 40 | bedrooms = fields.Integer(default=2)
|
31 | 41 | living_area = fields.Integer()
|
32 | 42 | facades = fields.Integer()
|
33 | 43 | garage = fields.Boolean()
|
34 |
| - |
35 | 44 | garden = fields.Boolean()
|
36 |
| - @api.onchange("garden") |
37 |
| - def _onchange_garden(self): |
38 |
| - if not self.garden: |
39 |
| - self.garden_area = 0 |
40 |
| - self.garden_orientation = False |
41 |
| - |
42 | 45 | garden_area = fields.Integer()
|
43 | 46 | garden_orientation = fields.Selection(
|
44 |
| - string='Garden Orientation', |
45 |
| - selection=[('n', 'North'), |
46 |
| - ('s', 'South'), |
47 |
| - ('e', 'East'), |
48 |
| - ('w', 'West')] |
| 47 | + string="Garden Orientation", |
| 48 | + selection=[ |
| 49 | + ('n', "North"), |
| 50 | + ('s', "South"), |
| 51 | + ('e', "East"), |
| 52 | + ('w', "West"), |
| 53 | + ], |
49 | 54 | )
|
50 | 55 | active = fields.Boolean(default=True)
|
51 | 56 | state = fields.Selection(
|
52 |
| - string='Status', |
53 |
| - selection=[('new', 'New'), |
54 |
| - ('offer_received', 'Offer Received'), |
55 |
| - ('offer_accepted', 'Offer Accepted'), |
56 |
| - ('sold', 'Sold'), |
57 |
| - ('cancelled', 'Cancelled')], |
| 57 | + string="Status", |
| 58 | + selection=[ |
| 59 | + ('new', "New"), |
| 60 | + ('offer_received', "Offer Received"), |
| 61 | + ('offer_accepted', "Offer Accepted"), |
| 62 | + ('sold', "Sold"), |
| 63 | + ('cancelled', "Cancelled"), |
| 64 | + ], |
58 | 65 | default='new',
|
59 | 66 | required=True,
|
60 |
| - copy=False |
| 67 | + copy=False, |
61 | 68 | )
|
62 |
| - |
63 |
| - buyer_id = fields.Many2one('res.partner', string='Buyer', index=True, copy=False) |
64 |
| - salesperson_id = fields.Many2one('res.users', string='Salesperson', index=True, default=lambda self: self.env.user) |
65 |
| - |
66 |
| - tag_ids = fields.Many2many('estate.property.tag', string='Tags') |
| 69 | + buyer_id = fields.Many2one( |
| 70 | + 'res.partner', string="Buyer", index=True, copy=False |
| 71 | + ) |
| 72 | + salesperson_id = fields.Many2one( |
| 73 | + 'res.users', |
| 74 | + string="Salesperson", |
| 75 | + index=True, |
| 76 | + default=lambda self: self.env.user, |
| 77 | + ) |
| 78 | + tag_ids = fields.Many2many('estate.property.tag', string="Tags") |
67 | 79 | offer_ids = fields.One2many('estate.property.offer', 'property_id')
|
68 |
| - type_id = fields.Many2one('estate.property.type', string='Property Type') |
69 |
| - |
| 80 | + type_id = fields.Many2one('estate.property.type', string="Property Type") |
70 | 81 | total_area = fields.Float(
|
71 |
| - compute='_compute_total_area', |
72 |
| - string='Total Area' |
| 82 | + compute="_compute_total_area" |
| 83 | + ) |
| 84 | + best_price = fields.Float( |
| 85 | + compute="_compute_best_price", readonly=True |
73 | 86 | )
|
| 87 | + |
| 88 | + # ---------------------------------------- |
| 89 | + # Compute, inverse and search methods |
| 90 | + # ---------------------------------------- |
74 | 91 | @api.depends('living_area', 'garden_area')
|
75 | 92 | def _compute_total_area(self):
|
76 |
| - for record in self: |
77 |
| - record.total_area = record.living_area + record.garden_area if record.garden_area else record.living_area |
| 93 | + for estate in self: |
| 94 | + estate.total_area = estate.living_area + estate.garden_area |
78 | 95 |
|
79 |
| - best_price = fields.Float( |
80 |
| - compute='_compute_best_price', |
81 |
| - string='Best Price', |
82 |
| - readonly=True |
83 |
| - ) |
84 | 96 | @api.depends('offer_ids.price')
|
85 | 97 | def _compute_best_price(self):
|
86 |
| - for record in self: |
87 |
| - record.best_price = max(record.offer_ids.mapped('price'), default=0.0) |
| 98 | + for estate in self: |
| 99 | + estate.best_price = max( |
| 100 | + estate.offer_ids.mapped('price'), default=0.0 |
| 101 | + ) |
| 102 | + |
| 103 | + # ---------------------------------------- |
| 104 | + # Constrains and onchange methods |
| 105 | + # ---------------------------------------- |
| 106 | + @api.constrains('selling_price', 'expected_price') |
| 107 | + def _check_selling_price(self): |
| 108 | + for estate in self: |
| 109 | + if ( |
| 110 | + estate.selling_price != 0 |
| 111 | + and float_utils.float_compare( |
| 112 | + estate.selling_price, |
| 113 | + estate.expected_price * 0.9, |
| 114 | + precision_digits=2, |
| 115 | + ) |
| 116 | + < 0 |
| 117 | + ): |
| 118 | + raise exceptions.ValidationError( |
| 119 | + _( |
| 120 | + "Selling price must be greater than or equal to 90% of expected price." |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + @api.onchange('garden') |
| 125 | + def _onchange_garden(self): |
| 126 | + if self.garden: |
| 127 | + if not self.garden_area: |
| 128 | + self.garden_area = 10 |
| 129 | + if not self.garden_orientation: |
| 130 | + self.garden_orientation = 's' |
| 131 | + else: |
| 132 | + self.garden_area = 0 |
| 133 | + self.garden_orientation = False |
| 134 | + |
| 135 | + # ---------------------------------------- |
| 136 | + # CRUD methods (ORM overrides) |
| 137 | + # ---------------------------------------- |
| 138 | + @api.ondelete(at_uninstall=False) |
| 139 | + def _unlink_if_user_new_cancelled(self): |
| 140 | + for estate in self: |
| 141 | + if estate.state not in ['new', 'cancelled']: |
| 142 | + raise exceptions.UserError( |
| 143 | + _("Can't delete an active property!") |
| 144 | + ) |
88 | 145 |
|
| 146 | + # ---------------------------------------- |
| 147 | + # Action methods |
| 148 | + # ---------------------------------------- |
89 | 149 | def action_sold_property(self):
|
90 |
| - for record in self: |
91 |
| - if record.state == 'cancelled': |
92 |
| - raise exceptions.UserError("Cannot set as sold if cancelled") |
93 |
| - record.state = 'sold' |
| 150 | + for estate in self: |
| 151 | + if estate.state == 'cancelled': |
| 152 | + raise exceptions.UserError(_("Cannot set as sold if cancelled")) |
| 153 | + estate.state = 'sold' |
| 154 | + # Set buyer from accepted offer |
| 155 | + accepted_offer = estate.offer_ids.filtered( |
| 156 | + lambda o: o.status == 'accepted' |
| 157 | + ) |
| 158 | + if accepted_offer: |
| 159 | + estate.buyer_id = accepted_offer.partner_id |
| 160 | + # Ensure selling price is set from the accepted offer |
| 161 | + if not estate.selling_price: |
| 162 | + estate.selling_price = accepted_offer.price |
94 | 163 | return True
|
95 | 164 |
|
96 | 165 | def action_cancel_property(self):
|
97 |
| - for record in self: |
98 |
| - if record.state == 'sold': |
99 |
| - raise exceptions.UserError("Cannot cancel if already sold") |
100 |
| - record.state = 'cancelled' |
| 166 | + for estate in self: |
| 167 | + if estate.state == 'sold': |
| 168 | + raise exceptions.UserError(_("Cannot cancel if already sold")) |
| 169 | + estate.state = 'cancelled' |
101 | 170 | return True
|
102 |
| - |
103 |
| - @api.ondelete(at_uninstall=False) |
104 |
| - def _unlink_if_user_new_cancelled(self): |
105 |
| - for record in self: |
106 |
| - if record.state not in ['new', 'cancelled']: |
107 |
| - raise exceptions.UserError("Can't delete an active property!") |
|
0 commit comments