Skip to content

Commit 00c56fc

Browse files
committed
[ADD] estate: Server Framework 101 implementation
- Create models for properties, types, tags, and offers - Add views: form, list, kanban, search with filters and grouped displays - Implement business logic: compute fields, onchanges, actions (sold, cancel, accept, refuse), constraints (SQL and Python) - Use field attributes: readonly, invisible, optional - Apply UI enhancements: decorations, badges, smart buttons - Add seller-based property listings - Prevent deletion of properties unless in allowed states - Restrict offer creation to higher prices than existing ones - Add default ordering on models - Create estate_account module to generate an invoice upon property sale
1 parent fbf9ee9 commit 00c56fc

19 files changed

+638
-0
lines changed

estate/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models

estate/__manifest__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
'name': 'Real Estate',
3+
'version': '1.0',
4+
'description': 'Real Estate Management System',
5+
'summary': 'Real Estate Management System',
6+
'license': 'LGPL-3',
7+
'depends': ['base'],
8+
'data': [
9+
'security/ir.model.access.csv',
10+
'views/estate_property_offer.xml',
11+
'views/estate_property_tag.xml',
12+
'views/estate_property_type.xml',
13+
'views/estate_property.xml',
14+
'views/estate_menus.xml',
15+
'views/res_user.xml',
16+
],
17+
'auto_install': False,
18+
'application': True,
19+
}

estate/models/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import estate_property
2+
from . import estate_property_offer
3+
from . import estate_property_tag
4+
from . import estate_property_type
5+
from . import res_users

estate/models/estate_property.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from dateutil.relativedelta import relativedelta
2+
from odoo import _, api, fields, models
3+
from odoo.exceptions import UserError
4+
5+
6+
class EstateProperty(models.Model):
7+
_name = 'estate.property'
8+
_description = 'Real estate property with offers, pricing, and availability.'
9+
_order = 'id desc'
10+
11+
_sql_constraints = [
12+
('check_expected_price', 'CHECK(expected_price > 0)', 'Expected price must be strictly positive!'),
13+
('check_selling_price', 'CHECK(selling_price >= 0)', 'Selling price must be positive!'),
14+
]
15+
16+
name = fields.Char('Title', required=True)
17+
description = fields.Text('Description')
18+
postcode = fields.Char('Postcode')
19+
date_availability = fields.Date(
20+
'Availability From',
21+
copy=False,
22+
default=lambda self: fields.Date.today() + relativedelta(months=3),
23+
)
24+
expected_price = fields.Float('Expected Price', required=True)
25+
selling_price = fields.Float('Selling Price', readonly=True, copy=False)
26+
best_price = fields.Float('Best Price', compute='_compute_best_price', store=True)
27+
property_type_id = fields.Many2one('estate.property.type', 'Property Type')
28+
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
29+
buyer_id = fields.Many2one('res.partner', 'Buyer')
30+
seller_id = fields.Many2one('res.users', 'Seller', default=lambda self: self.env.user)
31+
bedrooms = fields.Integer('Bedrooms', default=2)
32+
living_area = fields.Integer('Living Area (sqm)')
33+
facades = fields.Integer('Facades')
34+
garage = fields.Boolean('Garage')
35+
garden = fields.Boolean('Garden')
36+
garden_area = fields.Integer('Garden Area')
37+
garden_orientation = fields.Selection(
38+
[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')],
39+
'Garden Orientation',
40+
)
41+
total_area = fields.Integer('Total Area (sqm)', compute='_compute_total_area', store=True)
42+
state = fields.Selection(
43+
[
44+
('new', 'New'),
45+
('offer_received', 'Offer Received'),
46+
('offer_accepted', 'Offer Accepted'),
47+
('sold', 'Sold'),
48+
('cancelled', 'Cancelled'),
49+
],
50+
'State',
51+
default='new',
52+
required=True,
53+
copy=False,
54+
compute='_compute_state',
55+
store=True,
56+
)
57+
offer_ids = fields.One2many('estate.property.offer', 'property_id', 'Offers')
58+
active = fields.Boolean('Active', default=True)
59+
60+
###### COMPUTE ######
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.price')
67+
def _compute_best_price(self):
68+
for record in self:
69+
prices = record.offer_ids.mapped('price')
70+
if prices:
71+
record.best_price = max(prices)
72+
else:
73+
record.best_price = 0.0
74+
75+
@api.depends('offer_ids')
76+
def _compute_state(self):
77+
for record in self:
78+
if not record.offer_ids:
79+
record.state = 'new'
80+
elif record.state == 'new':
81+
record.state = 'offer_received'
82+
83+
###### ONCHANGE ######
84+
@api.onchange('garden')
85+
def _onchange_garden(self):
86+
if self.garden:
87+
self.garden_area = 10
88+
self.garden_orientation = 'north'
89+
else:
90+
self.garden_area = 0
91+
self.garden_orientation = False
92+
93+
###### CRUD ######
94+
def unlink(self):
95+
if any(record.state not in ['new', 'cancelled'] for record in self):
96+
raise UserError(_('You cannot delete a property that is not new or cancelled.'))
97+
return super().unlink()
98+
99+
###### ACTION ######
100+
def action_sold(self):
101+
for record in self:
102+
if record.state == 'sold':
103+
raise UserError(_('Property is already sold.'))
104+
if record.state == 'cancelled':
105+
raise UserError(_('Property is cancelled and cannot be sold.'))
106+
if record.state != 'offer_accepted':
107+
raise UserError(_('You must accept an offer before marking the property as sold.'))
108+
109+
record.state = 'sold'
110+
return True
111+
112+
def action_cancel(self):
113+
for record in self:
114+
if record.state == 'cancelled':
115+
raise UserError(_('Property is already cancelled.'))
116+
if record.state == 'sold':
117+
raise UserError(_('Sold properties cannot be cancelled.'))
118+
119+
record.state = 'cancelled'
120+
return True
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from dateutil.relativedelta import relativedelta
2+
from odoo import _, api, fields, models
3+
from odoo.exceptions import ValidationError
4+
from odoo.tools.float_utils import float_compare
5+
6+
7+
class EstatePropertyOffer(models.Model):
8+
_name = 'estate.property.offer'
9+
_description = 'An offer for a property'
10+
_order = 'price desc'
11+
12+
_sql_constraints = [
13+
('check_price', 'CHECK(price > 0)', 'Price must be strictly positive!'),
14+
]
15+
16+
price = fields.Float('Price')
17+
status = fields.Selection(
18+
[('accepted', 'Accepted'), ('refused', 'Refused'), ('pending', 'Pending')],
19+
'Status',
20+
default='pending',
21+
required=True,
22+
copy=False,
23+
)
24+
partner_id = fields.Many2one('res.partner', 'Partner', required=True)
25+
property_id = fields.Many2one('estate.property', 'Property', required=True)
26+
property_type_id = fields.Many2one(
27+
'estate.property.type',
28+
'Property Type',
29+
related='property_id.property_type_id',
30+
store=True,
31+
)
32+
validity = fields.Integer('Validity (days)', help='Validity in days', default=7)
33+
date_deadline = fields.Date(
34+
'Deadline',
35+
compute='_compute_date_deadline',
36+
inverse='_inverse_date_deadline',
37+
store=True,
38+
)
39+
40+
###### COMPUTE ######
41+
@api.depends('validity')
42+
def _compute_date_deadline(self):
43+
for record in self:
44+
if not record.validity:
45+
record.date_deadline = False
46+
else:
47+
record.date_deadline = fields.Date.today() + relativedelta(days=record.validity)
48+
49+
def _inverse_date_deadline(self):
50+
for record in self:
51+
if not record.date_deadline:
52+
record.validity = 0
53+
else:
54+
record.validity = (record.date_deadline - fields.Date.today()).days
55+
56+
###### CRUD ######
57+
@api.model_create_multi
58+
def create(self, vals):
59+
for val in vals:
60+
val_price = val.get('price')
61+
val_property_id = val.get('property_id')
62+
if not val_price or not val_property_id:
63+
raise ValidationError(_('Price and Property ID are required.'))
64+
property_id = self.env['estate.property'].browse(val_property_id)
65+
if not property_id:
66+
raise ValidationError(_(f'Property with ID {val_property_id} does not exist.'))
67+
best_price = property_id.best_price
68+
if float_compare(val_price, best_price, precision_digits=2) <= 0:
69+
raise ValidationError(_(f'The offer price must be higher than the current best price (${best_price}).'))
70+
return super().create(vals)
71+
72+
###### ACTIONS ######
73+
def action_accept(self):
74+
for record in self:
75+
record.status = 'accepted'
76+
record.property_id.selling_price = record.price
77+
record.property_id.buyer_id = record.partner_id
78+
record.property_id.state = 'offer_accepted'
79+
80+
other_offers = record.property_id.offer_ids.filtered(lambda x: x.id != record.id)
81+
other_offers.action_refuse()
82+
return True
83+
84+
def action_refuse(self):
85+
for record in self:
86+
record.status = 'refused'
87+
return True
88+
89+
def action_revert(self):
90+
for record in self:
91+
if record.status == 'accepted':
92+
record.property_id.state = 'offer_received'
93+
record.property_id.buyer_id = False
94+
record.property_id.selling_price = 0
95+
record.status = 'pending'
96+
return True
97+
98+
###### CONSTRAINTS ######
99+
@api.constrains('price')
100+
def _check_price(self):
101+
for record in self:
102+
min_offer_price = record.property_id.expected_price * 0.9
103+
if float_compare(record.price, min_offer_price, precision_digits=2) < 0:
104+
raise ValidationError(
105+
_(f'The offer price must be at least 90% of the expected price (${min_offer_price}).')
106+
)

estate/models/estate_property_tag.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from random import randint
2+
3+
from odoo import fields, models
4+
5+
6+
class EstatePropertyTag(models.Model):
7+
_name = 'estate.property.tag'
8+
_description = 'Tag for categorizing estate properties'
9+
_order = 'name asc'
10+
11+
_sql_constraints = [
12+
('unique_name', 'UNIQUE(name)', 'Tag name must be unique!'),
13+
]
14+
15+
def _get_default_color(self):
16+
return randint(1, 11)
17+
18+
name = fields.Char('Name', required=True)
19+
color = fields.Integer('Color', default=_get_default_color)

estate/models/estate_property_type.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from odoo import api, fields, models
2+
3+
4+
class EstatePropertyType(models.Model):
5+
_name = 'estate.property.type'
6+
_description = 'Type for categorizing estate properties'
7+
_order = 'name asc'
8+
9+
_sql_constraints = [
10+
('unique_name', 'UNIQUE(name)', 'Type name must be unique!'),
11+
]
12+
13+
sequence = fields.Integer('Sequence', default=10)
14+
name = fields.Char('Name', required=True)
15+
property_ids = fields.One2many('estate.property', 'property_type_id', 'Properties')
16+
offer_ids = fields.One2many('estate.property.offer', 'property_type_id', 'Offers')
17+
offer_count = fields.Integer('Offers Count', compute='_compute_offer_count')
18+
19+
@api.depends('offer_ids')
20+
def _compute_offer_count(self):
21+
for record in self:
22+
record.offer_count = len(record.offer_ids)

estate/models/res_users.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from odoo import fields, models
2+
3+
4+
class ResUsers(models.Model):
5+
_inherit = 'res.users'
6+
7+
property_ids = fields.One2many(
8+
'estate.property',
9+
'seller_id',
10+
'Properties',
11+
domain=[('state', 'in', ['new', 'offer_received'])],
12+
)

estate/security/ir.model.access.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1
3+
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1
4+
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
5+
estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1

estate/views/estate_menus.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo>
3+
<menuitem id="real_estate_root_menu" name="Real Estate">
4+
<menuitem id="advertissements_menu" name="Advertissements" sequence="10">
5+
<menuitem id="advertissements_property_menu" name="Properties" action="estate_property_action" sequence="10"/>
6+
</menuitem>
7+
8+
<menuitem id="settings_menu" name="Settings" sequence="20">
9+
<menuitem id="settings_property_types_menu" name="Property Types" action="estate_property_types_action" sequence="10"/>
10+
<menuitem id="settings_property_tags_menu" name="Property Tags" action="estate_property_tags_action" sequence="20"/>
11+
</menuitem>
12+
</menuitem>
13+
</odoo>

0 commit comments

Comments
 (0)