Skip to content

Commit

Permalink
[ADD] estate: models method, inherited the res.users, add estate_acco…
Browse files Browse the repository at this point in the history
…unt module

- Add ondelete decorator in models and override the models method
- Created the res_user_model and inherit the res.users
- Add new module estate_account
- Create the invoice on action of property sold from estate_account to invoice
  • Loading branch information
amya-odoo committed Aug 14, 2024
1 parent cae8bfd commit 090fd5b
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 54 deletions.
1 change: 1 addition & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

'data': [
'security/ir.model.access.csv',
'views/res_user_views.xml',
'views/estate_property_views.xml',
'views/estate_property_offer_view.xml',
'views/estate_property_type.xml',
Expand Down
1 change: 1 addition & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import res_user_model
22 changes: 14 additions & 8 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,20 @@ class EstateProperty(models.Model):
)
_order = "id desc"
salesman_id = fields.Many2one(
"res.users", # The model this field relates to
string="Salesman", # Label for the field
default=lambda self: self.env.user, # Set default to the current user's partner
"res.users",
string="Salesman",
default=lambda self: self.env.user,
)
buyer_id = fields.Many2one(
"res.partner", # The model this field relates to
string="Buyer", # Label for the field
copy=False, # Prevent the field value from being copied when duplicating the record
"res.partner",
string="Buyer",
copy=False,
)

tag_ids = fields.Many2many("estate.property.tag", string="Tags", ondelete="cascade")
offer_ids = fields.One2many("estate.property.offer", "property_id")
total = fields.Integer(compute="total_area")
best_offer = fields.Float(compute="best_offer_selete", store=True)
property_type_id = fields.Many2one("estate.property.type")

sql_constraints = [
(
"check_expected_price",
Expand Down Expand Up @@ -117,3 +115,11 @@ def _check_date_end(self):
price_percent = (record.selling_price / record.expected_price) * 100
if price_percent < 90:
raise ValidationError("Selling Price must be at least 90%")

@api.ondelete(at_uninstall=False)
def _check_property_state(self):
for recod in self:
if recod.state == "new" or recod.state == "canceled":
continue
else:
raise ValidationError("New or Canceled property can be delete only.")
29 changes: 22 additions & 7 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from odoo import models, fields, api
from datetime import timedelta, date
from odoo.exceptions import ValidationError
from odoo import api, fields, models
from datetime import date, timedelta
from odoo.exceptions import UserError, ValidationError


class EstatePropertyOffer(models.Model):
Expand All @@ -21,7 +21,6 @@ class EstatePropertyOffer(models.Model):
def _check_offer(self):
for record in self:
if record.status == "accepted":
# raise ValidationError("Accepted offer have benn deleted")
record.property_id.selling_price = 0
record.property_id.buyer_id = None
else:
Expand All @@ -43,13 +42,11 @@ def _compute_deadline(self):
def _inverse_deadline(self):
for record in self:
if record.deadline:
# Convert create_date to a date object
today = fields.Date.to_date(record.create_date)
# Convert deadline to a date object
deadline_date = fields.Date.from_string(record.deadline)
record.validity = (deadline_date - today).days
else:
record.validity = 7 # Default to 7 if deadline is not set
record.validity = 7

def action_status_accept(self):
self.status = "accepted"
Expand All @@ -73,3 +70,21 @@ def aciton_status_refused(self):
_sql_constraints = [
("check_price", "CHECK(price >= 0)", "The Price of Offer should be positive"),
]

@api.model
def create(self, vals):
property_id = vals.get('property_id')
price = vals.get('price', 0)
if not property_id:
raise ValidationError("Property ID is required.")
property_record = self.env['estate.property'].browse(property_id)
existing_offers = self.search([('property_id', '=', property_id)])
max_price = max(existing_offers.mapped('price'), default=0)
if price < max_price:
raise UserError("The offer price must be higher than the existing offers.")
record = super().create(vals)
if property_record:
property_record.state = 'offer_recived'
else:
raise ValidationError("Property record could not be found.")
return record
6 changes: 3 additions & 3 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from odoo import models, fields
from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = 'estate.property.tag'
_description = 'Estate Property tag'
name = fields.Char(required=True)
_sql_constraints = [('name_uniq', 'unique (name)', "Tag name already exists!")]
_order = 'name'
color = fields.Integer()
_order = 'name'
_sql_constraints = [('name_uniq', 'unique (name)', "Tag name already exists!")]
6 changes: 6 additions & 0 deletions estate/models/res_user_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from odoo import models, fields


class ResUserModel(models.Model):
_inherit = 'res.users'
property_ids = fields.One2many('estate.property', 'salesman_id', string='Properties', domain="['|', ('state', '=', 'new'),('state', '=', 'offer_recived')]")
3 changes: 0 additions & 3 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<odoo>
<menuitem id="estate_property_root" name="Real Estate">

<menuitem id="estate_property_first_level_menu" name="Advertisment">
<menuitem id="estate_property_model_menu_action" action="estate_property_action"/>
</menuitem>

<menuitem id="estate_property_type_level_menu" name="Setting">
<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>
4 changes: 0 additions & 4 deletions estate/views/estate_property_offer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
<tree editable="bottom" sample="1" decoration-danger="status in ('refused', 'Refused')" decoration-success="status in ('accepted', 'Accepted')">
<field name='price'/>
<field name='partner_id'/>

<field name='validity' string='Validity (days)'/>
<field name='deadline'/>
<button name="action_status_accept" invisible="status in ('accepted', 'refused')" title="accept" type="object" icon="fa-check" />
<button name="aciton_status_refused" invisible="status in ('accepted', 'refused')" title="refused" type="object" icon="fa-times"/>
<field name='status' column_invisible='1' />
</tree>
</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>
Expand Down
3 changes: 0 additions & 3 deletions estate/views/estate_property_tag_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
<field name="name">Property Tag</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">tree,form</field>

<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a new Property product
</p>
</field>
</record>

<record id='estate_property_tag_tree' model='ir.ui.view'>
<field name="name">estate_property_tag</field>
<field name="model">estate.property.tag</field>
Expand All @@ -19,6 +17,5 @@
<field name='name'/>
</tree>
</field>

</record>
</odoo>
8 changes: 0 additions & 8 deletions estate/views/estate_property_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,34 @@
<field name="name">Property Type</field>
<field name="res_model">estate.property.type</field>
<field name="view_mode">tree,form</field>


<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a new Property product
</p>
</field>
</record>

<record id="estate_property_type_tree" model="ir.ui.view">
<field name="name">estate_property_type_tree</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<tree sample="1">
<field name="sequence" widget="handle"/>
<field name="name"/>
<!-- <field name="offer_count"/> -->
</tree>
</field>
</record>

<record id='estate_property_form_view' model='ir.ui.view'>
<field name='name'> estate_property_type</field>
<field name='model'>estate.property.type</field>

<field name="arch" type="xml">
<form string='product Type'>
<sheet>

<button type="action" name="%(estate.action_estate_property_offer_y)d" string="Offers" class="oe_stat_button" icon="fa-ticket">
<div class="o_stat_info">
<field name="offer_count" widget="statinfo" string=""/>
<span class="o_stat_text"> Offer</span>
</div>
</button>

<h1>
<field name='name'/>
</h1>
Expand Down
18 changes: 0 additions & 18 deletions estate/views/estate_property_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
<field name="context">{'search_default_state': True, 'search_default_current': True}</field>


<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a new Property product
</p>
</field>
</record>

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

<record id="action_estate_property_form" model="ir.ui.view">
<field name="name">estate_property_form</field>
<field name="model">estate.property</field>
Expand All @@ -50,7 +46,6 @@
<field name="name"/>
</h1>
<field name="tag_ids" widget="many2many_tags" options="{'color_field': 'color'}" />

</div>
<group>
<group >
Expand All @@ -69,22 +64,14 @@
<page string="Description">
<group>
<field name="description"/>

<field name="bedrooms"/>

<field name="living_area" string="Living Area(sqm)"/>

<field name = "facades"/>

<field name = "garden"/>


<field name = "garden_area" string = "Garden Area(sqm)" invisible="garden != 1"/>
<field name = "garden_orientation" invisible="garden != 1"/>

<field name="state"/>
<field name="total" string="Total Area(sqm)"/>

<field name="active"/>
</group>
</page>
Expand All @@ -105,10 +92,8 @@
<field name="activity_ids" />
</div>
</form>

</field>
</record>

<record id="action_search_estate_property" model = "ir.ui.view">
<field name="name"> state_property_search</field>
<field name="model">estate.property</field>
Expand All @@ -122,15 +107,12 @@
<field name="facades"/>
<field name='property_type_id'/>
<separator/>
<!-- <filter string="Active" name="active" domain="[('active', '=', True)]"/> -->
<filter string="Available" name="state" domain="['|',('state', '=', 'new'),('state', '=', 'offer_recived')]" />
<group expand="1" string="Group By">

<filter string="Postcode" name="postcode" context="{'group_by':'postcode'}"/>
</group>
</search>
</field>
</record>

</odoo>

30 changes: 30 additions & 0 deletions estate/views/res_user_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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">
<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>

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_account",
'version': '0.1',
'depends': ['base', 'account', 'estate'],
'description': "Technical practice",
'installable': True,
'application': True,
'license': 'LGPL-3',
}
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
41 changes: 41 additions & 0 deletions estate_account/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from odoo import models, Command
import logging
from datetime import date


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

def status_action_sold_button(self):
logging.info("Request to create the invoices of property")
for record in self:
if record.buyer_id:
partner_id = record.buyer_id.id
self.env["account.move"].create(
{
"partner_id": partner_id,
"move_type": "out_invoice",
"invoice_date": date.today(),
"narration": "Tutorials traning",
"amount_total": record.selling_price,
"invoice_line_ids": [
Command.create(
{
"name": record.name,
"quantity": 1,
"price_unit": record.selling_price * 0.06,
"invoice_date": date.today(),
}
),
Command.create(
{
"name": "Administrative Fee",
"quantity": 1,
"price_unit": 100,
"invoice_date": date.today(),
},
),
],
}
)
return super().status_action_sold_button()

0 comments on commit 090fd5b

Please sign in to comment.