Skip to content

Commit 4a9c637

Browse files
committed
[IMP] estate: enrich the models with new fields (best price and total area in estate property and validity and deadline in property offer).
Best price is computed on the maximum price of all the offers linked to the property. Total area is computed as the sum of the lving area and garden area. Validity is the number days that have passed since the creation of the offer during wich it remains valid. Deadline is the date until wich the offer remains valid. It is computed based on validity and when changed, updates validity.
1 parent 5b6bde8 commit 4a9c637

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

estate/models/estate_property.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from odoo import fields, models
1+
from odoo import api, fields, models
22
from datetime import timedelta
33

44
def default_availability_date(recordset):
5-
return fields.Datetime.today() + timedelta(days=90)
5+
return fields.Date.context_today(recordset) + timedelta(days=90)
66

77
class EstateProperty(models.Model):
88
_name = "estate.property"
@@ -26,6 +26,7 @@ class EstateProperty(models.Model):
2626
('east', 'East'),
2727
('west', 'West')
2828
], string='Garden orientation')
29+
total_area = fields.Integer('Total Area', compute='_compute_total_area')
2930
active = fields.Boolean('Active' ,default=True)
3031
state = fields.Selection(selection=[
3132
('new', 'New'),
@@ -38,4 +39,25 @@ class EstateProperty(models.Model):
3839
property_buyer_id = fields.Many2one('res.partner', string='Buyer', copy=False)
3940
property_salesperson_id = fields.Many2one('res.users', string="Salesperson", default=lambda self: self.env.user)
4041
property_tag_ids = fields.Many2many('estate.property.tag', string="Property tags")
41-
property_offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')
42+
property_offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')
43+
best_price = fields.Float('Best Price', compute='_compute_best_price')
44+
45+
@api.depends('living_area', 'garden_area')
46+
def _compute_total_area(self):
47+
for record in self:
48+
record.total_area = record.living_area + record.garden_area
49+
50+
@api.depends('property_offer_ids.price')
51+
def _compute_best_price(self):
52+
for record in self:
53+
offer_prices = record.property_offer_ids.mapped('price')
54+
record.best_price = max(offer_prices) if offer_prices else 0
55+
56+
@api.onchange("garden")
57+
def _onchange_garden(self):
58+
if self.garden:
59+
self.garden_area = 10
60+
self.garden_orientation = 'north'
61+
else:
62+
self.garden_area = 0
63+
self.garden_orientation = ''

estate/models/estate_property_offer.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from odoo import fields, models
1+
from odoo import api, fields, models
2+
from dateutil.relativedelta import relativedelta
23

34
class EstatePropertyOffer(models.Model):
45
_name = "estate.property.offer"
@@ -10,4 +11,17 @@ class EstatePropertyOffer(models.Model):
1011
('refused', 'Refused')
1112
], copy=False, string='Status')
1213
property_buyer_id = fields.Many2one('res.partner', string="Buyer", required=True)
13-
property_id = fields.Many2one('estate.property', string="Property", required=True)
14+
property_id = fields.Many2one('estate.property', string="Property", required=True)
15+
validity = fields.Integer('Validity (Days)', default=7)
16+
date_deadline = fields.Date('Deadline', compute='_compute_date_deadline', inverse='_inverse_validity')
17+
18+
@api.depends('validity')
19+
def _compute_date_deadline(self):
20+
for record in self:
21+
starting_date = record.create_date if record.create_date else fields.Date.context_today(self)
22+
record.date_deadline = starting_date + relativedelta(days=record.validity)
23+
24+
def _inverse_validity(self):
25+
for record in self:
26+
starting_date = fields.Date.to_date(record.create_date) if record.create_date else fields.Date.context_today(self)
27+
record.validity = (record.date_deadline - starting_date).days

estate/views/estate_property_offer_views.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
<sheet>
99
<group>
1010
<field name="price"/>
11-
<field name="property_buyer_id"/>
11+
<field name="property_buyer_id" string="Partner"/>
1212
<field name="status"/>
13+
<field name="validity"/>
14+
<field name="date_deadline"/>
1315
</group>
1416
</sheet>
1517
</form>
@@ -23,6 +25,8 @@
2325
<list string="Estate property offer">
2426
<field name="price"/>
2527
<field name="property_buyer_id" string="Partner"/>
28+
<field name="validity"/>
29+
<field name="date_deadline"/>
2630
<field name="status"/>
2731
</list>
2832
</field>

estate/views/estate_property_views.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</group>
4343
<group>
4444
<field name="expected_price"/>
45+
<field name="best_price"/>
4546
<field name="selling_price"/>
4647
</group>
4748
</group>
@@ -57,6 +58,7 @@
5758
<field name="garden"/>
5859
<field name="garden_area"/>
5960
<field name="garden_orientation"/>
61+
<field name="total_area"/>
6062
<field name="state"/>
6163
</group>
6264
</page>

0 commit comments

Comments
 (0)