Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.analysis.extrapaths": [
"/home/odoo/Documents/odoo"
],

"files.insertFinalNewline": true
}
1 change: 1 addition & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
24 changes: 24 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
'name': "Estate",
'category': "Real Estate/Brokerage",
'depends': [
'base'
],
'data': [
'security/security.xml',
'security/ir.model.access.csv',
'data/estate.property.type.csv',
'views/estate_property_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/res_user_view.xml',
'views/estate_menus.xml',
],
"demo": [
'demo/demo_data.xml'
],
'application': True,
'author': "Odoo S.A.",
'license': "LGPL-3",
}
5 changes: 5 additions & 0 deletions estate/data/estate.property.type.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name
estate_property_type1,"Residential"
estate_property_type2,"Commercial"
estate_property_type3,"Industrial"
estate_property_type4,"Land"
102 changes: 102 additions & 0 deletions estate/demo/demo_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<odoo>

<!--
Resource: estate.property
-->

<record id="estate_property1" model="estate.property">
<field name="name">Big Villa</field>
<field name="state">new</field>
<field name="description">A nice and big villa</field>
<field name="postcode">12345</field>
<field name="date_availability">2026-02-02</field>
<field name="expected_price">1600000</field>
<field name="bedrooms">6</field>
<field name="living_area">100</field>
<field name="facades">4</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">100000</field>
<field name="garden_orientation">south</field>
<field name="property_type_id" ref="estate.estate_property_type1"/>
</record>

<record id="estate_property2" model="estate.property">
<field name="name">Trailer home</field>
<field name="state">canceled</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="date_availability">1970-01-01</field>
<field name="expected_price">100000</field>
<field name="selling_price">120000</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="estate.estate_property_type1"/>
</record>

<record id="estate_property3" model="estate.property">
<field name="name">One2many Test</field>
<field name="state">new</field>
<field name="description">Adding offers directly in the field test</field>
<field name="postcode">7965</field>
<field name="date_availability">2025-12-01</field>
<field name="expected_price">215000</field>
<field name="bedrooms">1</field>
<field name="living_area">65</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="estate.estate_property_type2"/>
<field name="offer_ids" eval="[
Command.create({
'partner_id': ref('base.res_partner_2'),
'price': '200000',
'validity': '14',
}),
Command.create({
'partner_id': ref('base.res_partner_12'),
'price': '210000',
'validity': '14',
})
]"/>
</record>

<!--
Resource: estate.property.offer
-->

<record id="estate_property_offer1" model="estate.property.offer">
<field name="property_id" ref="estate.estate_property1"/>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="price">10000</field>
<field name="validity">14</field>
<field name="create_date" eval="datetime.now()"/>
</record>

<record id="estate_property_offer2" model="estate.property.offer">
<field name="property_id" ref="estate.estate_property1"/>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="price">1500000</field>
<field name="validity">14</field>
<field name="create_date" eval="datetime.now()"/>
</record>

<record id="estate_property_offer3" model="estate.property.offer">
<field name="property_id" ref="estate.estate_property1"/>
<field name="partner_id" ref="base.res_partner_2"/>
<field name="price">1500001</field>
<field name="validity">14</field>
<field name="create_date" eval="datetime.now()"/>
</record>


<function model="estate.property.offer" name="accept_offer_button">
<value eval="[ref('estate_property_offer2')]"/>
</function>

<function model="estate.property.offer" name="refuse_offer_button">
<value eval="[ref('estate_property_offer1'), ref('estate_property_offer3')]"/>
</function>

</odoo>
5 changes: 5 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import res_users
132 changes: 132 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from odoo import models, fields, api, exceptions
from odoo.tools.float_utils import float_compare


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Estate Properties"
_order = "id desc"

_check_expected_price = models.Constraint(
'CHECK(expected_price > 0)',
'The expected price must be strictly positive.',
)
_check_selling_price = models.Constraint(
'CHECK(selling_price >= 0)',
'The selling price must be positive.',
)

####################################################
# FIELDS DECLARATION
####################################################

name = fields.Char(
string='Title',
required=True,
default="My new house"
)
description = fields.Text()
notes = fields.Html()
postcode = fields.Char()
date_availability = fields.Date(
string='Available From',
default=fields.Date.add(fields.Date.today(), months=3),
copy=False
)
expected_price = fields.Float(string='Expected price', required=True)
selling_price = fields.Float(
readonly=True,
copy=False
)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer(string='Living Area (sqm)')
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer(string='Garden Area (sqm)')
garden_orientation = fields.Selection(
selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')]
)
active = fields.Boolean(default=True)
state = fields.Selection(
selection=[('new', 'New'), ('offer_received', 'Offer Received'), ('offer_accepted', 'Offer Accepted'), ('sold', 'Sold'), ('canceled', 'Canceled')],
default='new'
)
property_type_id = fields.Many2one("estate.property.type")
partner_id = fields.Many2one("res.partner", string="Buyer")
user_id = fields.Many2one(
"res.users",
string="Salesperson",
default=lambda self: self.env.user
)
tag_ids = fields.Many2many("estate.property.tag")
offer_ids = fields.One2many(
"estate.property.offer",
"property_id"
)
total_area = fields.Float(compute="_compute_total_area")
best_offer = fields.Float(compute="_compute_best_price")
company_id = fields.Many2one(
"res.company",
required=True,
default=lambda self: self.env.user.company_id
)

####################################################
# FUNCTIONS DECLARATION
####################################################

def cancel_property_button(self):
self.ensure_one()
if self.state != "sold":
self.state = "canceled"
else:
raise exceptions.UserError("Sold properties cannot be canceled")
return True

def sold_property_button(self):
self.ensure_one()
if self.state != "canceled":
self.state = "sold"
else:
raise exceptions.UserError("Canceled properties cannot be sold")
return True

@api.depends("garden_area", "living_area")
def _compute_total_area(self):
for record in self:
record.total_area = record.garden_area + record.living_area

@api.depends("offer_ids.price")
def _compute_best_price(self):
for record in self:
record.best_offer = max(record.offer_ids.mapped('price')) if record.offer_ids else 0

@api.onchange("garden")
def _onchange_garden(self):
if not self.garden:
self.garden_orientation = "north"
self.garden_area = 10

@api.constrains("state")
def _check_offers_state(self):
self.ensure_one()
if self.state == 'sold' and not [offer for offer in self.offer_ids if offer.status == 'accepted']:
raise exceptions.UserError("You cannot sold a property without accepted offer")

@api.constrains('selling_price', 'expected_price')
def _check_date_end(self):
for record in self:
if float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0 and record.selling_price > 0:
raise exceptions.ValidationError(
r"The selling price must be at least 90% of the expected price! You must reduce the expected price if you want to accept this offer."
)

####################################################
# CRUD
####################################################

@api.ondelete(at_uninstall=False)
def _unlink_if_offer_unavailable(self):
if self.state not in ['new', 'canceled']:
raise exceptions.UserError("Can't delete an active property!")
93 changes: 93 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from odoo import models, fields, api, exceptions


class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Estate Property Offer"
_order = "price desc"

_check_offer_price = models.Constraint(
'CHECK(price > 0)',
'The offer price must be strictly positive.',
)

####################################################
# FIELDS DECLARATION
####################################################

price = fields.Float(required=True)
status = fields.Selection(
copy=False,
selection=[("accepted", "Accepted"), ("refused", "Refused")]
)
partner_id = fields.Many2one(
"res.partner",
required=True
)
property_id = fields.Many2one(
"estate.property",
required=True
)
validity = fields.Integer(
default=7,
string="Validity (days)"
)
date_deadline = fields.Date(
compute="_compute_date_deadline",
inverse="_inverse_date_deadline",
string="Deadline"
)
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

####################################################
# FUNCTIONS DECLARATION
####################################################

def accept_offer_button(self):
for record in self:
for offer in record.property_id.offer_ids:
if offer.status == "accepted" and offer.partner_id != record.partner_id:
raise exceptions.UserError("You can only accept one offer !")
break
record.status = "accepted"
record.property_id.partner_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id.state = "offer_accepted"
return True

def refuse_offer_button(self):
for record in self:
if record.status == "accepted":
record.property_id.partner_id = ""
record.property_id.selling_price = 0
record.status = "refused"
return True

@api.depends("create_date", "validity")
def _compute_date_deadline(self):
for record in self:
if not record.create_date:
record.create_date = fields.Date.today()
record.date_deadline = fields.Date.add(fields.Date.to_date(record.create_date), days=record.validity)

def _inverse_date_deadline(self):
for record in self:
record.validity = (fields.Date.to_date(record.date_deadline) - fields.Date.to_date(record.create_date)).days

####################################################
# CRUD
####################################################

@api.model
def create(self, vals):
for val in vals:
property_id = self.env['estate.property'].browse(val['property_id'])
if property_id.state == 'sold':
raise exceptions.UserError("Can't add an offer to an already sold property!")
for offer in property_id.offer_ids:
if offer.price > val['price']:
raise exceptions.UserError("Can't add an offer with smaller price than a previous one!")
offers = super().create(vals)
offers.property_id.state = "offer_received"

return offers
15 changes: 15 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import models, fields


class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Estate Property Tag"
_order = "name asc"

_check_unique_name = models.Constraint(
'UNIQUE (name)',
'Type names must be unique.',
)

name = fields.Char(required=True)
color = fields.Integer()
Loading