diff --git a/d2h/api.py b/d2h/api.py new file mode 100644 index 0000000..bfa6d79 --- /dev/null +++ b/d2h/api.py @@ -0,0 +1,35 @@ +import frappe +from frappe.utils import today + +LIMIT_PER_DAY = 2 + +@frappe.whitelist() +def increment_print_count(): + current_date = today() + + print_log = frappe.cache().hget('global_daily_print_log', 'count') or {} + + if print_log.get('date') != current_date: + print_log = {'count': 0, 'date': current_date} + + if print_log['count'] > LIMIT_PER_DAY: + return {'limit_reached': True} + + print_log['count'] += 1 + frappe.cache().hset('global_daily_print_log', 'count', print_log) + + return {'limit_reached': False} + +def before_print(_, __, ___): + current_date = today() + + print_log = frappe.cache().hget('global_daily_print_log', 'count') + + if not print_log: + return + + if print_log.get('date') != current_date: + return + + if print_log['count'] > LIMIT_PER_DAY: + frappe.throw('The daily print limit for all documents has been reached.') diff --git a/d2h/fixtures/custom_field.json b/d2h/fixtures/custom_field.json index 2af3a33..d53c34a 100644 --- a/d2h/fixtures/custom_field.json +++ b/d2h/fixtures/custom_field.json @@ -1,4 +1,59 @@ [ + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Target Detail", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_item", + "fieldtype": "Link", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 1, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "distribution_id", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Item", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-08-02 08:53:50.942378", + "module": null, + "name": "Target Detail-custom_item", + "no_copy": 0, + "non_negative": 0, + "options": "Item", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 0, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, { "allow_in_quick_entry": 0, "allow_on_submit": 0, diff --git a/d2h/hooks.py b/d2h/hooks.py index 69033da..16b309a 100644 --- a/d2h/hooks.py +++ b/d2h/hooks.py @@ -231,7 +231,12 @@ }, "Delivery Note": { "before_save": "d2h.overrides.delivery_note_before_save", + }, + "*": { + "before_print": "d2h.api.before_print", } } +app_include_js = "/assets/d2h/js/form.js" + doctype_js = {"Purchase Receipt" : "public/js/purchase_receipt.js", "Purchase Order" : "public/js/purchase_order.js", "Delivery Note" : "public/js/delivery_note.js"} \ No newline at end of file diff --git a/d2h/public/js/form.js b/d2h/public/js/form.js new file mode 100644 index 0000000..b3a3969 --- /dev/null +++ b/d2h/public/js/form.js @@ -0,0 +1,18 @@ +$(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(); + } + }, + }); + }); + }, 500); +}); diff --git a/d2h/public/js/purchase_order.js b/d2h/public/js/purchase_order.js index 05840ef..330b11b 100644 --- a/d2h/public/js/purchase_order.js +++ b/d2h/public/js/purchase_order.js @@ -1,9 +1,15 @@ frappe.ui.form.on("Purchase Order", { refresh: function (frm) { if (!frm.is_new()) { - frm.add_custom_button(__("Good In Transit"), function () { - show_items_dialog(frm); + qties = frm.doc.items.map((item) => { + return item.qty - item.custom_good_in_transit_qty; }); + total_qty = qties.reduce((a, b) => a + b, 0); + if (total_qty > 0) { + frm.add_custom_button(__("Good In Transit"), function () { + show_items_dialog(frm); + }); + } } }, });