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
20 changes: 20 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "estate",
"version": "0.0",
"depends": [
"base",
],
"installable": True,
"application": True,
"author": "me",
"license": "LGPL-3",
"data": [
"security/ir.model.access.csv",
"views/estate_property_views.xml",
"views/estate_property_actions.xml",
"views/estate_menus.xml",
Comment on lines +12 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Am I missing something? why didn't you introduced the other xml files here?
estate_property_offer_views, estate_property_type_views and res_users_views
I would be surprised if they are working without adding them to the manifest file😂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i forgot to add them but for some reason the local server didn't break so i thought everything is fine 😂😂

"views/estate_property_type_views.xml",
"views/estate_property_offer_views.xml",
"views/res_users_views.xml",
],
}
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
120 changes: 120 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare, float_is_zero


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

name = fields.Char(string="Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
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()
active = fields.Boolean(default=True)
garden_orientation = fields.Selection(
[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")],
)
state = fields.Selection(
required=True,
copy=False,
default="new",
selection=[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("canceled", "Canceled"),
],
)
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
user_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("estate.property.offer", "property_id")
total_area = fields.Integer(compute="_compute_total_area")
best_price = fields.Float(compute="_compute_best_price")

_positive_expected_price = models.Constraint(
"CHECK(expected_price > 0)",
"The expected price of a property cannot be negative.",
)
_positive_selling_price = models.Constraint(
"CHECK(selling_price >= 0)",
"The selling price of a property cannot be negative.",
)

@api.constrains("selling_price")
def _check_selling_price(self):
for record in self:
if float_is_zero(record.selling_price, precision_digits=2):
continue
if (
float_compare(
record.selling_price,
record.expected_price * 0.9,
precision_digits=2,
)
< 0
):
raise UserError(
"The selling price cannot be less than 90% of the expected price.",
)

@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:
if record.offer_ids:
record.best_price = max(record.offer_ids.mapped("price"))
else:
record.best_price = 0.0

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

def action_sold(self):
if self.state == "canceled":
raise UserError("You cannot mark a canceled property as sold.")
self.state = "sold"

def action_canceled(self):
if self.state == "sold":
raise UserError("You cannot cancel a sold property.")
self.state = "canceled"

@api.ondelete(at_uninstall=False)
def _delete_if_new_or_canceled(self):
Comment thread
YousefAllam221b marked this conversation as resolved.
for record in self:
if record.state not in ["new", "canceled"]:
raise UserError(
"You can only delete new or cancelled properties.",
)
95 changes: 95 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare


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

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_type_id = fields.Many2one(
related="property_id.property_type_id",
store=True,
)
date_deadline = fields.Date(
compute="_compute_date_deadline",
inverse="_inverse_date_deadline",
store=True,
)
validity = fields.Integer(default=7)

_positive_price = models.Constraint(
"CHECK(price > 0)",
"The price of an offer cannot be negative.",
)

@api.depends("create_date", "validity")
def _compute_date_deadline(self):
for record in self:
base_date = (
fields.Date.to_date(record.create_date)
if record.create_date
else fields.Date.today()
)
record.date_deadline = fields.Date.add(base_date, days=record.validity)

def _inverse_date_deadline(self):
for record in self:
if not record.date_deadline:
continue
base_date = (
fields.Date.to_date(record.create_date)
if record.create_date
else fields.Date.today()
)
record.validity = (record.date_deadline - base_date).days

def action_accept_offer(self):
for record in self:
if record.property_id.state not in ["new", "offer_received"]:
msg = "You can only accept offers for properties that are new or have received offers."
raise UserError(
msg,
)
record.status = "accepted"
record.property_id.state = "offer_accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id

def action_refuse_offer(self):
for record in self:
if record.status == "accepted":
msg = "You cannot refuse an offer that has already been accepted."
raise UserError(
msg,
)
record.status = "refused"

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
estate_property = self.env["estate.property"].browse(
vals.get("property_id"),
)
existing_prices = estate_property.offer_ids.mapped("price")

if existing_prices:
min_price = min(existing_prices)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you defined an _order for the offers model with price so any recordset of offers will be sorted according to the price so to get the minimum you can do something like estate_property.offer_ids[-1]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, that makes sense. But for the sake of readability, wouldn't it be better to leave it as is so the reader doesn't have to guess why I grabbed the last item? specially since its a low cost operation either way

if (
float_compare(vals.get("price", 0.0), min_price, precision_digits=2)
< 0
):
raise UserError(
f"The offer {vals.get('price', 0.0)} cannot be lower than the other offers.",
)
estate_property.state = "offer_received"

return super().create(vals_list)
15 changes: 15 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import fields, models


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

name = fields.Char(required=True)
color = fields.Integer()

_name_uniq = models.Constraint(
"UNIQUE (name)",
"The name of the tag must be unique!",
)
31 changes: 31 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from odoo import api, fields, models


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Estate Property Type"
_order = "sequence, name"

name = fields.Char(required=True)
_name_uniq = models.Constraint(
"UNIQUE (name)",
"The name of the property type must be unique!",
)

property_ids = fields.One2many(
"estate.property",
"property_type_id",
string="Properties",
)
offer_ids = fields.One2many(
"estate.property.offer",
"property_type_id",
string="Offers",
)
offer_count = fields.Integer(compute="_compute_offer_count")
sequence = fields.Integer("Sequence", default=1)

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


class ResUsers(models.Model):
_inherit = "res.users"

property_ids = fields.One2many(
"estate.property",
"user_id",
string="Estate Properties",
domain="[('state', 'in', ['new', 'offer_received'])]",
)
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
11 changes: 11 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<odoo>
<menuitem id="real_estate_menu_root" name="Real Estate">
<menuitem id="advertisements_menu" name="Advertisements">
<menuitem id="estate_property_menu_action" action="estate_property_action" />
</menuitem>
<menuitem id="settings_menu" name="Settings">
<menuitem id="estate_property_type_menu_action" action="estate_property_type_action" />
<menuitem id="estate_property_tag_menu_action" action="estate_property_tag_action" />
</menuitem>
</menuitem>
</odoo>
27 changes: 27 additions & 0 deletions estate/views/estate_property_actions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<odoo>
<record id="estate_property_action" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">list,form,kanban</field>
<field name="context">{'search_default_state_filter': 1}</field>
</record>

<record id="estate_property_type_action" model="ir.actions.act_window">
<field name="name">Property Types</field>
<field name="res_model">estate.property.type</field>
<field name="view_mode">list,form</field>
</record>

<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>

<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>
22 changes: 22 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<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 string="Property Offers" editable="bottom" decoration-success="status=='accepted'"
decoration-danger="status=='refused'">
<field name="price" />
<field name="partner_id" />
<field name="property_type_id" />
<field name="validity" />
<field name="date_deadline" />
<button name="action_accept_offer" type="object" icon="fa-check"
title="Accept Offer"
invisible="status" />
<button name="action_refuse_offer" type="object" icon="fa-times"
title="Refuse Offer"
invisible="status" />
</list>
</field>
</record>
</odoo>
Loading