-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ADD] real_estate,* : Full Implementation with ORM, Access Control and Accounting Integration #838
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
prbo-odoo
wants to merge
7
commits into
odoo:18.0
Choose a base branch
from
odoo-dev:18.0-training-prbo
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a82939
[ADD] real_estate: completed chapters 1-3 with module and basic model…
prbo-odoo c48844b
[ADD] real_estate: add security rules, UI menu and basic views for model
prbo-odoo 1601aac
[ADD] real_estate: add offer status buttons and state transitions on …
prbo-odoo 8f87fae
[IMP] real_estate: add SQL constraints and UI improvements for proper…
prbo-odoo c63782b
[ADD] estate_account: extend model via inheritance and integrate exte…
prbo-odoo 19ad3f0
[IMP] real_estate: code cleanup and security adjustments
prbo-odoo 5e1dd5c
[IMP] real_estate: restrict record access and define initial module data
prbo-odoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
'name': 'Estate Account', | ||
'version': '1.0', | ||
'sequence': 2, | ||
'depends': ['base', 'real_estate', 'account'], | ||
'category': 'Real Estate', | ||
'author': 'prbo', | ||
'summary': 'Invoice integration with Real Estate', | ||
'description': 'Creates an invoice when a property is sold.', | ||
'license': 'LGPL-3', | ||
'data': [], | ||
'installable': True, | ||
'application': False, | ||
'auto_install': False, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import estate_property |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from odoo import Command, models | ||
from odoo.exceptions import UserError | ||
|
||
|
||
class EstateProperty(models.Model): | ||
_inherit = "estate.property" | ||
|
||
def action_sold(self): | ||
self.check_access_rights('write') | ||
self.check_access_rule('write') | ||
print(" REACHED ".center(100, '=')) | ||
journal = self.env['account.journal'].sudo().search([ | ||
('type', '=', 'sale'), | ||
], limit=1) | ||
|
||
if not journal: | ||
raise UserError("No sales journal found!") | ||
|
||
for property in self: | ||
if not property.buyer: | ||
raise UserError("Buyer is required to create invoice.") | ||
|
||
selling_price = property.selling_price | ||
service_fee = selling_price * 0.06 | ||
|
||
invoice_vals = { | ||
'partner_id': property.buyer.id, | ||
'move_type': 'out_invoice', | ||
'journal_id': journal.id, | ||
'invoice_line_ids': [ | ||
Command.create({ | ||
'name': "Service Fees (6%)", | ||
'quantity': 1, | ||
'price_unit': service_fee, | ||
}), | ||
Command.create({ | ||
'name': "Administrative Fees", | ||
'quantity': 1, | ||
'price_unit': 100.00, | ||
}), | ||
], | ||
} | ||
self.env['account.move'].sudo().create(invoice_vals) | ||
return super().action_sold() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
'name': "Real Estate", | ||
'version': '1.0', | ||
'author': "prbo", | ||
'license': 'LGPL-3', | ||
'category': 'Real Estate/Brokerage', | ||
'sequence': 1, | ||
'application': True, | ||
'depends': ['base'], | ||
'data': [ | ||
'security/estate_security.xml', | ||
'security/ir.model.access.csv', | ||
'security/estate_property_rules.xml', | ||
'data/estate.property.type.csv', | ||
'views/estate_property_offer_view.xml', | ||
'views/estate_property_type_view.xml', | ||
'views/estate_property_views.xml', | ||
'views/estate_property_tag_view.xml', | ||
'views/res_users_views.xml', | ||
'views/estate_menus.xml', | ||
], | ||
'demo': [ | ||
'demo/demo_data.xml', | ||
], | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
id,name | ||
estate_property_type_residential,Residential | ||
estate_property_type_commercial,Commercial | ||
estate_property_type_industrial,Industrial | ||
estate_property_type_land,Land |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<odoo> | ||
<record id="estate_demo_property" model="estate.property"> | ||
<field name="name">Big Villa</field> | ||
<field name="description">A nice and big villa</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="postcode">12345</field> | ||
<field name="date_availability">2020-02-02</field> | ||
<field name="state">new</field> | ||
<field name="property_type" ref="estate_property_type_residential" /> | ||
</record> | ||
<record id="estate_demo_property_2" model="estate.property"> | ||
<field name="name">Trailer home</field> | ||
<field name="description">Home in a trailer park</field> | ||
<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> | ||
<field name="garden">False</field> | ||
<field name="postcode">54321</field> | ||
<field name="date_availability">1970-01-01</field> | ||
<field name="state">cancelled</field> | ||
<field name="property_type" ref="estate_property_type_residential" /> | ||
</record> | ||
<record id="offer_azure_accepted" model="estate.property.offer"> | ||
<field name="partner_id" ref="base.res_partner_1" /> | ||
<field name="property_id" ref="estate_demo_property" /> | ||
<field name="price">300000</field> | ||
<field name="validity">14</field> | ||
<field name="date_deadline" eval="(datetime.now() + timedelta(days=14)).date().isoformat()" /> | ||
</record> | ||
<record id="offer_azure_rejected" model="estate.property.offer"> | ||
<field name="partner_id" ref="base.res_partner_1" /> | ||
<field name="property_id" ref="estate_demo_property" /> | ||
<field name="price">1500000</field> | ||
<field name="validity">14</field> | ||
<field name="date_deadline" eval="(datetime.now() + timedelta(days=14)).date().isoformat()" /> | ||
</record> | ||
<record id="offer_deco_rejected" model="estate.property.offer"> | ||
<field name="partner_id" ref="base.res_partner_2" /> | ||
<field name="property_id" ref="estate_demo_property" /> | ||
<field name="price">1500001</field> | ||
<field name="validity">14</field> | ||
<field name="date_deadline" eval="(datetime.now() + timedelta(days=14)).date().isoformat()" /> | ||
</record> | ||
<function model="estate.property.offer" name="action_accept"> | ||
<value eval="[ref('offer_azure_accepted')]" /> | ||
</function> | ||
<function model="estate.property.offer" name="action_refuse"> | ||
<value eval="[ref('offer_azure_rejected')]" /> | ||
</function> | ||
<function model="estate.property.offer" name="action_refuse"> | ||
<value eval="[ref('offer_deco_rejected')]" /> | ||
</function> | ||
<record id="demo_property_modern_apartment" model="estate.property"> | ||
<field name="name">Modern Apartment</field> | ||
<field name="expected_price">250.0</field> | ||
<field name="description">Stylish and modern 2BHK in the city center.</field> | ||
<field name="date_availability">2024-06-10</field> | ||
<field name="postcode">b</field> | ||
<field name="selling_price">5000.0</field> | ||
<field name="state">new</field> | ||
<field name="bedrooms">2</field> | ||
<field name="living_area">950.0</field> | ||
<field name="facades">2</field> | ||
<field name="garage" eval="True"/> | ||
<field name="garden" eval="True"/> | ||
<field name="garden_area">200.0</field> | ||
<field name="property_type" ref="estate.property.type.residential"/> | ||
<field name="offer_ids" eval="[ | ||
Command.create({ | ||
'partner_id': ref('base.res_partner_12'), | ||
'price': 2600000.0, | ||
'validity': 14, | ||
}), | ||
Command.create({ | ||
'partner_id': ref('base.res_partner_2'), | ||
'price': 2700000.0, | ||
'validity': 14, | ||
}) | ||
]"/> | ||
</record> | ||
</odoo> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#license': 'LGPL-3 | ||
from . import estate_property | ||
from . import estate_property_type | ||
from . import estate_property_tag | ||
from . import estate_property_offer | ||
from . import res_users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from datetime import date, timedelta | ||
from odoo import api, fields, models | ||
from odoo.exceptions import UserError, ValidationError | ||
from odoo.tools.float_utils import float_compare | ||
|
||
|
||
class EstateProperty(models.Model): | ||
_name = "estate.property" | ||
_description = "Test Model" | ||
_order = "id desc" | ||
name = fields.Char(required=True) | ||
description = fields.Text() | ||
postcode = fields.Char() | ||
date_availability = fields.Date( | ||
string="Available From", | ||
copy=False, | ||
default=lambda self: date.today() + timedelta(days=90) | ||
) | ||
expected_price = fields.Float(required=True) | ||
selling_price = fields.Float(readonly=True, copy=False) | ||
best_price = fields.Float(compute="_compute_best_price") | ||
bedrooms = fields.Integer(string="Bedrooms", default=2) | ||
facades = fields.Integer(string="Facades") | ||
garage = fields.Boolean(string="Garage") | ||
garden = fields.Boolean(string="Garden") | ||
living_area = fields.Float(string="Living Area (m²)") | ||
garden_area = fields.Float(string="Garden Area (m²)") | ||
total_area = fields.Float(compute="_compute_total_area") | ||
garden_orientation = fields.Selection( | ||
string='Garden Orientation', | ||
selection=[ | ||
('north', 'North'), | ||
('south', 'South'), | ||
('east', 'East'), | ||
('west', 'West') | ||
], | ||
help="Direction of the garden" | ||
) | ||
active = fields.Boolean(default=True) | ||
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 = fields.Many2one("estate.property.type", string="Property Type") | ||
buyer = fields.Many2one("res.partner", string="Buyer", copy=False) | ||
seller = 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") | ||
_sql_constraints = [ | ||
('check_expected_price_positive', 'CHECK(expected_price > 0)', 'Expected price must be strictly positive.'), | ||
('check_selling_price_positive', 'CHECK(selling_price >= 0)', 'Selling price must be zero or positive.') | ||
] | ||
company_id = fields.Many2one( | ||
'res.company', | ||
string='Company', | ||
required=True, | ||
default=lambda self: self.env.company | ||
) | ||
@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.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_sold(self): | ||
for record in self: | ||
if record.state in ['sold', 'cancelled']: | ||
raise UserError("You cannot sell a cancelled or already sold property.") | ||
if not record.buyer: | ||
raise UserError("Please assign a buyer before marking the property as sold.") | ||
record.state = 'sold' | ||
def action_cancel(self): | ||
for record in self: | ||
if record.state in ['sold', 'cancelled']: | ||
raise UserError("You cannot cancel a sold or already cancelled property.") | ||
record.state = 'cancelled' | ||
@api.constrains('selling_price', 'expected_price') | ||
def _check_selling_price_margin(self): | ||
for record in self: | ||
if float_compare(record.selling_price, 0.0, precision_digits=2) > 0: | ||
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.ondelete(at_uninstall=False) | ||
def _check_property_state_on_delete(self): | ||
for record in self: | ||
if record.state not in ['new', 'cancelled']: | ||
raise UserError("Only properties with state 'New' or 'Cancelled' can be deleted.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from datetime import timedelta | ||
from odoo import api, models, fields | ||
from odoo.exceptions import UserError, ValidationError | ||
|
||
|
||
class EstatePropertyOffer(models.Model): | ||
_name = "estate.property.offer" | ||
_description = "Property Offer" | ||
_order = "price desc" | ||
price = fields.Float(required=True) | ||
status = fields.Selection([ | ||
('accepted', 'Accepted'), | ||
('refused', 'Refused') | ||
]) | ||
partner_id = fields.Many2one("res.partner", string="Partner", required=True) | ||
property_id = fields.Many2one( | ||
"estate.property", | ||
required=True, | ||
ondelete='cascade' | ||
) | ||
property_type_id = fields.Many2one( | ||
related="property_id.property_type", | ||
store=True, | ||
string="Property Type" | ||
) | ||
validity = fields.Integer(string="Validity (days)", default=7) | ||
date_deadline = fields.Date( string="Deadline Date", compute="_compute_date_deadline", inverse="_inverse_date_deadline", store=True) | ||
_sql_constraints = [ | ||
('check_offer_price_positive', 'CHECK(price > 0)', 'Offer price must be strictly positive.') | ||
] | ||
@api.depends("create_date", "validity") | ||
def _compute_date_deadline(self): | ||
for record in self: | ||
create_date = record.create_date or fields.Date.today() | ||
record.date_deadline = create_date + timedelta(days=record.validity) | ||
def _inverse_date_deadline(self): | ||
for record in self: | ||
create_date = record.create_date.date() if record.create_date else fields.Date.today() | ||
record.validity = (record.date_deadline - create_date).days | ||
def action_accept(self): | ||
for record in self: | ||
if record.property_id.state == 'sold': | ||
raise UserError("You cannot accept an offer on a sold property.") | ||
record.status = 'accepted' | ||
record.property_id.selling_price = record.price | ||
record.property_id.buyer = record.partner_id | ||
record.property_id.state = 'offer_accepted' | ||
|
||
other_offers = self.search([ | ||
('property_id', '=', record.property_id.id), | ||
('id', '!=', record.id) | ||
]) | ||
other_offers.write({'status': 'refused'}) | ||
def action_refuse(self): | ||
for offer in self: | ||
offer.status = 'refused' | ||
return True | ||
@api.model_create_multi | ||
def create(self, vals_list): | ||
property_ids = list({vals['property_id'] for vals in vals_list if 'property_id' in vals}) | ||
property_records = self.env['estate.property'].browse(property_ids) | ||
property_map = {prop.id: prop for prop in property_records} | ||
for vals in vals_list: | ||
property_id = vals.get('property_id') | ||
property_rec = property_map.get(property_id) | ||
if property_rec: | ||
existing_offers = property_rec.offer_ids.filtered( | ||
lambda o: o.price >= vals.get('price', 0) | ||
) | ||
if existing_offers: | ||
raise ValidationError("You cannot create an offer lower than or equal to an existing one.") | ||
property_rec.state = 'offer_received' | ||
return super().create(vals_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "Property Tag" | ||
_order = "name" | ||
name = fields.Char(required=True) | ||
color = fields.Integer(string="Color") | ||
sequence = fields.Integer(string="sequence", default="10") | ||
_sql_constraints = [ | ||
('unique_tag_name', 'UNIQUE(name)', 'Tag name must be unique.') | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.