Hi! I’m trying to create a report that shows some KPIs. I’m doing a test with just one KPI wich divides Current assets by current liabilities.
Table must include the following columns: Çategoria, Indicador, Valor al periodo, valor de referencia
To create the report I’m using the following configurations:
Report type: Script
Ref DocType: Account
Running: ERPNext v15.23 self-hosted.
As far as I undertand with a script report that is not standard you can in v15 add the script and java in the report UI but after adding the script and filters whatever I do the report always says ‘Nothing to show’
Script I’m using is:
def execute(filters=None):
if not filters or not filters.get("fecha"):
frappe.throw("Por favor selecciona una fecha para el reporte.")
fecha = filters.get("fecha")
empresa = filters.get("empresa") or frappe.defaults.get_user_default("Company")
def obtener_balance(cuenta_padre, es_pasivo=False):
cuenta_info = frappe.get_all("Account", filters={"name": cuenta_padre}, fields=["lft", "rgt"])
if not cuenta_info:
return 0
limites = cuenta_info[0]
cuentas = frappe.get_all("Account", filters={
"lft": [">", limites.lft],
"rgt": ["<", limites.rgt],
"is_group": 0,
"company": empresa
}, fields=["name"])
cuentas_hoja = [c.name for c in cuentas]
if not cuentas_hoja:
return 0
resultado = frappe.db.sql("""
SELECT SUM(debit - credit) AS balance
FROM `tabGL Entry`
WHERE account IN %(cuentas)s
AND posting_date <= %(fecha)s
AND company = %(empresa)s
""", {
"cuentas": tuple(cuentas_hoja),
"fecha": fecha,
"empresa": empresa
}, as_dict=True)
balance = resultado[0]["balance"] if resultado and resultado[0]["balance"] is not None else 0
return abs(balance) if es_pasivo else balance
# Indicadores base
indicadores = []
activos_corrientes = obtener_balance("1.1 - Activos corriente - IP")
pasivos_corrientes = obtener_balance("2.1 - Pasivo corriente - IP", es_pasivo=True)
razon_corriente = activos_corrientes / pasivos_corrientes if pasivos_corrientes else 0
indicadores.append({
"categoria": "Liquidez",
"indicador": "Razón corriente",
"valor": razon_corriente,
"referencia": 1.2
})
# Filtros dinámicos
if filters.get("categoria"):
indicadores = [i for i in indicadores if i["categoria"] == filters["categoria"]]
if filters.get("indicador"):
indicadores = [i for i in indicadores if i["indicador"] == filters["indicador"]]
columns = [
{"label": "Categoría", "fieldname": "categoria", "fieldtype": "Data", "width": 150},
{"label": "Indicador", "fieldname": "indicador", "fieldtype": "Data", "width": 200},
{"label": "Valor al período", "fieldname": "valor", "fieldtype": "Float", "width": 150},
{"label": "Valor de referencia", "fieldname": "referencia", "fieldtype": "Float", "width": 150}
]
return columns, indicadores or [{"categoria": "N/A", "indicador": "Sin datos", "valor": 0, "referencia": 0}]
I’d like to know what I should do since I have tested a couple of things and I don’t seems to have the report to read the script and generate the report.
I’m using chatgpt to help me with the code but since documentations is so lacky I couldn’t figure out what is wrong of feed the AI with proper guidance.
Thanks in advance!