Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] estate: add the real estate module #173

Draft
wants to merge 16 commits into
base: 18.0
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import wizards
from . import controllers
from . import models
31 changes: 31 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
'name': "Real State",
'depends': ['base', 'website'],
'author': "Sahil Mangukiya",
'category': 'Real Estate/Brokerage',
'description': "This is my First tutorial module.",
'data': [
'security/estate_security.xml',
'security/ir.model.access.csv',
'data/estate.property.type.csv',
'reports/estate_property_website_template.xml',
'reports/estate_property_detail_template.xml',
'reports/print_offer_table_subtemplate.xml',
'reports/estate_property_offer_report_template.xml',
'reports/estate_salesman_property_offer_report_template.xml',
'reports/estate_property_reports.xml',
'views/res_users_views.xml',
'wizards/add_offer.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_views.xml',
'views/estate_menu.xml'
],
'demo': [
'demo/estate_property_demo.xml',
],
'installable': True,
'application': True,
'auto_install': False
}
1 change: 1 addition & 0 deletions estate/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import property_website
39 changes: 39 additions & 0 deletions estate/controllers/property_website.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from odoo import http
from odoo.http import request

from odoo.addons.portal.controllers.portal import pager as portal_pager


class propertyWebsiteController(http.Controller):

@http.route(['/properties', '/properties/page/<int:page>'], website=True, auth='public', type='http')
def property_website(self, page=1, **kwargs):
property_limit_per_page = 6
property_count = request.env["estate.property"].sudo().search_count(
domain=[('state','in',('new', 'offer received', 'offer accepted')), ('active', '=', True)]
)
pager = portal_pager(
url="/properties",
total=property_count,
page=page,
step=property_limit_per_page
)
property = request.env["estate.property"].sudo().search(
domain=[('state','in',('new', 'offer received', 'offer accepted')), ('active', '=', True)],
offset=pager['offset'],
limit=property_limit_per_page
)
return request.render('estate.estate_property_website_template',
{
'properties': property,
'pager': pager
})


@http.route('/property/<int:property_id>', website=True, auth='public', type='http')
def property_detail_view(self, property_id):
property = request.env['estate.property'].sudo().browse(property_id)
return request.render('estate.estate_property_detail_template',
{
'property' : property,
})
5 changes: 5 additions & 0 deletions estate/data/estate.property.type.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name
1,"Residential"
2,"Commercial"
3,"Industrial"
4,"Land"
121 changes: 121 additions & 0 deletions estate/demo/estate_property_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<odoo>
<data>
<record id="id1" model="estate.property">
<field name="name">Big villa</field>
<field name="state">new</field>
<field name="description">A nice and big villa</field>
<field name="postcode">12345</field>
<field name="data_availability">2020-02-02</field>
<field name="expected_price">1600000</field>
<field name="bedrooms">6</field>
<field name="living_area">100</field>
<field name="facades">4</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">100000</field>
<field name="garden_orientation">south</field>
<field name="property_type_id" ref="1"/>
</record>

<record id="id2" model="estate.property">
<field name="name">Trailer home</field>
<field name="state">cancelled</field>
<field name="description">Home in a trailer park</field>
<field name="postcode">54321</field>
<field name="data_availability">1970-01-01</field>
<field name="expected_price">100000</field>
<field name="selling_price">120000</field>
<field name="bedrooms">1</field>
<field name="living_area">10</field>
<field name="facades">4</field>
<field name="garage">False</field>
<field name="property_type_id" ref="1"/>
</record>

<record id="id3" model="estate.property">
<field name="name">Sahil's Land</field>
<field name="state">new</field>
<field name="description">This is my land, Don't play cricket!!</field>
<field name="postcode">12345</field>
<field name="data_availability">2020-02-02</field>
<field name="expected_price">100</field>
<field name="bedrooms">30</field>
<field name="living_area">100</field>
<field name="facades">4</field>
<field name="garage">True</field>
<field name="garden">True</field>
<field name="garden_area">100000</field>
<field name="garden_orientation">south</field>
<field name="property_type_id" ref="4"/>
<field name="offer_ids" eval="[
Command.create({
'partner_id': ref('base.res_partner_12'),
'price': 92,
'validity': 100
}),
Command.create({
'partner_id': ref('base.res_partner_2'),
'price': 94,
'validity': 100
})]"/>
</record>

<record id="offer_1" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_12"/>
<field name="price">1600000</field>
<field name="property_id" ref="id1"/>
<field name="validity">14</field>
</record>

<record id="offer_2" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_12"/>
<field name="price">1600001</field>
<field name="property_id" ref="id1"/>
<field name="validity">14</field>
</record>

<record id="offer_3" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_2"/>
<field name="price">1600002</field>
<field name="property_id" ref="id1"/>
<field name="validity">14</field>
</record>

<record id="offer_4" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_2"/>
<field name="price">100001</field>
<field name="property_id" ref="id2"/>
<field name="validity">7</field>
</record>

<record id="offer_5" model="estate.property.offer">
<field name="partner_id" ref="base.res_partner_12"/>
<field name="price">100002</field>
<field name="property_id" ref="id2"/>
<field name="validity">7</field>

</record>

<function model="estate.property.offer" name="action_accept">
<value eval="[ref('offer_1')]"/>
</function>

<function model="estate.property.offer" name="action_refuse">
<value eval="[ref('offer_2')]"/>
</function>

<function model="estate.property.offer" name="action_refuse">
<value eval="[ref('offer_3')]"/>
</function>

<function model="estate.property.offer" name="action_accept">
<value eval="[ref('offer_4')]"/>
</function>

<function model="estate.property.offer" name="action_refuse">
<value eval="[ref('offer_5')]"/>
</function>

</data>

</odoo>
5 changes: 5 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import res_users
98 changes: 98 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from datetime import timedelta

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


class estateProperty(models.Model):
_name = "estate.property"
_description = "This is property Table."
_order = "selling_price desc, name"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
data_availability = fields.Date(default=lambda self: fields.Date.today() + timedelta(days=90), copy=False)
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(change_default=True)
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection(
string='Orientation',
selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')],
help="Garden Orientation is avialable in four faces of direction."
)
active = fields.Boolean(default=True)
state = fields.Selection(
string='State',
selection=[('new', 'New'), ('offer received', 'Offer Received'), ('offer accepted', 'Offer Accepted'), ('sold', 'Sold'), ('cancelled', 'Cancelled')],
help="Offer available in five type.",
required=True,
copy=False,
default='new'
)
number = fields.Integer(related='property_type_id.number')
totalArea = fields.Integer(compute='_compute_total_area', store="true")
best_offer = fields.Float(compute='_compute_best_offer')
property_image = fields.Binary()

property_type_id = fields.Many2one(comodel_name='estate.property.type', string='Property Type', domain="[('number', '>=', 0)]", ondelete='cascade')
seller_id = fields.Many2one('res.users', 'Salesman', default=lambda self: self.env.user)
buyer_id = fields.Many2one('res.partner', 'Buyer')
tag_ids = fields.Many2many(comodel_name='estate.property.tag', string='Tag', relation='property_join_tag', column1='property_id', column2='tag_id')
offer_ids = fields.One2many('estate.property.offer', 'property_id')
company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)


_sql_constraints = [
('check_selling_price', 'CHECK(selling_price >= 0)', 'A property selling price must be positive.'),
('check_expected_price', 'CHECK(expected_price > 0)', 'A property expected price must be strictly positive.')
]

@api.depends('garden_area', 'living_area')
def _compute_total_area(self):
for record in self:
record.totalArea = record.garden_area + record.living_area

@api.depends('offer_ids.price')
def _compute_best_offer(self):
for record in self:
if record.offer_ids:
record.best_offer = max(record.offer_ids.mapped('price'))
else:
record.best_offer = 0.0

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

def action_sold(self):
if(self.state == 'cancelled'):
raise UserError("You can not mark a cancelled property as sold.")
self.state = 'sold'

def action_cancelled(self):
if(self.state == 'sold'):
raise UserError("You can not mark sold property as cancelled")
self.state = 'cancelled'

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if record.selling_price < (0.9 * record.expected_price) and (record.selling_price > 0):
raise ValidationError("Selling price cannot be lower than 90 percentage of the expected price.")

@api.ondelete(at_uninstall=False)
def _prevent_delete(self):
for record in self:
if record.state not in ("new", "cancelled"):
raise UserError("You can not delete a property which are in offer received, offer accepted or sold state.")
78 changes: 78 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from datetime import timedelta, datetime

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


class estatePropertyOffer(models.Model):

_name = "estate.property.offer"
_description = "This is offer table"
_order = "id desc"

price = fields.Float(required=True)
status = fields.Selection(string='Status', selection=[('accepted', 'Accepted'), ('refused', 'Refused')])
validity = fields.Integer(default=7, string="Validity")
date_deadline = fields.Date(compute='_compute_deadline', inverse='_inverse_deadline', string="Date Deadline")
sum = fields.Integer(compute='_compute_sum', string="Sum")
sum2 = fields.Integer(compute='_compute_sum2', string="Sum2")

partner_id = fields.Many2one('res.partner', string='Buyer', required=True)
property_id = fields.Many2one('estate.property', string='Property')
property_type_id = fields.Many2one(comodel_name="estate.property.type", related="property_id.property_type_id", store=True)

_sql_constraints = [
('check_offer_price','CHECK(price > 0)','A property offer price must be strictly positive.')
]

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

def _inverse_deadline(self):
for record in self:
if record.date_deadline and record.create_date:
record.validity = (record.date_deadline - record.create_date.date()).days
else:
record.validity = (record.date_deadline - datetime.today()).days

@api.depends('validity')
def _compute_sum(self):
for record in self:
record.sum = 7 + record.validity

@api.depends('sum')
def _compute_sum2(self):
for record in self:
record.sum2 = 7 + record.sum

def action_accept(self):
for record in self:
if record.property_id.state in ['sold', 'offer accepted']:
raise UserError("Already one offer is accepted.")
if record.status == 'accepted':
continue
record.status = 'accepted'
record.property_id.state = 'offer accepted'
record.property_id.buyer_id = self.partner_id
record.property_id.selling_price = self.price

def action_refuse(self):
if(self.status == 'accepted'):
self.status = 'refused'
self.property_id.buyer_id = False
self.property_id.selling_price = False
else:
self.status = 'refused'

@api.model
def create(self, offer):
property_id = self.env["estate.property"].browse(offer["property_id"])
if offer["price"] < max(property_id.offer_ids.mapped("price") + [0]):
raise UserError("Offer price must be higher than existing offer.")
property_id.state = "offer received"
return super().create(offer)
Loading