Skip to content

Commit

Permalink
[ADD] estate: Added Interact With Other Modules
Browse files Browse the repository at this point in the history
feat(estate_account): Implement invoice creation on property sale

- Created `estate_account` module, dependent on `estate` and `account` modules.
- Added logic in `estate.property` model to generate a customer invoice when a
property is marked as 'Sold'.
- The invoice includes:
  - A line charging 6% of the property's selling price.
  - A line charging an additional .00 as administrative fees.
Completed chapter till Chapter-13
  • Loading branch information
krku-odoo committed Aug 13, 2024
1 parent 5e7c24a commit 2f35263
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 64 deletions.
1 change: 0 additions & 1 deletion estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from . import estate_property
from . import test_model
from . import property_type
from . import property_tag
from . import property_offer
Expand Down
51 changes: 25 additions & 26 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models


class MyModel(models.Model):
Expand Down Expand Up @@ -41,7 +41,6 @@ class MyModel(models.Model):
tracking=True,
default="new",
)

salesman_id = fields.Many2one(
"res.users", # The model this field relates to
string="Salesman", # Label for the field
Expand All @@ -60,6 +59,30 @@ class MyModel(models.Model):
total_area = fields.Float(compute="_compute_total_area")
best_offer = fields.Float(compute="_compute_best_offer")

_sql_constraints = [
("property_name", "unique(name)", "The property name must be unique!"),
(
"selling_price",
"check(selling_price >= 0)",
"A property selling price must be strictly positive.",
),
(
"expected_price",
"CHECK(expected_price >= 0)",
"A property expected price must be strictly positive.",
),
]

@api.constrains("selling_price", "expected_price")
def _check_selling(self):
for record in self:
if record.selling_price > 0:
price_per = (record.expected_price * 90) / 100
if record.selling_price < price_per:
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:
Expand Down Expand Up @@ -102,30 +125,6 @@ def _compute_best_offer(self):
else:
record.best_offer = 0.0

_sql_constraints = [
("property_name", "unique(name)", "The property name must be unique!"),
(
"selling_price",
"check(selling_price >= 0)",
"A property selling price must be strictly positive.",
),
(
"expected_price",
"CHECK(expected_price >= 0)",
"A property expected price must be strictly positive.",
),
]

@api.constrains("selling_price", "expected_price")
def _check_selling(self):
for record in self:
if record.selling_price > 0:
price_per = (record.expected_price * 90) / 100
if record.selling_price < price_per:
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:
Expand Down
2 changes: 1 addition & 1 deletion estate/models/inherited_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import models, fields
from odoo import fields, models


class InheritedModel(models.Model):
Expand Down
16 changes: 5 additions & 11 deletions estate/models/property_offer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from odoo import api, models, fields
from dateutil.relativedelta import relativedelta
from odoo.exceptions import UserError
from dateutil.relativedelta import relativedelta
from odoo import api, models, fields


class EstatePropertyOffer(models.Model):
Expand All @@ -14,7 +14,6 @@ class EstatePropertyOffer(models.Model):
string="Status",
copy=False,
)

partner_id = fields.Many2one("res.partner", string="Partner", required=True)
property_id = fields.Many2one(
"estate_property", string="Property", ondelete="cascade"
Expand All @@ -40,12 +39,8 @@ def _compute_deadline(self):
def _inverse_deadline(self):
for record in self:
if record.property_id.create_date and record.deadline_date:
flag = fields.Date.from_string(record.deadline_date)
flag1 = fields.Date.from_string(record.property_id.create_date)
if flag and flag1:
record.validity = (flag - flag1).days
else:
record.validity = 7

record.validity = (record.deadline_date - record.property_id.create_date.date()).days
else:
record.validity = 7

Expand All @@ -61,8 +56,7 @@ def action_accepted(self):

@api.model
def create(self, vals):
property_id = vals.get("property_id")
property_users = self.env["estate_property"].browse(property_id)
property_users = self.env["estate_property"].browse(vals.get("property_id"))
property_users.state = "offer received"
if property_users.offer_ids.filtered(lambda o: o.price >= vals.get("price")):
raise UserError(
Expand Down
2 changes: 1 addition & 1 deletion estate/models/property_tag.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import models, fields
from odoo import fields, models


class PropertyTag(models.Model):
Expand Down
3 changes: 1 addition & 2 deletions estate/models/property_type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import api, models, fields
from odoo import api, fields, models


class PropertyType(models.Model):
Expand All @@ -12,7 +12,6 @@ class PropertyType(models.Model):
property_ids = fields.One2many(
"estate_property", "property_type_id", string="Offers"
)
# nickname = fields.Char(related='property_ids.partner_id.name', store=True)
offer_ids = fields.One2many("estate.property.offer", "property_type_id", store=True)
sequence = fields.Integer("Sequence")
offer_count = fields.Integer(compute="_compute_offer_count")
Expand Down
10 changes: 0 additions & 10 deletions estate/models/test_model.py

This file was deleted.

2 changes: 2 additions & 0 deletions estate/views/estate_property_offer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[('property_type_id', '=', active_id)]
</field>
</record>

<record id="view_estate_property_offer_tree" model="ir.ui.view">
<field name="name">
estate.property.offer.tree
Expand All @@ -28,6 +29,7 @@
</tree>
</field>
</record>

<record id="view_estate_property_offer_form" model="ir.ui.view">
<field name="name">
estate.property.offer.form
Expand Down
2 changes: 1 addition & 1 deletion estate/views/estate_property_tree_views.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<!-- Tree View -->
<record id="view_estate_property_tree" model="ir.ui.view">
<field name="name">estate.property.tree</field>
<field name="model">estate_property</field>
Expand All @@ -22,6 +21,7 @@
</tree>
</field>
</record>

<record id="view_property_form" model="ir.ui.view">
<field name="name">property.form</field>
<field name="model">estate_property</field>
Expand Down
6 changes: 2 additions & 4 deletions estate/views/property_type_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</p>
</field>
</record>

<record id="view_estate_property_type_tree" model="ir.ui.view">
<field name="name">real.estate.property.type.tree</field>
<field name="model">real.estate.property.type</field>
Expand All @@ -23,15 +24,12 @@
</tree>
</field>
</record>

<record id="view_property_type_form" model="ir.ui.view">
<field name="name">property.type.form</field>
<field name="model">real.estate.property.type</field>
<field name="arch" type="xml">
<form string="Estate Properties Type">
<!--
<header>
</header>
-->
<sheet>
<button type="action" name="%(estate.estate_property_offer_action)d" string="Offers" class="oe_stat_button" icon="fa-ticket">
<div class="o_stat_info">
Expand Down
3 changes: 2 additions & 1 deletion estate/views/real_estate_property.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</p>
</field>
</record>

<record id="estate_property_view_search" model="ir.ui.view">
<field name="name">estate.property.view.search</field>
<field name="model">estate_property</field>
Expand All @@ -29,5 +30,5 @@
</group>
</search>
</field>
</record>
</record>
</odoo>
7 changes: 1 addition & 6 deletions estate/views/res_users_view.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?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">
Expand All @@ -27,6 +24,4 @@
</xpath>
</field>
</record>

</data>
</odoo>
</odoo>
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
12 changes: 12 additions & 0 deletions estate_account/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Estate Account",
"version": "1.0",
"license": "LGPL-3",
"category": "Estate Account",
"summary": "Manage Accounts",
"depends": ["estate", "account"],
"data": [],
"installable": True,
"application": True,
"auto_install": 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
31 changes: 31 additions & 0 deletions estate_account/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from odoo import Command, models


class InheritedModel(models.Model):
_inherit = "estate_property"

def action_sold(self):
invoices = self.env["account.move"].create(
{
"name": "Test",
"partner_id": self.buyer_id.id,
"move_type": "out_invoice",
"invoice_line_ids": [
Command.create(
{
"name": "6% of Selling Price",
"quantity": 1,
"price_unit": self.selling_price * 0.06,
}
),
Command.create(
{
"name": "Administrative Fees",
"quantity": 1,
"price_unit": 100.00,
}
),
],
}
)
return super().action_sold()

0 comments on commit 2f35263

Please sign in to comment.