Skip to content
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

[ADD] estate: new module created #193

Open
wants to merge 17 commits into
base: 18.0
Choose a base branch
from
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 estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
'name': 'Estate',
'version': '0.1',
'depends': ['base'],
'summary': 'Estate module',
'category': 'Tutorials/Estate',
'application': True,
'license': 'AGPL-3',
'data': [
'security/estate.security.xml',
'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_users_views.xml',
'data/master_data.xml',
],
"demo": [
"demo/demo_data.xml",
]
}
16 changes: 16 additions & 0 deletions estate/data/master_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="estate_property_type_1" model="estate.property.type">
<field name="name">Residential</field>
</record>
<record id="estate_property_type_2" model="estate.property.type">
<field name="name">Commercial</field>
</record>
<record id="estate_property_type_3" model="estate.property.type">
<field name="name">Industrial</field>
</record>
<record id="estate_property_type_4" model="estate.property.type">
<field name="name">Land</field>
</record>

</odoo>
74 changes: 74 additions & 0 deletions estate/demo/demo_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="estate_property_1" model="estate.property">
<field name="name">Big Villa</field>
<field name="state">new</field>
<field name="description">A nice and big villa</field>
<field name="postcode">12345</field>
<field name="date_availability">2020-02-02</field>
<field name="expected_price">1600000</field>
<field name="bedrooms">6</field>
<field name="living_area">100</field>
<field name="facades">4</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">100000</field>
<field name="garden_orientation">south</field>
<field name="garden_orientation">south</field>
<field name="property_type_id" ref="estate.estate_property_type_1"/>
</record>

<record id="estate_property_2" model="estate.property">
<field name="name">Trailer home</field>
<field name="state">cancelled</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="date_availability">1970-01-01</field>
<field name="expected_price">100000</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="estate.estate_property_type_1"/>

<field name="offer_ids" eval="[
Command.create({
'price': 10000,
'partner_id': ref('base.res_partner_12')
})
]"/>


</record>

<record id="estate_property_offer_1" model="estate.property.offer">
<field name="price">10000</field>
<field name="validity">14</field>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=14)"/>
<field name="property_id" ref="estate.estate_property_1"/>
<field name="partner_id" ref="base.res_partner_12"/>
</record>

<record id="estate_property_offer_2" model="estate.property.offer">
<field name="price">1500000</field>
<field name="validity">14</field>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=14)"/>
<field name="property_id" ref="estate.estate_property_1"/>
<field name="partner_id" ref="base.res_partner_12"/>
</record>

<record id="estate_property_offer_3" model="estate.property.offer">
<field name="price">1500001</field>
<field name="validity">14</field>
<field name="date_deadline" eval="datetime.now().date() + timedelta(days=20)"/>
<field name="property_id" ref="estate.estate_property_1"/>
<field name="partner_id" ref="base.res_partner_2"/>
</record>

<!-- Accept offer -->
<function model="estate.property.offer" name="action_accept">
<value eval="[ref('estate.estate_property_offer_3')]"/>
</function>

</odoo>
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_offer
from . import estate_property_tag
from . import estate_property_type
from . import res_user
128 changes: 128 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare, float_is_zero


class EstateProperty(models.Model):
_name = 'estate.property'
_description = "Estate Property"
_order = "id desc"
_sql_constraints = [
('check_expected_price', 'CHECK(expected_price >= 0)',
"The property expected price must be strictly positive."),
('check_selling_price', 'CHECK(selling_price >= 0)',
"The property selling price must be positive."),
]

name = fields.Char(required=True, string="Title")
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(copy=False,
default=lambda self: fields.Date.today() + relativedelta(months=3),
string="Available From")
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()
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"),
]
)

active = fields.Boolean(default=True)
state = fields.Selection(copy=False, default='new', required=True, string="Status",
selection=[
('new', "New"),
('offer_received', "Offer Received"),
('offer_accepted', "Offer Accepted"),
('sold', "Sold"),
('cancelled', "Cancelled"),
]
)
property_type_id = fields.Many2one('estate.property.type', string="Property Type")
buyer_id = fields.Many2one('res.partner', string="Buyer")
salesman_id = fields.Many2one('res.users', string="Salesman", 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')
total_area = fields.Float(compute='_compute_total_area', string="Total Area (sqm)")
best_price = fields.Float(compute='_compute_best_price', string="Best Offer")

@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_price = max(record.offer_ids.mapped('price'), default=0)

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if (not float_is_zero(record.selling_price, precision_digits=2) and
float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0):
raise ValidationError(self.env._("The selling price must be at least 90% of the expected 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

@api.ondelete(at_uninstall=False)
def prevent_delete(self):
for record in self:
if record.state not in ('new', 'cancelled'):
raise UserError(self.env._("Only new and cancelled properties can be deleted."))

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

if not any(offer.status == 'accepted' for offer in record.offer_ids):
raise UserError(self.env._("Properties without accepted offer cannot be sold"))

record.state = 'sold'
return True

def action_set_cancelled(self):
for record in self:
if record.state == 'sold':
raise UserError(self.env._("Sold properties cannot be cancelled."))
record.state = 'cancelled'
return True

def accept_offer(self, accepted_offer):
if self.state == 'offer_accepted':
raise UserError(self.env._("An offer has already been accepted."))
self.state = 'offer_accepted'
self.buyer_id = accepted_offer.partner_id
self.selling_price = accepted_offer.price
accepted_offer.status = 'accepted'

# Refuse other offers
for offer in self.offer_ids:
if offer != accepted_offer:
offer.status = 'refused'

def check_new_offer(self, offer_price):
self.state = 'offer_received'
if offer_price < min(self.offer_ids.mapped('price'), default=0):
raise UserError(self.env._("The offer price is less than the others."))
return True
56 changes: 56 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from datetime import timedelta
from dateutil.utils import today

from odoo import api, fields, models


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = "Estate Property Offer"
_order = "price desc"
_sql_constraints = [
('check_price', 'CHECK(price >= 0)', "The offer price must be strictly positive.")
]

price = fields.Float()
status = fields.Selection(
copy=False,
selection=[
('accepted', "Accepted"),
('refused', "Refused"),
]
)
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, string="Validity (days)")
date_deadline = fields.Date(compute='_compute_date_deadline', inverse='_inverse_date_deadline', string="Deadline")

@api.depends('create_date', 'validity')
def _compute_date_deadline(self):
for record in self:
create_date = record.create_date or today()
record.date_deadline = create_date + timedelta(days=record.validity)

def _inverse_date_deadline(self):
for record in self:
create_date = record.create_date or today()
record.validity = (record.date_deadline - create_date.date()).days

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
self.env['estate.property'].browse(vals['property_id']).check_new_offer(vals['price'])

return super().create(vals_list)

def action_accept(self):
for record in self:
self.property_id.accept_offer(record)
return True

def action_refuse(self):
for record in self:
record.status = 'refused'
return True
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 EstatePropertyTag(models.Model):
_name = 'estate.property.tag'
_description = "Estate Property Tag"
_order = "name"
_sql_constraints = [
('name_unique', 'unique(name)', "The tag name must be unique.")
]

name = fields.Char(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 EstatePropertyType(models.Model):
_name = 'estate.property.type'
_description = "Estate Property Type"
_order = "name"

name = fields.Char(required=True)
sequence = fields.Integer(default=1)

property_ids = fields.One2many('estate.property', 'property_type_id', string="Properties")
offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_type_id')

offer_count = fields.Integer(compute='_compute_offer_count', string="Offers")

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


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

property_ids = fields.One2many('estate.property', inverse_name='salesman_id',
domain=[('state', 'in', ['new', 'offer_received'])])
30 changes: 30 additions & 0 deletions estate/security/estate.security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>


<record id="estate_group_user" model="res.groups">
<field name="name">Agent</field>
<field name="category_id" ref="base.module_category_tutorials_estate"/>
</record>

<record id="estate_group_manager" model="res.groups">
<field name="name">Manager</field>
<field name="category_id" ref="base.module_category_tutorials_estate"/>
<field name="implied_ids" eval="[(4, ref('estate_group_user'))]"/>
</record>

<record id="estate_property_rule_agent" model="ir.rule">
<field name="name">Agent rule</field>
<field name="model_id" ref="estate.model_estate_property"/>
<field name="groups" eval="[Command.link(ref('estate.estate_group_user'))]"/>
<field name="domain_force">[('salesman_id', 'in', [False, user.id])]</field>
</record>

<record id="estate_property_rule_manager" model="ir.rule">
<field name="name">Manager rule</field>
<field name="model_id" ref="estate.model_estate_property"/>
<field name="groups" eval="[Command.link(ref('estate.estate_group_manager'))]"/>
<field name="domain_force">[]</field>
</record>

</odoo>
9 changes: 9 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
"access_estate_property_manager","access_estate_property_manager","model_estate_property","estate_group_manager",1,1,1,1
"access_estate_property_type_manager","access_estate_property_type_manager","model_estate_property_type","estate_group_manager",1,1,1,1
"access_estate_property_tag_manager","access_estate_property_tag_manager","model_estate_property_tag","estate_group_manager",1,1,1,1
"access_estate_property_offer_manager","access_estate_property_offer_manager","model_estate_property_offer","estate_group_manager",1,1,1,1
"access_estate_property","access_estate_property","model_estate_property","estate_group_user",1,1,1,0
"access_estate_property_type","access_estate_property_type","model_estate_property_type","estate_group_user",1,0,0,0
"access_estate_property_tag","access_estate_property_tag","model_estate_property_tag","estate_group_user",1,0,0,0
"access_estate_property_offer","access_estate_property_offer","model_estate_property_offer","estate_group_user",1,1,1,0
1 change: 1 addition & 0 deletions estate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_estate_property
Loading