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
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
18 changes: 18 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Real Estate",
"description": "Real Estate Advertising",
"author": "Odoo S.A.",
"license": "LGPL-3",
"depends": ["base"],
"application": True,
"data": [
"security/ir.model.access.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/estate_menus.xml",
"views/res_users_views.xml",
],
}
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_offer
from . import estate_property_tag
from . import estate_property_type
from . import estate_property
from . import res_users
113 changes: 113 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_compare, float_is_zero


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

name = fields.Char(string="Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(string="Available From", default=lambda self: fields.Date.context_today(self) + relativedelta(months=3), copy=False)
expected_price = fields.Float(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")],
)

# Reserved fields
active = fields.Boolean(default=True)
state = fields.Selection(
required=True,
selection=[("new", "New"), ("offer_received", "Offer Received"), ("offer_accepted", "Offer Accepted"), ("sold", "Sold"), ("cancelled", "Cancelled")],
default="new",
copy=False,
string="Status",
)

# Relations
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
salesman_id = fields.Many2one("res.users", default=lambda self: self.env.user)
buyer_id = fields.Many2one("res.partner", copy=False)
tag_ids = fields.Many2many("estate.property.tag")
offer_ids = fields.One2many("estate.property.offer", "property_id")

# Computed
total_area = fields.Integer(compute="_compute_total_area", string="Total Area (sqm)")
best_price = fields.Float(compute="_compute_best_price", string="Best Offer")

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

@api.depends("offer_ids.price")
def _compute_best_price(self) -> None:
for record in self:
record.best_price = max(record.offer_ids.mapped("price"), default=0)

# Methods that trigger on changes
@api.onchange("garden")
def _onchange_garden_defaults(self) -> None:
if self.garden:
self.garden_area = 10
self.garden_orientation = "north"
else:
self.garden_area = None
self.garden_orientation = None

@api.ondelete(at_uninstall=False)
def _unlink_except_new_or_cancelled(self) -> None:
if any(record.state not in {"new", "cancelled"} for record in self):
raise UserError(self.env._("Cannot delete properties unless they are new or cancelled."))

# Public methods
def action_set_sold(self) -> bool:
for record in self:
if record.state == "sold":
continue

if record.state == "cancelled":
raise UserError(record.env._("Cancelled properties cannot be sold."))

record.state = "sold"
return True

def action_set_cancelled(self) -> bool:
for record in self:
if record.state == "cancelled":
continue

if record.state == "sold":
raise UserError(record.env._("Sold properties cannot be cancelled."))

record.state = "cancelled"
return True

# Constraints
_check_expected_price_strict_positive = models.Constraint(
"CHECK(expected_price > 0)",
"A property's expected price must be strictly greater than 0.",
)

_check_selling_price_positive = models.Constraint(
"CHECK(selling_price >= 0)",
"A property's selling price must be equal to or greater than 0.",
)

@api.constrains("expected_price", "selling_price")
def _check_selling_price_percentage(self) -> None:
for record in self:
# Selling price is zero when no offer has been accepted
if not float_is_zero(record.selling_price, precision_digits=2) and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0:
raise ValidationError(record.env._("A property's selling price cannot be lower than 90 percent of its expected price."))
72 changes: 72 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare


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

price = fields.Float()
status = fields.Selection(
selection=[("accepted", "Accepted"), ("refused", "Refused")],
copy=False,
)
validity = fields.Integer(default=7, string="Validity (days)")

# Relations
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

# Computed
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_deadline", string="Deadline")

@api.depends("create_date", "validity")
def _compute_deadline(self) -> None:
for record in self:
ref_date = fields.Date.context_today(self) if not record.create_date else record.create_date.date()
record.date_deadline = ref_date + relativedelta(days=record.validity)

def _inverse_deadline(self) -> None:
for record in self:
ref_date = fields.Date.context_today(self) if not record.create_date else record.create_date.date()
record.validity = (record.date_deadline - ref_date).days

# CRUD overrides
@api.model
def create(self, vals):
for val in vals:
estate_property = self.env["estate.property"].browse(val["property_id"])
best_price = estate_property.best_price or 0.0
if float_compare(val["price"], best_price, precision_digits=2) < 0:
raise UserError(self.env._("A new offer must match or exceed the price of the current best offer."))
estate_property.state = "offer_received"
return super().create(vals)

# Public methods
def action_accept(self) -> bool:
# TODO double check validation
for record in self:
if record.property_id.offer_ids.filtered(lambda r: r.status == "accepted"):
raise UserError(record.env._("Cannot accept this offer because another offer has already been accepted for the property."))

record.status = "accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
record.property_id.state = "offer_accepted"
return True

def action_refuse(self) -> bool:
# TODO double check validation
for record in self:
record.status = "refused"
return True

# Constraints
_check_price_strict_positive = models.Constraint(
"CHECK(price > 0)",
"An offer's price must be strictly greater than 0.",
)
16 changes: 16 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from odoo import fields, models


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

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

# Constraints
_uniq_name = models.Constraint(
"UNIQUE(name)",
"A property tag's name must be unique.",
)
28 changes: 28 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from odoo import api, fields, models


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Estate property type"
_order = "name"

name = fields.Char(required=True)
sequence = fields.Integer(default=1)

# Relations
property_ids = fields.One2many("estate.property", "property_type_id")
offer_ids = fields.One2many("estate.property.offer", "property_type_id")

# Computed
offer_count = fields.Integer(compute="_compute_offer_count")

@api.depends("offer_ids")
def _compute_offer_count(self) -> None:
for record in self:
record.offer_count = len(record.offer_ids)

# Constraints
_uniq_name = models.Constraint(
"UNIQUE(name)",
"A property type's name must be unique.",
)
7 changes: 7 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


class User(models.Model):
_inherit = "res.users"

property_ids = fields.One2many("estate.property", "salesman_id", domain=[("state", "in", ["new", "offer_received"])])
5 changes: 5 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink
access_estate_property,access_estate_property,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer,access_estate_property_offer,model_estate_property_offer,base.group_user,1,1,1,1
12 changes: 12 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<odoo>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_property_menu" name="Properties">
<menuitem id="estate_property_menu_action" action="estate_property_action"/>
</menuitem>
<menuitem id="estate_settings_menu" name="Settings">
<menuitem id="estate_property_type_menu" action="estate_property_type_action"/>
<menuitem id="estate_property_tag_menu" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
42 changes: 42 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_offer_view_list" model="ir.ui.view">
<field name="name">estate.property.offer.list</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list editable="bottom" decoration-danger="status == 'refused'" decoration-success="status == 'accepted'">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_accept" type="object" icon="fa-check" string="Accept" invisible="status"/>
<button name="action_refuse" type="object" icon="fa-times" string="Refuse" invisible="status"/>
</list>
</field>
</record>

<record id="estate_property_offer_view_form" model="ir.ui.view">
<field name="name">estate.property.offer.form</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="status"/>
<field name="validity"/>
<field name="date_deadline"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">Offers</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list,form</field>
<field name="domain">[('property_type_id', '=', active_id)]</field>
</record>
</odoo>
32 changes: 32 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_tag_lview_list" model="ir.ui.view">
<field name="name">estate.property.tag.list</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<list editable="bottom">
<field name="name"/>
</list>
</field>
</record>

<record id="estate_property_tag_view_form" model="ir.ui.view">
<field name="name">estate.property.tag.form</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Property Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
Loading