2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-01-02 06:17:23 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.provide("erpnext.utils");
|
2016-04-12 13:46:33 +00:00
|
|
|
|
2022-07-29 07:50:34 +00:00
|
|
|
const SALES_DOCTYPES = ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice'];
|
|
|
|
const PURCHASE_DOCTYPES = ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'];
|
|
|
|
|
2014-05-28 07:19:20 +00:00
|
|
|
erpnext.utils.get_party_details = function(frm, method, args, callback) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!method) {
|
2014-02-03 10:44:56 +00:00
|
|
|
method = "erpnext.accounts.party.get_party_details";
|
2014-01-28 12:13:10 +00:00
|
|
|
}
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!args) {
|
|
|
|
if ((frm.doctype != "Purchase Order" && frm.doc.customer)
|
2019-07-03 05:04:31 +00:00
|
|
|
|| (frm.doc.party_name && in_list(['Quotation', 'Opportunity'], frm.doc.doctype))) {
|
|
|
|
|
|
|
|
let party_type = "Customer";
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.doc.quotation_to && frm.doc.quotation_to === "Lead") {
|
2019-07-03 05:04:31 +00:00
|
|
|
party_type = "Lead";
|
|
|
|
}
|
|
|
|
|
2014-04-23 13:45:15 +00:00
|
|
|
args = {
|
2019-07-03 05:04:31 +00:00
|
|
|
party: frm.doc.customer || frm.doc.party_name,
|
|
|
|
party_type: party_type,
|
2014-01-29 09:56:04 +00:00
|
|
|
price_list: frm.doc.selling_price_list
|
|
|
|
};
|
2020-07-03 16:33:25 +00:00
|
|
|
} else if (frm.doc.supplier) {
|
2014-04-23 13:45:15 +00:00
|
|
|
args = {
|
2014-02-03 10:44:56 +00:00
|
|
|
party: frm.doc.supplier,
|
|
|
|
party_type: "Supplier",
|
2018-02-16 07:35:21 +00:00
|
|
|
bill_date: frm.doc.bill_date,
|
2014-01-29 09:56:04 +00:00
|
|
|
price_list: frm.doc.buying_price_list
|
|
|
|
};
|
2014-01-28 12:13:10 +00:00
|
|
|
}
|
2016-02-01 10:02:32 +00:00
|
|
|
|
2022-07-29 07:50:34 +00:00
|
|
|
if (!args) {
|
|
|
|
if (in_list(SALES_DOCTYPES, frm.doc.doctype)) {
|
2019-12-10 10:25:05 +00:00
|
|
|
args = {
|
|
|
|
party: frm.doc.customer || frm.doc.party_name,
|
|
|
|
party_type: 'Customer'
|
2022-07-29 07:50:34 +00:00
|
|
|
};
|
2019-12-10 10:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 07:50:34 +00:00
|
|
|
if (in_list(PURCHASE_DOCTYPES, frm.doc.doctype)) {
|
2019-12-10 10:25:05 +00:00
|
|
|
args = {
|
|
|
|
party: frm.doc.supplier,
|
|
|
|
party_type: 'Supplier'
|
2022-07-29 07:50:34 +00:00
|
|
|
};
|
2019-12-10 10:25:05 +00:00
|
|
|
}
|
2022-07-29 07:50:34 +00:00
|
|
|
}
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-07-29 07:50:34 +00:00
|
|
|
if (!args || !args.party) return;
|
|
|
|
|
|
|
|
args.posting_date = frm.doc.posting_date || frm.doc.transaction_date;
|
|
|
|
args.fetch_payment_terms_template = cint(!frm.doc.ignore_default_payment_terms_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_list(SALES_DOCTYPES, frm.doc.doctype)) {
|
|
|
|
if (!args.company_address && frm.doc.company_address) {
|
|
|
|
args.company_address = frm.doc.company_address;
|
2019-12-10 10:25:05 +00:00
|
|
|
}
|
2022-07-29 07:50:34 +00:00
|
|
|
}
|
2019-12-10 10:25:05 +00:00
|
|
|
|
2022-07-29 07:50:34 +00:00
|
|
|
if (in_list(PURCHASE_DOCTYPES, frm.doc.doctype)) {
|
|
|
|
if (!args.company_address && frm.doc.billing_address) {
|
|
|
|
args.company_address = frm.doc.billing_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args.shipping_address && frm.doc.shipping_address) {
|
|
|
|
args.shipping_address = frm.doc.shipping_address;
|
2015-10-12 10:53:51 +00:00
|
|
|
}
|
2014-01-28 12:13:10 +00:00
|
|
|
}
|
2022-07-29 07:50:34 +00:00
|
|
|
|
2016-02-01 10:02:32 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frappe.meta.get_docfield(frm.doc.doctype, "taxes")) {
|
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Posting / Transaction Date",
|
2015-10-23 10:40:56 +00:00
|
|
|
args.posting_date, args.party_type=="Customer" ? "customer": "supplier")) return;
|
|
|
|
}
|
2016-02-01 10:02:32 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Company", frm.doc.company, args.party_type=="Customer" ? "customer": "supplier")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-03 10:44:56 +00:00
|
|
|
args.currency = frm.doc.currency;
|
|
|
|
args.company = frm.doc.company;
|
2014-06-18 11:10:27 +00:00
|
|
|
args.doctype = frm.doc.doctype;
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2014-01-02 06:17:23 +00:00
|
|
|
method: method,
|
|
|
|
args: args,
|
|
|
|
callback: function(r) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (r.message) {
|
2018-08-01 12:15:05 +00:00
|
|
|
frm.supplier_tds = r.message.supplier_tds;
|
2014-01-28 12:13:10 +00:00
|
|
|
frm.updating_party_details = true;
|
2017-07-17 10:01:17 +00:00
|
|
|
frappe.run_serially([
|
|
|
|
() => frm.set_value(r.message),
|
|
|
|
() => {
|
|
|
|
frm.updating_party_details = false;
|
2020-07-03 16:33:25 +00:00
|
|
|
if (callback) callback();
|
2017-07-17 10:01:17 +00:00
|
|
|
frm.refresh();
|
|
|
|
erpnext.utils.add_item(frm);
|
|
|
|
}
|
|
|
|
]);
|
2014-01-02 06:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:33:46 +00:00
|
|
|
erpnext.utils.add_item = function(frm) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.is_new()) {
|
2017-02-23 11:33:46 +00:00
|
|
|
var prev_route = frappe.get_prev_route();
|
2020-07-03 16:33:25 +00:00
|
|
|
if (prev_route[1]==='Item' && !(frm.doc.items && frm.doc.items.length)) {
|
2017-02-23 11:33:46 +00:00
|
|
|
// add row
|
2017-05-30 07:24:42 +00:00
|
|
|
var item = frm.add_child('items');
|
2017-02-23 11:33:46 +00:00
|
|
|
frm.refresh_field('items');
|
|
|
|
|
|
|
|
// set item
|
|
|
|
frappe.model.set_value(item.doctype, item.name, 'item_code', prev_route[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:52:03 +00:00
|
|
|
erpnext.utils.get_address_display = function(frm, address_field, display_field, is_your_company_address) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.updating_party_details) return;
|
2016-02-01 10:02:32 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!address_field) {
|
|
|
|
if (frm.doctype != "Purchase Order" && frm.doc.customer) {
|
2014-01-28 12:13:10 +00:00
|
|
|
address_field = "customer_address";
|
2020-07-03 16:33:25 +00:00
|
|
|
} else if (frm.doc.supplier) {
|
2014-01-28 12:13:10 +00:00
|
|
|
address_field = "supplier_address";
|
2014-02-19 12:13:24 +00:00
|
|
|
} else return;
|
2014-04-23 13:45:15 +00:00
|
|
|
}
|
2016-02-01 10:02:32 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!display_field) display_field = "address_display";
|
|
|
|
if (frm.doc[address_field]) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2017-06-13 09:56:35 +00:00
|
|
|
method: "frappe.contacts.doctype.address.address.get_address_display",
|
2014-02-19 12:13:24 +00:00
|
|
|
args: {"address_dict": frm.doc[address_field] },
|
2014-01-29 14:21:36 +00:00
|
|
|
callback: function(r) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (r.message) {
|
2014-02-19 12:13:24 +00:00
|
|
|
frm.set_value(display_field, r.message)
|
2015-08-26 05:11:31 +00:00
|
|
|
}
|
2014-01-29 14:21:36 +00:00
|
|
|
}
|
|
|
|
})
|
2016-03-16 10:52:03 +00:00
|
|
|
} else {
|
2017-04-14 12:20:32 +00:00
|
|
|
frm.set_value(display_field, '');
|
2014-01-29 14:21:36 +00:00
|
|
|
}
|
2016-04-08 12:06:10 +00:00
|
|
|
};
|
|
|
|
|
2018-12-28 20:49:11 +00:00
|
|
|
erpnext.utils.set_taxes_from_address = function(frm, triggered_from_field, billing_address_field, shipping_address_field) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.updating_party_details) return;
|
2018-12-28 20:49:11 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frappe.meta.get_docfield(frm.doc.doctype, "taxes")) {
|
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Lead / Customer / Supplier",
|
2019-07-03 05:04:31 +00:00
|
|
|
frm.doc.customer || frm.doc.supplier || frm.doc.lead || frm.doc.party_name, triggered_from_field)) {
|
2018-12-28 20:49:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Posting / Transaction Date",
|
2018-12-28 20:49:11 +00:00
|
|
|
frm.doc.posting_date || frm.doc.transaction_date, triggered_from_field)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.accounts.party.get_address_tax_category",
|
|
|
|
args: {
|
|
|
|
"tax_category": frm.doc.tax_category,
|
|
|
|
"billing_address": frm.doc[billing_address_field],
|
|
|
|
"shipping_address": frm.doc[shipping_address_field]
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!r.exc){
|
|
|
|
if (frm.doc.tax_category != r.message) {
|
2018-12-28 20:49:11 +00:00
|
|
|
frm.set_value("tax_category", r.message);
|
|
|
|
} else {
|
|
|
|
erpnext.utils.set_taxes(frm, triggered_from_field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
erpnext.utils.set_taxes = function(frm, triggered_from_field) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frappe.meta.get_docfield(frm.doc.doctype, "taxes")) {
|
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Company", frm.doc.company, triggered_from_field)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Lead / Customer / Supplier",
|
2019-07-15 07:14:11 +00:00
|
|
|
frm.doc.customer || frm.doc.supplier || frm.doc.lead || frm.doc.party_name, triggered_from_field)) {
|
2017-05-30 07:24:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-04-11 06:40:20 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!erpnext.utils.validate_mandatory(frm, "Posting / Transaction Date",
|
2018-12-26 21:11:07 +00:00
|
|
|
frm.doc.posting_date || frm.doc.transaction_date, triggered_from_field)) {
|
2016-04-08 12:06:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var party_type, party;
|
|
|
|
if (frm.doc.lead) {
|
|
|
|
party_type = 'Lead';
|
|
|
|
party = frm.doc.lead;
|
|
|
|
} else if (frm.doc.customer) {
|
|
|
|
party_type = 'Customer';
|
|
|
|
party = frm.doc.customer;
|
|
|
|
} else if (frm.doc.supplier) {
|
|
|
|
party_type = 'Supplier';
|
|
|
|
party = frm.doc.supplier;
|
2019-07-03 05:04:31 +00:00
|
|
|
} else if (frm.doc.quotation_to){
|
|
|
|
party_type = frm.doc.quotation_to;
|
|
|
|
party = frm.doc.party_name;
|
2016-04-08 12:06:10 +00:00
|
|
|
}
|
2016-04-11 12:04:25 +00:00
|
|
|
|
2020-09-15 09:17:36 +00:00
|
|
|
if (!frm.doc.company) {
|
2020-09-19 12:35:58 +00:00
|
|
|
frappe.throw(__("Kindly select the company first"));
|
2020-09-15 09:17:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 12:06:10 +00:00
|
|
|
frappe.call({
|
|
|
|
method: "erpnext.accounts.party.set_taxes",
|
|
|
|
args: {
|
|
|
|
"party": party,
|
|
|
|
"party_type": party_type,
|
|
|
|
"posting_date": frm.doc.posting_date || frm.doc.transaction_date,
|
|
|
|
"company": frm.doc.company,
|
2019-01-06 13:14:18 +00:00
|
|
|
"customer_group": frm.doc.customer_group,
|
|
|
|
"supplier_group": frm.doc.supplier_group,
|
2018-12-26 21:11:07 +00:00
|
|
|
"tax_category": frm.doc.tax_category,
|
2016-04-08 12:06:10 +00:00
|
|
|
"billing_address": ((frm.doc.customer || frm.doc.lead) ? (frm.doc.customer_address) : (frm.doc.supplier_address)),
|
|
|
|
"shipping_address": frm.doc.shipping_address_name
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (r.message){
|
2016-04-08 12:06:10 +00:00
|
|
|
frm.set_value("taxes_and_charges", r.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-26 21:11:07 +00:00
|
|
|
};
|
2014-01-02 06:17:23 +00:00
|
|
|
|
|
|
|
erpnext.utils.get_contact_details = function(frm) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.updating_party_details) return;
|
2014-04-23 13:45:15 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (frm.doc["contact_person"]) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.call({
|
2017-06-13 09:56:35 +00:00
|
|
|
method: "frappe.contacts.doctype.contact.contact.get_contact_details",
|
2014-01-29 14:21:36 +00:00
|
|
|
args: {contact: frm.doc.contact_person },
|
|
|
|
callback: function(r) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (r.message)
|
2014-01-29 14:21:36 +00:00
|
|
|
frm.set_value(r.message);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-04-23 13:45:15 +00:00
|
|
|
}
|
2015-10-23 10:40:56 +00:00
|
|
|
|
|
|
|
erpnext.utils.validate_mandatory = function(frm, label, value, trigger_on) {
|
2020-07-03 16:33:25 +00:00
|
|
|
if (!value) {
|
2015-10-23 10:40:56 +00:00
|
|
|
frm.doc[trigger_on] = "";
|
|
|
|
refresh_field(trigger_on);
|
2020-07-03 16:33:25 +00:00
|
|
|
frappe.throw({message:__("Please enter {0} first", [label]), title:__("Mandatory")});
|
2015-10-23 10:40:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-02-01 10:02:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 04:24:12 +00:00
|
|
|
erpnext.utils.get_shipping_address = function(frm, callback) {
|
2018-08-27 06:35:41 +00:00
|
|
|
if (frm.doc.company) {
|
2021-06-24 11:59:55 +00:00
|
|
|
if ((frm.doc.inter_company_order_reference || frm.doc.internal_invoice_reference ||
|
2021-01-28 07:39:56 +00:00
|
|
|
frm.doc.internal_order_reference)) {
|
|
|
|
if (callback) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 05:30:24 +00:00
|
|
|
frappe.call({
|
2020-10-14 14:39:05 +00:00
|
|
|
method: "erpnext.accounts.custom.address.get_shipping_address",
|
2018-06-04 05:30:24 +00:00
|
|
|
args: {
|
|
|
|
company: frm.doc.company,
|
|
|
|
address: frm.doc.shipping_address
|
|
|
|
},
|
2021-08-30 12:23:59 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if (r.message) {
|
2018-06-04 05:30:24 +00:00
|
|
|
frm.set_value("shipping_address", r.message[0]) //Address title or name
|
|
|
|
frm.set_value("shipping_address_display", r.message[1]) //Address to be displayed on the page
|
|
|
|
}
|
2016-04-11 06:40:20 +00:00
|
|
|
|
2020-07-03 16:33:25 +00:00
|
|
|
if (callback){
|
2018-06-04 05:30:24 +00:00
|
|
|
return callback();
|
|
|
|
}
|
2016-04-11 06:40:20 +00:00
|
|
|
}
|
2018-06-04 05:30:24 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
frappe.msgprint(__("Select company first"));
|
|
|
|
}
|
2020-09-19 12:35:58 +00:00
|
|
|
}
|