Skip to content

Commit

Permalink
[ADD] estate: Added inheritance to extend the functionality of curren…
Browse files Browse the repository at this point in the history
…t model

Add business logic and view inheritance for real estate module

- Prevent deletion of properties not in 'New' or 'Canceled' state using
`ondelete`.
- Set property state to 'Offer Received' on
 offer creation and prevent creating offers with lower amounts than existing
ones.
- Add `property_ids` field to `res.users` model to list salesperson’s available
 properties.
- Extend `base.view_users_form` to display `property_ids` in a new notebook
page.
  • Loading branch information
krku-odoo committed Aug 13, 2024
1 parent 947622a commit 71d6d84
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"views/estate_property_offer_view.xml",
"views/property_type_views.xml",
"views/property_tag.xml",
"views/res_users_view.xml",
"views/real_estate_menu.xml",
],
"installable": True,
Expand Down
1 change: 1 addition & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import property_type
from . import property_tag
from . import property_offer
from . import inherited_model
10 changes: 9 additions & 1 deletion estate/models/estate_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyModel(models.Model):
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
("offer receivedwest", "West"),
],
)
active = fields.Boolean(default=True)
Expand Down Expand Up @@ -125,3 +125,11 @@ def _check_selling(self):
raise ValidationError(
"selling price cannot be lower than 90% of the expected price."
)

@api.ondelete(at_uninstall=False)
def _check_state_before_delete(self):
for record in self:
if record.state not in ["new", "cancelled"]:
raise UserError(
"You cannot delete a property that is not in 'New' or 'Canceled' state."
)
12 changes: 12 additions & 0 deletions estate/models/inherited_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import models, fields, api


class InheritedModel(models.Model):
_inherit = "res.users"

property_ids = fields.One2many(
"estate_property",
"salesman_id",
string="Offers",
___domain="['|',('state', '=', 'offer received'),('state', '=', 'offer accepted')]",
)
23 changes: 14 additions & 9 deletions estate/models/property_offer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from odoo import api, models, fields
from dateutil.relativedelta import relativedelta
from odoo.exceptions import UserError, ValidationError


class EstatePropertyOffer(models.Model):
Expand All @@ -15,7 +16,9 @@ class EstatePropertyOffer(models.Model):
)

partner_id = fields.Many2one("res.partner", string="Partner", required=True)
property_id = fields.Many2one("estate_property", string="Property", required=True)
property_id = fields.Many2one(
"estate_property", string="Property", ondelete="cascade"
)
validity = fields.Integer(string="Validity (days)", default=7)
deadline_date = fields.Date(
string="Deadline", compute="_compute_deadline", inverse="_inverse_deadline"
Expand Down Expand Up @@ -56,11 +59,13 @@ def action_accepted(self):
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id

_sql_constraints = [
("price", "CHECK(price >= 0)", "A price must be strictly positive.")
]
# @api.constrains('property_id.bedrooms')
# def _check_bedrooms(self):
# for record in self:
# if record.property_id.bedrooms < 0:
# raise ValidationError("You have to set bedrooms")
@api.model
def create(self, vals):
property_id = vals.get("property_id")
property = self.env["estate_property"].browse(property_id)
property.state = "offer received"
if property.offer_ids.filtered(lambda o: o.price >= vals.get("price")):
raise UserError(
"You cannot create an offer with a lower amount than an existing offer."
)
return super().create(vals)
32 changes: 32 additions & 0 deletions estate/views/res_users_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>

<record id="res_users_view_form" model="ir.ui.view">
<field name="name">res.users.form.inherit.properties</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<!-- Add a new page to the notebook -->
<xpath expr="//notebook" position="inside">
<page string="Properties">
<field name="property_ids">
<tree string="Real Estate Properties">
<field name="name" />
<field name="postcode"/>
<field name="property_type_id" />
<field name="state"/>
<field name="tag_ids" widget="many2many_tags" />
<field name="bedrooms" />
<field name="living_area" />
<field name="selling_price" />
<field name="expected_price" />
</tree>
</field>
</page>
</xpath>
</field>
</record>

</data>
</odoo>

0 comments on commit 71d6d84

Please sign in to comment.