Skip to content
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
21 changes: 21 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Real Estate",
"version": "1.0.0",
"depends": ["base"],
"author": "Odoo S.A.",
"license": "LGPL-3",
"category": "Real Estate",
"description": """
This is a real estate management module that allows users to manage properties, agents, and clients.
""",
"application": True,
"data": [
"security/ir.model.access.csv",
"views/estate_property_offer_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_views.xml",
"views/estate_property_menus.xml",
"views/res_users_views.xml",
],
}
5 changes: 5 additions & 0 deletions estate/models/__init__.py
Comment thread
amgom-odoo marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import estate
from . import estate_offer
from . import estate_tag
from . import estate_type
from . import res_users
130 changes: 130 additions & 0 deletions estate/models/estate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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 Estate(models.Model):
_name = "estate.estate"
_description = "Real Estate"
_order = "id desc"

name = fields.Char(string="Name", required=True)
description = fields.Text(string="Description")
postcode = fields.Char(string="Postcode")
date_availability = fields.Date(
string="Available From",
default=fields.Date.today() + relativedelta(months=3),
copy=False,
)
expected_price = fields.Float(string="Expected Price", required=True)
selling_price = fields.Float(string="Selling price", readonly=True, copy=False)
bedrooms = fields.Integer(string="Bedrooms", default=2)
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="Garden Orientation",
selection=[
("north", "North"),
("east", "East"),
("west", "West"),
("south", "South"),
],
)
active = fields.Boolean(string="Active", default=True)
state = fields.Selection(
string="Status",
selection=[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
required=True,
copy=False,
default="new",
)
type_id = fields.Many2one(
string="Property Type",
comodel_name="estate.type",
)
salesman_id = fields.Many2one(
string="Salesman",
comodel_name="res.users",
default=lambda self: self.env.user,
)
buyer_id = fields.Many2one(string="Buyer", comodel_name="res.partner", copy=False)
tag_ids = fields.Many2many(string="Tags", comodel_name="estate.tag")
offer_ids = fields.One2many(
string="Offers",
comodel_name="estate.offer",
inverse_name="property_id",
)
total_area = fields.Float(string="Total Area", compute="_compute_total_area")
best_offer = fields.Float(string="Best Offer", compute="_compute_best_offer")

_positive_expected_price = models.Constraint(
"CHECK(expected_price > 0)",
"Expected price should be strictly positive",
)
_positive_selling_price = models.Constraint(
"CHECK(selling_price >= 0)",
"Selling price should be positive",
)

@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 len(record.offer_ids) else 0.0
)

@api.onchange("garden")
def _on_change_garden(self):
self.garden_area = 10 if self.garden else 0
self.garden_orientation = "north" if self.garden else None

def action_sell_property(self):
for property in self:
if property.state == "cancelled":
raise UserError(self.env._("Cancelled properties cannot be sold"))
property.state = "sold"

def action_cancel_property(self):
for property in self:
if property.state == "sold":
raise UserError(self.env._("Sold properties cannot be cancelled"))
property.state = "cancelled"

@api.constrains("expected_price", "selling_price")
def _check_selling_price_limit(self):
for property in self:
if not float_is_zero(property.selling_price, precision_digits=5):
limit = 0.9 * property.expected_price
if (
float_compare(property.selling_price, limit, precision_digits=5)
== -1
):
raise ValidationError(
self.env._(
"The selling price should not be less than 90% of the expected price of the property",
),
)

@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_cancelled(self):
for state in self.mapped("state"):
if state not in ["new", "cancelled"]:
raise UserError(
self.env._("New or cancelled estates only can be deleted"),
)
88 changes: 88 additions & 0 deletions estate/models/estate_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.exceptions import UserError


class EstateOFfer(models.Model):
_name = "estate.offer"
_description = "Offer made to some estate (property)"
_order = "price desc"

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

_positive_offer_price = models.Constraint(
"CHECK(price > 0)",
"Offer price should be strictly positive",
)

@api.depends("create_date", "validity")
def _compute_deadline(self):
for offer in self:
curr_date = offer.create_date if offer.create_date else fields.Date.today()
offer.date_deadline = curr_date + relativedelta(days=offer.validity)

def _inverse_deadline(self):
for offer in self:
date_diff = offer.date_deadline - (offer.create_date.date())
offer.validity = date_diff.days

def action_accept_offer(self):
# check if there is no other accepted offers
if "accepted" in self.property_id.offer_ids.mapped("status"):
raise UserError(
self.env._("Only one offer can be accepted for a given property!"),
)

# update status, selling price, buyer
self.status = "accepted"
self.property_id.selling_price = self.price
self.property_id.buyer_id = self.partner_id
Comment thread
amgom-odoo marked this conversation as resolved.
self.property_id.state = "offer_accepted"

def action_refuse_offer(self):
self.status = "refused"

@api.model_create_multi
def create(self, vals_list):
# check before saving if the offer price >= existing offer prices
for record in vals_list:
property = self.env["estate.estate"].browse(record["property_id"])
max_offer_price = max(property.offer_ids.mapped("price"), default=0)
if record["price"] < max_offer_price:
raise UserError(
self.env._(
"Cannot create an offer with price lower than an existing offer",
),
)

# update state and save to the database
property.state = "offer_received"
return super().create(vals_list)
15 changes: 15 additions & 0 deletions estate/models/estate_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import fields, models


class EstateTag(models.Model):
_name = "estate.tag"
_description = "A list of tags that categorize the properities"
_order = "name"

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

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


class EstateType(models.Model):
_name = "estate.type"
_description = "The type of the property to be sold such as House, apartment, ..."
_order = "sequence, name"

name = fields.Char(string="Name", required=True)
property_ids = fields.One2many(
string="Properties",
comodel_name="estate.estate",
inverse_name="type_id",
)
sequence = fields.Integer(string="Sequence", default=1)

offer_ids = fields.One2many(
string="Property type offers",
comodel_name="estate.offer",
inverse_name="property_type_id",
)
offer_count = fields.Integer(string="Offers count", compute="_compute_offers_count")

_unique_name = models.Constraint(
"UNIQUE(name)",
"Property type names must be unique!",
)

@api.depends("offer_ids")
def _compute_offers_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 Users(models.Model):
_inherit = "res.users"

property_ids = fields.One2many(
string="Properities",
comodel_name="estate.estate",
inverse_name="salesman_id",
domain=[("state", "in", ["new", "offer_received"])],
)
5 changes: 5 additions & 0 deletions estate/security/ir.model.access.csv
Comment thread
amgom-odoo marked this conversation as resolved.
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_estate,access.estate.estate,model_estate_estate,base.group_user,1,1,1,1
access_estate_offer,access.estate.offer,model_estate_offer,base.group_user,1,1,1,1
access_estate_type,access.estate.type,model_estate_type,base.group_user,1,1,1,1
access_estate_tag,access.estate.tag,model_estate_tag,base.group_user,1,1,1,1
10 changes: 10 additions & 0 deletions estate/views/estate_property_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<menuitem id="estate_root_menu" name="Real Estate">
<menuitem id="estate_property_menu" name="Advertisements" action="estate_property_action"/>
<menuitem id="estate_property_settings_menu" name="Settings">
<menuitem id="estate_property_type_menu" name="Property Types" action="estate_property_type_action"/>
<menuitem id="estate_property_tag_menu" name="Property Tags" 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" encoding="utf-8"?>
<odoo>
<record id="estate_property_offer_view_form" model="ir.ui.view">
<field name="name">estate.property.offer.view.form</field>
<field name="model">estate.offer</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Create Offers">
<sheet>
<group>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</group>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_offer_view_list" model="ir.ui.view">
<field name="name">estate.property.offer.view.list</field>
<field name="model">estate.offer</field>
<field name="type">list</field>
<field name="arch" type="xml">
<list string="Offers">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</list>
</field>
</record>
</odoo>
18 changes: 18 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Property Tags</field>
<field name="res_model">estate.tag</field>
<field name="view_mode">list,form</field>
</record>

<record id="estate_property_tag_view_list" model="ir.ui.view">
<field name="name">estate.property.tag.view.list</field>
<field name="model">estate.tag</field>
<field name="arch" type="xml">
<list string="Property Tags" editable="bottom">
<field name="name"/>
</list>
</field>
</record>
</odoo>
Loading