Hi, Check this function. You will get an idea. You have to return context. Also use context.update. Hope this helps. All the best
def get_context(context):
if (frappe.session.user == “Guest” or
frappe.db.get_value(“User”, frappe.session.user, “user_type”)==“Website User”):
frappe.throw(_(“You are not permitted to access this page.”), frappe.PermissionError)
hooks = frappe.get_hooks()
flag = False
company_name = frappe.defaults.get_user_default("company")
try:
boot = frappe.sessions.get()
except Exception as e:
boot = frappe._dict(status='failed', error = str(e))
print(frappe.get_traceback())
if 'Administrator' in frappe.get_roles(frappe.session.user):
flag = True
# this needs commit
csrf_token = frappe.sessions.get_csrf_token()
frappe.db.commit()
condition = "owner = '{user}'".format(user=frappe.session.user)
boot_json = frappe.as_json(boot)
# remove script tags from boot
boot_json = re.sub("\<script\>[^<]*\</script\>", "", boot_json)
if flag:
context.update({
"no_cache": 1,
"build_version": get_build_version(),
"include_js": hooks["app_include_js"],
"include_css": hooks["app_include_css"],
"sounds": hooks["sounds"],
"boot": boot if context.get("for_mobile") else boot_json,
"csrf_token": csrf_token,
"background_image": (boot.status != 'failed' and
(boot.user.background_image or boot.default_background_image) or None),
"google_analytics_id": frappe.conf.get("google_analytics_id"),
"mixpanel_id": frappe.conf.get("mixpanel_id"),
"total_qty":(frappe.db.sql("select case when sum(actual_qty) is null then 0 else sum(actual_qty) end as totalqty from `tabStock Ledger Entry`"))[0][0],
"total_sales_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1"))[0][0],
"total_sales_value":format_currency((frappe.db.sql(" SELECT case when sum(`tabSales Invoice`.grand_total) is null then 0 else sum(round(`tabSales Invoice`.grand_total,2)) end as gtt FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1"))[0][0],"INR", locale="en_IN"),
"today_sales_value":format_currency((frappe.db.sql("SELECT case when sum(`tabSales Invoice`.grand_total) is null then 0 else sum(round(`tabSales Invoice`.grand_total,2)) end as gtt FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 and `tabSales Invoice`.posting_date = curdate() "))[0][0],"INR", locale="en_IN"),
"today_sales_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabSales Invoice` WHERE (`tabSales Invoice`.docstatus=1 and `tabSales Invoice`.posting_date= curdate()) "))[0][0],
"supplier_count":(frappe.db.sql("select count(supplier_name) from `tabSupplier`"))[0][0],
"today_supplier_count":(frappe.db.sql("select count(supplier_name) from `tabSupplier` where DATE(`tabSupplier`.creation)=curdate();"))[0][0],
"expired":(frappe.db.sql("select count(expiry_date) from `tabBatch` where `tabBatch`.expiry_date<curdate()"))[0][0],
"today_purchase_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabPurchase Invoice` WHERE (`tabPurchase Invoice`.docstatus=1 and `tabPurchase Invoice`.posting_date= curdate()) "))[0][0],
"today_purchase_value":format_currency((frappe.db.sql("SELECT case when sum(`tabPurchase Invoice`.grand_total) is null then 0 else sum(round(`tabPurchase Invoice`.grand_total,2)) end as gtt FROM `tabPurchase Invoice` WHERE `tabPurchase Invoice`.docstatus = 1 and `tabPurchase Invoice`.posting_date = curdate() "))[0][0],"INR", locale="en_IN"),
"total_batch_count":(frappe.db.sql("select count(batch_id) from `tabBatch` where `tabBatch`.expiry_date>=curdate()"))[0][0],
"out_of_stock":(frappe.db.sql("select count(`tabItem`.item_code) from (`tabItem` left join `tabBin` on `tabItem`.item_code=`tabBin`.item_code) where `tabBin`.actual_qty is null"))[0][0],
"acc_payable":format_currency((frappe.db.sql("select case when sum(outstanding_amount) is null then 0 else sum(outstanding_amount) end as pay from `tabPurchase Invoice`"))[0][0],"INR", locale="en_IN"),
"acc_receivable":format_currency((frappe.db.sql("select case when sum(outstanding_amount) is null then 0 else sum(outstanding_amount) end as pay from `tabSales Invoice`"))[0][0],"INR", locale="en_IN"),
"company_name":company_name
})
else:
print("#33333333333333333333333")
context.update({
"no_cache": 1,
"build_version": get_build_version(),
"include_js": hooks["app_include_js"],
"include_css": hooks["app_include_css"],
"sounds": hooks["sounds"],
"boot": boot if context.get("for_mobile") else boot_json,
"csrf_token": csrf_token,
"background_image": (boot.status != 'failed' and
(boot.user.background_image or boot.default_background_image) or None),
"google_analytics_id": frappe.conf.get("google_analytics_id"),
"mixpanel_id": frappe.conf.get("mixpanel_id"),
"total_qty":(frappe.db.sql("select case when sum(actual_qty) is null then 0 else sum(actual_qty) end as totalqty from `tabStock Ledger Entry` where {cod} ".format(cod = condition)))[0][0],
"total_sales_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 AND {cod} ".format(cod = condition)))[0][0],
"total_sales_value":format_currency((frappe.db.sql(" SELECT case when sum(`tabSales Invoice`.grand_total) is null then 0 else sum(round(`tabSales Invoice`.grand_total,2)) end as gtt FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 AND {cod} ".format(cod = condition)))[0][0],"INR", locale="en_IN"),
"today_sales_value":format_currency((frappe.db.sql("SELECT case when sum(`tabSales Invoice`.grand_total) is null then 0 else sum(round(`tabSales Invoice`.grand_total,2)) end as gtt FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 and `tabSales Invoice`.posting_date = curdate() AND {cod} ".format(cod = condition)))[0][0],"INR", locale="en_IN"),
"today_sales_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabSales Invoice` WHERE (`tabSales Invoice`.docstatus=1 and `tabSales Invoice`.posting_date= curdate()) AND {cod} ".format(cod = condition)))[0][0],
"supplier_count":(frappe.db.sql("select count(supplier_name) from `tabSupplier`"))[0][0],
"today_supplier_count":(frappe.db.sql("select count(supplier_name) from `tabSupplier` where DATE(`tabSupplier`.creation)=curdate();"))[0][0],
"expired":(frappe.db.sql("select count(expiry_date) from `tabBatch` tb where tb.expiry_date<curdate() AND tb.{cod} ".format(cod = condition)))[0][0],
"today_purchase_invoice":(frappe.db.sql("SELECT count(*) AS count FROM `tabPurchase Invoice` tpi WHERE (tpi.docstatus=1 and tpi.posting_date= curdate()) and tpi.{cod} ".format(cod = condition)))[0][0],
"today_purchase_value":format_currency((frappe.db.sql("SELECT case when sum(tps.grand_total) is null then 0 else sum(round(tps.grand_total,2)) end as gtt FROM `tabPurchase Invoice` tps WHERE tps.docstatus = 1 and tps.posting_date = curdate() AND {cod} ".format(cod = condition)))[0][0],"INR", locale="en_IN"),
"total_batch_count":(frappe.db.sql("select count(batch_id) from `tabBatch` tb where tb.expiry_date>=curdate() AND tb.{cod} ".format(cod = condition)))[0][0],
"out_of_stock":(frappe.db.sql("select count(ts.item_code) from (`tabItem` ts left join `tabBin` on ts.item_code=`tabBin`.item_code) where `tabBin`.actual_qty is null AND ts.{cod} ".format(cod = condition)))[0][0],
"acc_payable":format_currency((frappe.db.sql("select case when sum(outstanding_amount) is null then 0 else sum(outstanding_amount) end as pay from `tabPurchase Invoice` where {cod} ".format(cod = condition)))[0][0],"INR", locale="en_IN"),
"acc_receivable":format_currency((frappe.db.sql("select case when sum(outstanding_amount) is null then 0 else sum(outstanding_amount) end as pay from `tabSales Invoice` where {cod} ".format(cod = condition)))[0][0],"INR", locale="en_IN"),
"company_name":company_name
})
return context