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
17 changes: 17 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'name': 'Real Estate',
'version': '1.0',
'depends': ['base'],
'application': True,
'installable': True,
'data': [
'security/ir.model.access.csv',
'views/estate_property_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_menu.xml',
],
'author': 'macai',
'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_tag
from . import estate_property_offer
134 changes: 134 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo.tools import float_is_zero
from odoo.tools.float_utils import float_compare


class EstateProperty(models.Model):
_name = 'estate.property'
_description = 'Real Estate property model'

# Property information
name = fields.Char('Name', required=True)
description = fields.Text('Description')
postcode = fields.Char('Postcode', required=True)
date_availability = fields.Date('Availability', copy=False, default=lambda self: fields.Date.add(fields.Date.today(), months=3))
expected_price = fields.Float('Expected Price', required=True)
selling_price = fields.Float('Selling Price', readonly=True, copy=False)
bedrooms = fields.Integer('Bedrooms', default=2)
living_area = fields.Integer('Living Area')
facades = fields.Integer('Facades')
has_garage = fields.Boolean('Garage')
has_garden = fields.Boolean('Garden')
garden_area = fields.Integer('Garden Area')
garden_orientation = fields.Selection(
string='Garden Orientation',
selection=[('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West'),
]
)
total_area = fields.Integer(compute='_compute_total_area', string='Total Area')
active = fields.Boolean('Active', default=True)
state = fields.Selection(
string='State',
selection=[('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled'),
],
required=True,
copy=False,
default='new'
)
property_type_id = fields.Many2one("estate.property.type", string='Property Type')

# Other Information
salesman_id = fields.Many2one('res.users', string='Salesman', default=lambda self: self.env.user)
buyer_id = fields.Many2one('res.partner', string='Buyer', copy=False)

# Tags
tag_ids = fields.Many2many('estate.property.tag', string='Tags')

# Offers
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offer')
best_price = fields.Float(compute='_compute_best_price', string='Best Offer')

# Constraints:
# SQL:
_check_expected_price = models.Constraint(
'CHECK(expected_price > 0)',
'The expected price should be stricly positive.'
)
_check_selling_price = models.Constraint(
'CHECK(selling_price > 0)',
'The proporty selling price should be stricly positive.'
)
Comment on lines +24 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The one line on the selection field is okay but when you have many values, it's better to have it like this.
Also, it's preferable to have the key as all small letters, so no confusion happens when they are used inside the code in if statements for example.

nitpick: some teams might stick to single quotes being only on technical strings (the ones that the user don't see) and double quotes are for the strings that the user can see)
so for example the selection can be ('east', "East") instead of ('east', 'East') but some teams don't do this, they just stick to their own convention which is all single quotes (if possible) or all double quotes
But I see you have all single quotes so good for me but thought to mention it so you know.

Suggested change
garden_orientation = fields.Selection(
string='Garden Orientation',
selection=[('North','North'),('South','South'),('East','East'),('West','West')]
)
garden_orientation = fields.Selection(
string='Garden Orientation',
selection=[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')
]
)


# Python:
@api.constrains('selling_price')
def _check_selling_price(self):
for record in self:
if not float_is_zero(record.selling_price, 2):
if float_compare(record.selling_price, record.expected_price, 2) == -1:
raise UserError('The Selling Price must be at least 90% of the Expected Price,'
' you must change the expected price to accepted this offer')

# Computation Fields:

@api.depends('living_area', 'garden_area')
def _compute_total_area(self):
for record in self: # QUESTION is the for each loop here necessary ? or can I just use 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 len(record.offer_ids) > 0:
record.best_price = max(record.offer_ids.mapped('price'))
else:
record.best_price = 0.

@api.onchange('has_garden')
def _onchange_has_garden(self):
if self.has_garden:
self.garden_area = 10
self.garden_orientation = 'north'
else:
self.garden_area = None
self.garden_orientation = None

# Actions buttons
def action_cancel_property_state(self):
for record in self:
if record.state == 'cancelled':
raise UserError('Cancelled properties cannot be cancelled again !')
elif record.state == 'sold':
raise UserError('Sold Properties cannot be cancelled !')
record.state = 'cancelled'
return True
return True

def action_sell_property_state(self):
for record in self:
if record.state == 'sold':
raise UserError('Sold Properties cannot be sold again !')
elif record.state == 'cancelled':
raise UserError('Cancelled properties cannot be sold !')
record.state = 'sold'
return True
return True

# Actions :
def action_set_selling_offer(self, buyer, price):
for record in self:
if record.buyer_id:
raise UserError('An offer is already accepted for this house')
record.buyer_id = buyer
record.selling_price = price
return True
return True


53 changes: 53 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from odoo import models, fields, api
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'Estate Property Offer'

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

# Constraints:
_check_price = models.Constraint(
'CHECK(price > 0)',
'The offer price should be stricty positive'
)

# computed fields

@api.depends('validity', 'create_date')
def _compute_date_deadline(self):
for record in self:
base_date = record.create_date or fields.Date.today() # Fallback when create_date is not set yet:
record.date_deadline = fields.Date.add(base_date, days=record.validity)

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

# Action buttons

def action_accept_property_offer(self):
for record in self:
for properties in record.property_id:
properties.action_set_selling_offer(record.partner_id, record.price)
record.status = 'accepted'
return True
return True

def action_refuse_property_offer(self):
for record in self:
if record.status == 'accepted':
raise UserError('Accepted offer cannot be refused !')
record.status = 'refused'
return True
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 models, fields


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

name = fields.Char(string='Name', required=True)
# Constraints:
_check_unique = models.Constraint(
'UNIQUE (name)',
'The tag name must be unique'
)
8 changes: 8 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import models, fields


class EstatePropertyType(models.Model):
_name = 'estate.property.type'
_description = 'Define the Real Estate Property Type'

name = fields.Char(string='Name', required=True)
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,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type_user,access.estate.property.type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag_user,access.estate.property.tag,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer_user,access.estate.property.offer,model_estate_property_offer,base.group_user,1,1,1,1
12 changes: 12 additions & 0 deletions estate/views/estate_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<odoo>
<menuitem id="estate_application_menu_root" name="Real Estate">
<menuitem id="estate_advertisement_menu" name="Advertisements" sequence="1">
<menuitem id="estate_menu_action" action="estate_property_action"/>
</menuitem>
<menuitem id="setting_menu" name="Settings" sequence="2">
<menuitem id="estate_property_type_menu_action" action="estate_property_type_action"/>
<menuitem id="estate_property_tag_menu_action" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
36 changes: 36 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<odoo>

<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>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_accept_property_offer" string="Confirm" type="object" icon="fa-check"/>
<button name="action_refuse_property_offer" string="Refuse" type="object" icon="fa-level-down"/>
<field name="status"/>
</list>
</field>
</record>

<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>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</group>
</form>
</field>
</record>

</odoo>
10 changes: 10 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<odoo>

<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>

</odoo>
10 changes: 10 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<odoo>

<record id="estate_property_type_action" 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