Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rmax_custom/api/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
155 changes: 105 additions & 50 deletions rmax_custom/public/js/create_customer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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",
Expand All @@ -93,79 +100,127 @@ 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",
fieldtype: "Link",
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;
});

});
}
2 changes: 0 additions & 2 deletions rmax_custom/public/js/vat_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading