Skip to content

[ADD] estate: adding new estate module #727

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

Open
wants to merge 12 commits into
base: 18.0
Choose a base branch
from
3 changes: 3 additions & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import models
from . import security
from . import views
17 changes: 17 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "estate",
"license": "LGPL-3",
"application": True,
"depends": [
"base",
],
"data": [
"views/estate_offer_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tags_views.xml",
"views/estate_property_views.xml",
"views/res_users_views.xml",
"views/estate_menus.xml",
"security/ir.model.access.csv",
],
}
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
124 changes: 124 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from datetime import date

from odoo import fields, models, api, _
from odoo.tools import date_utils, float_utils
from odoo.exceptions import UserError, ValidationError


class EstateProperty(models.Model):
_name = "estate.property"
_description = "A property module that adds the property as a listing"
_sql_constraints = [
(
"check_expected_price",
"CHECK(expected_price > 0)",
"Expected price of a property should be only positive",
),
(
"check_selling_price",
"CHECK(selling_price >= 0)",
"Selling price of a property should be positive",
),
]
_order = "id desc"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
copy=False, default=lambda _: date_utils.add(date.today(), months=3)
)
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection(
string="Orientation",
selection=[
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
],
)
total_area = fields.Float(compute="_compute_total_area")
active = fields.Boolean(default=True)
state = fields.Selection(
string="State",
selection=[
("new", "New"),
("offer-received", "Offer Received"),
("offer-accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
required=True,
copy=False,
default="new",
)
property_type_id = fields.Many2one("estate.property.type")
buyer_id = fields.Many2one("res.partner", copy=False)
seller_id = fields.Many2one(
"res.users", name="Salesperson", default=lambda self: self.env.user
)
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
offer_ids = fields.One2many(
"estate.property.offer", "property_id", string="Offers"
)
best_price = fields.Float(
compute="_compute_best_price", readonly=True, string="Best Offer"
)

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

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

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

def action_property_cancel(self):
for single_property in self:
if single_property.state == "sold":
raise UserError(_("Sold properties cannot be cancelled!"))
single_property.state = "cancelled"
return True

def action_property_sold(self):
for single_property in self:
if single_property.state == "cancelled":
raise UserError(_("Cancelled properties cannot be sold!"))
single_property.state = "sold"
return True

@api.constrains("selling_price", "expected_price")
def check_selling_price_in_range(self):
for single_property in self:
if not float_utils.float_is_zero(single_property.selling_price, precision_rounding=0.1):
if single_property.selling_price < (0.9 * single_property.expected_price):
raise ValidationError(_("Selling price cannot be lower than 90%% of Expected price"))
return True

@api.ondelete(at_uninstall=False)
def _unlink_check_property_state(self):
for single_property in self:
if single_property.state not in ["new", "cancelled"]:
raise UserError(_("Property cannot be deleted unless it is new or cancelled"))
return True
81 changes: 81 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from datetime import date

from odoo import fields, models, api, _
from odoo.tools import date_utils
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Offers made on a listing"
_sql_constraints = [
(
"check_price",
"CHECK(price > 0)",
"Price of an offer should be only positive",
),
]
_order = "price desc"

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

@api.depends("validity")
def _compute_date_deadline(self):
for offer in self:
create_date_actual = (
date.today() if not offer.create_date else offer.create_date.date()
)
offer.date_deadline = date_utils.add(
create_date_actual, days=offer.validity
)

def _inverse_date_deadline(self):
for offer in self:
create_date_actual = (
date.today() if not offer.create_date else offer.create_date.date()
)
offer.validity = (offer.date_deadline - create_date_actual).days

def action_offer_accept(self):
for offer in self:
offer.status = "accepted"
if self.property_id.state in ("offer-accepted", "sold"):
self.status = False
raise UserError(_("An offer has already been accepted!"))
else:
self.property_id.write({"state": "offer-accepted", "selling_price": self.price, "buyer_id": self.partner_id})
return True

@api.depends("status")
def action_offer_refuse(self):
self.status = "refused"
return True

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
single_property = self.env["estate.property"].browse(vals["property_id"])
price = vals.get("price")
if price is not None and price < single_property.best_price:
raise UserError(_("An offer cannot be lower than an existing offer"))
single_property.state = "offer-received"
return super().create(vals_list)
17 changes: 17 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Adds different property tags"
_sql_constraints = [
(
"property_tag_unique",
"UNIQUE (name)",
"Property Tag already exists.",
),
]
_order = "name"

name = fields.Char(required=True)
color = fields.Integer()
33 changes: 33 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from odoo import fields, models, api


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Adds different property types"
_sql_constraints = [
(
"property_type_unique",
"UNIQUE (name)",
"Property Type already exists.",
),
]
_order = "sequence, name"

name = fields.Char(required=True)
property_ids = fields.One2many(
"estate.property", "property_type_id", string="Properties"
)
sequence = fields.Integer(
"Sequence", default=1, help="Used to order stages. Lower is better."
)
offer_ids = fields.One2many(
"estate.property.offer", "property_type_id", string="Offers"
)
offer_count = fields.Integer(
compute="_compute_offer_count", string="Number of Offers"
)

@api.depends("offer_ids")
def _compute_offer_count(self):
for property_type in self:
property_type.offer_count = len(property_type.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",
"seller_id",
string="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
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1
estate.access_estate_property_offer,access_estate_property_offer,estate.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" encoding="UTF-8"?>
<odoo>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_ads_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>
40 changes: 40 additions & 0 deletions estate/views/estate_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.form</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<form string="Type">
<group>
<field name="price"/>
<field name="partner_id" string="Partner"/>
<field name="validity"/>
<field name="date_deadline"/>
</group>
</form>
</field>
</record>

<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 string="Offers" editable="top" decoration-success="status == 'accepted'" decoration-danger="status == 'refused'">
<field name="price"/>
<field name="partner_id" string="Partner"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_offer_accept" type="object" icon="fa-check" title="accept" invisible="status in ['accepted', 'refused']"/>
<button name="action_offer_refuse" type="object" icon="fa-times" title="refuse" invisible="status in ['accepted', 'refused']"/>
<field name="status" optional="hidden" invisible="1"/>
</list>
</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_tags_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="Type">
<sheet>
<h1>
<field name="name"/>
</h1>
</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="Type" editable="top">
<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