Skip to content

Commit

Permalink
[ADD] estate: best offer, validity date, sold/canceled buttons, SQL c…
Browse files Browse the repository at this point in the history
…onstraint

- Create Best Offer field with computed
- Create the Offer date with Validation date
- Create sold, canceled, accepted, refused button
- Create validation warning on negative number
- Add SQL constraint
- Covered from chapter 8 to 10
  • Loading branch information
amya-odoo committed Aug 6, 2024
1 parent 0db7313 commit 9564242
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 34 deletions.
2 changes: 2 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
'views/estate_property_views.xml',
'views/estate_property_type.xml',
'views/estate_property_tag_view.xml',
'views/estate_property_offer_view.xml',
'views/estate_menus.xml',
],
'license': 'LGPL-3',
}
63 changes: 52 additions & 11 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from odoo import models, fields
from odoo import models, fields, api
from dateutil.relativedelta import relativedelta
from datetime import date
from odoo.exceptions import UserError


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property Data"
Expand All @@ -17,20 +20,13 @@ class EstateProperty(models.Model):
garden = fields.Boolean()
garden_area = fields.Integer()
active = fields.Boolean(default=True)
state = fields.Selection(default='new',
selection=[('new', 'New'),
('offer_recived', 'Offer Recieved'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'), ('canceled', 'Canceled')])
garden_orientation = fields.Selection(selection=[('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')])
state = fields.Selection(default='new', selection=[('new', 'New'), ('offer_recived', 'Offer Recieved'), ('offer_accepted', 'Offer Accepted'), ('sold', 'Sold'), ('canceled', 'Canceled')])
garden_orientation = fields.Selection(selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')])
property_type_id = fields.Many2one("estate.property.type", string='Property Type')
salesman_id = fields.Many2one(
'res.users', # The model this field relates to
string='Salesman', # Label for the field
default=lambda self: self.env.user.partner_id # Set default to the current user's partner
default=lambda self: self.env.user.partner_id # Set default to the current user's partner
)
buyer_id = fields.Many2one(
'res.partner', # The model this field relates to
Expand All @@ -40,3 +36,48 @@ class EstateProperty(models.Model):

tag_ids = fields.Many2many('estate.property.tag', string='Tags')
offer_ids = fields.One2many('estate.property.offer', 'property_id')
total = fields.Integer(compute='total_area')

@api.depends('living_area', 'garden_area')
def total_area(self):
for recorde in self:
recorde.total = recorde.living_area + recorde.garden_area

best_offer = fields.Float(compute='best_offer_selete', store=True)

@api.depends('offer_ids')
def best_offer_selete(self):
temp = 0
for offer in self.offer_ids:
if offer.price > temp:
temp = offer.price
self.best_offer = temp

@api.onchange('garden')
def garden_change(self):
if self.garden:
self.garden_area = 10
self.garden_orientation = 'north'
else:
self.garden_area = 0
self.garden_orientation = ''

def status_action_sold_button(self):
if self.state == 'canceled':
raise UserError("Canceled property can't be sold")
else:
self.state = 'sold'

def status_action_canceled_button(self):
if self.state == 'sold':
raise UserError("Sold property can't be canceled")
else:
self.state = 'canceled'

_sql_constraints = [
('check_expected_price', 'CHECK(expected_price > 0)',
'The Expected Price of Property should be positive'),
('check_selling_price', 'CHECK(selling_price >= 0 )',
'The Selling Price of Property should be Positive'),
('name_uniq', 'unique (name)', "Property name already exists!"),
]
56 changes: 52 additions & 4 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
from odoo import models, fields
from odoo import models, fields, api
from datetime import timedelta, date
from odoo.exceptions import ValidationError


class EstatePropertyOffer(models.Model):
_name='estate.property.offer'
_description='Estate Property Offer'
_name = 'estate.property.offer'
_description = 'Estate Property Offer'
price = fields.Float()
status = fields.Selection(copy=False, selection=[('accepted', 'Accepted'), ('refused', 'Refused')])
partner_id = fields.Many2one('res.partner', required=True)
property_id = fields.Many2one('estate.property', required=True)
property_id = fields.Many2one('estate.property', required=True)
validity = fields.Integer(default=7)
deadline = fields.Date(compute='_compute_deadline', inverse='_inverse_deadline')

@api.depends('validity')
def _compute_deadline(self):
for record in self:
today = record.create_date
if today:
today = record.create_date
else:
today = date.today()
if record.validity:
record.deadline = today + timedelta(days=record.validity)
else:
record.deadline = today

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

@api.depends('property_id')
def action_status_accept(self):
self.status = 'accepted'
price_percent = (self.price / self.property_id.expected_price) * 100
if price_percent > 90:
self.property_id.selling_price = self.price
self.property_id.buyer_id = self.partner_id
else:
raise ValidationError("The selling price must be at least 90%")

def aciton_status_refused(self):
self.status = 'refused'

_sql_constraints = [
('check_price', 'CHECK(price >= 0)',
'The Price of Offer should be positive'),
]
7 changes: 5 additions & 2 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from odoo import models, fields


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


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Real Estate Property Type"
name = fields.Char(required=True)

39 changes: 39 additions & 0 deletions estate/views/estate_property_offer_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<odoo>

<record id='estate_property_offer_tree' model='ir.ui.view'>
<field name="name">estate_property_offer</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree sample="1">
<field name='price'/>
<field name='partner_id'/>

<field name='validity' string='Validity (days)'/>
<field name='deadline'/>
<button name="action_status_accept" type="object" icon="fa-check" />
<button name="aciton_status_refused" type="object" icon="fa-times"/>
<field name='status'/>
</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>
<field name="arch" type="xml">
<form >
<sheet>
<group>
<field name='price'/>
<field name='partner_id'/>
<field name="validity"/>
<field name="deadline"/>
<field name='status'/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions estate/views/estate_property_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</p>
</field>
</record>

<record id='estate_property_form_view' model='ir.ui.view'>

<field name='name'> estate_property_type</field>
Expand Down
24 changes: 9 additions & 15 deletions estate/views/estate_property_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@
<field name="model">estate.property</field>
<field name="arch" type="xml">
<form string="description">
<header>
<button name="status_action_sold_button" type="object" string="Sold"/>
<button name="status_action_canceled_button" type='object' string='Canceled'/>
</header>
<sheet>
<div>
<h1>
<field name="name"/>
</h1>
<field name="tag_ids" widget="many2many_tags"/>

</div>
<group>
<group >
<field name='state' string='status'/>
<field name="property_type_id"/>
<field name="postcode" class="o_stat_value"/>
<field name="expected_price"/>
Expand All @@ -51,6 +57,7 @@
<field name="date_availability" string="Available From"/>

<field name="selling_price"/>
<field name ='best_offer'/>
</group>
</group>
<notebook>
Expand All @@ -70,6 +77,7 @@

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

<field name="active"/>
</group>
Expand Down Expand Up @@ -106,26 +114,12 @@
<!-- <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>

<record id='estate_property_offer' model='ir.ui.view'>
<field name="name">estate_property_offer</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree sample="1">
<field name='price'/>
<field name='partner_id'/>
<field name='status'/>
</tree>
</field>

</record>


</odoo>

0 comments on commit 9564242

Please sign in to comment.