Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
'name': "Estate",
'version': '1.0',
'depends': ['base'],
'author': 'Dhrudeep',
'description': """
This module provides functionality to manage real estate properties.
""",
'data': [
'security/ir.model.access.csv',
'views/estate_property_tag_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_views.xml',
'views/estate_property_maintenance_requests_view.xml',
'views/estate_investor_profile_view.xml',
'views/estate_res_users_view.xml',
'views/estate_menus.xml',
],
'category': 'Tutorial',
'license': 'LGPL-3',
'application': True,
}
7 changes: 7 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import estate_property_maintenance_requests
from . import res_users
from . import estate_investor_profile
9 changes: 9 additions & 0 deletions estate/models/estate_investor_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import fields, models


class EstatePropertyInvestor(models.Model):
_name = 'estate.property.investor'
_description = 'Estate Property investor'

name = fields.Many2one('res.partner', string="investor")
property_ids = fields.One2many('estate.property', 'buyer_ids')
137 changes: 137 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError


class EstateProperty(models.Model):
_name = 'estate.property'
_description = 'Estate Property Planning'
_order = 'id desc' # defaultvalue = asc

name = fields.Char(required=True, default="Unknown")
description = fields.Text()
postcode = fields.Float()
date_availability = fields.Date(
"Available From", copy=False, default=lambda self: fields.Date.today() + relativedelta(months=3)
)
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection(
selection=[
('north', "North"),
('west', "West"),
('east', "East"),
('south', "South"),
],
help="Direction for 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"),
],
string="Status",
default='new',
)
property_type_id = fields.Many2one(
'estate.property.type', string="Property Type")
seller_id = fields.Many2one(
'res.users', string="Salesman", default=lambda self: self.env.user
)
buyer_ids = fields.Many2one(
'res.partner', string="Partner/Buyer", copy=False)
tags_ids = fields.Many2many('estate.property.tag')
offer_ids = fields.One2many('estate.property.offer', 'property_id')
total_area = fields.Float(compute='_compute_total_area')
best_price = fields.Float(
"Best offer", compute='_compute_best_price', store=True)
property_maintenance_requests = fields.One2many(
'estate.property.maintenance.requests', 'property_id')
total_maintenance_cost = fields.Float(
compute='_compute_total_maintenance_cost', string="Total Maintenance Cost")
is_favorite = fields.Boolean(string="Favorite")

# SQL Constraint
_check_expected_price = models.Constraint(
'CHECK(expected_price > 0)', "The expected price must be strictly positive")
_check_selling_price = models.Constraint(
'CHECK(selling_price >= 0)', "The selling price must be positive")

# Python Constriant
@api.constrains('selling_price', 'expected_price')
def _check_price(self):
if self.buyer_ids and self.selling_price < (self.expected_price * 0.9):
raise ValidationError(
"The selling price must be at least 90% of the expected price! You must reduce the expected price if you want to accept this offer.")

# Compute Methods
# Depends Decorator
@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):
result = dict(self.env['estate.property.offer']._read_group(
[('property_id', 'in', self.ids)],
['property_id'],
['price:max']
))
for record in self:
record.best_price = result.get(record, 0)

@api.depends('property_maintenance_requests.cost')
def _compute_total_maintenance_cost(self):
result = dict(self.env['estate.property.maintenance.requests']._read_group(
[('property_id', 'in', self.ids)],
['property_id'],
['cost:sum']
))
for record in self:
record.total_maintenance_cost = result.get(record, 0)

# Onchange Decorator
@api.onchange('garden')
def _onchange_garden(self):
if self.garden:
self.garden_area = 10
self.garden_orientation = 'north'
else:
self.garden_area = None
self.garden_orientation = None

# Action Methods
def action_sold(self):
if 'cancelled' in self.mapped('state'):
raise UserError("Cancelled properties cannot be sold.")
else:
for record in self:
if record.property_maintenance_requests.status != 'done':
raise UserError(
"Property cannot be sold if there is any maintenance request not done.")
return self.write({'state': 'sold'})

def action_cancel(self):
if 'sold' in self.mapped('state'):
raise UserError("Sold properties cannot be cancelled.")
return self.write({'state': 'cancelled'})

# Ondelete Decorator
@api.ondelete(at_uninstall=False)
def _check_state(self):
for record in self:
if record.state in ('offer_received', 'offer_accepted'):
raise UserError(
"Only new and canceled properties can be deleted.")
23 changes: 23 additions & 0 deletions estate/models/estate_property_maintenance_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class EstatePropertyMaintenanceRequests(models.Model):
_name = 'estate.property.maintenance.requests'
_description = 'Estate Property Maintenance Requests'

title = fields.Char()
cost = fields.Float()
status = fields.Selection(
default='new',
copy=False,
selection=[('new', "New"), ('approved', "Approved"), ('done', "Done")],
)
property_id = fields.Many2one('estate.property', required=True)

# Constraint Decorator
@api.constrains('status', 'cost')
def _check_cost(self):
if self.status == 'approved' and self.cost <= 0:
raise ValidationError(
"The cost must be greater than zero.")
80 changes: 80 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models

from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'Estate Property offer'
# _order = 'price desc'

price = fields.Float("Price")
status = fields.Selection(
copy=False,
selection=[('accepted', "Accepted"), ('refused', "Refused")],
)
partner_id = fields.Many2one(
'res.partner', string="Partner", required=True)
property_id = fields.Many2one('estate.property', required=True)
validity = fields.Integer(default=7)
date_deadline = fields.Date(
compute='_compute_date_deadline', inverse='_inverse_date_deadline')
property_type_id = fields.Many2one(
related='property_id.property_type_id', store=True)

# SQL Constraint
_check_offer_price = models.Constraint(
'CHECK(price > 0)', "The selling price must be positive")

# Compute Methods
# Depends Decorator
@api.depends('create_date', 'validity')
def _compute_date_deadline(self):
for offer in self:
date = offer.create_date.date() if offer.create_date else fields.Date.today()
offer.date_deadline = date + relativedelta(days=offer.validity)

def _inverse_date_deadline(self):
for record in self:
start_date = (
record.create_date.date() if record.create_date else fields.Date.today()
)
record.validity = (record.date_deadline - start_date).days

# Model Decorator
@api.model
def create(self, vals):
for vals in vals:
property_id = vals.get('property_id')
if property_id:
result = self.env['estate.property.offer']._read_group(
[('property_id', '=', property_id)],
[],
['price:max']
)
max_offer = result[0][0] if result else 0.0
if vals.get('price', 0.0) <= max_offer:
raise UserError(
"The offer must be higher than existing offers.")
self.env['estate.property'].browse(
property_id).state == 'offer_received'
return super().create(vals)

# Action Funcitons
def action_accept(self):
for record in self:
if record.property_id.state != 'offer_accepted':
record.status = 'accepted'
record.property_id.selling_price = record.price
record.property_id.buyer_ids = self.partner_id
record.property_id.state = 'offer_accepted'
else:
raise UserError('One offer has already been accepted')

def action_refuse(self):
for record in self:
record.status = 'refused'
record.property_id.state = 'new'
record.property_id.selling_price = '0'
record.property_id.buyer_ids = None
29 changes: 29 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from odoo import api, fields, models


class EstatePropertyTag(models.Model):
_name = 'estate.property.tag'
_description = 'Estate Property Tag'
_order = 'name'

name = fields.Char(required=True)
color = fields.Integer("Color Index")

# SQL Constraint
_check_name = models.Constraint('UNIQUE(name)', "The name must be unique")

local_datetime = fields.Char(
compute='_compute_local_datetime', store=True)

# Copmute Method - Depends Decorator
@api.depends('create_date')
def _compute_local_datetime(self):
for record in self:
if record.create_date:
# user_tz = self.env.user.tz or 'Asia/Kolkata'
local_dt = fields.Datetime.context_timestamp(
record, record.create_date
)
record.local_datetime = local_dt.strftime("%Y-%m-%d %H:%M:%S")
else:
record.local_datetime = False
26 changes: 26 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from odoo import api, fields, models


class EstatePropertyType(models.Model):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow this conventions while declaring fields, constraints, methods or CRUD operations.

_name = 'estate.property.type'
_description = 'Estate Property Type'
_order = 'name'
# _rec_name = 'id'

name = fields.Char(required=True)
sequence = fields.Integer(default=10)
offer_ids = fields.One2many('estate.property.offer', 'property_type_id')
offer_count = fields.Integer(compute='_compute_offer_count')

# Depends Decorator
@api.depends('offer_ids')
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)

# SQL Constarint
_check_name = models.Constraint('unique(name)', "The name must be unique")

# For Inline View
property_ids = fields.One2many(
"estate.property", "property_type_id", string="Properties")
22 changes: 22 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo import api, fields, models


class EstatePropertyUsers(models.Model):
_inherit = 'res.users'

property_ids = fields.One2many('estate.property', 'seller_id')
name = fields.Char()

prices = fields.One2many(
'estate.property', 'seller_id')
total_unsold_value = fields.Float(
compute='_compute_total_unsold_value')

@api.depends('prices.expected_price', 'prices.state')
def _compute_total_unsold_value(self):
for record in self:
unsold_prices = record.prices.filtered(lambda p: p.state != 'sold')
record.total_unsold_value = (
sum(unsold_prices.mapped('expected_price')
) if unsold_prices else 0.0
)
7 changes: 7 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink
access_estate_model,access_estate_model,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer,access_estate_property_offer,model_estate_property_offer,base.group_user,1,1,1,1
access_estate_property_maintenance_requests,access_estate_property_maintenance_requests,model_estate_property_maintenance_requests,base.group_user,1,1,1,1
access_estate_property_investor,access_estate_property_investor,model_estate_property_investor,base.group_user,1,1,1,1
Loading