Skip to content

[ADD] real_estate : add real estate property and create invoice #845

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

Draft
wants to merge 7 commits into
base: 18.0
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions estate_account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
9 changes: 9 additions & 0 deletions estate_account/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'name': 'Estate Accounting',
'version': '1.0',
'depends': ['real_estate', 'account'],
'category': 'Real Estate',
'license': 'LGPL-3',
'installable': True,
'application': False,
}
1 change: 1 addition & 0 deletions estate_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import estate_property
40 changes: 40 additions & 0 deletions estate_account/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from odoo import models, Command
from odoo.exceptions import UserError


class EstateProperty(models.Model):
_inherit = 'estate.property'

def action_sold(self):
res = super().action_sold()

for record in self:
if not record.buyer_id:
raise UserError("Please set a Buyer before generating an invoice.")
if not record.selling_price:
raise UserError("Please set a Selling Price before generating an invoice.")

journal = self.env['account.journal'].search([('type', '=', 'sale')], limit=1)

Choose a reason for hiding this comment

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

This should be moved outside of for loop. Can you tell me the reason why?

if not journal:
raise UserError("No sale journal found. Please configure at least one sale journal.")

invoice_vals = {
"partner_id": record.buyer_id.id,
"move_type": "out_invoice",
"journal_id": journal.id,
"invoice_line_ids": [
Command.create({
"name": "6% Commission",
"quantity": 1,
"price_unit": 0.06 * record.selling_price,
}),
Command.create({
"name": "Administrative Fees",
"quantity": 1,
"price_unit": 100.0,
}),
]
}

self.env["account.move"].create(invoice_vals)
return res
1 change: 1 addition & 0 deletions real_estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions real_estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'name': 'real_estate',
'depends': [
'base',
],

Choose a reason for hiding this comment

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

Shouldn't the dependency be base. Also Linting.
You change from base_setup to base in commit 2 better to use base from commit 1 itself.

'application': True,
'installable': True,
'license': 'LGPL-3',
'data': [
'security/ir.model.access.csv',
'view/estate_property_views.xml',
'view/estate_property_offer_views.xml',
'view/estate_property_type_views.xml',
'view/estate_property_tag_views.xml',
'view/estate_res_users_view.xml',
'view/estate_menu.xml',
],
}
5 changes: 5 additions & 0 deletions real_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 estate_res_users
107 changes: 107 additions & 0 deletions real_estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from datetime import date, timedelta
from odoo import fields, models, api
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_compare, float_is_zero

Choose a reason for hiding this comment

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

Unnecessary empty line.


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

Choose a reason for hiding this comment

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

Module is named real_estate, but model is estate.property. It's better to be consistent either both should use real_estate as prefix or stick to "Estate" as name.

_description = "Estate"

Choose a reason for hiding this comment

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

Description could be little better. How about "Real Estate Property".

_order = "id desc"

name = fields.Char(default="Unknown", required=True)
description = fields.Text(string="Description")
postcode = fields.Char(string="Postcode")
availability_date = fields.Date(default=lambda self: date.today() + timedelta(days=90), copy=False)
expected_price = fields.Float(string="Expected Price", required=True)
selling_price = fields.Float(readonly=True, copy=False)
_sql_constraints = [
('check_expected_price_positive', 'CHECK(expected_price > 0)', 'The expected price must be strictly positive.'),
('check_selling_price_positive', 'CHECK(selling_price >= 0)', 'The selling price must be positive.')
]
bedrooms = fields.Integer(default=2)
living_area = fields.Float(string="Living Area")
facades = fields.Integer(string="Facades")
garage = fields.Boolean(string="Garage")
garden = fields.Boolean(string="Garden")
garden_area = fields.Float(string="Garden Area")
total_area = fields.Float(string="Total Area", compute="_compute_total_area")
garden_orientation = fields.Selection(
[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')
],
string="Garden Orientation"
)
last_seen = fields.Datetime("Last Seen", default=fields.Datetime.now)

Choose a reason for hiding this comment

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

I don't see last_seen used anywhere.

active = fields.Boolean(default=False)
state = fields.Selection(
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")
buyer_id = fields.Many2one('res.partner', string="Buyer", copy=False)
salesperson_id = fields.Many2one('res.users', string="Salesperson", default=lambda self: self.env.user)
tag_ids = fields.Many2many('estate.property.tag', string="Tags")
offer_ids = fields.One2many('estate.property.offer', 'property_id', string="Offers")
best_price = fields.Float(string="Best Offer", compute="_compute_best_price")

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if (float_is_zero(record.selling_price, precision_digits=2)
or record.state not in ['offer_accepted', 'sold']):
continue
min_acceptable_price = 0.9 * record.expected_price
if float_compare(record.selling_price, min_acceptable_price, precision_digits=2) < 0:
raise ValidationError("Selling price cannot be lower than 90% of the expected price.")

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

@api.depends('offer_ids.price')
def _compute_best_price(self):
for record in self:
prices = record.offer_ids.mapped('price')
record.best_price = max(prices) if prices else 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 = False

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

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

@api.ondelete(at_uninstall=False)
def _check_deletion(self):
for record in self:
if record.state not in ['new', 'cancelled']:
raise UserError("You can only delete a property if its state is 'New' or 'Cancelled'.")
69 changes: 69 additions & 0 deletions real_estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from datetime import timedelta
from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError


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

price = fields.Float(string="Offer Price")
state = fields.Selection([
('new', 'New'),
('accepted', 'Accepted'),
('refused', 'Refused')
], string="State", default='new', required=True)
_sql_constraints = [
('check_price', 'CHECK(price > 0)', 'The offer price must be strictly positive.'),
]
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,
readonly=True
)
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

What will be the co-model for this?

validity = fields.Integer(default=7)
date_deadline = fields.Date(compute="_compute_date_deadline", inverse="_inverse_date_deadline", store=True)

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

def _inverse_date_deadline(self):
for record in self:
create_date = record.create_date or fields.Datetime.now()
delta = record.date_deadline - create_date.date()
record.validity = delta.days

def action_accept(self):
for offer in self:
if offer.property_id.buyer_id:
raise UserError("An offer has already been accepted.")
offer.state = 'accepted'
offer.property_id.write({
'selling_price': offer.price,
'buyer_id': offer.partner_id.id,
'state': 'offer_accepted'
})
return True

def action_refuse(self):
for offer in self:
offer.state = 'refused'
return True

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
price = vals.get('price')
property_obj = self.env['estate.property'].browse(vals.get('property_id'))
existing_offers = property_obj.offer_ids.filtered(lambda o: o.price >= price)
if existing_offers:
raise ValidationError("You cannot create an offer with a lower or equal price than existing offers.")
property_obj.state = 'offer_received'

return super().create(vals_list)
13 changes: 13 additions & 0 deletions real_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 = "Property Tag"
_order = "name"

name = fields.Char(string="Name", required=True)
color = fields.Integer('Color')
_sql_constraints = [
('unique_tag_name', 'UNIQUE(name)', 'A property tag name must be unique')
]
28 changes: 28 additions & 0 deletions real_estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from odoo import models, fields, api


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

name = fields.Char(required=True)
_sql_constraints = [
('unique_property_type', 'UNIQUE(name)', 'A property type name must be unique')
]
property_ids = fields.One2many("estate.property", "property_type_id", string="Properties")
sequence = fields.Integer(string="Sequence", default=10)
offer_ids = fields.One2many(
"estate.property.offer",
"property_type_id",
string="Offers"
)
offer_count = fields.Integer(
string="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)
12 changes: 12 additions & 0 deletions real_estate/models/estate_res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import models, fields


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

property_ids = fields.One2many(
'estate.property',
'salesperson_id',
string="Properties",
domain=[('state', '!=', 'cancelled')]
)
5 changes: 5 additions & 0 deletions real_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,access_estate_property,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
25 changes: 25 additions & 0 deletions real_estate/view/estate_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<!-- Root Menu -->
<menuitem id="menu_real_estate_root" name="Real Estate"/>

<!-- First-Level Menu -->
<menuitem id="menu_real_estate_properties" name="Properties" parent="menu_real_estate_root"/>

<!-- Action Menu -->
<menuitem id="menu_estate_property_action" action="action_estate_property" parent="menu_real_estate_properties"/>

<menuitem id="setting"
name="Settings"
parent="menu_real_estate_root"/>

<menuitem id="menu_property_type_root"
name="Property Types"
parent="setting"
action="estate_property_type_action"/>

<menuitem id="menu_estate_property_tag"
name="Property Tags"
parent="setting"
action="action_estate_property_tag"/>
</odoo>
49 changes: 49 additions & 0 deletions real_estate/view/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<odoo>
<data>
<record id="action_estate_property_offer" model="ir.actions.act_window">
<field name="name">Real estate offer</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>

<record id="estate_property_offer_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="Property Offer">
<header>
<button name="action_accept" string="Accept" type="object"
invisible="state != 'accepted'"/>

<button name="action_refuse" string="Refuse" type="object"
invisible="state == 'accepted'"/>
</header>

<sheet>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="state"/>
</group>
</sheet>
</form>
</field>
</record>

<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 editable="bottom" string="Offers"
decoration-danger="state == 'refused'"
decoration-success="state == 'accepted'">
<field name="price"/>
<field name="partner_id"/>
<button name="action_accept" type="object" string="✓" icon="fa-check"/>
<button name="action_refuse" type="object" string="✗" icon="fa-times"/>
</list>
</field>
</record>
</data>
</odoo>
Loading