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 Module #617

Draft
wants to merge 2 commits into
base: 17.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
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
16 changes: 16 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
'name' : 'Real Estate Management',
'depends': ['base'],
'application' :True,
'description' : 'Modulo test para la induccion a sistemas Adhoc',
'license': 'AGPL-3',
'data': [
'security/ir.model.access.csv',
'views/estate_property_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_offer_views.xml',
'views/res_user_views.xml',
'views/estate_menu.xml',
],
}
4 changes: 4 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_tag, estate_property_offer
from . import user
128 changes: 128 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from odoo import api, fields, models
from odoo.exceptions import UserError,ValidationError
from odoo.tools.float_utils import float_compare


class EstateProperty(models.Model):
_name = "estate.property"
_description ="Real Estate Property"
_order = "id desc"

name = fields.Char(string='Name',required=True)
description = fields.Text(string='Description')
postcode = fields.Char(string='Postcode')
date_availability = fields.Date(string='Date Availability', copy=False,default=lambda self: fields.Date.add(fields.Date.today(),months=3))
expected_price = fields.Float(string='Expected Price', required=True)
selling_price = fields.Float(string='Selling Price', readonly=True,copy=False )
bedrooms = fields.Integer(string='Bedrooms',default=2)
living_area = fields.Integer(string='Living Area')
facades = fields.Integer(string='Facades')
garage = fields.Boolean(string='Garage')
garden = fields.Boolean(string='Garden')
active = fields.Boolean(string='Active', default=True)
garden_area = fields.Integer(string='Garden Area')
garden_orientation = fields.Selection(string='Garden Orientation',
selection=[
('north', 'North'),
('south', 'South'),
('west', 'West'),
('east', 'East')
]
)
state = fields.Selection(string='State',
selection=[
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('canceled', 'Canceled'),
],
default='new',
readonly=True
)
property_type_id = fields.Many2one(comodel_name='estate.property.type', string='Property Type')
sales_person_id = fields.Many2one('res.users', string='Salesperson', default=lambda self: self.env.user)
buyer_id = fields.Many2one('res.partner', string='Buyer',copy=False)
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_id', string='Offers')
total_area = fields.Integer(string='Total Area',compute="_compute_total_area")
best_price = fields.Float(string='Best Offer', compute="_compute_best_price",store =True)
_sql_constraints = [
('check_expected_price', 'CHECK(expected_price >= 0)',
'The expected price must be above 0'),
('check_selling_price', 'CHECK(selling_price >= 0)',
'The selling price must be above 0')
]



@api.ondelete(at_uninstall=False)
def _delete_property(self):
for rec in self:
if rec.state not in ['new','canceled']:
raise UserError('You can only delete properties that are New or Canceled')




@api.constrains('expected_price','selling_price')
def _check_expected_price(self):
#raise ValidationError('asdasd')
if self.offer_ids.filtered(lambda x:x.status == 'accepted'):
#import pdb; pdb.set_trace()
if float_compare(self.selling_price, self.expected_price * 0.9, precision_rounding=2) < 0:
raise ValidationError('The selling price cannot be lower tan 90% of the expected price')

@api.depends('total_area',)
def _compute_total_area(self):
self.total_area = self.garden_area + self.living_area

@api.depends('offer_ids','offer_ids.price')
def _compute_best_price(self):
if self.offer_ids :
self.best_price = max(self.offer_ids.mapped('price'))
else:
self.best_price = 0

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

def sold_property(self):
if self.state == 'canceled':
raise UserError('Properties canceled can not be marked as sold')
self.state='sold'

def cancel_property(self):
if self.state == 'sold':
raise UserError('Properties sold can not be marked as canceled')
self.state='canceled'

def offer_recieved(self):
if self.state not in ['canceled','sold','offer_accepted']:
self.state = 'offer_received'
else:
raise UserError('Cannot change the state of this property')


















68 changes: 68 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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(string='Offer')
status = fields.Selection(string='Status', selection=[('accepted', 'Accepted'), ('refused', 'Refused'),])
partner_id = fields.Many2one(comodel_name='res.partner', string='Partner',required=True)
property_id = fields.Many2one(comodel_name='estate.property', string='Property',required=True)
validity = fields.Integer(string='Validity',default=7)
date_deadline = fields.Date(string='Deadline', compute="_compute_date_deadline", inverse="_inverse_date_deadline")
property_type_id = fields.Many2one(comodel_name='estate.property.type',related="property_id.property_type_id", string='Property Type', store=True)




_sql_constraints = [
('check_price', 'CHECK(price > 0)',
'The Offer price must be above 0')
]


@api.model
def create(self, values):
# Add code here
estate_property = self.env['estate.property'].browse(values['property_id'])
if values['price'] < estate_property.best_price:
raise UserError (f'The offer must be above{estate_property.best_price}')
estate_property.state = 'offer_received'
return super().create(values)


@api.depends('create_date','validity')
def _compute_date_deadline(self):
for rec in self:
rec.date_deadline = fields.Date.add(rec.create_date, days=rec.validity) if rec.create_date else False

@api.onchange('date_deadline')
def _inverse_date_deadline(self):
for rec in self.filtered('date_deadline'):
create_date = rec.create_date if rec.create_date else fields.Datetime.now()
rec.validity = (rec.date_deadline - create_date.date()).days

def accept_offer(self):
if 'accepted' not in self.property_id.offer_ids.mapped('status'):
self.property_id.buyer_id = self.partner_id
self.property_id.selling_price = self.price
self.status = 'accepted'
self.property_id.state='offer_accepted'
else:
raise UserError('There is an already accepted Offer')


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






18 changes: 18 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from odoo import fields, models


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

name = fields.Text(string='Name')
color = fields.Integer(string='')


_sql_constraints = [
('check_unique_name', 'UNIQUE(name)',
'The property tag name must be unique'),

]

27 changes: 27 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from odoo import api, fields, models



class EstatePropertyType(models.Model):
_name = 'estate.property.type'
_description = 'Real estate property type'
_order = "sequence, name"

name = fields.Text(string='Name',required=True)
property_ids = fields.One2many(comodel_name='estate.property', inverse_name='property_type_id', string='')
sequence = fields.Integer('Sequence', default=1, help="Used to order stages. Lower is better.")
#offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_type_id', string='Offers')
offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_type_id', string='Offers2')
offer_count = fields.Integer(compute="_compute_count_offers")


_sql_constraints = [
('check_unique_name', 'UNIQUE(name)',
'The property type name must be unique'),
]


@api.depends('offer_ids')
def _compute_count_offers(self):
for record in self:
record.offer_count = len(record.offer_ids)
7 changes: 7 additions & 0 deletions estate/models/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


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

property_ids = fields.One2many(comodel_name='estate.property', inverse_name='sales_person_id', string='')
5 changes: 5 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink
access_estate_property,access_estate_property,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
12 changes: 12 additions & 0 deletions estate/views/estate_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0'?>
<odoo>
<menuitem id="real_estate" name="Real Estate"/>
<!-- Advertisement-->
<menuitem id="advertisement_menu" name="Advertisement" parent="real_estate" />
<menuitem id="properties_menu" action="estate_property_action" parent="advertisement_menu"/>
<!-- Property Type-->
<menuitem id="settings_menu" name="Settings" parent="real_estate" />
<menuitem id="properties_type_menu" action="estate_property_type_action" parent="settings_menu"/>
<!-- Property Tags-->
<menuitem id="properties_tags_menu" action="estate_property_tag_action" parent="settings_menu"/>
</odoo>
47 changes: 47 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<odoo>
<!-- estate.property.offer action window -->
<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">estate.property.offer.action</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('property_type_id','=',active_id)]</field>
</record>
<!-- estate.property.offer form view -->
<record id="estate_property_offer_view_form" model="ir.ui.view">
<field name="name">estate.property.offer.view.form</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<form string="">
<sheet>
<group>
<!-- Add your fields here -->
<field name="price"/>
<field name="partner_id"/>
<field name="status"/>
<field name="date_deadline"/>
<field name="validity"/>
</group>
</sheet>
</form>
</field>
</record>

<!-- estate.property.offer tree view -->
<record id="estate.property.offer_view_tree" model="ir.ui.view">
<field name="name">estate.property.offer.view.tree</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree editable="top">
<!-- Add your fields here -->
<field name="price"/>
<field name="partner_id"/>
<field name="date_deadline"/>
<field name="status"/>
<button name="accept_offer" string="" type="object" icon="fa-check" title="Accept" invisible="status in ['accepted','rejected']"/>
<button name="reject_offer" string="" type="object" icon="fa-times" title="Reject" invisible="status in ['accepted','rejected']"/>
</tree>
</field>
</record>


</odoo>
19 changes: 19 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<odoo>
<!-- estate.property.tag action window -->
<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Property Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">tree,form</field>
</record>

<!-- estate.property.tag tree view -->
<record id="estate_property_tag_view_tree" model="ir.ui.view">
<field name="name">estate.property.tag.view.tree</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
</tree>
</field>
</record>
</odoo>
Loading