From db5f727b6288547e82e331b7fdb76685b3f841ef Mon Sep 17 00:00:00 2001 From: Neha Fathima Date: Wed, 11 Mar 2026 10:28:45 +0530 Subject: [PATCH] fix: Added mandatory depends on customere creation dialog --- rmax_custom/fixtures/custom_field.json | 57 ---- rmax_custom/inter_company.py | 22 ++ rmax_custom/public/js/create_customer.js | 16 +- .../rmax_custom/custom/sales_invoice.json | 16 +- .../custom_scripts/quotation/quotation.js | 3 - .../sales_invoice/sales_invoice.js | 294 ------------------ .../sales_invoice/sales_invoice.py | 67 ---- 7 files changed, 38 insertions(+), 437 deletions(-) delete mode 100644 rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.js delete mode 100644 rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.py diff --git a/rmax_custom/fixtures/custom_field.json b/rmax_custom/fixtures/custom_field.json index f98890d..8484b8a 100644 --- a/rmax_custom/fixtures/custom_field.json +++ b/rmax_custom/fixtures/custom_field.json @@ -1,61 +1,4 @@ [ - { - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "collapsible_depends_on": null, - "columns": 0, - "default": null, - "depends_on": "eval:doc.is_internal_customer && doc.represents_company", - "description": null, - "docstatus": 0, - "doctype": "Custom Field", - "dt": "Sales Invoice", - "fetch_from": null, - "fetch_if_empty": 0, - "fieldname": "custom_inter_company_branch", - "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": 0, - "in_preview": 0, - "in_standard_filter": 0, - "insert_after": "company_tax_id", - "is_system_generated": 0, - "is_virtual": 0, - "label": "Inter Company Branch", - "length": 0, - "link_filters": null, - "mandatory_depends_on": null, - "modified": "2026-03-07 23:10:01.661809", - "module": null, - "name": "Sales Invoice-custom_inter_company_branch", - "no_copy": 0, - "non_negative": 0, - "options": "Inter Company Branch", - "permlevel": 0, - "placeholder": null, - "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": 1, - "search_index": 0, - "show_dashboard": 0, - "sort_options": 0, - "translatable": 0, - "unique": 0, - "width": null - }, { "allow_in_quick_entry": 0, "allow_on_submit": 0, diff --git a/rmax_custom/inter_company.py b/rmax_custom/inter_company.py index 24e45fe..6ad69c1 100644 --- a/rmax_custom/inter_company.py +++ b/rmax_custom/inter_company.py @@ -38,6 +38,28 @@ def sales_invoice_on_submit(doc, method=None): # Fetch Supplier Invoice No & Date from source Sales Invoice (built-in does not set these) pi.bill_no = doc.name pi.bill_date = doc.posting_date + if doc.is_return: + original_pi = frappe.db.get_value( + "Purchase Invoice", + {"inter_company_invoice_reference": doc.return_against}, + "name" + ) + if original_pi: + pi.is_return = 1 + pi.return_against = original_pi + if not pi.items: + for si_item in doc.items: + pi.append("items", { + "item_code": si_item.item_code, + "item_name": si_item.item_name, + "description": si_item.description, + "qty": si_item.qty, + "rate": si_item.rate, + "amount": si_item.amount, + "uom": si_item.uom, + "stock_uom": si_item.stock_uom, + "conversion_factor": si_item.conversion_factor, + }) # Cost center & warehouse: from Inter Company Branch if selected, else company default branch_data = _get_branch_data(doc, pi.company) frappe.flags._inter_company_pi_branch_data = branch_data diff --git a/rmax_custom/public/js/create_customer.js b/rmax_custom/public/js/create_customer.js index 94c6a18..2b6927b 100644 --- a/rmax_custom/public/js/create_customer.js +++ b/rmax_custom/public/js/create_customer.js @@ -2,7 +2,7 @@ frappe.ui.form.on("Sales Invoice", { refresh: function (frm) { add_create_customer_button(frm); - + console.log("Sales Invoice Custom Script Loaded"); } }); @@ -76,13 +76,13 @@ function open_create_customer_dialog(frm) { label: "Address Type", options: "Billing\nShipping", default: "Billing", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, { fieldname: "address_line1", fieldtype: "Data", label: "Address Line 1", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, { fieldname: "address_line2", @@ -93,19 +93,19 @@ function open_create_customer_dialog(frm) { fieldname: "custom_building_number", fieldtype: "Data", label: "Building Number", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, { fieldname: "custom_area", fieldtype: "Data", label: "Area/District", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number ", }, { fieldname: "city", fieldtype: "Data", label: "City/Town", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, { fieldname: "country", @@ -113,13 +113,13 @@ function open_create_customer_dialog(frm) { options: "Country", label: "Country", default: country, - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, { fieldname: "pincode", fieldtype: "Data", label: "Postal Code", - reqd: 1 + mandatory_depends_on: "eval:doc.custom_vat_registration_number", }, ], diff --git a/rmax_custom/rmax_custom/custom/sales_invoice.json b/rmax_custom/rmax_custom/custom/sales_invoice.json index bfe1dd9..8947410 100644 --- a/rmax_custom/rmax_custom/custom/sales_invoice.json +++ b/rmax_custom/rmax_custom/custom/sales_invoice.json @@ -11,9 +11,9 @@ "collapsible": 0, "collapsible_depends_on": null, "columns": 0, - "creation": "2026-03-07 23:10:01.661809", + "creation": "2026-03-11 10:10:28.430666", "default": null, - "depends_on": "eval:doc.is_internal_customer && doc.represents_company", + "depends_on": "eval: doc.is_internal_customer && doc.represents_company", "description": null, "docstatus": 0, "dt": "Sales Invoice", @@ -38,8 +38,8 @@ "label": "Inter Company Branch", "length": 0, "link_filters": null, - "mandatory_depends_on": null, - "modified": "2026-03-07 23:10:01.661809", + "mandatory_depends_on": "eval: doc.is_internal_customer && doc.represents_company", + "modified": "2026-03-11 10:10:28.430666", "modified_by": "Administrator", "module": null, "name": "Sales Invoice-custom_inter_company_branch", @@ -56,7 +56,7 @@ "read_only": 0, "read_only_depends_on": null, "report_hide": 0, - "reqd": 1, + "reqd": 0, "search_index": 0, "show_dashboard": 0, "sort_options": 0, @@ -75,7 +75,7 @@ "collapsible": 0, "collapsible_depends_on": null, "columns": 0, - "creation": "2026-03-07 16:50:18.832354", + "creation": "2026-03-10 23:18:11.539766", "default": null, "depends_on": null, "description": null, @@ -320,7 +320,7 @@ "_comments": null, "_liked_by": null, "_user_tags": null, - "creation": "2026-03-07 23:10:01.561453", + "creation": "2026-03-11 10:10:28.326730", "default_value": null, "doc_type": "Sales Invoice", "docstatus": 0, @@ -328,7 +328,7 @@ "field_name": null, "idx": 0, "is_system_generated": 0, - "modified": "2026-03-07 23:10:01.561453", + "modified": "2026-03-11 10:10:28.326730", "modified_by": "Administrator", "module": null, "name": "Sales Invoice-main-field_order", diff --git a/rmax_custom/rmax_custom/custom_scripts/quotation/quotation.js b/rmax_custom/rmax_custom/custom_scripts/quotation/quotation.js index 37ff376..3cac873 100644 --- a/rmax_custom/rmax_custom/custom_scripts/quotation/quotation.js +++ b/rmax_custom/rmax_custom/custom_scripts/quotation/quotation.js @@ -1,8 +1,5 @@ frappe.ui.form.on('Quotation', { refresh(frm) { - setTimeout(() => { - frm.remove_custom_button(__('Sales Order'), __('Create')); - }, 10); if (frm.doc.docstatus === 1) { frm.add_custom_button('Sales Invoice', () => { frappe.model.open_mapped_doc({ diff --git a/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.js b/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.js deleted file mode 100644 index 7b0f825..0000000 --- a/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.js +++ /dev/null @@ -1,294 +0,0 @@ -frappe.ui.form.on("Sales Invoice", { - refresh: function (frm) { - if (frm.doc.docstatus === 0 || frm.doc.docstatus === 1) { - frm.add_custom_button(__("New Invoice"), function () { - window.open("/app/sales-invoice/new", "_blank"); - }); - } - set_pos_behavior(frm); - }, - custom_payment_mode(frm) { - set_pos_behavior(frm); - set_customer_filter(frm); - }, - onload(frm) { - set_customer_filter(frm); - }, - before_save(frm) { - if (frm.doc.docstatus !== 0) return; - if (frm.is_new()) return; - if (frm._submit_checked) return; - frappe.validated = false; - frappe.confirm( - "Do you want to Submit this Sales Invoice now?", - - function () { - frm._submit_checked = true; - frm.save('Submit'); - }, - - function () { - frm._submit_checked = true; - frm.save(); - } - ); - }, - items_add: function(frm) { - check_stock(frm); - }, - onload(frm) { - let grid = frm.fields_dict.items.grid; - grid.wrapper.off('keydown.enter_nav'); - grid.wrapper.on('keydown.enter_nav', 'input, select, textarea', function(e) { - if (e.key !== "Enter") return; - e.preventDefault(); - let $currentRow = $(this).closest('.grid-row'); - let current_docname = $currentRow.attr('data-name'); - let current_row = grid.get_row(current_docname); - if (!current_row) return; - let row_index = grid.grid_rows.indexOf(current_row); - let $inputs = $currentRow - .find('input, select, textarea') - .filter(':visible:not([readonly]):not([disabled])'); - - let index = $inputs.index(this); - if (index < $inputs.length - 1) { - $inputs.eq(index + 1).focus(); - } - else { - if (row_index < grid.grid_rows.length - 1) { - - let next_row = grid.grid_rows[row_index + 1]; - next_row.activate(); - - setTimeout(() => { - let $nextInputs = $(next_row.row) - .find('input, select, textarea') - .filter(':visible:not([readonly]):not([disabled])'); - - if ($nextInputs.length) { - $nextInputs.eq(0).focus(); - } - }, 50); - - } else { - grid.add_new_row(); - - setTimeout(() => { - let new_row = grid.grid_rows[grid.grid_rows.length - 1]; - - if (new_row) { - new_row.activate(); - - let $newInputs = $(new_row.row) - .find('input, select, textarea') - .filter(':visible:not([readonly]):not([disabled])'); - - if ($newInputs.length) { - $newInputs.eq(0).focus(); - } - } - }, 100); - } - } - }); - } -}); - - -function set_pos_behavior(frm) { - if (!frm.doc.custom_payment_mode) return; - if (frm.doc.custom_payment_mode === "Cash") { - frm.set_value("is_pos", 1); - } - else if (frm.doc.custom_payment_mode === "Credit") { - frm.set_value("is_pos", 0); - } -} - - -function set_customer_filter(frm) { - if (frm.doc.custom_payment_mode === 'Credit') { - frm.set_query('customer', function () { - return { - filters: [ - ["Customer Credit Limit", "credit_limit", ">", 0] - ] - }; - }); - } else { - frm.set_query('customer', function () { - return {}; - }); - } -} - -frappe.ui.form.on("Sales Invoice Item", { - items_add: function(frm, cdt, cdn) { - check_stock(frm, cdt, cdn); - }, - item_code: function(frm, cdt, cdn) { - check_stock(frm, cdt, cdn); - }, - qty: function(frm, cdt, cdn) { - check_stock(frm, cdt, cdn); - } -}); - -function check_stock(frm, cdt, cdn) { - let row = locals[cdt][cdn]; - if (!row || !row.item_code || !row.qty || !row.warehouse) return; - frappe.call({ - method: "erpnext.stock.utils.get_stock_balance", - args: { - item_code: row.item_code, - warehouse: row.warehouse - }, - callback: function(r) { - let stock = r.message || 0; - if (row.qty > stock) { - frappe.msgprint({ - title: "Stock Alert", - message: `Only ${stock} item${stock > 1 ? 's' : ''} are currently available in ${row.warehouse}.`, - indicator: "red" - }); - - frappe.model.set_value(cdt, cdn, "qty", stock); - } - } - }); -} - - -function add_create_customer_button(frm) { - - if (frm.doc.docstatus !== 0) return; - if (!frm.fields_dict.customer) return; - - const $field = frm.fields_dict.customer.$wrapper; - const $parent = $field.parent(); - - if ($parent.find(".create-customer-btn").length) return; - - const $btn = $(` - - `); - - $btn.on("click", function () { - open_create_customer_dialog(frm); - }); - - $field.before($btn); -} - - -function open_create_customer_dialog(frm) { - - let company = frm.doc.company || frappe.defaults.get_default("company"); - - frappe.db.get_value("Company", company, - ["country", "default_currency"], function(r) { - - let country = r.country; - let default_currency = r.default_currency; - - let d = new frappe.ui.Dialog({ - title: "Create New Customer", - fields: [ - { - fieldname: "customer_name", - fieldtype: "Data", - label: "Customer Name", - reqd: 1 - }, - { - fieldname: "mobile_no", - fieldtype: "Data", - label: "Mobile No", - reqd: 1 - }, - { - fieldname: "email_id", - fieldtype: "Data", - label: "Email ID" - }, - { fieldtype: "Section Break", label: "Address Details" }, - { - fieldname: "address_type", - fieldtype: "Select", - label: "Address Type", - options: "Billing\nShipping", - default: "Billing", - reqd: 1 - }, - { - fieldname: "address_line1", - fieldtype: "Data", - label: "Address Line 1", - reqd: 1 - }, - { - fieldname: "address_line2", - fieldtype: "Data", - label: "Address Line 2" - }, - { - fieldname: "city", - fieldtype: "Data", - label: "City/Town", - reqd: 1 - }, - { - fieldname: "country", - fieldtype: "Link", - options: "Country", - label: "Country", - default: country, - reqd: 1 - }, - - - ], - primary_action_label: "Create Customer", - primary_action(values) { - - frappe.call({ - method: "rmax_custom.rmax_custom.custom_scripts.sales_invoice.sales_invoice.create_customer_with_primary_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, - city: values.city, - country: 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(); - }); -} - - diff --git a/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.py b/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.py deleted file mode 100644 index 79109e7..0000000 --- a/rmax_custom/rmax_custom/custom_scripts/sales_invoice/sales_invoice.py +++ /dev/null @@ -1,67 +0,0 @@ -import frappe -from frappe import _ -from frappe.utils import nowdate, add_days, flt - - - - - -@frappe.whitelist() -def create_customer_with_primary_address( - customer_name, - mobile_no=None, - email_id=None, - address_type=None, - address_line1=None, - address_line2=None, - city=None, - country=None, - default_currency=None -): - - if not customer_name: - frappe.throw("Customer Name is required") - - if not mobile_no: - frappe.throw("Mobile No is required") - - if not address_line1 or not city or not country: - frappe.throw("Address details are required") - customer = frappe.get_doc({ - "doctype": "Customer", - "customer_name": customer_name, - "customer_type": "Individual", - "customer_group": "All Customer Groups", - "territory": "All Territories", - "mobile_no": mobile_no, - "email_id": email_id, - "default_currency": default_currency - }) - - customer.insert(ignore_permissions=True) - address = frappe.get_doc({ - "doctype": "Address", - "address_title": customer_name, - "address_type": address_type, - "address_line1": address_line1, - "address_line2": address_line2, - "city": city, - "country": country - }) - address.append("links", { - "link_doctype": "Customer", - "link_name": customer.name, - "link_title": customer.customer_name - }) - - address.insert(ignore_permissions=True) - customer.customer_primary_address = address.name - customer.save(ignore_permissions=True) - - return { - "customer": customer.name, - "address": address.name, - "message": "Customer and Address created successfully" - } - -