From 809660467bfb8bd9876651a2aeef6fd25ed16c23 Mon Sep 17 00:00:00 2001 From: Ashkar Date: Tue, 6 May 2025 16:37:15 +0530 Subject: [PATCH] fix: pos branch validation --- posnext/doc_events/pos_profile.py | 11 +++++- posnext/public/js/pos_item_cart.js | 59 ++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/posnext/doc_events/pos_profile.py b/posnext/doc_events/pos_profile.py index 1f9d906..dbdcbc5 100644 --- a/posnext/doc_events/pos_profile.py +++ b/posnext/doc_events/pos_profile.py @@ -4,4 +4,13 @@ def validate_pf(doc,method): if not doc.custom_edit_rate_and_uom: doc.custom_use_discount_percentage = 0 - doc.custom_use_discount_amount = 0 \ No newline at end of file + doc.custom_use_discount_amount = 0 + + +@frappe.whitelist() +def get_pos_profile_branch(pos_profile_name): + if not pos_profile_name: + frappe.throw("POS Profile name is required.") + + branch = frappe.db.get_value("POS Profile", pos_profile_name, "branch") + return {"branch": branch} diff --git a/posnext/public/js/pos_item_cart.js b/posnext/public/js/pos_item_cart.js index 7505f6d..1cf784c 100644 --- a/posnext/public/js/pos_item_cart.js +++ b/posnext/public/js/pos_item_cart.js @@ -523,32 +523,51 @@ this.highlight_checkout_btn(true); }); this.$component.on('click', '.add-branch-wrapper', function () { - const $wrapper = $(this); // Store reference to the clicked element + const $wrapper = $(this); + const posProfileName = me.settings.name; + console.log("POS Profile:", posProfileName); - // Create a div container for the link field const branchFieldWrapper = $('
'); - - // Replace the wrapper with the new div $wrapper.replaceWith(branchFieldWrapper); - // Create a Frappe Link Field dynamically - let branchField = new frappe.ui.form.ControlLink({ - df: { - fieldtype: 'Link', - options: 'Branch', // Link to Branch Doctype - fieldname: 'branch', - label: 'Branch', - placeholder: 'Select Branch', + frappe.call({ + method: "posnext.doc_events.pos_profile.get_pos_profile_branch", + args: { + pos_profile_name: posProfileName }, - parent: branchFieldWrapper, // Append inside the same container - value: '', - change: function (value) { - console.log('Selected Branch:', value); - } - }); + callback: function (r) { + const branch_name = r.message && r.message.branch; + + console.log(branch_name) + + + if (!branch_name) { + frappe.msgprint(__('Create Branch Accounting Dimensions')); + return; + } - // Render the field - branchField.refresh(); + + let branchField = new frappe.ui.form.ControlLink({ + df: { + fieldtype: 'Link', + options: 'Branch', + fieldname: 'branch', + label: 'Branch', + placeholder: 'Select Branch', + default: branch_name, + reqd: 1, + get_query: () => ({ + filters: { disabled: 0 } + }) + }, + parent: branchFieldWrapper + }); + + branchField.make(); + branchField.set_value(branch_name); + branchField.refresh(); + }, + }); }); frappe.ui.form.on("Sales Invoice", "paid_amount", frm => {