Skip to content

Commit

Permalink
faet: print change sales person item
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Aug 2, 2024
1 parent 5bd74d0 commit 7feb0fe
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
35 changes: 35 additions & 0 deletions d2h/api.py
Original file line number Diff line number Diff line change
@@ -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.')
55 changes: 55 additions & 0 deletions d2h/fixtures/custom_field.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 5 additions & 0 deletions d2h/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
18 changes: 18 additions & 0 deletions d2h/public/js/form.js
Original file line number Diff line number Diff line change
@@ -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);
});
10 changes: 8 additions & 2 deletions d2h/public/js/purchase_order.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
},
});
Expand Down

0 comments on commit 7feb0fe

Please sign in to comment.