blob: 19c907309084364d63480504982b6b9218b071a8 [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");
Anand Doshie65e6212012-11-19 15:43:18 +05305
Anand Doshibdee6e02013-07-09 13:03:39 +05306$.extend(erpnext, {
7 get_currency: function(company) {
8 if(!company && cur_frm)
9 company = cur_frm.doc.company;
10 if(company)
Rushabh Mehta66d52b52014-03-27 14:17:33 +053011 return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053012 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013 return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053014 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053015
tundebabzyc8978252018-02-12 10:34:50 +010016 get_presentation_currency_list: () => {
17 const docs = frappe.boot.docs;
Zarrare25dcd22018-06-21 10:53:12 +053018 let currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name);
19 currency_list.unshift("");
tundebabzyc8978252018-02-12 10:34:50 +010020 return currency_list;
21 },
22
Anand Doshi8bde7f92014-04-24 18:11:49 +053023 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053024 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053025 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
26 }
27 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053028
Anand Doshibdee6e02013-07-09 13:03:39 +053029 hide_company: function() {
30 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053031 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053032 if(companies.length === 1) {
33 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
34 cur_frm.toggle_display("company", false);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053035 } else if(erpnext.last_selected_company) {
36 if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053037 }
38 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053039 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053040
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053041 is_perpetual_inventory_enabled: function(company) {
42 if(company) {
43 return frappe.get_doc(":Company", company).enable_perpetual_inventory
44 }
45 },
46
Makarand Bauskare6712c12017-10-25 12:17:40 +053047 stale_rate_allowed: () => {
rohitwaghchaure7677ff02017-11-02 18:12:14 +053048 return cint(frappe.boot.sysdefaults.allow_stale);
Makarand Bauskare6712c12017-10-25 12:17:40 +053049 },
50
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053051 setup_serial_no: function() {
52 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053053 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
54 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053055
Pratik Vyasb52618c2014-04-14 16:25:30 +053056 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053057 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053058 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053059 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053060
scmmishraed4b0522019-04-16 17:03:25 +053061 var me = this;
Rushabh Mehta62030e02013-08-14 18:37:28 +053062 $btn.on("click", function() {
Rohit Waghchauredadd0492019-08-02 19:11:31 +053063 let callback = '';
64 let on_close = '';
65
Marica29a2e162019-11-12 14:43:41 +053066 frappe.model.get_value('Item', {'name':grid_row.doc.item_code}, 'has_serial_no',
67 (data) => {
68 if(data) {
69 grid_row.doc.has_serial_no = data.has_serial_no;
70 me.show_serial_batch_selector(grid_row.frm, grid_row.doc,
71 callback, on_close, true);
72 }
73 }
74 );
Rushabh Mehta62030e02013-08-14 18:37:28 +053075 });
deepeshgarg007e64c3572019-05-14 08:50:45 +053076 },
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053077
78 route_to_adjustment_jv: (args) => {
79 frappe.model.with_doctype('Journal Entry', () => {
80 // route to adjustment Journal Entry to handle Account Balance and Stock Value mismatch
81 let journal_entry = frappe.model.get_new_doc('Journal Entry');
82
83 args.accounts.forEach((je_account) => {
84 let child_row = frappe.model.add_child(journal_entry, "accounts");
85 child_row.account = je_account.account;
86 child_row.debit_in_account_currency = je_account.debit_in_account_currency;
87 child_row.credit_in_account_currency = je_account.credit_in_account_currency;
88 child_row.party_type = "" ;
89 });
90 frappe.set_route('Form','Journal Entry', journal_entry.name);
91 });
92 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053093});
Rushabh Mehta7d368752014-11-24 14:16:47 +053094
Nabin Haitb072c742014-12-16 12:49:13 +053095
96$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053097 set_party_dashboard_indicators: function(frm) {
98 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053099 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530100 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530101 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530102 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530103 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530104 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530105 frm.dashboard.add_indicator(__('Annual Billing: {0}',
106 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
107 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
108 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530109 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530110
deepeshgarg00764238ee2018-12-25 16:28:39 +0530111 if(company_wise_info[0].loyalty_points) {
112 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
113 [company_wise_info[0].loyalty_points]), 'blue');
114 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530115 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530116 }
117 },
118
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530119 add_indicator_for_multicompany: function(frm, info) {
120 frm.dashboard.stats_area.removeClass('hidden');
121 frm.dashboard.stats_area_row.addClass('flex');
122 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
123
124 var color = info.total_unpaid ? 'orange' : 'green';
125
126 var indicator = $('<div class="flex-column col-xs-6">'+
127 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
128
129 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
130 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
131
132 '<div class="badge-link small" style="margin-bottom:10px">'+
133 '<span class="indicator '+color+'">Total Unpaid: '
134 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
135
136
137 '</div>').appendTo(frm.dashboard.stats_area_row);
138
139 if(info.loyalty_points){
140 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
141 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
142 }
143
144 return indicator;
145 },
146
Shreya Shah149f7ee2018-03-27 11:29:25 +0530147 get_party_name: function(party_type) {
148 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
149 'Member': 'member_name'};
150 return dict[party_type];
151 },
152
Rushabh Mehta49f97472018-08-30 18:50:48 +0530153 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530154 var d = locals[dt][dn];
155 if(d[fieldname]){
156 var cl = doc[table_fieldname] || [];
157 for(var i = 0; i < cl.length; i++) {
158 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
159 }
160 }
161 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530162 },
163
164 get_terms: function(tc_name, doc, callback) {
165 if(tc_name) {
166 return frappe.call({
167 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
168 args: {
169 template_name: tc_name,
170 doc: doc
171 },
172 callback: function(r) {
173 callback(r)
174 }
175 });
176 }
tundebabzy1a947db2017-08-29 13:48:27 +0100177 },
178
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530179 make_bank_account: function(doctype, docname) {
180 frappe.call({
181 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
182 args: {
183 doctype: doctype,
184 docname: docname
185 },
186 freeze: true,
187 callback: function(r) {
188 var doclist = frappe.model.sync(r.message);
189 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
190 }
191 })
192 },
193
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530194 add_dimensions: function(report_name, index) {
195 let filters = frappe.query_reports[report_name].filters;
196
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530197 frappe.call({
198 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
199 callback: function(r) {
200 let accounting_dimensions = r.message[0];
201 accounting_dimensions.forEach((dimension) => {
202 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530203
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530204 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530205 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530206 "fieldname": dimension["fieldname"],
207 "label": __(dimension["label"]),
208 "fieldtype": "Link",
209 "options": dimension["document_type"]
210 });
211 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530212 });
213 }
214 });
215 },
216
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530217 make_subscription: function(doctype, docname) {
218 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530219 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530220 args: {
221 doctype: doctype,
222 docname: docname
223 },
224 callback: function(r) {
225 var doclist = frappe.model.sync(r.message);
226 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
227 }
228 })
229 },
230
Saif90cf2dd2018-09-30 21:46:31 +0500231 make_pricing_rule: function(doctype, docname) {
232 frappe.call({
233 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
234 args: {
235 doctype: doctype,
236 docname: docname
237 },
238 callback: function(r) {
239 var doclist = frappe.model.sync(r.message);
240 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
241 }
242 })
243 },
244
tundebabzy1a947db2017-08-29 13:48:27 +0100245 /**
246 * Checks if the first row of a given child table is empty
247 * @param child_table - Child table Doctype
248 * @return {Boolean}
249 **/
250 first_row_is_empty: function(child_table){
251 if($.isArray(child_table) && child_table.length > 0) {
252 return !child_table[0].item_code;
253 }
254 return false;
255 },
256
257 /**
258 * Removes the first row of a child table if it is empty
259 * @param {_Frm} frm - The current form
260 * @param {String} child_table_name - The child table field name
261 * @return {Boolean}
262 **/
263 remove_empty_first_row: function(frm, child_table_name){
264 const rows = frm['doc'][child_table_name];
265 if (this.first_row_is_empty(rows)){
266 frm['doc'][child_table_name] = rows.splice(1);
267 }
268 return rows;
269 },
Zarrar6ffdf942018-06-14 12:24:16 +0530270 get_tree_options: function(option) {
271 // get valid options for tree based on user permission & locals dict
272 let unscrub_option = frappe.model.unscrub(option);
273 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530274 let options;
275
Zarrar6ffdf942018-06-14 12:24:16 +0530276 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530277 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530278 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530279 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530280 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530281
282 // filter unique values, as there may be multiple user permissions for any value
283 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530284 },
285 get_tree_default: function(option) {
286 // set default for a field based on user permission
287 let options = this.get_tree_options(option);
288 if(options.includes(frappe.defaults.get_default(option))) {
289 return frappe.defaults.get_default(option);
290 } else {
291 return options[0];
292 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400293 },
Andy Zhubffe9332021-04-12 22:49:26 +1200294 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530295 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200296 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530297 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400298 cl[i][fieldname] = doc[parent_fieldname];
299 }
Andy Zhubffe9332021-04-12 22:49:26 +1200300 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400301 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400302 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530303 create_new_doc: function (doctype, update_fields) {
304 frappe.model.with_doctype(doctype, function() {
305 var new_doc = frappe.model.get_new_doc(doctype);
306 for (let [key, value] of Object.entries(update_fields)) {
307 new_doc[key] = value;
308 }
309 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
310 });
311 }
312
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530313});
314
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530315erpnext.utils.select_alternate_items = function(opts) {
316 const frm = opts.frm;
317 const warehouse_field = opts.warehouse_field || 'warehouse';
318 const item_field = opts.item_field || 'item_code';
319
320 this.data = [];
321 const dialog = new frappe.ui.Dialog({
322 title: __("Select Alternate Item"),
323 fields: [
324 {fieldtype:'Section Break', label: __('Items')},
325 {
326 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
327 in_place_edit: true, data: this.data,
328 get_data: () => {
329 return this.data;
330 },
331 fields: [{
332 fieldtype:'Data',
333 fieldname:"docname",
334 hidden: 1
335 }, {
336 fieldtype:'Link',
337 fieldname:"item_code",
338 options: 'Item',
339 in_list_view: 1,
340 read_only: 1,
341 label: __('Item Code')
342 }, {
343 fieldtype:'Link',
344 fieldname:"alternate_item",
345 options: 'Item',
346 default: "",
347 in_list_view: 1,
348 label: __('Alternate Item'),
349 onchange: function() {
350 const item_code = this.get_value();
351 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
352 if (item_code && warehouse) {
353 frappe.call({
354 method: "erpnext.stock.utils.get_latest_stock_qty",
355 args: {
356 item_code: item_code,
357 warehouse: warehouse
358 },
359 callback: (r) => {
360 this.grid_row.on_grid_fields_dict
361 .actual_qty.set_value(r.message || 0);
362 }
363 })
364 }
365 },
366 get_query: (e) => {
367 return {
368 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
369 filters: {
370 item_code: e.item_code
371 }
372 };
373 }
374 }, {
375 fieldtype:'Link',
376 fieldname:"warehouse",
377 options: 'Warehouse',
378 default: "",
379 in_list_view: 1,
380 label: __('Warehouse'),
381 onchange: function() {
382 const warehouse = this.get_value();
383 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
384 if (item_code && warehouse) {
385 frappe.call({
386 method: "erpnext.stock.utils.get_latest_stock_qty",
387 args: {
388 item_code: item_code,
389 warehouse: warehouse
390 },
391 callback: (r) => {
392 this.grid_row.on_grid_fields_dict
393 .actual_qty.set_value(r.message || 0);
394 }
395 })
396 }
397 },
398 }, {
399 fieldtype:'Float',
400 fieldname:"actual_qty",
401 default: 0,
402 read_only: 1,
403 in_list_view: 1,
404 label: __('Available Qty')
405 }]
406 },
407 ],
408 primary_action: function() {
409 const args = this.get_values()["alternative_items"];
410 const alternative_items = args.filter(d => {
411 if (d.alternate_item && d.item_code != d.alternate_item) {
412 return true;
413 }
414 });
415
416 alternative_items.forEach(d => {
417 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800418 let qty = null;
419 if (row.doctype === 'Work Order Item') {
420 qty = row.required_qty;
421 } else {
422 qty = row.qty;
423 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530424 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530425 frm.script_manager.trigger(item_field, row.doctype, row.name)
426 .then(() => {
427 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
428 frappe.model.set_value(row.doctype, row.name,
429 opts.original_item_field, d.item_code);
430 });
431 });
432
433 refresh_field(opts.child_docname);
434 this.hide();
435 },
436 primary_action_label: __('Update')
437 });
438
439 frm.doc[opts.child_docname].forEach(d => {
440 if (!opts.condition || opts.condition(d)) {
441 dialog.fields_dict.alternative_items.df.data.push({
442 "docname": d.name,
443 "item_code": d[item_field],
444 "warehouse": d[warehouse_field],
445 "actual_qty": d.actual_qty
446 });
447 }
448 })
449
450 this.data = dialog.fields_dict.alternative_items.df.data;
451 dialog.fields_dict.alternative_items.grid.refresh();
452 dialog.show();
453}
454
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800455erpnext.utils.update_child_items = function(opts) {
456 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100457 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
458 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530459 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
460 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
461
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800462 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530463 const fields = [{
464 fieldtype:'Data',
465 fieldname:"docname",
466 read_only: 1,
467 hidden: 1,
468 }, {
469 fieldtype:'Link',
470 fieldname:"item_code",
471 options: 'Item',
472 in_list_view: 1,
473 read_only: 0,
474 disabled: 0,
475 label: __('Item Code')
476 }, {
Saqib61314242020-09-15 11:14:31 +0530477 fieldtype:'Link',
478 fieldname:'uom',
479 options: 'UOM',
480 read_only: 0,
481 label: __('UOM'),
482 reqd: 1,
483 onchange: function () {
484 frappe.call({
485 method: "erpnext.stock.get_item_details.get_conversion_factor",
486 args: { item_code: this.doc.item_code, uom: this.value },
487 callback: r => {
488 if(!r.exc) {
489 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530490
Saqib61314242020-09-15 11:14:31 +0530491 const docname = this.doc.docname;
492 dialog.fields_dict.trans_items.df.data.some(doc => {
493 if (doc.docname == docname) {
494 doc.conversion_factor = r.message.conversion_factor;
495 dialog.fields_dict.trans_items.grid.refresh();
496 return true;
497 }
498 })
499 }
500 }
501 });
502 }
503 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530504 fieldtype:'Float',
505 fieldname:"qty",
506 default: 0,
507 read_only: 0,
508 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530509 label: __('Qty'),
510 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530511 }, {
512 fieldtype:'Currency',
513 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530514 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530515 default: 0,
516 read_only: 0,
517 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530518 label: __('Rate'),
519 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530520 }];
521
522 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
523 fields.splice(2, 0, {
524 fieldtype: 'Date',
525 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
526 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530527 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
528 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530529 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530530 fields.splice(3, 0, {
531 fieldtype: 'Float',
532 fieldname: "conversion_factor",
533 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530534 label: __("Conversion Factor"),
535 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530536 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530537 }
538
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800539 const dialog = new frappe.ui.Dialog({
540 title: __("Update Items"),
541 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800542 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100543 fieldname: "trans_items",
544 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530545 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100546 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530547 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530548 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100549 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800550 get_data: () => {
551 return this.data;
552 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530553 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800554 },
555 ],
556 primary_action: function() {
557 const trans_items = this.get_values()["trans_items"];
558 frappe.call({
559 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100560 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800561 args: {
562 'parent_doctype': frm.doc.doctype,
563 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100564 'parent_doctype_name': frm.doc.name,
565 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800566 },
567 callback: function() {
568 frm.reload_doc();
569 }
570 });
571 this.hide();
572 refresh_field("items");
573 },
574 primary_action_label: __('Update')
575 });
576
577 frm.doc[opts.child_docname].forEach(d => {
578 dialog.fields_dict.trans_items.df.data.push({
579 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530580 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800581 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530582 "delivery_date": d.delivery_date,
583 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530584 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800585 "qty": d.qty,
586 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530587 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800588 });
589 this.data = dialog.fields_dict.trans_items.df.data;
590 dialog.fields_dict.trans_items.grid.refresh();
591 })
592 dialog.show();
593}
594
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530595erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530596 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530597 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530598 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530599 if(!cur_frm.doc.items[0].item_code) {
600 cur_frm.doc.items = cur_frm.doc.items.splice(1);
601 }
bhupen3c7e70f2016-11-30 14:32:11 +0530602
603 // find the doctype of the items table
604 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530605
bhupen3c7e70f2016-11-30 14:32:11 +0530606 // find the link fieldname from items table for the given
607 // source_doctype
608 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530609 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530610 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
611
Nabin Hait802b4352017-01-09 15:32:20 +0530612 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530613 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530614 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530615
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530616 $.each(cur_frm.doc.items, function(i, d) {
617 opts.source_name.forEach(function(src) {
618 if(d[link_fieldname]==src) {
619 already_set = true;
620 if (item_qty_map[d.item_code])
621 item_qty_map[d.item_code] += flt(d.qty);
622 else
623 item_qty_map[d.item_code] = flt(d.qty);
624 }
625 });
626 });
627
628 if(already_set) {
629 opts.source_name.forEach(function(src) {
630 frappe.model.with_doc(opts.source_doctype, src, function(r) {
631 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
632 $.each(source_doc.items || [], function(i, row) {
633 if(row.qty > flt(item_qty_map[row.item_code])) {
634 already_set = false;
635 return false;
636 }
637 })
638 })
639
640 if(already_set) {
641 frappe.msgprint(__("You have already selected items from {0} {1}",
642 [opts.source_doctype, src]));
643 return;
644 }
645
646 })
Nabin Hait802b4352017-01-09 15:32:20 +0530647 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530648 }
649
650 return frappe.call({
651 // Sometimes we hit the limit for URL length of a GET request
652 // as we send the full target_doc. Hence this is a POST request.
653 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530654 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530655 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530656 "method": opts.method,
657 "source_names": opts.source_name,
658 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530659 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530660 },
661 callback: function(r) {
662 if(!r.exc) {
663 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530664 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530665 cur_frm.refresh();
666 }
667 }
668 });
669 }
Sagar Vora78777d62021-03-09 20:35:08 +0530670
671 let query_args = {};
672 if (opts.get_query_filters) {
673 query_args.filters = opts.get_query_filters;
674 }
675
676 if (opts.get_query_method) {
677 query_args.query = opts.get_query_method;
678 }
679
680 if (query_args.filters || query_args.query) {
681 opts.get_query = () => query_args;
682 }
683
684 if (opts.source_doctype) {
685 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530686 doctype: opts.source_doctype,
687 target: opts.target,
688 date_field: opts.date_field || undefined,
689 setters: opts.setters,
690 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530691 add_filters_group: 1,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530692 action: function(selections, args) {
693 let values = selections;
694 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530695 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530696 return;
697 }
698 opts.source_name = values;
699 opts.setters = args;
700 d.dialog.hide();
701 _map();
702 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530703 });
Sagar Vora78777d62021-03-09 20:35:08 +0530704
705 return d;
706 }
707
708 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530709 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530710 _map();
711 }
712}
713
Rushabh Mehta180e4352016-07-26 17:57:42 +0530714frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530715 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530716 return value + ': ' + doc.item_name;
717 } else if (!value && doc.doctype && doc.item_name) {
718 // format blank value in child table
719 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530720 } else {
marination824f48f2020-10-12 20:08:03 +0530721 // 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 +0530722 return value;
723 }
724}
725
726frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530727 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530728 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530729 } else {
730 return value;
731 }
732}
733
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530734// add description on posting time
735$(document).on('app_ready', function() {
736 if(!frappe.datetime.is_timezone_same()) {
737 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
738 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
739 frappe.ui.form.on(d, "onload", function(frm) {
740 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530741 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530742 });
743 });
744 }
akshay14384c22016-07-28 13:53:17 +0530745});