"""Create 3 Script Reports in ERPNext for DiDi.""" import sys, io, json, requests sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") URL = "http://localhost:8080" s = requests.Session() s.post(f"{URL}/api/method/login", data={"usr": "Administrator", "pwd": "admin"}) print("Logged in") REPORTS = [ { "report_name": "DiDi Dashboard Financiar", "ref_doctype": "Sales Invoice", "report_type": "Script Report", "is_standard": "No", "module": "Accounts", "report_script": """result = [] total = frappe.db.sql("SELECT COALESCE(SUM(grand_total),0) FROM `tabSales Invoice` WHERE docstatus=1 AND status!='Cancelled'")[0][0] result.append({"indicator": "Total Venituri (RON)", "valoare": total}) luna = frappe.db.sql("SELECT COALESCE(SUM(grand_total),0) FROM `tabSales Invoice` WHERE docstatus=1 AND status!='Cancelled' AND MONTH(posting_date)=MONTH(CURDATE()) AND YEAR(posting_date)=YEAR(CURDATE())")[0][0] result.append({"indicator": "Venituri Luna Curenta (RON)", "valoare": luna}) nr_facturi = frappe.db.sql("SELECT COUNT(*) FROM `tabSales Invoice` WHERE docstatus=1 AND status!='Cancelled'")[0][0] result.append({"indicator": "Nr. Facturi Emise", "valoare": nr_facturi}) nr_platite = frappe.db.sql("SELECT COUNT(*) FROM `tabSales Invoice` WHERE docstatus=1 AND status='Paid'")[0][0] result.append({"indicator": "Nr. Facturi Platite", "valoare": nr_platite}) nr_clienti = frappe.db.sql("SELECT COUNT(DISTINCT customer) FROM `tabSales Invoice` WHERE docstatus=1")[0][0] result.append({"indicator": "Clienti Activi", "valoare": nr_clienti}) columns = [{"fieldname":"indicator","label":"Indicator","fieldtype":"Data","width":300},{"fieldname":"valoare","label":"Valoare","fieldtype":"Currency","width":200}] data = result""", }, { "report_name": "DiDi Raport CRM", "ref_doctype": "Lead", "report_type": "Script Report", "is_standard": "No", "module": "CRM", "report_script": """result = [] total = frappe.db.sql("SELECT COUNT(*) FROM `tabLead`")[0][0] result.append({"metric": "Total Lead-uri", "numar": total}) surse = frappe.db.sql("SELECT IFNULL(source,'Necunoscut') as s, COUNT(*) as c FROM `tabLead` GROUP BY source ORDER BY c DESC") for row in surse: result.append({"metric": f"Sursa: {row[0]}", "numar": row[1]}) statusuri = frappe.db.sql("SELECT status, COUNT(*) as c FROM `tabLead` GROUP BY status ORDER BY c DESC") for row in statusuri: result.append({"metric": f"Status: {row[0]}", "numar": row[1]}) columns = [{"fieldname":"metric","label":"Metric","fieldtype":"Data","width":350},{"fieldname":"numar","label":"Numar","fieldtype":"Int","width":150}] data = result""", }, { "report_name": "DiDi Clienti per Plan", "ref_doctype": "Customer", "report_type": "Script Report", "is_standard": "No", "module": "CRM", "report_script": """result = [] total = frappe.db.sql("SELECT COUNT(*) FROM `tabCustomer`")[0][0] result.append({"plan": "Total Clienti", "numar": total}) roluri = frappe.db.sql("SELECT IFNULL(NULLIF(iam_role,''),'fara_rol') as r, COUNT(*) as c FROM `tabCustomer` GROUP BY iam_role ORDER BY c DESC") for row in roluri: result.append({"plan": f"Rol: {row[0]}", "numar": row[1]}) planuri = frappe.db.sql("SELECT IFNULL(NULLIF(active_plan,''),'fara_plan') as p, COUNT(*) as c FROM `tabCustomer` GROUP BY active_plan ORDER BY c DESC") for row in planuri: result.append({"plan": f"Plan: {row[0]}", "numar": row[1]}) columns = [{"fieldname":"plan","label":"Plan / Rol","fieldtype":"Data","width":350},{"fieldname":"numar","label":"Numar","fieldtype":"Int","width":150}] data = result""", }, ] for rpt in REPORTS: name = rpt["report_name"] check = s.get(f"{URL}/api/resource/Report/{requests.utils.quote(name)}") if check.status_code == 200: print(f" Exists: {name}") continue r = s.post(f"{URL}/api/resource/Report", json=rpt) if r.status_code in (200, 201): print(f" Created: {name}") else: print(f" Error {name}: {r.status_code} {r.text[:200]}") print("Done!")