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

17.0-rd-training-manv #140

Closed
wants to merge 10 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ It has 3 branches for each Odoo version: one for the bases, one for
The first contains the code of the modules that serve as base for the tutorials,
and the others contains the code of each chapter with the complete
solution.

First changes
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
17 changes: 17 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'name': 'Real estate',
'depends': [
'base',
],
'data': [
'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/estate_menu.xml',
'views/res_users_views.xml',
],
'application': True,
'license': 'OEEL-1'
}
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_user
90 changes: 90 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from odoo import fields, models, api
from datetime import timedelta
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_is_zero, float_compare
from odoo.exceptions import ValidationError

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

active = fields.Boolean(default=True)
name = fields.Char(required=True, string="Title")
postcode = fields.Char()
date_availability = fields.Date(copy=False, default=lambda self: fields.Date.today() + timedelta(days=90))
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer(string="Living area (sqm)")
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection(
[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")]
)
state = fields.Selection(
[("new", "New"), ("offer_received", "Offer Received"), ("offer_accepted", "Offer Accepted"), ("sold", "Sold"), ("canceled", "Canceled")],
default="new",
required=True,
copy = False,
)
property_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="Salesperson", default=lambda self: self.env.user)
tag_ids = fields.Many2many("estate.property.tag", string="Property Tags")
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
total_area = fields.Float(compute="_compute_total_area")
best_price = fields.Float(compute="_compute_best_price")

_sql_constraints = [
('property_positive_expected_price', 'CHECK (expected_price > 0)', 'Expected price must be positive'),
('property_positive_selling_price', 'CHECK (selling_price >= 0)', 'Selling price must be positive'),
]

@api.constrains("selling_price")
def _check_selling_price(self):
for record in self:
# selling_price < expected_price*0.9
if not float_is_zero(record.selling_price, 2) and float_compare(record.selling_price, record.expected_price*0.9, 2) == -1:
raise ValidationError('The selling price cannot be lower 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:
record.best_price = max(record.offer_ids.mapped('price'), default=0)

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

def action_sold_property(self):
for record in self:
if record.state == "canceled":
raise UserError("A canceled property cannot be sold")
record.state = "sold"
return True

def action_cancel_property(self):
for record in self:
if record.state == "sold":
raise UserError("A sold property cannot be canceled")
record.state = "canceled"
return True

@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_canceled(self):
for record in self:
if record.state not in ('new', 'canceled'):
raise UserError("Can't delete active property")
59 changes: 59 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from odoo import fields, models, api
from odoo.exceptions import UserError


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

price = fields.Float(required=True)
status = fields.Selection(
[("accepted", "Accepted"), ("refused", "Refused")],
copy=False,
)
partner_id = fields.Many2one("res.partner", string="Partner", required=True)
property_id = fields.Many2one("estate.property", string="Property", required=True)
validity = fields.Integer(default=7)
date_deadline = fields.Date(compute="_compute_date_deadline", inverse="_inverse_date_deadline")
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

_sql_constraints = [
('property_offer_positive_price', 'CHECK (price > 0)', 'Price must be positive')
]

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

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

def action_accept_offer(self):
for record in self:
if record.property_id.state == "sold":
raise UserError("The property is already sold")
record.status = "accepted"
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id.state = "sold"
return True

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

@api.model
def create(self, vals):
property_id = self.env["estate.property"].browse(vals['property_id'])
if vals['price'] < property_id.best_price:
raise UserError("Your offer is too low")
property_id.state = "offer_received"
return super().create(vals)
14 changes: 14 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import fields, models


class EstatePropertyTags(models.Model):
_name = "estate.property.tag"
_description = "Real estate property tag"
_order = "name"

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

_sql_constraints = [
('property_tag_name_unique', 'UNIQUE(name)', 'Property tag name must be unique')
]
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 = "Real estate property type"
_order = "sequence, name"

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

_sql_constraints = [
('property_type_name_unique', 'UNIQUE(name)', 'Property type name must be unique')
]

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

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

property_ids = fields.One2many("estate.property", "salesperson_id", string="Sales person")
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
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1
estate.access_estate_property_offer,access_estate_property_offer,estate.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="estate_menu_root" name="Real Estate">
<menuitem id="estate_first_level_menu" name="Advertisements">
<menuitem id="estate_model_menu_action" action="test_model_action"/>
</menuitem>
<menuitem id="estate_second_level_menu" name="Settings">
<menuitem id="estate_property_type_menu_action" action="property_type_action"/>
<menuitem id="estate_property_tag_menu_action" action="property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
41 changes: 41 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_offer_view" model="ir.ui.view">
<field name="name">estate.property.offer.tree</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree string="Properties" editable="bottom" decoration-danger="status == 'refused'" decoration-success="status == 'accepted' ">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_accept_offer" type="object" title="Accept" icon="fa-check" invisible="status"/>
<button name="action_refuse_offer" type="object" title="Refuse" icon="fa-times" invisible="status"/>
<field name="status" invisible="True"/>
</tree>
</field>
</record>

<record id="estate_property_offer_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 string="Properties">
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</group>
</form>
</field>
</record>

<record id="property_type_form_offers" model="ir.actions.act_window">
<field name="name">Property offers</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>
</odoo>
18 changes: 18 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<odoo>
<record id="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>

<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 string="Title" editable="bottom">
<field name="name" />
</tree>
</field>
</record>
</odoo>
51 changes: 51 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<odoo>
<record id="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">tree,form</field>
</record>

<record id="estate_property_type_form" model="ir.ui.view">
<field name="name">estate.property.type.form</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<form string="Property type">
<sheet>
<div class="oe_button_box" name="button_box" invisible="not offer_count">
<button class="oe_stat_button" type="action" name="%(estate.property_type_form_offers)d" icon="fa-money">
<field string="Offers" name="offer_count" widget="statinfo"/>
</button>
</div>
<div class="oe_title">
<h1>
<field name="name" class="mb16"/>
</h1>
</div>
<notebook>
<page string="Properties">
<field name="property_ids">
<tree>
<field name="name" />
<field name="expected_price" />
<field name="state" />
</tree>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>

<record id="estate_property_type_view_tree" model="ir.ui.view">
<field name="name">estate_property_type.view.tree</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<tree string="Property types">
<field name="sequence" widget="handle"/>
<field name="name"/>
</tree>
</field>
</record>
</odoo>
Loading