blob: ed4b4d266d1ceb92926d9b23841cd5151ddd73bd [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;
18 const currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name);
19 return currency_list;
20 },
21
Anand Doshi8bde7f92014-04-24 18:11:49 +053022 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053023 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053024 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
25 }
26 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053027
Anand Doshibdee6e02013-07-09 13:03:39 +053028 hide_company: function() {
29 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053030 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053031 if(companies.length === 1) {
32 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
33 cur_frm.toggle_display("company", false);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053034 } else if(erpnext.last_selected_company) {
35 if(!cur_frm.doc.company) cur_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
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053040 is_perpetual_inventory_enabled: function(company) {
41 if(company) {
42 return frappe.get_doc(":Company", company).enable_perpetual_inventory
43 }
44 },
45
Makarand Bauskare6712c12017-10-25 12:17:40 +053046 stale_rate_allowed: () => {
rohitwaghchaure7677ff02017-11-02 18:12:14 +053047 return cint(frappe.boot.sysdefaults.allow_stale);
Makarand Bauskare6712c12017-10-25 12:17:40 +053048 },
49
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053050 setup_serial_no: function() {
51 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053052 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
53 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053054
Pratik Vyasb52618c2014-04-14 16:25:30 +053055 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053056 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053057 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053058 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053059
Rushabh Mehta62030e02013-08-14 18:37:28 +053060 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053061 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053062 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053063 fields: [
64 {
65 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053066 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053067 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053068 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053069 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053070 return {
71 filters: {
Shreya14bd43d2018-04-27 16:24:05 +053072 item_code:grid_row.doc.item_code,
Nabin Haitb7c0c552015-08-04 12:18:12 +053073 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053074 }
75 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053076 }
77 },
78 {
79 "fieldtype": "Button",
Kanchan Chauhan69818702017-02-27 12:53:05 +053080 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053081 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053082 }
83 ]
84 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053085
Rushabh Mehta62030e02013-08-14 18:37:28 +053086 d.get_input("add").on("click", function() {
87 var serial_no = d.get_value("serial_no");
88 if(serial_no) {
89 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053090 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053091 }
92 d.hide();
93 return false;
94 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053095
Rushabh Mehta62030e02013-08-14 18:37:28 +053096 d.show();
97 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053098 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053099});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530100
Nabin Haitb072c742014-12-16 12:49:13 +0530101
102$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530103 set_party_dashboard_indicators: function(frm) {
104 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
105 var info = frm.doc.__onload.dashboard_info;
106 frm.dashboard.add_indicator(__('Annual Billing: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530107 [format_currency(info.billing_this_year, info.currency)]), 'blue');
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530108 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530109 [format_currency(info.total_unpaid, info.currency)]),
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530110 info.total_unpaid ? 'orange' : 'green');
111 }
112 },
113
Shreya Shah149f7ee2018-03-27 11:29:25 +0530114 get_party_name: function(party_type) {
115 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
116 'Member': 'member_name'};
117 return dict[party_type];
118 },
119
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530120 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
121 var d = locals[dt][dn];
122 if(d[fieldname]){
123 var cl = doc[table_fieldname] || [];
124 for(var i = 0; i < cl.length; i++) {
125 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
126 }
127 }
128 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530129 },
130
131 get_terms: function(tc_name, doc, callback) {
132 if(tc_name) {
133 return frappe.call({
134 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
135 args: {
136 template_name: tc_name,
137 doc: doc
138 },
139 callback: function(r) {
140 callback(r)
141 }
142 });
143 }
tundebabzy1a947db2017-08-29 13:48:27 +0100144 },
145
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530146 make_subscription: function(doctype, docname) {
147 frappe.call({
rohitwaghchauredd092e32017-09-15 16:16:36 +0530148 method: "erpnext.accounts.doctype.subscription.subscription.make_subscription",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530149 args: {
150 doctype: doctype,
151 docname: docname
152 },
153 callback: function(r) {
154 var doclist = frappe.model.sync(r.message);
155 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
156 }
157 })
158 },
159
tundebabzy1a947db2017-08-29 13:48:27 +0100160 /**
161 * Checks if the first row of a given child table is empty
162 * @param child_table - Child table Doctype
163 * @return {Boolean}
164 **/
165 first_row_is_empty: function(child_table){
166 if($.isArray(child_table) && child_table.length > 0) {
167 return !child_table[0].item_code;
168 }
169 return false;
170 },
171
172 /**
173 * Removes the first row of a child table if it is empty
174 * @param {_Frm} frm - The current form
175 * @param {String} child_table_name - The child table field name
176 * @return {Boolean}
177 **/
178 remove_empty_first_row: function(frm, child_table_name){
179 const rows = frm['doc'][child_table_name];
180 if (this.first_row_is_empty(rows)){
181 frm['doc'][child_table_name] = rows.splice(1);
182 }
183 return rows;
184 },
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530185});
186
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530187erpnext.utils.select_alternate_items = function(opts) {
188 const frm = opts.frm;
189 const warehouse_field = opts.warehouse_field || 'warehouse';
190 const item_field = opts.item_field || 'item_code';
191
192 this.data = [];
193 const dialog = new frappe.ui.Dialog({
194 title: __("Select Alternate Item"),
195 fields: [
196 {fieldtype:'Section Break', label: __('Items')},
197 {
198 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
199 in_place_edit: true, data: this.data,
200 get_data: () => {
201 return this.data;
202 },
203 fields: [{
204 fieldtype:'Data',
205 fieldname:"docname",
206 hidden: 1
207 }, {
208 fieldtype:'Link',
209 fieldname:"item_code",
210 options: 'Item',
211 in_list_view: 1,
212 read_only: 1,
213 label: __('Item Code')
214 }, {
215 fieldtype:'Link',
216 fieldname:"alternate_item",
217 options: 'Item',
218 default: "",
219 in_list_view: 1,
220 label: __('Alternate Item'),
221 onchange: function() {
222 const item_code = this.get_value();
223 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
224 if (item_code && warehouse) {
225 frappe.call({
226 method: "erpnext.stock.utils.get_latest_stock_qty",
227 args: {
228 item_code: item_code,
229 warehouse: warehouse
230 },
231 callback: (r) => {
232 this.grid_row.on_grid_fields_dict
233 .actual_qty.set_value(r.message || 0);
234 }
235 })
236 }
237 },
238 get_query: (e) => {
239 return {
240 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
241 filters: {
242 item_code: e.item_code
243 }
244 };
245 }
246 }, {
247 fieldtype:'Link',
248 fieldname:"warehouse",
249 options: 'Warehouse',
250 default: "",
251 in_list_view: 1,
252 label: __('Warehouse'),
253 onchange: function() {
254 const warehouse = this.get_value();
255 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
256 if (item_code && warehouse) {
257 frappe.call({
258 method: "erpnext.stock.utils.get_latest_stock_qty",
259 args: {
260 item_code: item_code,
261 warehouse: warehouse
262 },
263 callback: (r) => {
264 this.grid_row.on_grid_fields_dict
265 .actual_qty.set_value(r.message || 0);
266 }
267 })
268 }
269 },
270 }, {
271 fieldtype:'Float',
272 fieldname:"actual_qty",
273 default: 0,
274 read_only: 1,
275 in_list_view: 1,
276 label: __('Available Qty')
277 }]
278 },
279 ],
280 primary_action: function() {
281 const args = this.get_values()["alternative_items"];
282 const alternative_items = args.filter(d => {
283 if (d.alternate_item && d.item_code != d.alternate_item) {
284 return true;
285 }
286 });
287
288 alternative_items.forEach(d => {
289 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800290 let qty = null;
291 if (row.doctype === 'Work Order Item') {
292 qty = row.required_qty;
293 } else {
294 qty = row.qty;
295 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530296 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530297 frm.script_manager.trigger(item_field, row.doctype, row.name)
298 .then(() => {
299 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
300 frappe.model.set_value(row.doctype, row.name,
301 opts.original_item_field, d.item_code);
302 });
303 });
304
305 refresh_field(opts.child_docname);
306 this.hide();
307 },
308 primary_action_label: __('Update')
309 });
310
311 frm.doc[opts.child_docname].forEach(d => {
312 if (!opts.condition || opts.condition(d)) {
313 dialog.fields_dict.alternative_items.df.data.push({
314 "docname": d.name,
315 "item_code": d[item_field],
316 "warehouse": d[warehouse_field],
317 "actual_qty": d.actual_qty
318 });
319 }
320 })
321
322 this.data = dialog.fields_dict.alternative_items.df.data;
323 dialog.fields_dict.alternative_items.grid.refresh();
324 dialog.show();
325}
326
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530327erpnext.utils.map_current_doc = function(opts) {
328 if(opts.get_query_filters) {
329 opts.get_query = function() {
330 return {filters: opts.get_query_filters};
331 }
332 }
333 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530334 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530335 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530336 if(!cur_frm.doc.items[0].item_code) {
337 cur_frm.doc.items = cur_frm.doc.items.splice(1);
338 }
bhupen3c7e70f2016-11-30 14:32:11 +0530339
340 // find the doctype of the items table
341 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530342
bhupen3c7e70f2016-11-30 14:32:11 +0530343 // find the link fieldname from items table for the given
344 // source_doctype
345 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530346 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530347 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
348
Nabin Hait802b4352017-01-09 15:32:20 +0530349 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530350 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530351 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530352
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530353 $.each(cur_frm.doc.items, function(i, d) {
354 opts.source_name.forEach(function(src) {
355 if(d[link_fieldname]==src) {
356 already_set = true;
357 if (item_qty_map[d.item_code])
358 item_qty_map[d.item_code] += flt(d.qty);
359 else
360 item_qty_map[d.item_code] = flt(d.qty);
361 }
362 });
363 });
364
365 if(already_set) {
366 opts.source_name.forEach(function(src) {
367 frappe.model.with_doc(opts.source_doctype, src, function(r) {
368 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
369 $.each(source_doc.items || [], function(i, row) {
370 if(row.qty > flt(item_qty_map[row.item_code])) {
371 already_set = false;
372 return false;
373 }
374 })
375 })
376
377 if(already_set) {
378 frappe.msgprint(__("You have already selected items from {0} {1}",
379 [opts.source_doctype, src]));
380 return;
381 }
382
383 })
Nabin Hait802b4352017-01-09 15:32:20 +0530384 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530385 }
386
387 return frappe.call({
388 // Sometimes we hit the limit for URL length of a GET request
389 // as we send the full target_doc. Hence this is a POST request.
390 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530391 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530392 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530393 "method": opts.method,
394 "source_names": opts.source_name,
395 "target_doc": cur_frm.doc,
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530396 },
397 callback: function(r) {
398 if(!r.exc) {
399 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530400 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530401 cur_frm.refresh();
402 }
403 }
404 });
405 }
406 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530407 var d = new frappe.ui.form.MultiSelectDialog({
408 doctype: opts.source_doctype,
409 target: opts.target,
410 date_field: opts.date_field || undefined,
411 setters: opts.setters,
412 get_query: opts.get_query,
413 action: function(selections, args) {
414 let values = selections;
415 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530416 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530417 return;
418 }
419 opts.source_name = values;
420 opts.setters = args;
421 d.dialog.hide();
422 _map();
423 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530424 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530425 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530426 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530427 _map();
428 }
429}
430
Rushabh Mehta180e4352016-07-26 17:57:42 +0530431frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530432 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530433 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530434 } else {
435 return value;
436 }
437}
438
439frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530440 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530441 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530442 } else {
443 return value;
444 }
445}
446
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530447// add description on posting time
448$(document).on('app_ready', function() {
449 if(!frappe.datetime.is_timezone_same()) {
450 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
451 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
452 frappe.ui.form.on(d, "onload", function(frm) {
453 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530454 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530455 });
456 });
457 }
akshay14384c22016-07-28 13:53:17 +0530458});