Bismillah.., Segala puji milik Allah. Yang mana kita patut memuji-Nya, yang mana kita harapkan pertolongan-Nya dan kita harapkan ampunan-Nya. Serta sepatutnya kita berlindung diri kepada-Nya dari segala keburukan diri dan kejelekan perbuatan yang ada pada diri kita. Siapa yang diberi hidayah oleh Allah maka tidak ada yang mampu menyesatkannya dan siapa yang disesatkan oleh Allah maka tidak akan ada yang dapat memberikan hidayah. Sebelumnya Saya juga bersaksi bahwa tidak ada tuhan (yang berhak disembah) selain Allah semata yang tidak ada sekutu bagi-Nya dan saya bersaksi bahwa Nabi Muhammad Shalallahu'alaihi wa sallam adalah hamba dan utusan-Nya.
Selanjutnya saya ucapkan Ahlan wa sahlan wa Baarokallohu fiikum jamii'an bagi para pembaca blog ini dan tidak lupa juga saya berdo'a semoga seluruh kaum muslimin yang sedang sakit diberikan kesehatan dan yang sehat selalu dijaga oleh Allah azza wa jalla dan selalu diberikan hidayah islam dan diatas sunnah Nabi kita (Muhammad). Aamiin..!. Untuk kali ini saya akan sedikit membahas mengenai cara memaggil method python dengan javascript di ODOO. Adapun caranya anda bisa mengikuti langkah-langkah sebagai berikut.
1. Untuk langkah pertama buatlah sebuah method/function di dalam sebuah model (object)
from odoo import _, api, fields, modelsimport re
class InvestmentReport(models.TransientModel): _name = 'investment.report' _description = 'Investment Report'
date_from = fields.Date('') date_to = fields.Date('') company_ids = fields.Many2many('res.company', string='Company')
def get_calculation_profit_loss(self): data_account_type_revenue = self.env.ref('account.data_account_type_revenue').name # OPINC data_account_type_other_income = self.env.ref('account.data_account_type_other_income').name # OIN data_account_type_direct_costs = self.env.ref('account.data_account_type_direct_costs').name # COS data_account_type_expense = self.env.ref('account.data_account_type_expenses').name # EXP data_account_type_depreciation = self.env.ref('account.data_account_type_depreciation').name # DEP
cr = self._cr
case_debit_date ="aml.debit" case_credit_date ="aml.credit"
if self.date_from and self.date_to: domain += [('date', '>=', self.date_from), ('date', '<=', self.date_to)] case_debit_date = "CASE WHEN aml.date >= '%s' AND aml.date <= '%s' THEN aml.debit
ELSE 0 END" % (self.date_from, self.date_to) case_credit_date = "CASE WHEN aml.date >= '%s' AND aml.date <= '%s' THEN aml.credit
ELSE 0 END" % (self.date_from, self.date_to)
elif self.date_from and (not self.date_to or self.date_to == False): domain += [('date', '>=', self.date_from)] case_debit_date = "CASE WHEN aml.date >= '%s' THEN aml.debit ELSE 0 END" % (self.date_from) case_credit_date = "CASE WHEN aml.date >= '%s' THEN aml.credit ELSE 0 END" % (self.date_from)
elif self.date_to and (not self.date_from or self.datecase_credit_date_from == False): domain += [('date', '<=', self.date_to)] case_debit_date = "CASE WHEN aml.date <= '%s' THEN aml.debit ELSE 0 END" % (self.date_to) case_credit_date = "CASE WHEN aml.date <= '%s' THEN aml.credit ELSE 0 END" % (self.date_to) type_account= [data_account_type_revenue, data_account_type_other_income,
data_account_type_direct_costs, data_account_type_depreciation, data_account_type_expense]
sql = """ SELECT aml.id as "ID", acc.code as "CODE", acc.name as "DESCRIPTION", SUM(COALESCE(%s, 0)) as "DEBIT", SUM(COALESCE(%s, 0)) as "CREDIT", (SUM(COALESCE(%s, 0)) - SUM(COALESCE(%s, 0))) as "BALANCE", at.type as "TIPE", at.name as "NAMA_TIPE" FROM account_account acc LEFT JOIN account_move_line aml on (aml.account_id = acc.id) LEFT JOIN account_journal aj on (aml.journal_id = aj.id) LEFT JOIN account_account_type at on (at.id = acc.user_type_id) LEFT JOIN account_move am on (am.id = aml.move_id)
LEFT JOIN res_company rp on rp.id = aml.company_id
WHERE aml.parent_state = 'posted' AND at.name in %s AND aj.type != 'purchase' GROUP BY rp.name, acc.code, acc.name, at.type, at.name, aml.id ORDER BY acc.code, rp.name asc
""" % (case_debit_date, case_credit_date, case_credit_date, case_debit_date, tuple(type_account))
cr.execute(sql) results = cr.dictfetchall()
total_profit_and_loss = round(sum([line['BALANCE'] for line in results]), 2)
return total_profit_and_loss
from odoo import _, api, fields, models
import re
class InvestmentReport(models.TransientModel):
_name = 'investment.report'
_description = 'Investment Report'
date_from = fields.Date('')
date_to = fields.Date('')
company_ids = fields.Many2many('res.company', string='Company')
def get_calculation_profit_loss(self):
data_account_type_revenue = self.env.ref('account.data_account_type_revenue').name # OPINC
data_account_type_other_income = self.env.ref('account.data_account_type_other_income').name # OIN
data_account_type_direct_costs = self.env.ref('account.data_account_type_direct_costs').name # COS
data_account_type_expense = self.env.ref('account.data_account_type_expenses').name # EXP
data_account_type_depreciation = self.env.ref('account.data_account_type_depreciation').name # DEP
cr = self._cr
case_debit_date ="aml.debit"
case_credit_date ="aml.credit"
if self.date_from and self.date_to:
domain += [('date', '>=', self.date_from), ('date', '<=', self.date_to)]
case_debit_date = "CASE WHEN aml.date >= '%s' AND aml.date <= '%s' THEN aml.debit
ELSE 0 END" % (self.date_from, self.date_to)
ELSE 0 END" % (self.date_from, self.date_to)
case_credit_date = "CASE WHEN aml.date >= '%s' AND aml.date <= '%s' THEN aml.credit
ELSE 0 END" % (self.date_from, self.date_to)
ELSE 0 END" % (self.date_from, self.date_to)
elif self.date_from and (not self.date_to or self.date_to == False):
domain += [('date', '>=', self.date_from)]
case_debit_date = "CASE WHEN aml.date >= '%s' THEN aml.debit ELSE 0 END" % (self.date_from)
case_credit_date = "CASE WHEN aml.date >= '%s' THEN aml.credit ELSE 0 END" % (self.date_from)
elif self.date_to and (not self.date_from or self.datecase_credit_date_from == False):
domain += [('date', '<=', self.date_to)]
case_debit_date = "CASE WHEN aml.date <= '%s' THEN aml.debit ELSE 0 END" % (self.date_to)
case_credit_date = "CASE WHEN aml.date <= '%s' THEN aml.credit ELSE 0 END" % (self.date_to)
type_account= [data_account_type_revenue, data_account_type_other_income,
data_account_type_direct_costs, data_account_type_depreciation, data_account_type_expense]
data_account_type_direct_costs, data_account_type_depreciation, data_account_type_expense]
sql = """
SELECT
aml.id as "ID",
acc.code as "CODE",
acc.name as "DESCRIPTION",
SUM(COALESCE(%s, 0)) as "DEBIT",
SUM(COALESCE(%s, 0)) as "CREDIT",
(SUM(COALESCE(%s, 0)) - SUM(COALESCE(%s, 0))) as "BALANCE",
at.type as "TIPE",
at.name as "NAMA_TIPE"
FROM account_account acc
LEFT JOIN account_move_line aml on (aml.account_id = acc.id)
LEFT JOIN account_journal aj on (aml.journal_id = aj.id)
LEFT JOIN account_account_type at on (at.id = acc.user_type_id)
LEFT JOIN account_move am on (am.id = aml.move_id)
LEFT JOIN res_company rp on rp.id = aml.company_id
WHERE aml.parent_state = 'posted' AND at.name in %s AND aj.type != 'purchase'
GROUP BY rp.name, acc.code, acc.name, at.type, at.name, aml.id
ORDER BY acc.code, rp.name asc
""" % (case_debit_date, case_credit_date, case_credit_date, case_debit_date, tuple(type_account))
cr.execute(sql)
results = cr.dictfetchall()
total_profit_and_loss = round(sum([line['BALANCE'] for line in results]), 2)
return total_profit_and_loss
Keterangan:
Di kasus ini saya telah membuat sebuah model (Transient Model) dengan dengan nama "investment.report". Didalam model tersebut saya membuat satu mehod yang mana isi method tersebut menghasilkan nilai profit and loss, dengan memberikan beberapa kondisi yang nantinya akan memfilter jika tanggal pada wizard di input/ di isi.
2. Selanjutnya buatlah folder static/src/js/, dan pada folder js buatlah file dengan extension .js (nama_file.js)
Setelah anda membuat file.js, jangan lupa untuk memanggil file.js ini dengan cara inherit template untuk ODOO versi 14 kebawah dan untuk ODOO versi 15 cukup memanggil di __manifest__.py , kemudianikutilah codingan saya dibawah ini:
odoo.define("nama_modul.investment_report", function (require) { "use strict";
this._rpc({ model: "investment.report", method: "get_calculation_profit_loss", args: [false], }).then(function (res) { console.log("===========>>>", res) return res; });
odoo.define("nama_modul.investment_report", function (require) {
"use strict";
this._rpc({
model: "investment.report",
method: "get_calculation_profit_loss",
args: [false],
}).then(function (res) {
console.log("===========>>>", res)
return res;
});