blob: 980a4d2819b1384e08066b22b6ce2185335a3b4c [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05303frappe.provide("erpnext");
Nabin Haitb072c742014-12-16 12:49:13 +05304frappe.provide("erpnext.utils");
rohitwaghchaure0da19a42024-03-12 12:27:21 +05305frappe.provide("erpnext.stock.utils");
Anand Doshie65e6212012-11-19 15:43:18 +05306
Anand Doshibdee6e02013-07-09 13:03:39 +05307$.extend(erpnext, {
Ankush Menatec74a5e2024-03-10 19:45:40 +05308 get_currency: function (company) {
9 if (!company && cur_frm) company = cur_frm.doc.company;
10 if (company)
ruthra kumarfa2d33c2023-12-12 15:43:39 +053011 return frappe.get_doc(":Company", company)?.default_currency || frappe.boot.sysdefaults.currency;
Ankush Menatec74a5e2024-03-10 19:45:40 +053012 else return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053013 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053014
tundebabzyc8978252018-02-12 10:34:50 +010015 get_presentation_currency_list: () => {
16 const docs = frappe.boot.docs;
Ankush Menatec74a5e2024-03-10 19:45:40 +053017 let currency_list = docs.filter((d) => d.doctype === ":Currency").map((d) => d.name);
Zarrare25dcd22018-06-21 10:53:12 +053018 currency_list.unshift("");
tundebabzyc8978252018-02-12 10:34:50 +010019 return currency_list;
20 },
21
Ankush Menatec74a5e2024-03-10 19:45:40 +053022 toggle_naming_series: function () {
23 if (cur_frm && cur_frm.fields_dict.naming_series) {
24 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal ? true : false);
Anand Doshibdee6e02013-07-09 13:03:39 +053025 }
26 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053027
Ankush Menatec74a5e2024-03-10 19:45:40 +053028 hide_company: function (frm) {
29 if (frm?.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053030 var companies = Object.keys(locals[":Company"] || {});
Ankush Menatec74a5e2024-03-10 19:45:40 +053031 if (companies.length === 1) {
32 if (!frm.doc.company) frm.set_value("company", companies[0]);
Gursheen Anandceeb8fc2024-02-27 13:05:29 +053033 frm.toggle_display("company", false);
Ankush Menatec74a5e2024-03-10 19:45:40 +053034 } else if (erpnext.last_selected_company) {
35 if (!frm.doc.company) frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053036 }
37 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053038 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053039
Ankush Menatec74a5e2024-03-10 19:45:40 +053040 is_perpetual_inventory_enabled: function (company) {
41 if (company) {
Rohit Waghchaure44ed52c2024-02-24 15:33:59 +053042 let company_local = locals[":Company"] && locals[":Company"][company];
Ankush Menatec74a5e2024-03-10 19:45:40 +053043 if (company_local) {
Rohit Waghchaure44ed52c2024-02-24 15:33:59 +053044 return cint(company_local.enable_perpetual_inventory);
45 }
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053046 }
47 },
48
Makarand Bauskare6712c12017-10-25 12:17:40 +053049 stale_rate_allowed: () => {
rohitwaghchaure7677ff02017-11-02 18:12:14 +053050 return cint(frappe.boot.sysdefaults.allow_stale);
Makarand Bauskare6712c12017-10-25 12:17:40 +053051 },
52
Ankush Menatec74a5e2024-03-10 19:45:40 +053053 setup_serial_or_batch_no: function () {
Rohit Waghchaure732b6e12024-04-17 12:43:31 +053054 // Deprecated in v15
deepeshgarg007e64c3572019-05-14 08:50:45 +053055 },
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053056
57 route_to_adjustment_jv: (args) => {
Ankush Menatec74a5e2024-03-10 19:45:40 +053058 frappe.model.with_doctype("Journal Entry", () => {
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053059 // route to adjustment Journal Entry to handle Account Balance and Stock Value mismatch
Ankush Menatec74a5e2024-03-10 19:45:40 +053060 let journal_entry = frappe.model.get_new_doc("Journal Entry");
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053061
62 args.accounts.forEach((je_account) => {
63 let child_row = frappe.model.add_child(journal_entry, "accounts");
64 child_row.account = je_account.account;
65 child_row.debit_in_account_currency = je_account.debit_in_account_currency;
66 child_row.credit_in_account_currency = je_account.credit_in_account_currency;
Ankush Menatec74a5e2024-03-10 19:45:40 +053067 child_row.party_type = "";
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053068 });
Ankush Menatec74a5e2024-03-10 19:45:40 +053069 frappe.set_route("Form", "Journal Entry", journal_entry.name);
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053070 });
Frappe PR Bot255b99e2021-08-24 20:19:22 +053071 },
72
Ankush Menatd37541d2021-12-10 12:04:10 +053073 route_to_pending_reposts: (args) => {
Ankush Menatec74a5e2024-03-10 19:45:40 +053074 frappe.set_route("List", "Repost Item Valuation", args);
Ankush Menatd37541d2021-12-10 12:04:10 +053075 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053076});
Rushabh Mehta7d368752014-11-24 14:16:47 +053077
Nabin Haitb072c742014-12-16 12:49:13 +053078$.extend(erpnext.utils, {
Ankush Menatec74a5e2024-03-10 19:45:40 +053079 set_party_dashboard_indicators: function (frm) {
80 if (frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053081 var company_wise_info = frm.doc.__onload.dashboard_info;
Ankush Menatec74a5e2024-03-10 19:45:40 +053082 if (company_wise_info.length > 1) {
83 company_wise_info.forEach(function (info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053084 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053085 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053086 } else if (company_wise_info.length === 1) {
Ankush Menatec74a5e2024-03-10 19:45:40 +053087 frm.dashboard.add_indicator(
88 __("Annual Billing: {0}", [
89 format_currency(
90 company_wise_info[0].billing_this_year,
91 company_wise_info[0].currency
92 ),
93 ]),
94 "blue"
95 );
96 frm.dashboard.add_indicator(
97 __("Total Unpaid: {0}", [
98 format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency),
99 ]),
100 company_wise_info[0].total_unpaid ? "orange" : "green"
101 );
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530102
Ankush Menatec74a5e2024-03-10 19:45:40 +0530103 if (company_wise_info[0].loyalty_points) {
104 frm.dashboard.add_indicator(
105 __("Loyalty Points: {0}", [company_wise_info[0].loyalty_points]),
106 "blue"
107 );
deepeshgarg00764238ee2018-12-25 16:28:39 +0530108 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530109 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530110 }
111 },
112
Ankush Menatec74a5e2024-03-10 19:45:40 +0530113 view_serial_batch_nos: function (frm) {
rohitwaghchaure3c15fea2023-08-24 17:24:44 +0530114 if (!frm.doc?.items) {
115 return;
116 }
117
Ankush Menatec74a5e2024-03-10 19:45:40 +0530118 let bundle_ids = frm.doc.items.filter((d) => d.serial_and_batch_bundle);
Rohit Waghchaure708eefb2023-07-10 16:54:55 +0530119
120 if (bundle_ids?.length) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530121 frm.add_custom_button(
122 __("Serial / Batch Nos"),
123 () => {
124 frappe.route_options = {
125 voucher_no: frm.doc.name,
126 voucher_type: frm.doc.doctype,
127 from_date: frm.doc.posting_date || frm.doc.transaction_date,
128 to_date: frm.doc.posting_date || frm.doc.transaction_date,
129 company: frm.doc.company,
130 };
131 frappe.set_route("query-report", "Serial and Batch Summary");
132 },
133 __("View")
134 );
Rohit Waghchaure708eefb2023-07-10 16:54:55 +0530135 }
136 },
137
Ankush Menatec74a5e2024-03-10 19:45:40 +0530138 add_indicator_for_multicompany: function (frm, info) {
Deepak867f2c62022-05-17 11:52:52 +0530139 frm.dashboard.stats_area.show();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530140 frm.dashboard.stats_area_row.addClass("flex");
141 frm.dashboard.stats_area_row.css("flex-wrap", "wrap");
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530142
Ankush Menatec74a5e2024-03-10 19:45:40 +0530143 var color = info.total_unpaid ? "orange" : "green";
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530144
Ankush Menatec74a5e2024-03-10 19:45:40 +0530145 var indicator = $(
146 '<div class="flex-column col-xs-6">' +
147 '<div style="margin-top:10px"><h6>' +
148 info.company +
149 "</h6></div>" +
150 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">' +
151 "Annual Billing: " +
152 format_currency(info.billing_this_year, info.currency) +
153 "</span></div>" +
154 '<div class="badge-link small" style="margin-bottom:10px">' +
155 '<span class="indicator ' +
156 color +
157 '">Total Unpaid: ' +
158 format_currency(info.total_unpaid, info.currency) +
159 "</span></div>" +
160 "</div>"
161 ).appendTo(frm.dashboard.stats_area_row);
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530162
Ankush Menatec74a5e2024-03-10 19:45:40 +0530163 if (info.loyalty_points) {
164 $(
165 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">' +
166 "Loyalty Points: " +
167 info.loyalty_points +
168 "</span></div>"
169 ).appendTo(indicator);
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530170 }
171
172 return indicator;
173 },
174
Ankush Menatec74a5e2024-03-10 19:45:40 +0530175 get_party_name: function (party_type) {
176 var dict = {
177 Customer: "customer_name",
178 Supplier: "supplier_name",
179 Employee: "employee_name",
180 Member: "member_name",
181 };
Shreya Shah149f7ee2018-03-27 11:29:25 +0530182 return dict[party_type];
183 },
184
Ankush Menatec74a5e2024-03-10 19:45:40 +0530185 copy_value_in_all_rows: function (doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530186 var d = locals[dt][dn];
Ankush Menatec74a5e2024-03-10 19:45:40 +0530187 if (d[fieldname]) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530188 var cl = doc[table_fieldname] || [];
Ankush Menatec74a5e2024-03-10 19:45:40 +0530189 for (var i = 0; i < cl.length; i++) {
190 if (!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530191 }
192 }
193 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530194 },
195
Ankush Menatec74a5e2024-03-10 19:45:40 +0530196 get_terms: function (tc_name, doc, callback) {
197 if (tc_name) {
Makarand Bauskar157c3342017-05-26 21:32:33 +0530198 return frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530199 method: "erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions",
Makarand Bauskar157c3342017-05-26 21:32:33 +0530200 args: {
201 template_name: tc_name,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530202 doc: doc,
Makarand Bauskar157c3342017-05-26 21:32:33 +0530203 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530204 callback: function (r) {
205 callback(r);
206 },
Makarand Bauskar157c3342017-05-26 21:32:33 +0530207 });
208 }
tundebabzy1a947db2017-08-29 13:48:27 +0100209 },
210
Ankush Menatec74a5e2024-03-10 19:45:40 +0530211 make_bank_account: function (doctype, docname) {
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530212 frappe.call({
213 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
214 args: {
215 doctype: doctype,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530216 docname: docname,
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530217 },
218 freeze: true,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530219 callback: function (r) {
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530220 var doclist = frappe.model.sync(r.message);
221 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530222 },
223 });
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530224 },
225
Ankush Menatec74a5e2024-03-10 19:45:40 +0530226 add_dimensions: function (report_name, index) {
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530227 let filters = frappe.query_reports[report_name].filters;
228
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530229 frappe.call({
230 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
Ankush Menatec74a5e2024-03-10 19:45:40 +0530231 callback: function (r) {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530232 let accounting_dimensions = r.message[0];
233 accounting_dimensions.forEach((dimension) => {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530234 let found = filters.some((el) => el.fieldname === dimension["fieldname"]);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530235
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530236 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530237 filters.splice(index, 0, {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530238 fieldname: dimension["fieldname"],
239 label: __(dimension["label"]),
240 fieldtype: "MultiSelectList",
241 get_data: function (txt) {
Rohit Waghchaure69be22b2022-05-14 17:19:34 +0530242 return frappe.db.get_link_options(dimension["document_type"], txt);
243 },
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530244 });
245 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530246 });
Ankush Menatec74a5e2024-03-10 19:45:40 +0530247 },
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530248 });
249 },
250
Ankush Menatec74a5e2024-03-10 19:45:40 +0530251 add_inventory_dimensions: function (report_name, index) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530252 let filters = frappe.query_reports[report_name].filters;
253
254 frappe.call({
255 method: "erpnext.stock.doctype.inventory_dimension.inventory_dimension.get_inventory_dimensions",
Ankush Menatec74a5e2024-03-10 19:45:40 +0530256 callback: function (r) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530257 if (r.message && r.message.length) {
258 r.message.forEach((dimension) => {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530259 let existing_filter = filters.filter((el) => el.fieldname === dimension["fieldname"]);
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530260
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530261 if (!existing_filter.length) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530262 filters.splice(index, 0, {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530263 fieldname: dimension["fieldname"],
264 label: __(dimension["doctype"]),
265 fieldtype: "MultiSelectList",
266 get_data: function (txt) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530267 return frappe.db.get_link_options(dimension["doctype"], txt);
268 },
269 });
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530270 } else {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530271 existing_filter[0]["fieldtype"] = "MultiSelectList";
272 existing_filter[0]["get_data"] = function (txt) {
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530273 return frappe.db.get_link_options(dimension["doctype"], txt);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530274 };
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530275 }
276 });
277 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530278 },
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530279 });
280 },
281
Ankush Menatec74a5e2024-03-10 19:45:40 +0530282 make_subscription: function (doctype, docname) {
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530283 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530284 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530285 args: {
286 doctype: doctype,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530287 docname: docname,
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530288 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530289 callback: function (r) {
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530290 var doclist = frappe.model.sync(r.message);
291 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530292 },
293 });
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530294 },
295
Ankush Menatec74a5e2024-03-10 19:45:40 +0530296 make_pricing_rule: function (doctype, docname) {
Saif90cf2dd2018-09-30 21:46:31 +0500297 frappe.call({
298 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
299 args: {
300 doctype: doctype,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530301 docname: docname,
Saif90cf2dd2018-09-30 21:46:31 +0500302 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530303 callback: function (r) {
Saif90cf2dd2018-09-30 21:46:31 +0500304 var doclist = frappe.model.sync(r.message);
305 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530306 },
307 });
Saif90cf2dd2018-09-30 21:46:31 +0500308 },
309
tundebabzy1a947db2017-08-29 13:48:27 +0100310 /**
Ankush Menatec74a5e2024-03-10 19:45:40 +0530311 * Checks if the first row of a given child table is empty
312 * @param child_table - Child table Doctype
313 * @return {Boolean}
314 **/
315 first_row_is_empty: function (child_table) {
316 if ($.isArray(child_table) && child_table.length > 0) {
tundebabzy1a947db2017-08-29 13:48:27 +0100317 return !child_table[0].item_code;
318 }
319 return false;
320 },
321
322 /**
Ankush Menatec74a5e2024-03-10 19:45:40 +0530323 * Removes the first row of a child table if it is empty
324 * @param {_Frm} frm - The current form
325 * @param {String} child_table_name - The child table field name
326 * @return {Boolean}
327 **/
328 remove_empty_first_row: function (frm, child_table_name) {
329 const rows = frm["doc"][child_table_name];
330 if (this.first_row_is_empty(rows)) {
331 frm["doc"][child_table_name] = rows.splice(1);
tundebabzy1a947db2017-08-29 13:48:27 +0100332 }
333 return rows;
334 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530335 get_tree_options: function (option) {
Zarrar6ffdf942018-06-14 12:24:16 +0530336 // get valid options for tree based on user permission & locals dict
337 let unscrub_option = frappe.model.unscrub(option);
338 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530339 let options;
340
Ankush Menatec74a5e2024-03-10 19:45:40 +0530341 if (user_permission && user_permission[unscrub_option]) {
342 options = user_permission[unscrub_option].map((perm) => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530343 } else {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530344 options = $.map(locals[`:${unscrub_option}`], function (c) {
345 return c.name;
346 }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530347 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530348
349 // filter unique values, as there may be multiple user permissions for any value
350 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530351 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530352 get_tree_default: function (option) {
Zarrar6ffdf942018-06-14 12:24:16 +0530353 // set default for a field based on user permission
354 let options = this.get_tree_options(option);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530355 if (options.includes(frappe.defaults.get_default(option))) {
Zarrar6ffdf942018-06-14 12:24:16 +0530356 return frappe.defaults.get_default(option);
357 } else {
358 return options[0];
359 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400360 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530361 overrides_parent_value_in_all_rows: function (doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530362 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200363 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530364 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400365 cl[i][fieldname] = doc[parent_fieldname];
366 }
Andy Zhubffe9332021-04-12 22:49:26 +1200367 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400368 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400369 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530370 create_new_doc: function (doctype, update_fields) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530371 frappe.model.with_doctype(doctype, function () {
Nabin Hait34c551d2019-07-03 10:34:31 +0530372 var new_doc = frappe.model.get_new_doc(doctype);
373 for (let [key, value] of Object.entries(update_fields)) {
374 new_doc[key] = value;
375 }
376 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
377 });
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530378 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530379
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530380 // check if payments app is installed on site, if not warn user.
381 check_payments_app: () => {
382 if (frappe.boot.versions && !frappe.boot.versions.payments) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530383 const marketplace_link =
384 '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>';
385 const github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>';
386 const msg = __("payments app is not installed. Please install it from {0} or {1}", [
387 marketplace_link,
388 github_link,
389 ]);
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530390 frappe.msgprint(msg);
391 }
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530392 },
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530393
394 pick_serial_and_batch_bundle(frm, cdt, cdn, type_of_transaction, warehouse_field) {
395 let item_row = frappe.get_doc(cdt, cdn);
396 item_row.type_of_transaction = type_of_transaction;
397
Ankush Menatec74a5e2024-03-10 19:45:40 +0530398 frappe.db.get_value("Item", item_row.item_code, ["has_batch_no", "has_serial_no"]).then((r) => {
399 item_row.has_batch_no = r.message.has_batch_no;
400 item_row.has_serial_no = r.message.has_serial_no;
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530401
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530402 new erpnext.SerialBatchPackageSelector(frm, item_row, (r) => {
403 if (r) {
404 let update_values = {
405 serial_and_batch_bundle: r.name,
406 qty: Math.abs(r.total_qty),
407 };
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530408
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530409 if (!warehouse_field) {
410 warehouse_field = "warehouse";
Ankush Menatec74a5e2024-03-10 19:45:40 +0530411 }
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530412
413 if (r.warehouse) {
414 update_values[warehouse_field] = r.warehouse;
415 }
416
417 frappe.model.set_value(item_row.doctype, item_row.name, update_values);
418 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530419 });
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530420 });
Deepesh Garg62706072023-07-16 12:58:42 +0530421 },
422
Ankush Menatec74a5e2024-03-10 19:45:40 +0530423 get_fiscal_year: function (date, with_dates = false, boolean = false) {
Corentin Flrc203faf2024-04-09 14:59:58 +0200424 if (!frappe.boot.setup_complete) {
425 return;
426 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530427 if (!date) {
Deepesh Garg3759a412023-07-19 13:17:12 +0530428 date = frappe.datetime.get_today();
429 }
430
Ankush Menatec74a5e2024-03-10 19:45:40 +0530431 let fiscal_year = "";
Deepesh Garg62706072023-07-16 12:58:42 +0530432 frappe.call({
433 method: "erpnext.accounts.utils.get_fiscal_year",
434 args: {
ruthra kumarc31ee8e2023-11-14 06:44:49 +0530435 date: date,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530436 boolean: boolean,
Deepesh Garg62706072023-07-16 12:58:42 +0530437 },
438 async: false,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530439 callback: function (r) {
Deepesh Garg62706072023-07-16 12:58:42 +0530440 if (r.message) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530441 if (with_dates) fiscal_year = r.message;
442 else fiscal_year = r.message[0];
Deepesh Garg62706072023-07-16 12:58:42 +0530443 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530444 },
Deepesh Garg62706072023-07-16 12:58:42 +0530445 });
446 return fiscal_year;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530447 },
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530448});
449
Ankush Menatec74a5e2024-03-10 19:45:40 +0530450erpnext.utils.select_alternate_items = function (opts) {
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530451 const frm = opts.frm;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530452 const warehouse_field = opts.warehouse_field || "warehouse";
453 const item_field = opts.item_field || "item_code";
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530454
455 this.data = [];
456 const dialog = new frappe.ui.Dialog({
457 title: __("Select Alternate Item"),
458 fields: [
Ankush Menatec74a5e2024-03-10 19:45:40 +0530459 { fieldtype: "Section Break", label: __("Items") },
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530460 {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530461 fieldname: "alternative_items",
462 fieldtype: "Table",
463 cannot_add_rows: true,
464 in_place_edit: true,
465 data: this.data,
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530466 get_data: () => {
467 return this.data;
468 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530469 fields: [
470 {
471 fieldtype: "Data",
472 fieldname: "docname",
473 hidden: 1,
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530474 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530475 {
476 fieldtype: "Link",
477 fieldname: "item_code",
478 options: "Item",
479 in_list_view: 1,
480 read_only: 1,
481 label: __("Item Code"),
482 },
483 {
484 fieldtype: "Link",
485 fieldname: "alternate_item",
486 options: "Item",
487 default: "",
488 in_list_view: 1,
489 label: __("Alternate Item"),
490 onchange: function () {
491 const item_code = this.get_value();
492 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
493 if (item_code && warehouse) {
494 frappe.call({
495 method: "erpnext.stock.utils.get_latest_stock_qty",
496 args: {
497 item_code: item_code,
498 warehouse: warehouse,
499 },
500 callback: (r) => {
501 this.grid_row.on_grid_fields_dict.actual_qty.set_value(
502 r.message || 0
503 );
504 },
505 });
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530506 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530507 },
508 get_query: (e) => {
509 return {
510 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
511 filters: {
512 item_code: e.item_code,
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530513 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530514 };
515 },
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530516 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530517 {
518 fieldtype: "Link",
519 fieldname: "warehouse",
520 options: "Warehouse",
521 default: "",
522 in_list_view: 1,
523 label: __("Warehouse"),
524 onchange: function () {
525 const warehouse = this.get_value();
526 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
527 if (item_code && warehouse) {
528 frappe.call({
529 method: "erpnext.stock.utils.get_latest_stock_qty",
530 args: {
531 item_code: item_code,
532 warehouse: warehouse,
533 },
534 callback: (r) => {
535 this.grid_row.on_grid_fields_dict.actual_qty.set_value(
536 r.message || 0
537 );
538 },
539 });
540 }
541 },
542 },
543 {
544 fieldtype: "Float",
545 fieldname: "actual_qty",
546 default: 0,
547 read_only: 1,
548 in_list_view: 1,
549 label: __("Available Qty"),
550 },
551 ],
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530552 },
553 ],
Ankush Menatec74a5e2024-03-10 19:45:40 +0530554 primary_action: function () {
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530555 const args = this.get_values()["alternative_items"];
Ankush Menatec74a5e2024-03-10 19:45:40 +0530556 const alternative_items = args.filter((d) => {
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530557 if (d.alternate_item && d.item_code != d.alternate_item) {
558 return true;
559 }
560 });
561
Ankush Menatec74a5e2024-03-10 19:45:40 +0530562 alternative_items.forEach((d) => {
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530563 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800564 let qty = null;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530565 if (row.doctype === "Work Order Item") {
Doridel Cahanape42192b2018-05-16 13:28:39 +0800566 qty = row.required_qty;
567 } else {
568 qty = row.qty;
569 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530570 row[item_field] = d.alternate_item;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530571 frappe.model.set_value(row.doctype, row.name, "qty", qty);
mergify[bot]c1a0f642021-12-17 19:06:19 +0530572 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
573 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530574 });
575
576 refresh_field(opts.child_docname);
577 this.hide();
578 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530579 primary_action_label: __("Update"),
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530580 });
581
Ankush Menatec74a5e2024-03-10 19:45:40 +0530582 frm.doc[opts.child_docname].forEach((d) => {
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530583 if (!opts.condition || opts.condition(d)) {
584 dialog.fields_dict.alternative_items.df.data.push({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530585 docname: d.name,
586 item_code: d[item_field],
587 warehouse: d[warehouse_field],
588 actual_qty: d.actual_qty,
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530589 });
590 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530591 });
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530592
593 this.data = dialog.fields_dict.alternative_items.df.data;
594 dialog.fields_dict.alternative_items.grid.refresh();
595 dialog.show();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530596};
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530597
Ankush Menatec74a5e2024-03-10 19:45:40 +0530598erpnext.utils.update_child_items = function (opts) {
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800599 const frm = opts.frm;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530600 const cannot_add_row = typeof opts.cannot_add_row === "undefined" ? true : opts.cannot_add_row;
601 const child_docname = typeof opts.cannot_add_row === "undefined" ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530602 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
s-aga-r2d8363a2023-09-02 11:02:24 +0530603 const has_reserved_stock = opts.has_reserved_stock ? true : false;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530604 const get_precision = (fieldname) => child_meta.fields.find((f) => f.fieldname == fieldname).precision;
Saqib56fea7d2020-10-09 21:19:25 +0530605
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500606 this.data = frm.doc[opts.child_docname].map((d) => {
607 return {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530608 docname: d.name,
609 name: d.name,
610 item_code: d.item_code,
611 delivery_date: d.delivery_date,
612 schedule_date: d.schedule_date,
613 conversion_factor: d.conversion_factor,
614 qty: d.qty,
615 rate: d.rate,
616 uom: d.uom,
617 fg_item: d.fg_item,
618 fg_item_qty: d.fg_item_qty,
619 };
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500620 });
621
Ankush Menatec74a5e2024-03-10 19:45:40 +0530622 const fields = [
623 {
624 fieldtype: "Data",
625 fieldname: "docname",
626 read_only: 1,
627 hidden: 1,
628 },
629 {
630 fieldtype: "Link",
631 fieldname: "item_code",
632 options: "Item",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530633 in_list_view: 1,
s-aga-rfaf9f132023-08-21 15:51:57 +0530634 read_only: 0,
635 disabled: 0,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530636 label: __("Item Code"),
637 get_query: function () {
638 let filters;
639 if (frm.doc.doctype == "Sales Order") {
640 filters = { is_sales_item: 1 };
641 } else if (frm.doc.doctype == "Purchase Order") {
642 if (frm.doc.is_subcontracted) {
643 if (frm.doc.is_old_subcontracting_flow) {
644 filters = { is_sub_contracted_item: 1 };
645 } else {
646 filters = { is_stock_item: 0 };
647 }
648 } else {
649 filters = { is_purchase_item: 1 };
s-aga-rfaf9f132023-08-21 15:51:57 +0530650 }
651 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530652 return {
653 query: "erpnext.controllers.queries.item_query",
654 filters: filters,
655 };
s-aga-rfaf9f132023-08-21 15:51:57 +0530656 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530657 },
658 {
659 fieldtype: "Link",
660 fieldname: "uom",
661 options: "UOM",
662 read_only: 0,
663 label: __("UOM"),
s-aga-rfaf9f132023-08-21 15:51:57 +0530664 reqd: 1,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530665 onchange: function () {
666 frappe.call({
667 method: "erpnext.stock.get_item_details.get_conversion_factor",
668 args: { item_code: this.doc.item_code, uom: this.value },
669 callback: (r) => {
670 if (!r.exc) {
671 if (this.doc.conversion_factor == r.message.conversion_factor) return;
672
673 const docname = this.doc.docname;
674 dialog.fields_dict.trans_items.df.data.some((doc) => {
675 if (doc.docname == docname) {
676 doc.conversion_factor = r.message.conversion_factor;
677 dialog.fields_dict.trans_items.grid.refresh();
678 return true;
679 }
680 });
681 }
682 },
683 });
684 },
685 },
686 {
687 fieldtype: "Float",
688 fieldname: "qty",
s-aga-rfaf9f132023-08-21 15:51:57 +0530689 default: 0,
690 read_only: 0,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530691 in_list_view: 1,
692 label: __("Qty"),
693 precision: get_precision("qty"),
694 },
695 {
696 fieldtype: "Currency",
697 fieldname: "rate",
698 options: "currency",
699 default: 0,
700 read_only: 0,
701 in_list_view: 1,
702 label: __("Rate"),
703 precision: get_precision("rate"),
704 },
705 ];
706
707 if (frm.doc.doctype == "Sales Order" || frm.doc.doctype == "Purchase Order") {
708 fields.splice(2, 0, {
709 fieldtype: "Date",
710 fieldname: frm.doc.doctype == "Sales Order" ? "delivery_date" : "schedule_date",
711 in_list_view: 1,
712 label: frm.doc.doctype == "Sales Order" ? __("Delivery Date") : __("Reqd by date"),
713 default: frm.doc.doctype == "Sales Order" ? frm.doc.delivery_date : frm.doc.schedule_date,
714 reqd: 1,
715 });
716 fields.splice(3, 0, {
717 fieldtype: "Float",
718 fieldname: "conversion_factor",
719 label: __("Conversion Factor"),
720 precision: get_precision("conversion_factor"),
721 });
722 }
723
724 if (
725 frm.doc.doctype == "Purchase Order" &&
726 frm.doc.is_subcontracted &&
727 !frm.doc.is_old_subcontracting_flow
728 ) {
729 fields.push(
730 {
731 fieldtype: "Link",
732 fieldname: "fg_item",
733 options: "Item",
734 reqd: 1,
735 in_list_view: 0,
736 read_only: 0,
737 disabled: 0,
738 label: __("Finished Good Item"),
739 get_query: () => {
740 return {
741 filters: {
742 is_stock_item: 1,
743 is_sub_contracted_item: 1,
744 default_bom: ["!=", ""],
745 },
746 };
747 },
748 },
749 {
750 fieldtype: "Float",
751 fieldname: "fg_item_qty",
752 reqd: 1,
753 default: 0,
754 read_only: 0,
755 in_list_view: 0,
756 label: __("Finished Good Item Qty"),
757 precision: get_precision("fg_item_qty"),
758 }
759 );
s-aga-rfaf9f132023-08-21 15:51:57 +0530760 }
761
Deepesh Garg40772542023-07-22 23:07:18 +0530762 let dialog = new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800763 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200764 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800765 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800766 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100767 fieldname: "trans_items",
768 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530769 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100770 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530771 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530772 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100773 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800774 get_data: () => {
775 return this.data;
776 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530777 fields: fields,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800778 },
779 ],
Ankush Menatec74a5e2024-03-10 19:45:40 +0530780 primary_action: function () {
s-aga-r2d8363a2023-09-02 11:02:24 +0530781 if (frm.doctype == "Sales Order" && has_reserved_stock) {
782 this.hide();
783 frappe.confirm(
Ankush Menatec74a5e2024-03-10 19:45:40 +0530784 __(
785 "The reserved stock will be released when you update items. Are you certain you wish to proceed?"
786 ),
787 () => this.update_items()
788 );
s-aga-r2d8363a2023-09-02 11:02:24 +0530789 } else {
790 this.update_items();
791 }
792 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530793 update_items: function () {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530794 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800795 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530796 method: "erpnext.controllers.accounts_controller.update_child_qty_rate",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100797 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800798 args: {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530799 parent_doctype: frm.doc.doctype,
800 trans_items: trans_items,
801 parent_doctype_name: frm.doc.name,
802 child_docname: child_docname,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800803 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530804 callback: function () {
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800805 frm.reload_doc();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530806 },
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800807 });
808 this.hide();
809 refresh_field("items");
810 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530811 primary_action_label: __("Update"),
812 });
Deepesh Garg40772542023-07-22 23:07:18 +0530813
814 dialog.show();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530815};
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800816
Ankush Menatec74a5e2024-03-10 19:45:40 +0530817erpnext.utils.map_current_doc = function (opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530818 function _map() {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530819 if ($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530820 // remove first item row if empty
Ankush Menatec74a5e2024-03-10 19:45:40 +0530821 if (!cur_frm.doc.items[0].item_code) {
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530822 cur_frm.doc.items = cur_frm.doc.items.splice(1);
823 }
bhupen3c7e70f2016-11-30 14:32:11 +0530824
825 // find the doctype of the items table
Ankush Menatec74a5e2024-03-10 19:45:40 +0530826 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, "items").options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530827
bhupen3c7e70f2016-11-30 14:32:11 +0530828 // find the link fieldname from items table for the given
829 // source_doctype
830 var link_fieldname = null;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530831 frappe.get_meta(items_doctype).fields.forEach(function (d) {
832 if (d.options === opts.source_doctype) link_fieldname = d.fieldname;
833 });
bhupen3c7e70f2016-11-30 14:32:11 +0530834
Nabin Hait802b4352017-01-09 15:32:20 +0530835 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530836 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530837 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530838
Ankush Menatec74a5e2024-03-10 19:45:40 +0530839 $.each(cur_frm.doc.items, function (i, d) {
840 opts.source_name.forEach(function (src) {
841 if (d[link_fieldname] == src) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530842 already_set = true;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530843 if (item_qty_map[d.item_code]) item_qty_map[d.item_code] += flt(d.qty);
844 else item_qty_map[d.item_code] = flt(d.qty);
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530845 }
846 });
847 });
848
Ankush Menatec74a5e2024-03-10 19:45:40 +0530849 if (already_set) {
850 opts.source_name.forEach(function (src) {
851 frappe.model.with_doc(opts.source_doctype, src, function (r) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530852 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
Ankush Menatec74a5e2024-03-10 19:45:40 +0530853 $.each(source_doc.items || [], function (i, row) {
854 if (row.qty > flt(item_qty_map[row.item_code])) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530855 already_set = false;
856 return false;
857 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530858 });
859 });
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530860
Ankush Menatec74a5e2024-03-10 19:45:40 +0530861 if (already_set) {
862 frappe.msgprint(
863 __("You have already selected items from {0} {1}", [opts.source_doctype, src])
864 );
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530865 return;
866 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530867 });
Nabin Hait802b4352017-01-09 15:32:20 +0530868 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530869 }
870
871 return frappe.call({
872 // Sometimes we hit the limit for URL length of a GET request
873 // as we send the full target_doc. Hence this is a POST request.
874 type: "POST",
Ankush Menatec74a5e2024-03-10 19:45:40 +0530875 method: "frappe.model.mapper.map_docs",
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530876 args: {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530877 method: opts.method,
878 source_names: opts.source_name,
879 target_doc: cur_frm.doc,
880 args: opts.args,
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530881 },
s-aga-r2d8363a2023-09-02 11:02:24 +0530882 freeze: true,
883 freeze_message: __("Mapping {0} ...", [opts.source_doctype]),
Ankush Menatec74a5e2024-03-10 19:45:40 +0530884 callback: function (r) {
885 if (!r.exc) {
Deepesh Garg9b1c2222023-12-27 22:37:01 +0530886 frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530887 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530888 cur_frm.refresh();
889 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530890 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530891 });
892 }
Sagar Vora78777d62021-03-09 20:35:08 +0530893
894 let query_args = {};
895 if (opts.get_query_filters) {
896 query_args.filters = opts.get_query_filters;
897 }
898
899 if (opts.get_query_method) {
900 query_args.query = opts.get_query_method;
901 }
902
903 if (query_args.filters || query_args.query) {
904 opts.get_query = () => query_args;
905 }
906
907 if (opts.source_doctype) {
ruthra kumar7da9ffa2024-01-03 17:59:15 +0530908 let data_fields = [];
ruthra kumar550cbbd2024-04-01 16:25:35 +0530909 if (["Purchase Receipt", "Delivery Note"].includes(opts.source_doctype)) {
ruthra kumar7da9ffa2024-01-03 17:59:15 +0530910 data_fields.push({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530911 fieldname: "merge_taxes",
912 fieldtype: "Check",
913 label: __("Merge taxes from multiple documents"),
ruthra kumar7da9ffa2024-01-03 17:59:15 +0530914 });
915 }
Sagar Vora78777d62021-03-09 20:35:08 +0530916 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530917 doctype: opts.source_doctype,
918 target: opts.target,
919 date_field: opts.date_field || undefined,
920 setters: opts.setters,
ruthra kumar7da9ffa2024-01-03 17:59:15 +0530921 data_fields: data_fields,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530922 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530923 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530924 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800925 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530926 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530927 size: opts.size,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530928 action: function (selections, args) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530929 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530930 if (values.length === 0) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530931 frappe.msgprint(__("Please select {0}", [opts.source_doctype]));
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530932 return;
933 }
934 opts.source_name = values;
ruthra kumar550cbbd2024-04-01 16:25:35 +0530935 if (
936 opts.allow_child_item_selection ||
937 ["Purchase Receipt", "Delivery Note"].includes(opts.source_doctype)
938 ) {
ruthra kumar7da9ffa2024-01-03 17:59:15 +0530939 // args contains filtered child docnames
940 opts.args = args;
941 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530942 d.dialog.hide();
943 _map();
944 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530945 });
Sagar Vora78777d62021-03-09 20:35:08 +0530946
947 return d;
948 }
949
950 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530951 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530952 _map();
953 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530954};
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530955
Ankush Menatec74a5e2024-03-10 19:45:40 +0530956frappe.form.link_formatters["Item"] = function (value, doc) {
Alan157b3882021-04-21 21:00:22 +0530957 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530958 return value + ": " + doc.item_name;
marination824f48f2020-10-12 20:08:03 +0530959 } else if (!value && doc.doctype && doc.item_name) {
960 // format blank value in child table
961 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530962 } else {
marination824f48f2020-10-12 20:08:03 +0530963 // if value is blank in report view or item code and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530964 return value;
965 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530966};
Rushabh Mehta180e4352016-07-26 17:57:42 +0530967
Ankush Menatec74a5e2024-03-10 19:45:40 +0530968frappe.form.link_formatters["Employee"] = function (value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530969 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530970 return value + ": " + doc.employee_name;
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530971 } else if (!value && doc.doctype && doc.employee_name) {
972 // format blank value in child table
973 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530974 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530975 // if value is blank in report view or project name and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530976 return value;
977 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530978};
Rushabh Mehta180e4352016-07-26 17:57:42 +0530979
Ankush Menatec74a5e2024-03-10 19:45:40 +0530980frappe.form.link_formatters["Project"] = function (value, doc) {
Rucha Mahabal59961f72021-05-20 23:43:19 +0530981 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530982 return value + ": " + doc.project_name;
Rucha Mahabal59961f72021-05-20 23:43:19 +0530983 } else if (!value && doc.doctype && doc.project_name) {
984 // format blank value in child table
985 return doc.project;
986 } else {
987 // if value is blank in report view or project name and name are the same, return as is
988 return value;
989 }
990};
991
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530992// add description on posting time
Ankush Menatec74a5e2024-03-10 19:45:40 +0530993$(document).on("app_ready", function () {
994 if (!frappe.datetime.is_timezone_same()) {
995 $.each(
996 [
997 "Stock Reconciliation",
998 "Stock Entry",
999 "Stock Ledger Entry",
1000 "Delivery Note",
1001 "Purchase Receipt",
1002 "Sales Invoice",
1003 ],
1004 function (i, d) {
1005 frappe.ui.form.on(d, "onload", function (frm) {
1006 cur_frm.set_df_property("posting_time", "description", frappe.sys_defaults.time_zone);
1007 });
1008 }
1009 );
Rushabh Mehta95b995b2015-05-28 12:10:55 +05301010 }
akshay14384c22016-07-28 13:53:17 +05301011});
Alanc6dc9ea2021-05-07 20:30:04 +05301012
Himanshuec25d592021-06-14 19:05:52 +05301013// Show SLA dashboard
Ankush Menatec74a5e2024-03-10 19:45:40 +05301014$(document).on("app_ready", function () {
1015 $.each(frappe.boot.service_level_agreement_doctypes, function (_i, d) {
Ankush Menatbd9ef742023-07-16 11:34:42 +05301016 frappe.ui.form.on(d, {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301017 onload: function (frm) {
1018 if (!frm.doc.service_level_agreement) return;
Himanshuec25d592021-06-14 19:05:52 +05301019
Ankush Menatbd9ef742023-07-16 11:34:42 +05301020 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +05301021 method: "erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters",
Ankush Menatbd9ef742023-07-16 11:34:42 +05301022 args: {
1023 doctype: frm.doc.doctype,
1024 name: frm.doc.service_level_agreement,
Ankush Menatec74a5e2024-03-10 19:45:40 +05301025 customer: frm.doc.customer,
Himanshuec25d592021-06-14 19:05:52 +05301026 },
Ankush Menatbd9ef742023-07-16 11:34:42 +05301027 callback: function (r) {
1028 if (r && r.message) {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301029 frm.set_query("priority", function () {
Ankush Menatbd9ef742023-07-16 11:34:42 +05301030 return {
1031 filters: {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301032 name: ["in", r.message.priority],
1033 },
Ankush Menatbd9ef742023-07-16 11:34:42 +05301034 };
Himanshuec25d592021-06-14 19:05:52 +05301035 });
Ankush Menatec74a5e2024-03-10 19:45:40 +05301036 frm.set_query("service_level_agreement", function () {
Ankush Menatbd9ef742023-07-16 11:34:42 +05301037 return {
1038 filters: {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301039 name: ["in", r.message.service_level_agreements],
1040 },
Ankush Menatbd9ef742023-07-16 11:34:42 +05301041 };
1042 });
Himanshuec25d592021-06-14 19:05:52 +05301043 }
Ankush Menatec74a5e2024-03-10 19:45:40 +05301044 },
Himanshuec25d592021-06-14 19:05:52 +05301045 });
Ankush Menatbd9ef742023-07-16 11:34:42 +05301046 },
1047
Ankush Menatec74a5e2024-03-10 19:45:40 +05301048 refresh: function (frm) {
1049 if (
1050 frm.doc.status !== "Closed" &&
1051 frm.doc.service_level_agreement &&
1052 ["First Response Due", "Resolution Due"].includes(frm.doc.agreement_status)
1053 ) {
Ankush Menatbd9ef742023-07-16 11:34:42 +05301054 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +05301055 method: "frappe.client.get",
Ankush Menatbd9ef742023-07-16 11:34:42 +05301056 args: {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301057 doctype: "Service Level Agreement",
1058 name: frm.doc.service_level_agreement,
Ankush Menatbd9ef742023-07-16 11:34:42 +05301059 },
Ankush Menatec74a5e2024-03-10 19:45:40 +05301060 callback: function (data) {
Ankush Menatbd9ef742023-07-16 11:34:42 +05301061 let statuses = data.message.pause_sla_on;
1062 const hold_statuses = [];
1063 $.each(statuses, (_i, entry) => {
1064 hold_statuses.push(entry.status);
1065 });
1066 if (hold_statuses.includes(frm.doc.status)) {
1067 frm.dashboard.clear_headline();
Ankush Menatec74a5e2024-03-10 19:45:40 +05301068 let message = {
1069 indicator: "orange",
1070 msg: __("SLA is on hold since {0}", [
1071 moment(frm.doc.on_hold_since).fromNow(true),
1072 ]),
1073 };
Ankush Menatbd9ef742023-07-16 11:34:42 +05301074 frm.dashboard.set_headline_alert(
1075 '<div class="row">' +
1076 '<div class="col-xs-12">' +
Ankush Menatec74a5e2024-03-10 19:45:40 +05301077 '<span class="indicator whitespace-nowrap ' +
1078 message.indicator +
1079 '"><span>' +
1080 message.msg +
1081 "</span></span> " +
1082 "</div>" +
1083 "</div>"
Ankush Menatbd9ef742023-07-16 11:34:42 +05301084 );
1085 } else {
1086 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
1087 }
Ankush Menatec74a5e2024-03-10 19:45:40 +05301088 },
Ankush Menatbd9ef742023-07-16 11:34:42 +05301089 });
1090 } else if (frm.doc.service_level_agreement) {
1091 frm.dashboard.clear_headline();
1092
Ankush Menatec74a5e2024-03-10 19:45:40 +05301093 let agreement_status =
1094 frm.doc.agreement_status == "Fulfilled"
1095 ? { indicator: "green", msg: "Service Level Agreement has been fulfilled" }
1096 : { indicator: "red", msg: "Service Level Agreement Failed" };
Ankush Menatbd9ef742023-07-16 11:34:42 +05301097
1098 frm.dashboard.set_headline_alert(
1099 '<div class="row">' +
1100 '<div class="col-xs-12">' +
Ankush Menatec74a5e2024-03-10 19:45:40 +05301101 '<span class="indicator whitespace-nowrap ' +
1102 agreement_status.indicator +
1103 '"><span class="hidden-xs">' +
1104 agreement_status.msg +
1105 "</span></span> " +
1106 "</div>" +
1107 "</div>"
Ankush Menatbd9ef742023-07-16 11:34:42 +05301108 );
1109 }
1110 },
1111 });
Himanshuec25d592021-06-14 19:05:52 +05301112 });
1113});
1114
1115function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
1116 frm.dashboard.clear_headline();
1117
Saqib24114772021-12-15 11:45:34 +05301118 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301119 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +05301120 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301121 } else {
1122 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +05301123 }
1124
1125 let alert = `
1126 <div class="row">
1127 <div class="col-xs-12 col-sm-6">
1128 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
1129 <span>Time to Respond: ${time_to_respond.diff_display}</span>
1130 </span>
1131 </div>`;
1132
Himanshuec25d592021-06-14 19:05:52 +05301133 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +05301134 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301135 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +05301136 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301137 } else {
1138 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301139 }
1140
1141 alert += `
1142 <div class="col-xs-12 col-sm-6">
1143 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1144 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1145 </span>
1146 </div>`;
1147 }
1148
Ankush Menatec74a5e2024-03-10 19:45:40 +05301149 alert += "</div>";
Himanshuec25d592021-06-14 19:05:52 +05301150
1151 frm.dashboard.set_headline_alert(alert);
1152}
1153
1154function get_time_left(timestamp, agreement_status) {
Ankush Menateaf86a62023-12-14 15:53:57 +05301155 const diff = moment(timestamp).diff(frappe.datetime.system_datetime(true));
Ankush Menatec74a5e2024-03-10 19:45:40 +05301156 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : "Failed";
1157 let indicator = diff_display == "Failed" && agreement_status != "Fulfilled" ? "red" : "green";
1158 return { diff_display: diff_display, indicator: indicator };
Himanshuec25d592021-06-14 19:05:52 +05301159}
1160
Saqib24114772021-12-15 11:45:34 +05301161function get_status(expected, actual) {
1162 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301163 if (time_left >= 0) {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301164 return { diff_display: "Fulfilled", indicator: "green" };
Himanshuec25d592021-06-14 19:05:52 +05301165 } else {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301166 return { diff_display: "Failed", indicator: "red" };
Himanshuec25d592021-06-14 19:05:52 +05301167 }
1168}
1169
Alanc6dc9ea2021-05-07 20:30:04 +05301170function attach_selector_button(inner_text, append_loction, context, grid_row) {
Ankush Menatec74a5e2024-03-10 19:45:40 +05301171 let $btn_div = $("<div>").css({ "margin-bottom": "10px", "margin-top": "10px" }).appendTo(append_loction);
1172 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`).appendTo($btn_div);
Alanc6dc9ea2021-05-07 20:30:04 +05301173
Ankush Menatec74a5e2024-03-10 19:45:40 +05301174 $btn.on("click", function () {
Alanc6dc9ea2021-05-07 20:30:04 +05301175 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1176 });
ruthra kumar5981c7e2023-08-29 13:19:26 +05301177}
rohitwaghchaure0da19a42024-03-12 12:27:21 +05301178
1179$.extend(erpnext.stock.utils, {
1180 set_item_details_using_barcode(frm, child_row, callback) {
1181 const barcode_scanner = new erpnext.utils.BarcodeScanner({ frm: frm });
1182 barcode_scanner.scan_api_call(child_row.barcode, callback);
1183 },
1184});