From 45ce39370dc43f79000d667686da41acf5864ec9 Mon Sep 17 00:00:00 2001 From: Neha Fathima Date: Tue, 14 Apr 2026 16:52:32 +0530 Subject: [PATCH] feat: Validation for the customer creation in sales invoice --- rmax_custom/api/customer.py | 4 +- rmax_custom/public/js/create_customer.js | 8 +- rmax_custom/public/js/vat_validation.js | 122 +++++++---------------- 3 files changed, 44 insertions(+), 90 deletions(-) diff --git a/rmax_custom/api/customer.py b/rmax_custom/api/customer.py index c1b3651..a89dd03 100644 --- a/rmax_custom/api/customer.py +++ b/rmax_custom/api/customer.py @@ -20,7 +20,7 @@ def _get_default_territory(): def create_customer_with_address( customer_name, mobile_no=None, - customer_type="Individual", + customer_type="Company", email_id=None, country=None, default_currency=None, @@ -67,7 +67,7 @@ def create_customer_with_address( customer = frappe.get_doc({ "doctype": "Customer", "customer_name": customer_name, - "customer_type": customer_type or "Individual", + "customer_type": customer_type or "Company", "customer_group": _get_default_customer_group(), "territory": _get_default_territory(), "default_currency": default_currency, diff --git a/rmax_custom/public/js/create_customer.js b/rmax_custom/public/js/create_customer.js index 2b6927b..b270f60 100644 --- a/rmax_custom/public/js/create_customer.js +++ b/rmax_custom/public/js/create_customer.js @@ -125,7 +125,13 @@ function open_create_customer_dialog(frm) { ], primary_action_label: "Create Customer", primary_action(values) { - + function count_digits(val) { + if (!val) return 0; + return val.replace(/\D/g, "").length; + } + if (count_digits(values.mobile_no) < 10) { + frappe.throw("Mobile number must have at least 10 digits."); + } frappe.call({ method: "rmax_custom.api.customer.create_customer_with_address", args: { diff --git a/rmax_custom/public/js/vat_validation.js b/rmax_custom/public/js/vat_validation.js index cf83895..14fbd1f 100644 --- a/rmax_custom/public/js/vat_validation.js +++ b/rmax_custom/public/js/vat_validation.js @@ -15,104 +15,52 @@ frappe.ui.form.on('Customer', { } set_customer_type_filter(frm); }, + validate: async function(frm) { -// validate: async function(frm) { - -// let vat = frm.doc.custom_vat_registration_number; - -// // ---------------- VAT VALIDATION ---------------- -// if (vat) { -// if (vat.length > 15) { -// frappe.throw("VAT cannot exceed 15 digits"); -// } - -// await frappe.call({ -// method: "rmax_custom.api.customer.validate_vat_customer", -// args: { -// vat: vat, -// customer_type: frm.doc.customer_type, -// name: frm.doc.name || null -// } -// }); -// } - -// if (frm.doc.customer_primary_contact) { - -// let r = await frappe.call({ -// method: "frappe.client.get", -// args: { -// doctype: "Contact", -// name: frm.doc.customer_primary_contact -// } -// }); - -// let contact = r.message; - -// function count_digits(val) { -// if (!val) return 0; -// return val.replace(/\D/g, "").length; -// } - -// (contact.phone_nos || []).forEach(row => { - -// if (row.phone && count_digits(row.phone) < 10) { - -// if (row.is_primary_mobile) { -// frappe.throw("Mobile number must have at least 10 digits."); -// } else { -// frappe.throw("Phone number must have at least 10 digits."); -// } - -// } + let vat = frm.doc.custom_vat_registration_number; + if (vat && vat.length !== 15) { + frappe.throw("VAT must be exactly 15 digits"); + } + if (vat) { + await frappe.call({ + method: "rmax_custom.api.customer.validate_vat_customer", + args: { + vat: vat, + customer_type: frm.doc.customer_type, + name: frm.doc.name || null + } + }); + } + if (frm.doc.customer_primary_contact) { -// }); -// } -validate: async function(frm) { + let r = await frappe.call({ + method: "frappe.client.get", + args: { + doctype: "Contact", + name: frm.doc.customer_primary_contact + } + }); - let vat = frm.doc.custom_vat_registration_number; - if (vat && vat.length !== 15) { - frappe.throw("VAT must be exactly 15 digits"); - } - if (vat) { - await frappe.call({ - method: "rmax_custom.api.customer.validate_vat_customer", - args: { - vat: vat, - customer_type: frm.doc.customer_type, - name: frm.doc.name || null - } - }); - } - if (frm.doc.customer_primary_contact) { + let contact = r.message; - let r = await frappe.call({ - method: "frappe.client.get", - args: { - doctype: "Contact", - name: frm.doc.customer_primary_contact + function count_digits(val) { + if (!val) return 0; + return val.replace(/\D/g, "").length; } - }); - - let contact = r.message; - - function count_digits(val) { - if (!val) return 0; - return val.replace(/\D/g, "").length; - } - for (let row of (contact.phone_nos || [])) { + for (let row of (contact.phone_nos || [])) { - if (row.phone && count_digits(row.phone) < 10) { + if (row.phone && count_digits(row.phone) < 10) { - if (row.is_primary_mobile) { - frappe.throw("Mobile number must have at least 10 digits."); - } else { - frappe.throw("Phone number must have at least 10 digits."); + if (row.is_primary_mobile) { + frappe.throw("Mobile number must have at least 10 digits."); + } else { + frappe.throw("Phone number must have at least 10 digits."); + } } } } - } -}, + }, onload: function(frm) { set_customer_type_filter(frm); }