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
28 changes: 28 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "Estate",
"summary": """
test
""",
"description": """
test
""",
"author": "covey",
"website": "",
"category": "Tutorials",
"version": "19.0.0.1.0",
"application": True,
"installable": True,
"depends": [
"base",
],
"data": [
"security/ir.model.access.csv",
"views/res_users_views.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_menus.xml"
],
"license": "AGPL-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
113 changes: 113 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_compare, float_is_zero


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

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
copy=False, default=fields.Date.add(fields.Date.today(), 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(
string="Garden orientation",
selection=[
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
],
help="this is used to indicated the garden orientation",
)
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="indicates the state of the property ad",
)
estate_type_id = fields.Many2one("estate.property.type", string="Property Type")
buyer_id = fields.Many2one("res.partner", string="Buyer", copy=False)
salesperson_id = fields.Many2one("res.users", string="Sales Person")
tag_ids = fields.Many2many("estate.property.tag")
offer_ids = fields.One2many("estate.property.offer", "property_id")
total_area = fields.Float(compute="_compute_total")

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

@api.depends("living_area", "garden_area")
def _compute_total(self):
for record in self:
record.total_area = record.living_area + record.garden_area

@api.onchange("garden")
def _onchange_garden(self):
for record in self:
if record.garden:
record.garden_area = 10
record.garden_orientation = "north"
else:
record.garden_area = 0
record.garden_orientation = None

def action_cancel_sale(self):
for record in self:
record.state = "cancelled"

def action_mark_as_sold(self):
for record in self:
if record.state == "cancelled":
raise ValidationError("Cancelled properties cannot be sold.")
record.state = "sold"

@api.constrains("expected_price")
def _check_expected_price(self):
for record in self:
if float_is_zero(record.expected_price, precision_digits=2):
raise ValidationError("Expected price must be greater than zero.")

@api.constrains("selling_price")
def _check_selling_price(self):
for record in self:
if (
float_compare(
record.selling_price,
record.expected_price * 0.9,
precision_digits=2,
)
< 0
):
raise ValidationError(
"Selling price cannot be lower than 90%of expected price!"
)

@api.ondelete(at_uninstall=False)
def _unlink_if_user_inactive(self):
if any(record.state not in ("new", "cancelled") for record in self):
raise ValidationError(
"Can't delete an opportunity that is neither New or Cancelled."
)
90 changes: 90 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api
from odoo.exceptions import ValidationError


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

price = fields.Float()
state = fields.Selection(
string="State",
selection=[("accepted", "Accepted"), ("refused", "Refused")],
help="State of the offer",
copy=False,
)
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)
property_type_id = fields.Many2one(
"estate.property.type", related="property_id.estate_type_id", store=True
)
validity = fields.Integer(default=7)
date_deadline = fields.Date(
compute="_compute_deadline", inverse="_inverse_deadline"
)

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

def _inverse_deadline(self):
for record in self:
diff = int((record.date_deadline - fields.Date.today()).days)
record.validity = diff

_check_offer_price_positive = models.Constraint(
"CHECK(price > 0)", "The offer price must be positive."
)

def action_accept_offer(self):
for record in self:
record.state = "accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
record.property_id.state = "sold"

for offer in record.property_id.offer_ids:
if offer.id != record.id:
offer.state = "refused"
return True

def action_refuse_offer(self):
for record in self:
record.state = "refused"
return True

@api.model
def create(self, vals_list):
current_property = self.env["estate.property"].browse(
vals_list[0]["property_id"]
)
if current_property.state not in {"new", "offer_received"}:
error_message = (
"The sale is closed and no offer can be added for this property"
)
raise ValidationError(error_message)
current_offers = current_property.offer_ids
if len(current_offers) > 0:
best_offer_price = max(offer.price for offer in current_offers)
feedback_buffer = []
for new_offer in vals_list:
if new_offer["price"] < best_offer_price:
feedback_buffer.append(str(new_offer["price"]))
if len(feedback_buffer) > 0:
error_message = (
f"The following offer prices are lower than the current best price ({best_offer_price}) and can't therefor not be added:\n - "
+ "\n - ".join(feedback_buffer)
)
raise ValidationError(error_message)
current_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 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models


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

_check_name_unique = models.Constraint(
"UNIQUE(name)", "The tag name must be unique."
)

name = fields.Char(required=True)
color = fields.Integer("Color")
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 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api


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

_check_name_unique = models.Constraint(
"UNIQUE(name)", "The property type name must be unique."
)

name = fields.Char(required=True)
property_ids = fields.One2many(
"estate.property", "estate_type_id", string="Properties"
)
offer_ids = fields.One2many("estate.property.offer", "property_type_id")
offer_count = fields.Integer(compute="_compute_offer_count")
sequence = fields.Integer(
"Sequence", default=1, help="Used to order stages. Lower is better."
)

@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",
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
1 change: 1 addition & 0 deletions estate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_estate_property
42 changes: 42 additions & 0 deletions estate/tests/test_estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from odoo.exceptions import ValidationError
from odoo.tests import TransactionCase
from odoo import Command


class TestEstateProperty(TransactionCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.estate = cls.env['estate.property'].create({
'name': 'Super test estate',
'expected_price': 100000.0,
'state': 'new',
})
cls.test_partner = cls.env['res.partner'].create({
'name': 'Maman ours',
})

def test_estate_best_price(self):
'''
Ensure best price is correctly updated when an offer is received.
'''
self.assertEqual(self.estate.best_price, 0.0)
self.estate.offer_ids = [Command.create({
'price': 125000.0,
'partner_id': self.test_partner.id,
})]
self.assertEqual(self.estate.best_price, 125000.0)

def test_accept_offer_south_facing_garden(self):
'''
Ensure offers for estates with south-facing gardens can only be accepted if above expected
price.
'''
self.estate.expected_price = 500000
self.estate.offer_ids = [Command.create({
'price': 475000.0,
'partner_id': self.test_partner.id,
})]
with self.assertRaises(ValidationError):
self.estate.offer_ids.accept_offer()
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="test_menu_root" name="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>
26 changes: 26 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_offer_view_list" model="ir.ui.view">
<field name="name">estate.property.offer.view.list</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list string="Channel">
<field name="price"/>
<field name="partner_id" string="Partner"/>
<field name="state"/>
</list>
</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>
<record id="estate_property_offer_action_view" model="ir.actions.act_window">
<field name="name">Property view offer </field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
Loading