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
19 changes: 19 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
'name': 'Real Estate',
'version': '1.0',
'category': 'Real Estate',
'depends': ['base'],
'author': 'snrav-odoo',
'license': 'LGPL-3',
'description': 'Real estate purchase & sales',
'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/res_user_views.xml',
'views/estate_property_menu.xml'
],
'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
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import res_users
110 changes: 110 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from dateutil.relativedelta import relativedelta

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


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

Choose a reason for hiding this comment

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

Should be one line empty after it.

_order = "id desc"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
copy=False,
default=lambda self: fields.Date.context_today(self) + relativedelta(months=3),
)
expected_price = fields.Float(required=True)
selling_price = fields.Float()
bedrooms = fields.Integer(default=2)
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer(string="Garden Area (sqft)")
garden_orientation = fields.Selection([
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
])
state = fields.Selection(
selection=[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
string="Status",
default="new",
)
active = fields.Boolean(default=True)
property_type_id = fields.Many2one("estate.property.type", string="Property Type", required=True)
customer = fields.Many2one("res.partner", string="Customer", copy=False)
salesperson = fields.Many2one(
"res.users", string="Salesperson", default=lambda self: self.env.user
)
tag_ids = fields.Many2many("estate.property.tag", string="Property Tags")
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offer")
total_area = fields.Integer(compute="_compute_total_area")
best_price = fields.Integer(compute="_compute_best_price")

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

_check_selling_price = models.Constraint(
"CHECK(selling_price >= 0)", "Selling price must 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_price(self):
for record in self:
if not record.mapped("offer_ids.price"):
record.best_price = 0
else:
record.best_price = max(record.mapped("offer_ids.price"))

@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_sold_property(self):
for record in self:
if record.state == "cancelled":
raise exceptions.UserError("Properties which are Cancelled cannot be Sold")
else:
record.state = "sold"
Comment on lines +86 to +90

Choose a reason for hiding this comment

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

It's good to make the error message translatable.
raise exceptions.UserError(_("Properties which are Cancelled cannot be Sold"))

We can use filtered() here.

if self.filtered(lamda x: x.state=="cancelled")

return True

def action_cancel_offer(self):
for record in self:
if record.state == "sold":
raise exceptions.UserError("Properties which are Sold cannot be Cancelled")

Choose a reason for hiding this comment

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

It's good to make the error message translatable.
raise exceptions.UserError(_("Properties which are Sold cannot be Cancelled"))

We can use filtered() here.

if self.filtered(lamda x: x.state=="sold")

else:
record.state = "cancelled"
return True

@api.constrains("selling_price", "expected_price")
def _check_selling_price_percentage_criteria(self):
for record in self:
selling_price_percentage = (record.selling_price / record.expected_price) * 100
if selling_price_percentage >= 90 or selling_price_percentage == 0:
pass
else:
raise ValidationError(
"The selling price cannot be lower then 90% of the expected price."
)
63 changes: 63 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from dateutil.relativedelta import relativedelta

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


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
)
partner_id = fields.Many2one("res.partner", string="Partner", 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)
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_date")

_check_price = models.Constraint(
"CHECK(price > 0)", "Price of an offer must be positive"
)

@api.depends("validity")
def _compute_deadline(self):
for record in self:
base_date = record.create_date or fields.Date.today()
record.date_deadline = base_date + relativedelta(days=record.validity)

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

def action_accept(self):
for record in self:
record.status = "accepted"
record.property_id.state = "offer_accepted"
record.property_id.selling_price = record.price
record.property_id.customer = record.partner_id
return True

def action_refuse(self):
for record in self:
record.status = "refused"
record.property_id.selling_price = 0.00
record.property_id.customer = None
return True

@api.model
def create(self, vals):
if len(vals) > 0:
property = self.env["estate.property"].browse(vals[0]["property_id"])
for record in vals:
if property.state == "new":
property.state = "offer_received"
if record["price"] < property.best_price:
raise UserError(
"Offer with an amount lower than an existing offer cannot be created."
)
return super().create(vals)
13 changes: 13 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from odoo import fields, models


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

name = fields.Char(required=True)
color = fields.Integer()
_check_tag_name = models.Constraint(
"UNIQUE(name)", "Property tag should be unique."
)
22 changes: 22 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo import models, api, fields


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

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

_check_type_name = models.Constraint(
"UNIQUE(name)", "Property type should be unique."
)

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


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

property_ids = fields.One2many(
"estate.property",
"salesperson",
domain="[('state', '!=', 'sold')]"
)
6 changes: 6 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"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

36 changes: 36 additions & 0 deletions estate/views/estate_property_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<odoo>
<menuitem
id="estate_property_menu_root"
name="Real Estate"
/>
<menuitem
id="estate_property_menu_advertisement"
name="Advertisement"
parent="estate_property_menu_root"
/>
<menuitem
id="estate_property_menu_type"
name="Properties"
parent="estate_property_menu_advertisement"
action="main_action_estate"
/>
<menuitem
id="estate_menu_configuration"
name="Settings"
parent="estate_property_menu_root"
/>
<menuitem
id="configuration_menu_property_types"
name="Property Types"
parent="estate_menu_configuration"
action="action_estate_property_type"
/>
<menuitem
id="configuration_menu_property_tags"
name="Property Tags"
parent="estate_menu_configuration"
action="action_estate_property_tag"
/>
</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="action_estate_property_offer" model="ir.actions.act_window">
<field name="name">Property 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>
<record id="action_estate_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="Channel" editable="bottom" decoration-danger="status=='refused'" decoration-success="status=='accepted'">
<field name="price"/>
<field name="partner_id"/>
<field name="validity" string="Validity(days)"/>
<field name="date_deadline"/>
<button name="action_accept" string="Accept" type="object" icon="fa-check" invisible="status in ('accepted', 'refused')"/>
<button name="action_refuse" string="Refuse" type="object" icon="fa-times" invisible="status in ('accepted', 'refused')"/>
<field name="status"/>
</list>
</field>
</record>
<record id="action_estate_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="Offer">
<sheet>
<group>
<field name="price" />
<field name="partner_id" />
<field name="status" />
<field name="validity" string="Validity(days)"/>
<field name="date_deadline"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>

24 changes: 24 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<odoo>
<record id="action_estate_property_tag" model="ir.actions.act_window">
<field name="name">Properties Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create your first Property Tag!
</p>
</field>
</record>
<record id="action_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="Tags" editable="bottom">
<field name="name"/>
<field name="color" widget="color_picker"/>
</list>
</field>
</record>
</odoo>

Loading