Server Framework 101 - Solusi 10
solusi untuk tutorial
https://www.odoo.com/documentation/18.0/developer/tutorials/server_framework_101/11_sprinkles.html
file estate_property.py
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare, float_is_zero
class EstateProperty(models.Model):
_name = "estate.property"
_description = "Estate property"
_order = "id desc"
_sql_constraints = [
("check_expected_price", "CHECK(expected_price > 0)", "Expected price should be positive."),
("check_selling_price", "CHECK(selling_price >= 0)", "Selling price should be positive.")
]
name = fields.Char('Estate Name', required=True)
description = fields.Text('Description')
postcode = fields.Char('Poscode')
date_availability = fields.Date('Available from', copy=False,
default=lambda self: fields.Date.today() + relativedelta(months=3))
expected_price = fields.Float('Expected price', required=True)
selling_price = fields.Float('Selling price', readonly=True, copy=False)
bedrooms = fields.Integer('Bedrooms', default=2)
living_area = fields.Integer('Living area (sqm)')
facades = fields.Integer('Facades')
garage = fields.Boolean('Garage')
garden = fields.Boolean('Garden')
garden_area = fields.Integer('Garden area (sqm)')
garden_orientation = fields.Selection(
string='Garden orientation',
selection=[('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')])
active = fields.Boolean('Active', default=True)
state = fields.Selection(
string='Status',
selection=[('new', 'New'),
('offer received', 'Offer Received'),
('offer accepted', 'Offer Accepted'),
('sold', 'Sold'),
('canceled', 'Canceled')],
default='new',
copy=False)
property_type_id = fields.Many2one('estate.property.type', string="Property Type")
user_id = fields.Many2one('res.users', string='Salesman', 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 offer', compute="_compute_best_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")) if record.offer_ids else 0.0
@api.onchange("garden")
def _onchange_garden(self):
if self.garden:
self.garden_area = 10
self.garden_orientation = 'north'
else:
self.garden_area = 0
self.garden_orientation = False
def action_sold(self):
if "canceled" in self.mapped("state"):
raise UserError("Canceled properties cannot be sold.")
return self.write({"state": "sold"})
def action_cancel(self):
if "sold" in self.mapped("state"):
raise UserError("Sold properties cannot be canceled.")
return self.write({"state": "canceled"})
@api.constrains("expected_price", "selling_price")
def _check_price_difference(self):
for prop in self:
if (
not float_is_zero(prop.selling_price, precision_rounding=0.01)
and float_compare(prop.selling_price, prop.expected_price * 0.9, precision_rounding=0.01) < 0
):
raise ValidationError(
"The selling price must be at least 90% of the expected price! "
+ "You must reduce the expected price if you want to accept this offer."
)
file estate_property_type.py
from odoo import fields, models
class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Estate property type"
_order = "sequence, name"
_sql_constraints = [
("check_name", "UNIQUE(name)", "The name must be unique"),
]
name = fields.Char('Property type', required=True)
sequence = fields.Integer("Sequence", default=10)
property_ids = fields.One2many("estate.property", "property_type_id")
offer_count = fields.Integer(string="Offers Count", compute="_compute_offer")
offer_ids = fields.One2many("estate.property.offer", string="Offers", compute="_compute_offer")
def _compute_offer(self):
data = self.env["estate.property.offer"].read_group(
[("property_id.state", "!=", "canceled"), ("property_type_id", "!=", False)],
["ids:array_agg(id)", "property_type_id"],
["property_type_id"],
)
mapped_count = {d["property_type_id"][0]: d["property_type_id_count"] for d in data}
mapped_ids = {d["property_type_id"][0]: d["ids"] for d in data}
for prop_type in self:
prop_type.offer_count = mapped_count.get(prop_type.id, 0)
prop_type.offer_ids = mapped_ids.get(prop_type.id, [])
file estate_property_tag.py
from odoo import fields, models
class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Estate property type"
_order = "name"
_sql_constraints = [
("check_name", "UNIQUE(name)", "The name must be unique"),
]
name = fields.Char('Property tag', required=True)
color = fields.Integer("Color")
file estate_property_offer.py
from dateutil.relativedelta import relativedelta
from odoo.exceptions import UserError
from odoo import api, fields, models
class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'Estate property offer'
_order = "price desc"
_sql_constraints = [
('check_price', 'CHECK(price > 0)','Price should be positive.')
]
price = fields.Float('Price', required=True)
status = fields.Selection(string='Status',
selection=[
('accepted', 'Accepted'),
('refused', 'Refused'),
],
copy=False,
default=False)
partner_id = fields.Many2one('res.partner', string='Partner', required=True)
property_id = fields.Many2one('estate.property', string="Property", required=True)
validity = fields.Integer('Validity (days)', default=7)
date_deadline = fields.Date('Deadline', compute="_compute_deadline", inverse="_inverse_deadline")
property_type_id = fields.Many2one('estate.property.type', related="property_id.property_type_id", string="Property Type", store=True)
@api.depends("create_date", "validity")
def _compute_deadline(self):
for rec in self:
date = rec.create_date.date() if rec.create_date else fields.date.today()
rec.date_deadline = date + relativedelta(days=rec.validity)
def _inverse_deadline(self):
for rec in self:
date = rec.create_date.date() if rec.create_date else fields.date.today()
rec.validity = (rec.date_deadline - date).days
def action_accept(self):
if "accepted" in self.mapped("property_id.offer_ids.status"):
raise UserError("An offer as already been accepted.")
self.write(
{
"status": "accepted",
}
)
return self.mapped("property_id").write(
{
"state": "offer accepted",
"selling_price": self.price,
"buyer_id": self.partner_id.id,
}
)
def action_refuse(self):
return self.write(
{
"status": "refused",
}
)
file estate_property.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="estate_property_action" model="ir.actions.act_window">
<field name="name">Estate Property</field>
<field name="res_model">estate.property</field>
<field name="view_mode">list,form</field>
<field name="context">{'search_default_avaliable': True}</field>
</record>
<!--search-->
<record id="estate_property_search" model="ir.ui.view">
<field name="name">estate.property.search</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<search string="Search properties">
<field name="name" string="Title"/>
<field name="postcode"/>
<field name="expected_price"/>
<field name="bedrooms"/>
<field name="living_area" string="Living Area (sqm)" filter_domain="[('living_area', '>=', self)]"/>
<field name="facades"/>
<separator/>
<filter string="Available" name="avaliable" domain="['|', ('state', '=', 'new'),
('state', '=', 'offer received')]"/>
<group expand="1" string="Group By">
<filter string="Postcode" name="postcode" context="{'group_by':'postcode'}"/>
</group>
</search>
</field>
</record>
<!--list view-->
<record id="estate_property_view_tree" model="ir.ui.view">
<field name="name">estate.property.tree</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<list string="Properties" decoration-success="state in ('offer received', 'offer accepted')" decoration-muted="state == 'sold'" decoration-bf="state == 'offer accepted'">
<field name="name" string="Title"/>
<field name="postcode"/>
<field name="bedrooms"/>
<field name="living_area"/>
<field name="expected_price"/>
<field name="selling_price"/>
<field name="date_availability" string="Available from" optional='hide'/>
</list>
</field>
</record>
<!--form-->
<record id="estate_property_view_form" model="ir.ui.view">
<field name="name">estate.property.form</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<form>
<header>
<button name="action_sold" type="object" string="Sold" invisible="state=='sold'"/>
<button name="action_cancel" type="object" string="Cancel" invisible="state=='sold'"/>
<field name="state" widget="statusbar" statusbar_visible="new,over accepted,offer received,sold"/>
</header>
<sheet>
<h1><field name="name" placeholder="Property Title"/></h1>
<field name="tag_ids" widget="many2many_tags" options="{'color_field': 'color'}"/>
<group>
<group>
<field name="property_type_id" options="{'no_create': True, 'no_edit': True}"/>
<field name="postcode"/>
<field name="date_availability" string="Available From"/>
</group>
<group>
<field name="expected_price"/>
<field name="best_price"/>
<field name="selling_price"/>
</group>
</group>
<notebook>
<page string="Description">
<group>
<field name="description"/>
<field name="bedrooms"/>
<field name="living_area" string="Living Area (sqm)"/>
<field name="facades"/>
<field name="garage"/>
<field name="garden"/>
<field name="garden_area" string="Garden Area (sqm)" invisible="not garden"/>
<field name="garden_orientation" invisible="not garden"/>
<field name="total_area"/>
</group>
</page>
<page string="Offers">
<field name="offer_ids" readonly="state in ('offer accepted', 'sold', 'canceled')" />
</page>
<page string="Other Info">
<group>
<field name="user_id"/>
<field name="buyer_id"/>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</odoo>
file estate_property_type.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Action Window -->
<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>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a property type
</p>
</field>
</record>
<!-- List View -->
<record id="estate_property_type_view_tree" model="ir.ui.view">
<field name="name">estate.property.type.list</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<list string="Property Types">
<field name="sequence" widget="handle"/>
<field name="name"/>
</list>
</field>
</record>
<!-- Form View -->
<record id="estate_property_type_view_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>
<sheet>
<div class="oe_button_box" name="button_box">
<button name="%(estate_property_offer_action)d" type="action" class="oe_stat_button" icon="fa-money">
<field name="offer_count" widget="statinfo" string="Offers"/>
</button>
</div>
<div class="oe_title">
<h1>
<field name="name"/>
</h1>
</div>
<notebook>
<page string="Properties">
<field name="property_ids">
<list>
<field name="name"/>
<field name="expected_price"/>
<field name="state"/>
</list>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</odoo>
file estate_property_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Action Window -->
<record id="estate_property_tag_action" model="ir.actions.act_window">
<field name="name">Property Tag</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>
<!-- List View -->
<record id="estate_property_tag_view_tree" model="ir.ui.view">
<field name="name">estate.property.tag.list</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<list string="Property Tag" editable="bottom">
<field name="name"/>
</list>
</field>
</record>
<!-- Form View -->
<record id="estate_property_tag_view_form" model="ir.ui.view">
<field name="name">estate.property.tag.form</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="name"/>
</sheet>
</form>
</field>
</record>
</odoo>
file estate_property_offer.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<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 string="Property Offer">
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
</group>
</form>
</field>
</record>
<record id="estate_property_offer_view_tree" model="ir.ui.view">
<field name="name">estate.property.offer.tree</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list string="Properties Offers" editable="bottom" decoration-success="status == 'accepted'" decoration-danger="status == 'refused'">
<field name="price" string="Price"/>
<field name="partner_id" string="Partner"/>
<field name="status" string="Status"/>
<button name="action_accept" type="object" title="Accept" icon="fa-check" invisible="status == 'accepted'"/>
<button name="action_refuse" type="object" title="Refuse" icon="fa-times" invisible="status == 'accepted'"/>
</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="domain">[('property_type_id','=', active_id)]</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
Comments
Post a Comment