From 399e43c4adfa78133626b62e19c1ce57b45ea062 Mon Sep 17 00:00:00 2001 From: Ashish Baravaliya Date: Tue, 20 Aug 2024 23:43:31 -0400 Subject: [PATCH] fix: print logic updated --- d2h/api.py | 15 +++++++++++++++ d2h/public/js/form.js | 39 +++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/d2h/api.py b/d2h/api.py index b6fd4d0..308e611 100644 --- a/d2h/api.py +++ b/d2h/api.py @@ -38,6 +38,21 @@ def before_print(_, __, ___): if print_log['count'] > LIMIT_PER_DAY: frappe.throw('The daily print limit for all documents has been reached.') +@frappe.whitelist() +def get_print_limit(): + current_date = today() + + print_log = frappe.cache().hget('global_daily_print_log', 'count') + + if not print_log: + return { "limit_reached" : False } + + if print_log.get('date') != current_date: + return { "limit_reached" : False } + + if print_log['count'] > LIMIT_PER_DAY: + return { "limit_reached" : True } + @frappe.whitelist() def short_close_purchase_order(purchase_order): diff --git a/d2h/public/js/form.js b/d2h/public/js/form.js index b3a3969..3ef90b6 100644 --- a/d2h/public/js/form.js +++ b/d2h/public/js/form.js @@ -1,18 +1,25 @@ -$(document).ready(function () { - setTimeout(() => { - $(`button[data-label='Print']`).on("click", function (e) { - frappe.call({ - method: "d2h.api.increment_print_count", - callback: function (r) { - if (r.message && r.message.limit_reached) { - frappe.throw( - "The daily print limit for all documents has been reached." - ); - } else { - frm.print_doc(); - } - }, - }); +$(document).on("page-change", function () { + if (document.baseURI.includes("/app/print/")) { + $(`button[data-label='Print']`).hide(); + frappe.call({ + method: "d2h.api.get_print_limit", + callback: function (r) { + if (r.message && !r.message.limit_reached) { + $(`button[data-label='Print']`).show(); + $(`button[data-label='Print']`).on("click", function (e) { + frappe.call({ + method: "d2h.api.increment_print_count", + callback: function (r) { + if (r.message && r.message.limit_reached) { + frappe.throw( + "The daily print limit for all documents has been reached." + ); + } + }, + }); + }); + } + }, }); - }, 500); + } });