Skip to content

Implementation of the Odoo Estate module as outlined in the official tutorial #847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: 18.0
Choose a base branch
from
Draft
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
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": "Real Estate",
"category": "Real Estate/Brokerage",
"application": True,
"installable": True,
"data": [
"security/security.xml",
"security/ir.model.access.csv",
"data/property_type.xml",
"views/estate_property_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_property_type_views.xml",
"views/estate_menus.xml",
"views/inherit_res_users_view.xml",
"report/estate_property_templates.xml",
"report/estate_property_reports.xml"
],
"demo": [
"demo/property.xml",
"demo/property_offer.xml",
],
"license": "AGPL-3",
}
22 changes: 22 additions & 0 deletions estate/data/property_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

<odoo>
<data noupdate="0">

<record id="property_type_residential" model="estate.property.type">
<field name="name">Residential</field>
</record>

<record id="property_type_commercial" model="estate.property.type">
<field name="name">Commercial</field>
</record>

<record id="property_type_industrial" model="estate.property.type">
<field name="name">Industrial</field>
</record>

<record id="property_type_land" model="estate.property.type">
<field name="name">Land</field>
</record>

</data>
</odoo>
66 changes: 66 additions & 0 deletions estate/demo/property.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

<odoo>
<record id="property_id1" 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">2020-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>
</record>

<record id="property_id2" model="estate.property">
<field name="name">Trailer home</field>
<field name="state">cancelled</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>
</record>

<record id="property_id3" model="estate.property">
<field name="name">Small home</field>
<field name="state">new</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="offer_ids" eval="[
Command.create({
'partner_id': ref('base.res_partner_12'),
'price': 1200000,
'validity': 14,
'date_deadline': (datetime.now() + timedelta(days=14)).date(),
}),
Command.create({
'partner_id': ref('base.res_partner_12'),
'price': 1300000,
'validity': 14,
'date_deadline': (datetime.now() + timedelta(days=14)).date(),
}),
Command.create({
'partner_id': ref('base.res_partner_10'),
'price': 1500000,
'validity': 14,
'date_deadline': (datetime.now() + timedelta(days=14)).date(),
})
]"/>
</record>
</odoo>
39 changes: 39 additions & 0 deletions estate/demo/property_offer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<odoo>
<record id="offer_azure_villa_1" model="estate.property.offer">
<field name="price">10000</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="property_id1"/>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=14)"/>
</record>

<record id="offer_azure_villa_2" model="estate.property.offer">
<field name="price">1500000</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="property_id1"/>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=14)"/>
</record>

<record id="offer_azure_villa_3" model="estate.property.offer">
<field name="price">1500001</field>
<field name="validity">14</field>
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="property_id1"/>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=14)"/>
</record>

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

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

<function model="estate.property.offer" name="action_offer_refuse">
<value eval="[ ref('offer_azure_villa_3')]"/>
</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 inherited_res_users
183 changes: 183 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
from odoo import fields, models, api
from odoo.exceptions import UserError, ValidationError
from dateutil.relativedelta import relativedelta
from datetime import date
from odoo.tools.float_utils import float_compare


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Estate Model"

name = fields.Char(string="Name")
description = fields.Text(string="Description")
postcode = fields.Text(string="Postcode")
date_availability = fields.Date(
string="Date Availability",
copy=False,
default=lambda self: date.today() + relativedelta(months=3),
)
expected_price = fields.Float(string="Expected Price")
selling_price = fields.Integer(string="Selling Price")
bedrooms = fields.Integer(string="Bedrooms")
living_area = fields.Integer(string="Living Area")
facades = fields.Integer(string="Facades")
garage = fields.Boolean(string="Garage")
garden = fields.Boolean(string="Garden")
garden_area = fields.Integer(string="Garden Area")

garden_orientation = fields.Selection(
string="Type",
selection=[
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
],
)

active = fields.Boolean(default=True, string="Active")

state = fields.Selection(
selection=[
("new", "New"),
("offer_received", "Offer Received "),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
string="State",
default="new",
copy=False,
)

# property will have Many to one relation with property type since many properties can belong to one property type

property_type_id = fields.Many2one("estate.property.type", "Property Type")

user_id = fields.Many2one(
"res.users",
string="Salesperson",
copy=False,
)

partner_id = fields.Many2one(
"res.partner",
string="Buyer",
copy=False,
)

tag_ids = fields.Many2many("estate.property.tag", string="Tags")

offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")

total_area = fields.Integer(
compute="_compute_total_property_area", string="Total Area"
)

best_price = fields.Integer(compute="_compute_best_price", string="Best Price")

status = fields.Char(default="new", string="Status")

company_id = fields.Many2one(
"res.company",
string="Company",
default=lambda self: self.env.company,
required=True,
)

_order = "id desc"

_sql_constraints = [
(
"check_expected_price",
"CHECK(expected_price > 0)",
"Expected price must be strictly positive",
),
(
"check_selling_price",
"CHECK(selling_price >= 0)",
"Selling price should be positive",
),
]

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

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

# on change of garden status , update gardern area and its orientation

@api.onchange("garden")
def _onchange_garden_status(self):
if self.garden:
self.garden_area = 10
self.garden_orientation = "north"
return
self.garden_area = 0
self.garden_orientation = ""

# acts when property is sold
# In case property is cancelled it cannot be sold
def action_sell_property(self):
# dictionary for the property status
property_sell_status_dict = {"new": True, "sold": True, "cancelled": False}
for record in self:
if self.state != "offer_accepted":
raise UserError("Property has no offer yet!!!")
if property_sell_status_dict[record.status]:
record.status = "sold"
record.state = "sold"
else:
raise UserError("Cancelled property cannot be sold.")

# action in case of cancel property button
# If property is sold than Cannot be cancelled

def action_cancel_property_selling(self):
property_cancel_status_dict = {
"new": True,
"cancelled": True,
"sold": False,
}
for record in self:
if property_cancel_status_dict[record.status]:
record.status = "cancelled"
else:
raise UserError("Sold property cannot be cancelled.")

# constrains for the selling price

@api.constrains("selling_price", "expected_price")
def _check_selling_price(self):
for data in self:
# if call will come after selling price change than it will allow updated price to work
if data.selling_price <= 0:
return

price_float_ratio = data.selling_price / data.expected_price
ratio_diffrence = float_compare(price_float_ratio, 0.9, precision_digits=2)
if ratio_diffrence == -1:
data.selling_price = 0
raise ValidationError(
"The selling price cannot be lower than 90% of the expected price"
)

# delete opration for the process

@api.ondelete(at_uninstall=False)
def _unlink_if_state_new_or_cancelled(self):
for data in self:
if not bool(data.state == "new" or data.state == "cancelled"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and you changed it in the last commit better to do it in the original commit.

raise UserError(
"Can't delete property which is not in new or cancelled state!"
)
Loading