Skip to content
Open
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
22 changes: 22 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "estate",
"website": "",
"category": "Tutorials",
"version": "0.1",
"application": True,
"installable": True,
"depends": ["base"],
"data": [
"security/security.xml",
"security/ir.model.access.csv",
"views/estate_property_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_property_type_views.xml",
"views/res_users_views.xml",
"views/estate_menus.xml",
],
"assets": {},
"author": "Odoo S.A.",
"license": "LGPL-3",
}
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
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 dateutil.relativedelta import relativedelta
from datetime import datetime
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_compare


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Estate Property"
name = fields.Char("Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
copy=False, default=lambda self: datetime.now() + 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("Living Area (sqm)")
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"),
],
)
active = fields.Boolean(default=True)
state = fields.Selection(
readonly=True,
string="State",
selection=[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
default="new",
required=True,
copy=False,
store=True,

Choose a reason for hiding this comment

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

we do not need this as this is not computed anymore 👀

)
type_id = fields.Many2one("estate.property.type", string="Property Type")
salesperson_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")
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
total_area = fields.Integer("Total Area (sqm)", compute="_compute_total_area")
best_price = fields.Float("Best Price", compute="_compute_best_price")

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

_order = "id desc"

Choose a reason for hiding this comment

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

let's keep all the meta data at first before any field declaration


@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):
for record in self:
record.best_price = max(record.offer_ids.mapped("price") or [0])

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

def action_cancel_property(self):
if self.state == "sold":
raise UserError("Sold property cannot be cancelled!")

for record in self:
record.state = "cancelled"

return True

def action_sell_property(self):
if self.state == "cancelled":
raise UserError("Cancelled property cannot be sold!")

for record in self:
record.state = "sold"

return True

@api.constrains("expected_price", "selling_price")
def _check_selling_price(self):
for record in self:
if len(record.offer_ids) > 0 and (
float_compare(record.selling_price, record.expected_price * 0.9, 5)
== -1
):
raise ValidationError(
"The selling price cannot be lower than the 90% of the expected price"
)

@api.ondelete(at_uninstall=False)
def _unlink_except_few_states(self):
if any(
(record.state != "new" and record.state != "cancelled") for record in self
):
raise UserError(
"You cannot remove the property except when the state is new or cancelled!"
)

def offer_received(self):
if self.state == "new":
self.state = "offer_received"
103 changes: 103 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from odoo import api, fields, models
from datetime import timedelta
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Estate Property Offer"
price = fields.Float()
status = fields.Selection(
[("accepted", "Accepted"), ("refused", "Refused")], copy=False
)
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)
validity = fields.Integer("Validity", default=7)
date_deadline = fields.Date("Deadline", compute="_compute_date_deadline")
hide_offer_buttons = fields.Boolean(compute="_compute_hide_offer_buttons")
property_type_id = fields.Many2one(related="property_id.type_id", store=True)
_check_price = models.Constraint(
"CHECK(price > 0)", "Offer's price must be positive"
)
_order = "price desc"

@api.model
def create(self, vals_list):
an_estate_property = self.env["estate.property"].browse(
vals_list[0]["property_id"]
)
if any(
vals_list[0]["price"] < offer_id.price
for offer_id in an_estate_property.offer_ids
):
raise UserError("Offer cannot be lower than any of previous offers!")

an_estate_property.offer_received()

return super().create(vals_list)

@api.depends("validity", "create_date")
def _compute_date_deadline(self):
for record in self:
record.date_deadline = (
record.create_date or fields.Datetime.now()
) + timedelta(days=record.validity)

@api.depends("property_id.state")
def _compute_hide_offer_buttons(self):
for record in self:
a_state = record.property_id.state
if (
a_state == "offer_accepted"
or a_state == "cancelled"
or a_state == "sold"
or record.status
):
record.hide_offer_buttons = True
else:
record.hide_offer_buttons = False

def action_accept_offer(self):
if self.status:
raise UserError("You cannot change the status!")
if self.property_id.state == "offer_accepted":
raise UserError("One offer has been already accepted, sorry!")
if self.property_id.state == "cancelled":
raise UserError("This property is cancelled")
if self.property_id.state == "sold":
raise UserError("This property is sold")

for record in self:
record.status = "accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
record.property_id.state = "offer_accepted"
return True

def action_refuse_offer(self):
if self.status:
raise UserError("You cannot change the status!")
for record in self:
record.status = "refused"
return True

def is_still_open_to_offers(self):
if (
self.partner_id.state == "offer_accepted"
or self.partner_id.state == "sold"
or self.partner_id.state == "cancelled"
):
print("HELLO!!")
return True

print("FALSE case!!")
return False

# @api.depends("create_date", "date_deadline") Not working right now!!!
# def _inverse_date_deadline(self):
# print("Hello!")
# for record in self:
# record.validity = (
# (record.date_deadline or fields.Datetime.now())
# - (record.create_date or fields.Datetime.now())
# ).days
10 changes: 10 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Estate Property Tag"
name = fields.Char("Name", required=True)
color = fields.Integer("Color")
_name_unique = models.Constraint("unique(name)", "Tag name must be unique")
_order = "name"
22 changes: 22 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo import fields, models, api


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Estate Property Type"
name = fields.Char(required=True)
_name_unique = models.Constraint("unique(name)", "Type must be unique")
property_ids = fields.One2many("estate.property", "type_id", string="Properties")
_order = "sequence, name"
sequence = fields.Integer(
"Sequence", default=1, help="Used to order property types"
)
offer_ids = fields.One2many(
"estate.property.offer", "property_type_id", string="Offers"
)
offer_count = fields.Integer("Offer Count", compute="_compute_offer_count")

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


class ResUsers(models.Model):
_inherit = "res.users"
property_ids = fields.One2many(
"estate.property",
"salesperson_id",
string="User Properties",
domain=["|", ("state", "=", "new"), ("state", "=", "offer_received")],
)
6 changes: 6 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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_readonly,access_estate_property_readonly,model_estate_property,group_readonly_user,1,0,0,0
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
10 changes: 10 additions & 0 deletions estate/security/security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<odoo>
<data>
<record id="group_all_rights_user" model="res.groups">
<field name="name">Complete User</field>
</record>
<record id="group_readonly_user" model="res.groups">
<field name="name">Readonly User</field>
</record>
</data>
</odoo>
12 changes: 12 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<odoo>
<menuitem id="estate_menu_root" name="Estate Property">
<menuitem id="estate_first_level_menu" name="Advertisements">
<menuitem id="estate_second_level_menu" action="estate_property_action" />
</menuitem>
<menuitem id="estate_settings_menu" name="Settings">
<menuitem id="estate_property_type_menu" action="estate_property_type_action" />
<menuitem id="estate_property_tag_menu" action="estate_property_tag_action" />
</menuitem>
</menuitem>
</odoo>
45 changes: 45 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<odoo>
<record id="estate_property_offer_view_list" model="ir.ui.view">
<field name="name">estate_property_offer.list</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list editable="bottom" decoration-success="status == 'accepted'"
decoration-danger="status == 'refused'">
<field name="price" />
<field name="partner_id" />
<field name="validity" />
<field name="date_deadline" />
<field name="property_type_id" />
<button name="action_accept_offer" type="object" string="" icon="fa-check"
title="accept" invisible="hide_offer_buttons" />
<button name="action_refuse_offer" type="object" string="" icon="fa-times"
title="refuse" invisible="hide_offer_buttons" />
</list>
</field>
</record>
<record id="estate_property_offer_view_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>
<group>
<field name="price" />
<field name="partner_id" />
<field name="status" />
<field name="validity" />
<field name="date_deadline" />
</group>
</group>
</sheet>
</form>
</field>
</record>
<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">Property Offers</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list,form</field>
<field name="domain">[('property_type_id', '=', active_id)]</field>
</record>
</odoo>
7 changes: 7 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<odoo>
<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">list,form</field>
</record>
</odoo>
Loading