Skip to content
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
25 changes: 25 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.


{
'name': 'estate',
'category': 'Tutorials',
'depends': [
'base',
],
'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/res_users_views.xml',
'views/estate_menus.xml',
],
'application': True,
'installable': True,
'auto_install': True,
'author': 'Odoo S.A.',
'license': 'LGPL-3',

}
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
94 changes: 94 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

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


class EstateProperty(models.Model):
_name = 'estate.property'
_description = "estate property"

name = fields.Char(required=True, string="Title")
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(default=lambda self: fields.Date.add(fields.Date.today(), months=3), copy=False)
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()
garden_orientation = fields.Selection([('north', "North"), ('south', "South"), ('east', "East"), ('west', "West")])
total_area = fields.Integer(compute='_compute_total_area')
property_type_id = fields.Many2one(comodel_name='estate.property.type', string="House Type")
buyer_id = fields.Many2one(comodel_name='res.partner', string="Buyer", copy=False)
seller_id = fields.Many2one(comodel_name='res.users', string="Seller", default=lambda self: self.env.user)
tag_ids = fields.Many2many(comodel_name='estate.property.tag', string="Tags")
offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_id', string="")
best_offer = fields.Float(compute='_compute_best_offer')
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,
default='new',
copy=False,
)

_check_expected_price = models.Constraint('CHECK(expected_price > 0)', "The expected price must be stricly positive")

_check_selling_price = models.Constraint('CHECK(selling_price > 0)', "The selling price must be stricly positive")

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

@api.depends('offer_ids.price')
def _compute_best_offer(self):
for record_property in self:
record_property.best_offer = max(record_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 = ''

@api.constrains('selling_price', 'expected_price')
def _check_selling_expected_price(self):
for record_property in self:
if not float_is_zero(record_property.selling_price, precision_digits=2) and (
float_compare(record_property.expected_price * 0.9, record_property.selling_price, precision_digits=2) > 0):
raise UserError(self.env._("The selling price must be a least 90% of the expected price!"))

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

def action_sold(self):
for record_property in self:
if record_property.state == 'cancelled':
raise UserError(record_property.env_("Canceled properties cannot be sold"))
record_property.state = 'sold'
return True

@api.ondelete(at_uninstall=False)
def _ondelete(self):
if any((property_id.state not in ('new', 'cancelled')) for property_id in self):
raise UserError(self.env._("You can only delete property in the state New or Cancelled"))
58 changes: 58 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

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


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = "Property Offer"
_order = "price desc"

price = fields.Float(string="Price")
status = fields.Selection([('accepted', "Accepted"), ('refused', "Refused")], copy=False)
partner_id = fields.Many2one('res.partner', required=True)
property_id = fields.Many2one('estate.property', required=True, ondelete='cascade')
validity = fields.Integer(default=7)
date_deadline = fields.Date(compute='_compute_date_deadline', inverse='_inverse_date_deadline')
property_type_id = fields.Many2one(related='property_id.property_type_id')

_check_offer_price = models.Constraint('CHECK(price > 0)', "The offer price must be stricly positive")

@api.depends('create_date', 'validity')
def _compute_date_deadline(self):
for record in self:
starting_date = record.create_date.date() if record.create_date else fields.Date.today()
record.date_deadline = fields.Date.add(starting_date, days=record.validity)

@api.depends('create_date', 'validity')
def _inverse_date_deadline(self):
for record in self:
record.validity = (record.date_deadline - record.create_date.date()).days

def action_accept_offer(self):
for record in self:
if record.property_id.offer_ids.filtered(lambda offer: offer.status == 'accepted'):
raise UserError(record.env_("Another offer has already been accepted."))
record.status = 'accepted'
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id.state = 'offer_accepted'
return True

def action_refuse_offer(self):
for record in self:
record.status = 'refused'
return True

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
estate_property = self.env['estate.property'].browse(vals['property_id'])
if float_compare(vals['price'], estate_property.best_offer, precision_digits=2) < 0:
raise UserError(self.env._("The price must be higher than %s", estate_property.best_offer))

estate_property.state = 'offer_received'

return super().create(vals_list)
16 changes: 16 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


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

name = fields.Char(required=True, string="Property Tag")
color = fields.Integer()

_unique_tag = models.Constraint(
"UNIQUE(name)", "The tag must be unique"
)
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 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


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

name = fields.Char(required=True, string="Property Type")
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')

@api.depends('offer_ids')
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
15 changes: 15 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


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

property_ids = fields.One2many(
'estate.property',
'seller_id',
string="Available 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
access_estate_property_user,access_estate_property_user,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type_user,access_estate_property_type_user,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag_user,access_estate_property_tag_user,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer_user,access_estate_property_offer_user,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"?>
<odoo>
<menuitem id="estate_menu" name="Real Estate">
<menuitem id="estate_menu_advertisements" name="Advertisements">
<menuitem id="estate_property_menu_advertisements" action="estate_property_action_view"/>
</menuitem>
<menuitem id="estate_menu_settings" name="Settings">
<menuitem id="estate_property_type_menu_settings" action="estate_property_type_action_view" />
<menuitem id="estate_property_tag_menu_settings" action="estate_property_tag_action_view" />
</menuitem>
</menuitem>
</odoo>
23 changes: 23 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_offer_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="bottom" decoration-danger="status == 'refused'" decoration-success="status == 'accepted'">
<field name="price"/>
<field name="partner_id"/>
<field name="date_deadline"/>
<field name="property_type_id"/>
<button name="action_accept_offer" type="object" title="Accepted" icon="fa-check" invisible="status"/>
<button name="action_refuse_offer" type="object" title="Refuse" icon="fa-times" invisible="status"/>
</list>
</field>
</record>
<record id="estate_property_offers_action" 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>
</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"?>
<odoo>
<record id="estate_property_tag_action_view" model="ir.actions.act_window">
<field name="name">Property Tag</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>
<record id="estate_property_tag_list_view" 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>
48 changes: 48 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_type_action_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>
<record id="estate_property_type_list_view" model="ir.ui.view">
<field name="name">estate.property.type.list</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<list string="Channel">
<field name="sequence" widget="handle"/>
<field name="name"/>
<field name="offer_count"/>
</list>
</field>
</record>
<record id="estate_property_type_form_view" 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="Property type">
<sheet>
<h1>
<field name="name"/>
</h1>
<div class="oe_button_box" name="button_box" invisible="not offer_count">
<button class="oe_stat_button" type="action" name="estate.estate_property_offers_action" icon="fa-money">
<field name="offer_count" string="Offers" widget="statinfo"/>
</button>
</div>
<notebook>
<page string="Properties">
<field name="property_ids">
<list>
<field name="name"/>
<field name="expected_price"/>
<field name="state"/>
</list>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</odoo>
Loading