Skip to content

[ADD] estate: new module to manage estate #822

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

Closed
wants to merge 4 commits into from
Closed
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
15 changes: 15 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
'name': "Estate",
'depends': ['base'],
'application': True,
'installable': 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',
],
"license": "LGPL-3",
}
4 changes: 4 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_offer
from . import estate_property_tag
115 changes: 115 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from datetime import datetime
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 = "Real Estate Property"

_sql_constraints = [
('check_expected_price', 'CHECK(expected_price > 0)', "Expected price must be strictly positive."),
('check_selling_price', 'CHECK(selling_price >= 0)', "Selling price must be strictly positive.")
]

name = fields.Char(string="Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(copy=False, default=lambda self: datetime.today() + relativedelta(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(string="Living Area (sqm)")
facades = fields.Integer(string="Facade")
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"),
],
string="Garden Orientation"
)
available = fields.Boolean(default=True)
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
salesman_id = fields.Many2one("res.users", string="Salesman", default=lambda self: self.env.user)
buyer_id = fields.Many2one("res.partner", string="Buyer")
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
state = fields.Selection(
selection=[
('new', "New"),
('offer_received', "Received"),
('offer_accepted', "Accepted"),
('sold', "Sold"),
('cancelled', "Cancelled"),
('refused', "Refused")
],
required=True,
copy=False,
default='new'
)
note = fields.Text(string="Special mention about the property.")
total_area = fields.Integer(string="Total Area (sqm)", compute="_compute_total_area")
best_offer = fields.Float(string="Best Offer", compute="_compute_best_price")

@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:
record.best_offer = max(record.offer_ids.mapped("price"), default=0.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 cancel_property_button(self):
for record in self:
if record.state != 'sold':
record.state = 'cancelled'
else:
raise UserError(_("You cannot cancel a sold property."))

def sold_property_button(self):
for record in self:
if record.state != 'cancelled':
record.state = 'sold'
else:
raise UserError("You cannot sell a cancelled property.")

@api.constrains('expected_price')
def _check_expected_price_positive(self):
for record in self:
if record.expected_price <= 0:
raise ValidationError("Expected price must be strictly positive.")

@api.constrains('selling_price', 'expected_price')
def _check_selling_price_margin(self):
for record in self:
if float_is_zero(record.selling_price, precision_digits=2):
continue
min_acceptable_price = record.expected_price * 0.9
if float_compare(record.selling_price, min_acceptable_price, precision_digits=2) < 0:
raise ValidationError("The selling price cannot be lower than 90% of the expected price.")

@api.ondelete(at_uninstall=False)
def unlink(self):

Choose a reason for hiding this comment

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

This is called upon the module deletion, to avoid triggering the error when the module is deleted by a user, add the api.ondelete(at_uninstall=False)

for record in self:
if record.state not in ['new', 'cancelled']:
raise UserError("YOU CANNOT DELETE A PROPERTY THAT IS NOT IN NEW OR CANCELLED STATUS. REVERT!")
return super().unlink()
65 changes: 65 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from datetime import timedelta

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


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

price = fields.Float()
status = fields.Selection(
selection=[
('accepted', "Accepted"),
('refused', "Refused")
],
copy=False
)
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)
validity = fields.Integer(default=7)
deadline = fields.Date(string="Deadline", compute="_compute_deadline", inverse="_inverse_deadline")
creation_date = fields.Datetime("Creation Date", readonly=True)

@api.depends("validity")
def _compute_deadline(self):
for record in self:
if record.creation_date:
creation_date = fields.Datetime.from_string(record.creation_date)
record.deadline = creation_date.date() + timedelta(days=record.validity)

def _inverse_deadline(self):
for record in self:
if record.deadline:
if record.creation_date:
creation_date = fields.Datetime.from_string(record.creation_date)
record.validity = (record.deadline - creation_date.date()).days

def offer_accept(self):
if 'accepted' in self.mapped("property_id.offer_ids.status"):
raise UserError("An offer is already accepted.")
for record in self:
record.status = 'accepted'
record.property_id.state = 'offer_accepted'
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price

def offer_refuse(self):
for record in self:
record.status = 'refused'

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
property_id = vals.get('property_id')
price = vals.get('price', default=0.0)

if property_id and price is not None:
property_rec = self.env['estate.property'].browse(property_id)
if property_rec.best_offer > price:
raise exceptions.UserError("Cannot add offer for a lower amount than current offer")
else:
property_rec.state = "offer_received"

return super().create(vals_list)
9 changes: 9 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import fields, models


class estate_property_tag(models.Model):
_name = "estate.property.tag"
_description = "Estate property tag file"

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


class estate_property_type(models.Model):
_name = "estate.property.type"
_description = "Property type models file"
_order = "name"

_sql_constraints = [
('unique_type_name', 'UNIQUE(name)', 'The name should be unique')
]

name = fields.Char("Name", required=True)
property_id = fields.One2many("estate.property", "property_type_id")
offer_counts = fields.Integer(compute="_compute_offer_count")

@api.depends("property_id.offer_ids")
def _compute_offer_count(self):
for record in self:
record.offer_counts = len(record.property_id.offer_ids)
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 res_users(models.Model):
_inherit = 'res.users'

property_ids = fields.One2many('estate.property', "salesman_id", domain="('state','=','new'),('state','=','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_offer","access_estate_property_offer","estate.model_estate_property_offer","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
14 changes: 14 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<odoo>
<menuitem id="estate_menu_root" name="Estate">
<menuitem id="estate_menu_advertissements" name="Advertissement">
<menuitem id="estate_menu_properties" name="Property" action="estate_property_action"/>
<menuitem id="estate_menu_properties_offer" name="Offers" action="estate_property_offer_view"/>
</menuitem>

<menuitem id="estate_menu_settings" name="Settings" sequence="20">
<menuitem id="estate_menu_properties_type" name="Property Type" action="estate_property_type_view"/>
<menuitem id="estate_menu_properties_tags" name="Property Tags" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
45 changes: 45 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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="Estate Property Offers">
<sheet>
<group>
<field name="property_id"/>
<h1>
<field name="price"/>
</h1>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="deadline"/>
<field name="status"/>
</group>
</sheet>
</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="Channel" editable="top"
decoration-success="status == 'accepted'"
decoration-danger="status=='refused'">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="deadline"/>
<button name="offer_accept" type="object" title="Accept" icon="fa-check" invisible="status != False"/>
<button name="offer_refuse" type="object" title="Refused" icon="fa-times" invisible="status != False"/>
</list>
</field>
</record>
<record id="estate_property_offer_view" model="ir.actions.act_window">
<field name="name">Estate Properties Offers</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
17 changes: 17 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<odoo>
<record id="estate_property_tag_view_list" model="ir.ui.view">
<field name="name">estate.property.tag.view.list</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<list string="Tag List" editable="top">
<field name="name"/>
<field name="color" widget="color_picker"/>
</list>
</field>
</record>
<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Estate Properties Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
39 changes: 39 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<odoo>
<record id="estate_property_type_view_form" model="ir.ui.view">
<field name="name">estate.property.type.form</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<form string="Estate Property Type">
<sheet>
<header>
<button type="action"
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

This a lot of indent 👀

Suggested change
<header>
<button type="action"
<header>
<button type="action"

name="%(estate_property_offer_view)d"
class="oe_stat_button"
icon="fa-money">
<div><field name="offer_counts" /> Offers </div>
</button>
</header>
<group>
<field name="name" />
</group>
<notebook>
<page string="Properties">
<field name="property_id">
<list>
<field name="name" />
<field name="expected_price" />
<field name="state" />
</list>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record id="estate_property_type_view" model="ir.actions.act_window">
<field name="name">Property Types</field>
<field name="res_model">estate.property.type</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
Loading