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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
ruff.toml
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
20 changes: 20 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Estate",
"version": "1.9",
"category": "Real Estate",
"summary": "Manage your real estate properties",
"author": "Odoo",
"license": "LGPL-3",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"views/estate_property_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_menus.xml",
"views/res_users_views.xml",
],
"installable": True,
"application": True,
}
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
127 changes: 127 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare, float_is_zero


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

name = fields.Char(string="Name", required=True)
postcode = fields.Char(string="Postcode")
available_from = fields.Date(
string="Available From",
copy=False,
default=fields.Date.add(fields.Date.today(), months=3),
)

expected_price = fields.Float(string="Expected Price", required=True)
selling_price = fields.Float(string="Selling Price", readonly=True)

description = fields.Text(string="Description")
bedrooms = fields.Integer(string="Bedrooms", default=2)
living_area = fields.Integer(string="Living Area (sqm)")
facades = fields.Integer(string="Facades")
garage = fields.Boolean(string="Garage")
garden = fields.Boolean(string="Garden")
garden_area = fields.Integer(string="Garden Area (sqm)")
garden_orientation = fields.Selection(
[
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
],
string="Garden Orientation",
)
total_area = fields.Integer(
string="Total Area (sqm)",
compute="_compute_total_area",
readonly=True,
)
active = fields.Boolean(string="Active", default=True)
state = fields.Selection(
[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("canceled", "Canceled"),
],
string="State",
default="new",
required=True,
)

property_type_id = fields.Many2one("estate.property.type", string="Property Type")
salesman_id = fields.Many2one(
"res.users",
string="Salesman",
index=True,
default=lambda self: self.env.user,
)
buyer_id = fields.Many2one("res.partner", string="Buyer", index=True)
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
best_offer = fields.Float(string="Best Offer", compute="_compute_best_offer")

_positive_selling_price = models.Constraint(
"CHECK (selling_price > 0)",
"Selling price must be positive",
)

_positive_expected_price = models.Constraint(
"CHECK (expected_price > 0)",
"Expected price must be positive",
)

@api.ondelete(at_uninstall=False)
def _unlink_if_state_is_new_or_canceled(self):
if any(state not in ("new", "canceled") for state in self.mapped("state")):
raise UserError(
"Only properties in 'New' or 'Canceled' state can be deleted.",
)

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

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

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

@api.constrains("selling_price")
def _constrains_selling_price(self):
for record in self:
if not float_is_zero(record.selling_price, precision_digits=2):
if float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) < 0:
raise UserError("Selling price must be at least 90% of the expected price")

def action_cancel(self):
for record in self:
if record.state == "sold":
message = "Sold properties cannot be canceled"
raise UserError(message)
record.state = "canceled"

def action_sold(self):
for record in self:
if record.state == "canceled":
message = "Canceled properties cannot be sold"
raise UserError(message)
record.state = "sold"
return True
78 changes: 78 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare


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

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

_positive_price = models.Constraint(
"CHECK (price > 0)",
"Price must be positive",
)

@api.model
def create(self, vals):
for record in vals:
this_property = self.env["estate.property"].browse(record["property_id"])
if float_compare(this_property.best_offer, record["price"], precision_digits=2) > 0:
raise UserError("Can't create offer with less price than best price.")
this_property.state = "offer_received"
return super().create(vals)

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

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

def action_accept(self):
for record in self:
if record.property_id.state == "offer_accepted":
message = "Another offer already accepted"
raise UserError(message)
if record.property_id.state == "sold":
message = "Property is already sold"
raise UserError(message)
record.status = "accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
record.property_id.state = "offer_accepted"

def action_refuse(self):
for record in self:
record.status = "refused"
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 fields, models


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

name = fields.Char(string="Name", required=True)
color = fields.Integer(string="Color")

_unique_name = models.Constraint(
"UNIQUE (name)",
"Name must be unique",
)
34 changes: 34 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from odoo import api, fields, models


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

name = fields.Char(string="Name", required=True)
property_ids = fields.One2many(
"estate.property",
"property_type_id",
string="Properties",
)
sequence = fields.Integer(string="Sequence", default=1)
offer_ids = fields.One2many(
"estate.property.offer",
"property_type_id",
string="Offers",
)
offer_count = fields.Integer(
string="Offer Count",
compute="_compute_offer_count",
)

_unique_name = models.Constraint(
"UNIQUE (name)",
"Name must be unique",
)

@api.depends("offer_ids")
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
12 changes: 12 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import fields, models


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

property_ids = fields.One2many(
"estate.property",
"salesman_id",
string="My Properties",
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="Estate">
<menuitem id="estate_advertisements_menu" name="Advertisements">
<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" action="estate_property_type_action"/>
<menuitem id="estate_property_tag_menu_action" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
38 changes: 38 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<odoo>
<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 string="Property Offer">
<sheet>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_offer_view_tree" 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 string="Property Offers" 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" title="Accept" invisible="status"/>
<button name="action_refuse" type="object" icon="fa-times" title="Refuse" invisible="status"/>
</list>
</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_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 string="Property Tag">
<sheet>
<group>
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_tag_view_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 string="Property Tags" editable="bottom">
<field name="name"/>
</list>
</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