Skip to content

[ADD] estate: implement data and demo data #795

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 2 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
24 changes: 24 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
'name': 'Real Estate',
'version': '1.0',
'description': 'Real Estate Management System',
'summary': 'Real Estate Management System',
'license': 'LGPL-3',
'depends': ['base'],
'data': [
'security/ir.model.access.csv',
'views/estate_property_offer.xml',
'views/estate_property_tag.xml',
'views/estate_property_type.xml',
'views/estate_property.xml',
'views/estate_menus.xml',
'views/res_user.xml',
'data/estate.property.type.csv',
],
'demo': [
'demo/estate_property.xml',
'demo/estate_property_offer.xml',
],
'auto_install': False,
'application': True,
}
5 changes: 5 additions & 0 deletions estate/data/estate.property.type.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name,sequence
property_type_residential,Residential,1
property_type_commercial,Commercial,2
property_type_industrial,Industrial,3
property_type_land,Land,4
58 changes: 58 additions & 0 deletions estate/demo/estate_property.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="estate_property_1" model="estate.property">
<field name="name">Big Villa</field>
<field name="property_type_id" ref="estate.property_type_residential"/>
<field name="state">new</field>
<field name="description">A nice and big villa</field>
<field name="postcode">12345</field>
<field name="date_availability" eval="(datetime.now() + timedelta(days=10)).date()" />
<field name="expected_price">1</field>
<field name="selling_price">0</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>
</record>
<record id="estate_property_2" model="estate.property">
<field name="name">Trailer home</field>
<field name="property_type_id" ref="estate.property_type_residential"/>
<field name="state">cancelled</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="date_availability" eval="(datetime.now() + timedelta(days=-1500)).date()" />
<field name="expected_price">100000</field>
<field name="selling_price">120000</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
</record>
<record id="estate_property_3" model="estate.property">
<field name="name">Disneyland</field>
<field name="property_type_id" ref="estate.property_type_industrial"/>
<field name="state">offer_received</field>
<field name="description">Disneyland Paris park</field>
<field name="postcode">6000</field>
<field name="date_availability" eval="(datetime.now() + timedelta(days=-200)).date()" />
<field name="expected_price">15000000</field>
<field name="selling_price">0</field>
<field name="bedrooms">1000</field>
<field name="living_area">457600</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">7881</field>
<field name="garden_orientation">south</field>
<field name="offer_ids" eval="[
Command.create({ 'partner_id': ref('base.res_partner_12'), 'price': 13900000, 'validity': 40 }),
Command.create({ 'partner_id': ref('base.res_partner_2'), 'price': 14500000, 'validity': 30 }),
Command.create({ 'partner_id': ref('base.res_partner_12'), 'price': 16100000, 'validity': 45 }),
Command.create({ 'partner_id': ref('base.res_partner_2'), 'price': 16500000, 'validity': 15 }),
]"/>
</record>
</data>
</odoo>
32 changes: 32 additions & 0 deletions estate/demo/estate_property_offer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="property_offer_1" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="estate_property_1"/>
<field name="price">1000</field>
<field name="validity">14</field>
</record>
<record id="property_offer_2" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_12"/>
<field name="property_id" ref="estate_property_1"/>
<field name="price">1500000</field>
<field name="validity">14</field>
</record>
<record id="property_offer_3" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_2"/>
<field name="property_id" ref="estate_property_1"/>
<field name="price">1500001</field>
<field name="validity">14</field>
</record>

<function model="estate.property" name="write">
<value eval="[ref('estate.estate_property_1')]"/>
<value eval="{'expected_price': 1600000}"/>
</function>

<function model="estate.property.offer" name="action_accept">
<value eval="[ref('estate.property_offer_3')]"/>
</function>
</data>
</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_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 _, api, fields, models
from odoo.exceptions import UserError


class EstateProperty(models.Model):
_name = 'estate.property'
_description = 'Real estate property with offers, pricing, and availability.'
_order = 'id desc'

_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 positive!'),
]

name = fields.Char('Title', required=True)
description = fields.Text('Description')
postcode = fields.Char('Postcode')
date_availability = fields.Date(
'Availability From',
copy=False,
default=lambda self: fields.Date.today() + relativedelta(months=3),
)
expected_price = fields.Float('Expected Price', required=True)
selling_price = fields.Float('Selling Price', readonly=True, copy=False)
best_price = fields.Float('Best Price', compute='_compute_best_price', store=True)
property_type_id = fields.Many2one('estate.property.type', 'Property Type')
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
buyer_id = fields.Many2one('res.partner', 'Buyer')
seller_id = fields.Many2one('res.users', 'Seller', default=lambda self: self.env.user)
bedrooms = fields.Integer('Bedrooms', default=2)
living_area = fields.Integer('Living Area (sqm)')
facades = fields.Integer('Facades')
garage = fields.Boolean('Garage')
garden = fields.Boolean('Garden')
garden_area = fields.Integer('Garden Area')
garden_orientation = fields.Selection(
[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')],
'Garden Orientation',
)
total_area = fields.Integer('Total Area (sqm)', compute='_compute_total_area', store=True)
state = fields.Selection(
[
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled'),
],
'State',
default='new',
required=True,
copy=False,
)
offer_ids = fields.One2many('estate.property.offer', 'property_id', 'Offers')
active = fields.Boolean('Active', default=True)

###### COMPUTE ######
@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:
prices = record.offer_ids.mapped('price')
if prices:
record.best_price = max(prices)
else:
record.best_price = 0.0

###### ONCHANGE ######
@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 = False

###### CRUD ######
def unlink(self):
if any(record.state not in ['new', 'cancelled'] for record in self):
raise UserError(_('You cannot delete a property that is not new or cancelled.'))
return super().unlink()

###### ACTION ######
def action_sold(self):
for record in self:
if record.state == 'sold':
raise UserError(_('Property is already sold.'))
if record.state == 'cancelled':
raise UserError(_('Property is cancelled and cannot be sold.'))
if record.state != 'offer_accepted':
raise UserError(_('You must accept an offer before marking the property as sold.'))

record.state = 'sold'
return True

def action_cancel(self):
for record in self:
if record.state == 'cancelled':
raise UserError(_('Property is already cancelled.'))
if record.state == 'sold':
raise UserError(_('Sold properties cannot be cancelled.'))

record.state = 'cancelled'
return True
109 changes: 109 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_compare


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'An offer for a property'
_order = 'price desc'

_sql_constraints = [
('check_price', 'CHECK(price > 0)', 'Price must be strictly positive!'),
]

price = fields.Float('Price')
status = fields.Selection(
[('accepted', 'Accepted'), ('refused', 'Refused'), ('pending', 'Pending')],
'Status',
default='pending',
required=True,
copy=False,
)
partner_id = fields.Many2one('res.partner', 'Partner', required=True)
property_id = fields.Many2one('estate.property', 'Property', required=True)
property_type_id = fields.Many2one(
'estate.property.type',
'Property Type',
related='property_id.property_type_id',
store=True,
)
validity = fields.Integer('Validity (days)', help='Validity in days', default=7)
date_deadline = fields.Date(
'Deadline',
compute='_compute_date_deadline',
inverse='_inverse_date_deadline',
store=True,
)

###### COMPUTE ######
@api.depends('validity')
def _compute_date_deadline(self):
for record in self:
if not record.validity:
record.date_deadline = False
else:
record.date_deadline = fields.Date.today() + relativedelta(days=record.validity)

def _inverse_date_deadline(self):
for record in self:
if not record.date_deadline:
record.validity = 0
else:
record.validity = (record.date_deadline - fields.Date.today()).days

###### CRUD ######
@api.model_create_multi
def create(self, vals):
for val in vals:
val_price = val.get('price')
val_property_id = val.get('property_id')
if not val_price or not val_property_id:
raise ValidationError(_('Price and Property ID are required.'))
property_id = self.env['estate.property'].browse(val_property_id)
if not property_id:
raise ValidationError(_(f'Property with ID {val_property_id} does not exist.'))
best_price = property_id.best_price
if float_compare(val_price, best_price, precision_digits=2) <= 0:
raise ValidationError(_(f'The offer price must be higher than the current best price (${best_price}).'))

if property_id.state == 'new':
property_id.state = 'offer_received'
return super().create(vals)

###### ACTIONS ######
def action_accept(self):
for record in self:
record.status = 'accepted'
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
record.property_id.state = 'offer_accepted'

other_offers = record.property_id.offer_ids.filtered(lambda x: x.id != record.id)
other_offers.action_refuse()
return True

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

def action_revert(self):
for record in self:
if record.status == 'accepted':
record.property_id.state = 'offer_received'
record.property_id.buyer_id = False
record.property_id.selling_price = 0
record.status = 'pending'
return True

###### CONSTRAINTS ######
@api.constrains('price')
def _check_price(self):
for record in self:
min_offer_price = record.property_id.expected_price * 0.9
if float_compare(record.price, min_offer_price, precision_digits=2) < 0:
raise ValidationError(
_(f'The offer price must be at least 90% of the expected price (${min_offer_price}).')
)
19 changes: 19 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from random import randint

from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = 'estate.property.tag'
_description = 'Tag for categorizing estate properties'
_order = 'name asc'

_sql_constraints = [
('unique_name', 'UNIQUE(name)', 'Tag name must be unique!'),
]

def _get_default_color(self):
return randint(1, 11)

name = fields.Char('Name', required=True)
color = fields.Integer('Color', default=_get_default_color)
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 api, fields, models


class EstatePropertyType(models.Model):
_name = 'estate.property.type'
_description = 'Type for categorizing estate properties'
_order = 'name asc'

_sql_constraints = [
('unique_name', 'UNIQUE(name)', 'Type name must be unique!'),
]

sequence = fields.Integer('Sequence', default=10)
name = fields.Char('Name', required=True)
property_ids = fields.One2many('estate.property', 'property_type_id', 'Properties')
offer_ids = fields.One2many('estate.property.offer', 'property_type_id', 'Offers')
offer_count = fields.Integer('Offers Count', compute='_compute_offer_count')

@api.depends('offer_ids')
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
Loading