From be33eaf7f9810d2798a0e8fd223f4f7756af34f6 Mon Sep 17 00:00:00 2001 From: Neha Fathima Date: Fri, 17 Apr 2026 14:42:02 +0530 Subject: [PATCH 1/2] fix: implmented customer validation --- rmax_custom/api/customer.py | 3 + rmax_custom/public/js/create_customer.js | 155 +++++++++++++++-------- 2 files changed, 108 insertions(+), 50 deletions(-) diff --git a/rmax_custom/api/customer.py b/rmax_custom/api/customer.py index a89dd03..0a35a2c 100644 --- a/rmax_custom/api/customer.py +++ b/rmax_custom/api/customer.py @@ -45,6 +45,9 @@ def create_customer_with_address( if not mobile_no: frappe.throw(_("Mobile No is required")) + if count_digits(mobile_no) < 10: + frappe.throw("Mobile number must have at least 10 digits.") + # Prevent duplicate if frappe.db.exists("Customer", {"customer_name": customer_name}): frappe.throw(_("Customer already exists")) diff --git a/rmax_custom/public/js/create_customer.js b/rmax_custom/public/js/create_customer.js index 7ab0ff0..32c559b 100644 --- a/rmax_custom/public/js/create_customer.js +++ b/rmax_custom/public/js/create_customer.js @@ -1,9 +1,7 @@ - frappe.ui.form.on("Sales Invoice", { refresh: function (frm) { add_create_customer_button(frm); - console.log("Sales Invoice Custom Script Loaded"); - } + } }); function add_create_customer_button(frm) { @@ -44,7 +42,7 @@ function open_create_customer_dialog(frm) { let d = new frappe.ui.Dialog({ title: "Create New Customer", - size: "large", // important when many fields + size: "large", fields: [ { @@ -64,25 +62,34 @@ function open_create_customer_dialog(frm) { fieldtype: "Data", label: "Email ID" }, + { + fieldname: "customer_type", + fieldtype: "Select", + label: "Customer Type", + options: "Company\nIndividual\nPartnership\nBranch", + default: "Company" + }, { fieldname: "custom_vat_registration_number", fieldtype: "Data", label: "VAT Registration Number" }, + { fieldtype: "Section Break", label: "Address Details" }, + { fieldname: "address_type", fieldtype: "Select", label: "Address Type", options: "Billing\nShipping", default: "Billing", - mandatory_depends_on: "eval:doc.custom_vat_registration_number", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "address_line1", fieldtype: "Data", label: "Address Line 1", - mandatory_depends_on: "eval:doc.custom_vat_registration_number", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "address_line2", @@ -93,19 +100,19 @@ function open_create_customer_dialog(frm) { fieldname: "custom_building_number", fieldtype: "Data", label: "Building Number", - mandatory_depends_on: "eval:doc.custom_vat_registration_number", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "custom_area", fieldtype: "Data", label: "Area/District", - mandatory_depends_on: "eval:doc.custom_vat_registration_number ", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "city", fieldtype: "Data", label: "City/Town", - mandatory_depends_on: "eval:doc.custom_vat_registration_number", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "country", @@ -113,59 +120,107 @@ function open_create_customer_dialog(frm) { options: "Country", label: "Country", default: country, - mandatory_depends_on: "eval:doc.custom_vat_registration_number", + mandatory_depends_on: "eval:doc.custom_vat_registration_number" }, { fieldname: "pincode", fieldtype: "Data", label: "Postal Code", - mandatory_depends_on: "eval:doc.custom_vat_registration_number", - }, - + mandatory_depends_on: "eval:doc.custom_vat_registration_number" + } ], + primary_action_label: "Create Customer", + primary_action(values) { - const digits = (values.mobile_no || "").replace(/\D/g, "").length; - if (digits < 10) { - frappe.throw("Mobile number must have at least 10 digits."); - return; + + let mobile = values.mobile_no || ""; + + if (mobile.length < 10) { + frappe.msgprint("Mobile number must have at least 10 digits."); + return; + } + let vat = values.custom_vat_registration_number; + let type = values.customer_type; + if (vat && vat.length !== 15) { + frappe.msgprint("VAT must be exactly 15 digits."); + return; + } + let pincode = values.pincode || ""; + if (pincode && pincode.length !== 5) { + frappe.msgprint("Pincode must be exactly 5 digits."); + return; } - return frappe.call({ - method: "rmax_custom.api.customer.create_customer_with_address", - args: { - customer_name: values.customer_name, - mobile_no: values.mobile_no, - email_id: values.email_id || null, - address_type: values.address_type, - address_line1: values.address_line1, - address_line2: values.address_line2 || null, - custom_vat_registration_number: values.custom_vat_registration_number || null, - custom_building_number: values.custom_building_number, - custom_area: values.custom_area, - pincode: values.pincode, - city: values.city, - country: values.country, - default_currency: default_currency - }, - callback: function(r) { - if (r.message) { - - frm.set_value("customer", r.message.customer); - frm.refresh_field("customer"); - - frappe.show_alert({ - message: r.message.message, - indicator: "green" - }); - - d.hide(); + if (vat && ["Company", "Branch"].includes(type)) { + + frappe.db.get_value("Customer", { + custom_vat_registration_number: vat + }, "name").then(r => { + + if (r.message && r.message.name) { + frappe.msgprint( + `VAT already exists for Customer: ${r.message.name}` + ); + return; + } + + create_customer(); + }); + + } else { + create_customer(); + } + + function create_customer() { + frappe.call({ + method: "rmax_custom.api.customer.create_customer_with_address", + args: { + customer_name: values.customer_name, + mobile_no: values.mobile_no, + email_id: values.email_id || null, + customer_type: values.customer_type, + address_type: values.address_type, + address_line1: values.address_line1, + address_line2: values.address_line2 || null, + custom_vat_registration_number: vat || null, + custom_building_number: values.custom_building_number, + custom_area: values.custom_area, + pincode: values.pincode, + city: values.city, + country: values.country, + default_currency: default_currency + }, + callback: function(r) { + if (r.message) { + + frm.set_value("customer", r.message.customer); + frm.refresh_field("customer"); + + frappe.show_alert({ + message: r.message.message, + indicator: "green" + }); + + d.hide(); + } } - } - }); + }); + } } }); d.show(); - }); -} + d.fields_dict.mobile_no.$input.on("input", function () { + this.value = this.value.replace(/[^0-9]/g, ''); + }); + d.fields_dict.custom_vat_registration_number.$input.on("input", function () { + let value = this.value.replace(/[^0-9]/g, ''); + if (value.length > 15) { + value = value.slice(0, 15); + } + this.value = value; + }); + + }); +} \ No newline at end of file From d946b9462e431a099c3b601ff35bd0d1f53342a4 Mon Sep 17 00:00:00 2001 From: Neha Fathima Date: Fri, 17 Apr 2026 14:44:54 +0530 Subject: [PATCH 2/2] fix: remove unwanted code --- rmax_custom/public/js/vat_validation.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/rmax_custom/public/js/vat_validation.js b/rmax_custom/public/js/vat_validation.js index 14fbd1f..915f037 100644 --- a/rmax_custom/public/js/vat_validation.js +++ b/rmax_custom/public/js/vat_validation.js @@ -83,7 +83,5 @@ function set_customer_type_filter(frm) { field.df.options = options.join("\n"); frm.refresh_field("customer_type"); - - console.log("Branch removedss from Customer Type"); } } \ No newline at end of file