Server Framework 101 - Solusi 11
solusi untuk tutorial https://www.odoo.com/documentation/18.0/developer/tutorials/server_framework_101/12_inheritance.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."
)
@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_canceled(self):
if not set(self.mapped("state")) <= {"new", "canceled"}:
raise UserError("Only new and canceled properties can be deleted.")
file estate_property_offer.py
from dateutil.relativedelta import relativedelta
from odoo.exceptions import UserError
from odoo import api, fields, models
from odoo.tools import float_compare
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",
}
)
@api.model
def create(self, vals):
if vals.get("property_id") and vals.get("price"):
prop = self.env["estate.property"].browse(vals["property_id"])
# We check if the offer is higher than the existing offers
if prop.offer_ids:
max_offer = max(prop.mapped("offer_ids.price"))
if float_compare(vals["price"], max_offer, precision_rounding=0.01) <= 0:
raise UserError("The offer must be higher than %.2f" % max_offer)
prop.state = "offer received"
return super().create(vals)
buat file baru models/res_users.py
# -*- coding: utf-8 -*-
from odoo import fields, models
class ResUsers(models.Model):
# ---------------------------------------- Private Attributes ---------------------------------
_inherit = "res.users"
# --------------------------------------- Fields Declaration ----------------------------------
# Relational
# This domain gives the opportunity to mention the evaluated and non-evaluated domains
property_ids = fields.One2many(
"estate.property", "user_id", string="Properties", domain=[("state", "in", ["new", "offer received"])]
)
file res_users.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_users_form" model="ir.ui.view">
<field name="name">res.users.form.inherit.estate</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<notebook position="inside">
<page name="estate_properties" string="Real Estate Properties">
<field name="property_ids"/>
</page>
</notebook>
</field>
</record>
</odoo>
Comments
Post a Comment