diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index e1add14eed7..0c38e83e55a 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -727,6 +727,7 @@ frappe.ui.form.on('Sales Invoice', { } }; }, +<<<<<<< HEAD // When multiple companies are set up. in case company name is changed set default company address company: function(frm){ if (frm.doc.company) { @@ -747,6 +748,9 @@ frappe.ui.form.on('Sales Invoice', { }, onload: function(frm) { +======= + onload: function (frm) { +>>>>>>> a31075692c (fix: set default company address in Sales Doctype on change of company) frm.redemption_conversion_factor = null; }, diff --git a/erpnext/public/js/utils/sales_common.js b/erpnext/public/js/utils/sales_common.js new file mode 100644 index 00000000000..ef0848f4949 --- /dev/null +++ b/erpnext/public/js/utils/sales_common.js @@ -0,0 +1,564 @@ +// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +// License: GNU General Public License v3. See license.txt + +frappe.provide("erpnext.selling"); + +erpnext.sales_common = { + setup_selling_controller: function () { + erpnext.selling.SellingController = class SellingController extends erpnext.TransactionController { + setup() { + super.setup(); + this.toggle_enable_for_stock_uom("allow_to_edit_stock_uom_qty_for_sales"); + this.frm.email_field = "contact_email"; + } + + onload() { + super.onload(); + this.setup_queries(); + this.frm.set_query("shipping_rule", function (doc) { + return { + filters: { + shipping_rule_type: "Selling", + company: doc.company, + }, + }; + }); + + this.frm.set_query("project", function (doc) { + return { + query: "erpnext.controllers.queries.get_project_name", + filters: { + customer: doc.customer, + company: doc.company, + }, + }; + }); + } + + setup_queries() { + var me = this; + + $.each( + [ + ["customer", "customer"], + ["lead", "lead"], + ], + function (i, opts) { + if (me.frm.fields_dict[opts[0]]) me.frm.set_query(opts[0], erpnext.queries[opts[1]]); + } + ); + + me.frm.set_query("contact_person", erpnext.queries.contact_query); + me.frm.set_query("company_contact_person", erpnext.queries.company_contact_query); + me.frm.set_query("customer_address", erpnext.queries.address_query); + me.frm.set_query("shipping_address_name", erpnext.queries.address_query); + me.frm.set_query("dispatch_address_name", erpnext.queries.dispatch_address_query); + me.frm.set_query("company_address", erpnext.queries.company_address_query); + + erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype); + + if (this.frm.fields_dict.selling_price_list) { + this.frm.set_query("selling_price_list", function () { + return { filters: { selling: 1 } }; + }); + } + + if (this.frm.fields_dict.tc_name) { + this.frm.set_query("tc_name", function () { + return { filters: { selling: 1 } }; + }); + } + + if (!this.frm.fields_dict["items"]) { + return; + } + + if (this.frm.fields_dict["items"].grid.get_field("item_code")) { + this.frm.set_query("item_code", "items", function () { + let customer = me.frm.doc.customer; + if (me.frm.doc.doctype == "Quotation" && me.frm.doc.quotation_to == "Customer") { + customer = me.frm.doc.party_name; + } + return { + query: "erpnext.controllers.queries.item_query", + filters: { is_sales_item: 1, customer: customer, has_variants: 0 }, + }; + }); + } + + if ( + this.frm.fields_dict["packed_items"] && + this.frm.fields_dict["packed_items"].grid.get_field("batch_no") + ) { + this.frm.set_query("batch_no", "packed_items", function (doc, cdt, cdn) { + return me.set_query_for_batch(doc, cdt, cdn); + }); + } + + if (this.frm.fields_dict["items"].grid.get_field("item_code")) { + this.frm.set_query("item_tax_template", "items", function (doc, cdt, cdn) { + return me.set_query_for_item_tax_template(doc, cdt, cdn); + }); + } + } + + refresh() { + super.refresh(); + + frappe.dynamic_link = { doc: this.frm.doc, fieldname: "customer", doctype: "Customer" }; + + this.frm.toggle_display( + "customer_name", + this.frm.doc.customer_name && this.frm.doc.customer_name !== this.frm.doc.customer + ); + + this.toggle_editable_price_list_rate(); + } + + company() { + super.company(); + this.set_default_company_address(); + } + + set_default_company_address() { + if (!frappe.meta.has_field(this.frm.doc.doctype, "company_address")) return; + var me = this; + if (this.frm.doc.company) { + frappe.call({ + method: "erpnext.setup.doctype.company.company.get_default_company_address", + args: { + name: this.frm.doc.company, + existing_address: this.frm.doc.company_address || "", + }, + debounce: 2000, + callback: function (r) { + if (r.message) { + me.frm.set_value("company_address", r.message); + } else { + me.frm.set_value("company_address", ""); + } + }, + }); + } + } + + customer() { + var me = this; + erpnext.utils.get_party_details(this.frm, null, null, function () { + me.apply_price_list(); + }); + } + + customer_address() { + erpnext.utils.get_address_display(this.frm, "customer_address"); + erpnext.utils.set_taxes_from_address( + this.frm, + "customer_address", + "customer_address", + "shipping_address_name" + ); + } + + shipping_address_name() { + erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address"); + erpnext.utils.set_taxes_from_address( + this.frm, + "shipping_address_name", + "customer_address", + "shipping_address_name" + ); + } + + dispatch_address_name() { + erpnext.utils.get_address_display(this.frm, "dispatch_address_name", "dispatch_address"); + } + + sales_partner() { + this.apply_pricing_rule(); + } + + campaign() { + this.apply_pricing_rule(); + } + + selling_price_list() { + this.apply_price_list(); + this.set_dynamic_labels(); + } + + discount_percentage(doc, cdt, cdn) { + var item = frappe.get_doc(cdt, cdn); + item.discount_amount = 0.0; + this.apply_discount_on_item(doc, cdt, cdn, "discount_percentage"); + } + + discount_amount(doc, cdt, cdn) { + if (doc.name === cdn) { + return; + } + + var item = frappe.get_doc(cdt, cdn); + item.discount_percentage = 0.0; + this.apply_discount_on_item(doc, cdt, cdn, "discount_amount"); + } + + commission_rate() { + this.calculate_commission(); + } + + total_commission() { + frappe.model.round_floats_in(this.frm.doc, [ + "amount_eligible_for_commission", + "total_commission", + ]); + + const { amount_eligible_for_commission } = this.frm.doc; + if (!amount_eligible_for_commission) return; + + this.frm.set_value( + "commission_rate", + flt((this.frm.doc.total_commission * 100.0) / amount_eligible_for_commission) + ); + } + + allocated_percentage(doc, cdt, cdn) { + var sales_person = frappe.get_doc(cdt, cdn); + if (sales_person.allocated_percentage) { + sales_person.allocated_percentage = flt( + sales_person.allocated_percentage, + precision("allocated_percentage", sales_person) + ); + + sales_person.allocated_amount = flt( + (this.frm.doc.amount_eligible_for_commission * sales_person.allocated_percentage) / + 100.0, + precision("allocated_amount", sales_person) + ); + refresh_field(["allocated_amount"], sales_person); + + this.calculate_incentive(sales_person); + refresh_field( + ["allocated_percentage", "allocated_amount", "commission_rate", "incentives"], + sales_person.name, + sales_person.parentfield + ); + } + } + + sales_person(doc, cdt, cdn) { + var row = frappe.get_doc(cdt, cdn); + this.calculate_incentive(row); + refresh_field("incentives", row.name, row.parentfield); + } + + warehouse(doc, cdt, cdn) { + if (doc.docstatus === 0 && doc.is_return && !doc.return_against) { + frappe.model.set_value(cdt, cdn, "incoming_rate", 0.0); + } + + this.set_actual_qty(doc, cdt, cdn); + } + + set_actual_qty(doc, cdt, cdn) { + let row = locals[cdt][cdn]; + let sales_doctypes = ["Sales Invoice", "Delivery Note", "Sales Order"]; + + if (row.item_code && row.warehouse && sales_doctypes.includes(doc.doctype)) { + frappe.call({ + method: "erpnext.stock.get_item_details.get_bin_details", + args: { + item_code: row.item_code, + warehouse: row.warehouse, + }, + callback(r) { + if (r.message) { + frappe.model.set_value(cdt, cdn, "actual_qty", r.message.actual_qty); + } + }, + }); + } + } + + toggle_editable_price_list_rate() { + var df = frappe.meta.get_docfield( + this.frm.doc.doctype + " Item", + "price_list_rate", + this.frm.doc.name + ); + var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate")); + + if (df && editable_price_list_rate) { + const parent_field = frappe.meta.get_parentfield( + this.frm.doc.doctype, + this.frm.doc.doctype + " Item" + ); + if (!this.frm.fields_dict[parent_field]) return; + + this.frm.fields_dict[parent_field].grid.update_docfield_property( + "price_list_rate", + "read_only", + 0 + ); + } + } + + calculate_commission() { + if (!this.frm.fields_dict.commission_rate || this.frm.doc.docstatus === 1) return; + + if (this.frm.doc.commission_rate > 100) { + this.frm.set_value("commission_rate", 100); + frappe.throw( + `${__( + frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name) + )} ${__("cannot be greater than 100")}` + ); + } + + this.frm.doc.amount_eligible_for_commission = this.frm.doc.items.reduce( + (sum, item) => (item.grant_commission ? sum + item.base_net_amount : sum), + 0 + ); + + this.frm.doc.total_commission = flt( + (this.frm.doc.amount_eligible_for_commission * this.frm.doc.commission_rate) / 100.0, + precision("total_commission") + ); + + refresh_field(["amount_eligible_for_commission", "total_commission"]); + } + + calculate_contribution() { + var me = this; + $.each(this.frm.doc.doctype.sales_team || [], function (i, sales_person) { + frappe.model.round_floats_in(sales_person); + if (!sales_person.allocated_percentage) return; + + sales_person.allocated_amount = flt( + (me.frm.doc.amount_eligible_for_commission * sales_person.allocated_percentage) / + 100.0, + precision("allocated_amount", sales_person) + ); + }); + } + + calculate_incentive(row) { + if (row.allocated_amount) { + row.incentives = flt( + (row.allocated_amount * row.commission_rate) / 100.0, + precision("incentives", row) + ); + } + } + + set_dynamic_labels() { + super.set_dynamic_labels(); + this.set_product_bundle_help(this.frm.doc); + } + + set_product_bundle_help(doc) { + if (!this.frm.fields_dict.packing_list) return; + if ((doc.packed_items || []).length) { + $(this.frm.fields_dict.packing_list.row.wrapper).toggle(true); + + if (["Delivery Note", "Sales Invoice"].includes(doc.doctype)) { + var help_msg = + "