blob: 721f216888bbbf94e2a74ca97d70039a55eb101f [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
Anand Doshi8bde7f92014-04-24 18:11:49 +053016 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053017 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053018 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
19 }
20 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053021
Anand Doshibdee6e02013-07-09 13:03:39 +053022 hide_company: function() {
23 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053024 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053025 if(companies.length === 1) {
26 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
27 cur_frm.toggle_display("company", false);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053028 } else if(erpnext.last_selected_company) {
29 if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053030 }
31 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053032 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053033
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053034 is_perpetual_inventory_enabled: function(company) {
35 if(company) {
36 return frappe.get_doc(":Company", company).enable_perpetual_inventory
37 }
38 },
39
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053040 setup_serial_no: function() {
41 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053042 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
43 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053044
Pratik Vyasb52618c2014-04-14 16:25:30 +053045 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053046 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053047 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053048 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053049
Rushabh Mehta62030e02013-08-14 18:37:28 +053050 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053051 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053052 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053053 fields: [
54 {
55 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053056 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053057 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053058 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053059 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053060 return {
61 filters: {
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053062 item_code:grid_row.doc.item_code ,
Nabin Haitb7c0c552015-08-04 12:18:12 +053063 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053064 }
65 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053066 }
67 },
68 {
69 "fieldtype": "Button",
Kanchan Chauhan69818702017-02-27 12:53:05 +053070 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053071 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053072 }
73 ]
74 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053075
Rushabh Mehta62030e02013-08-14 18:37:28 +053076 d.get_input("add").on("click", function() {
77 var serial_no = d.get_value("serial_no");
78 if(serial_no) {
79 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053080 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053081 }
82 d.hide();
83 return false;
84 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053085
Rushabh Mehta62030e02013-08-14 18:37:28 +053086 d.show();
87 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053088 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053089});
Rushabh Mehta7d368752014-11-24 14:16:47 +053090
Nabin Haitb072c742014-12-16 12:49:13 +053091
92$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053093 set_party_dashboard_indicators: function(frm) {
94 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
95 var info = frm.doc.__onload.dashboard_info;
96 frm.dashboard.add_indicator(__('Annual Billing: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +053097 [format_currency(info.billing_this_year, info.currency)]), 'blue');
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053098 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +053099 [format_currency(info.total_unpaid, info.currency)]),
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530100 info.total_unpaid ? 'orange' : 'green');
101 }
102 },
103
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530104 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
105 var d = locals[dt][dn];
106 if(d[fieldname]){
107 var cl = doc[table_fieldname] || [];
108 for(var i = 0; i < cl.length; i++) {
109 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
110 }
111 }
112 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530113 },
114
115 get_terms: function(tc_name, doc, callback) {
116 if(tc_name) {
117 return frappe.call({
118 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
119 args: {
120 template_name: tc_name,
121 doc: doc
122 },
123 callback: function(r) {
124 callback(r)
125 }
126 });
127 }
tundebabzy1a947db2017-08-29 13:48:27 +0100128 },
129
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530130 make_subscription: function(doctype, docname) {
131 frappe.call({
rohitwaghchauredd092e32017-09-15 16:16:36 +0530132 method: "erpnext.accounts.doctype.subscription.subscription.make_subscription",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530133 args: {
134 doctype: doctype,
135 docname: docname
136 },
137 callback: function(r) {
138 var doclist = frappe.model.sync(r.message);
139 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
140 }
141 })
142 },
143
tundebabzy1a947db2017-08-29 13:48:27 +0100144 /**
145 * Checks if the first row of a given child table is empty
146 * @param child_table - Child table Doctype
147 * @return {Boolean}
148 **/
149 first_row_is_empty: function(child_table){
150 if($.isArray(child_table) && child_table.length > 0) {
151 return !child_table[0].item_code;
152 }
153 return false;
154 },
155
156 /**
157 * Removes the first row of a child table if it is empty
158 * @param {_Frm} frm - The current form
159 * @param {String} child_table_name - The child table field name
160 * @return {Boolean}
161 **/
162 remove_empty_first_row: function(frm, child_table_name){
163 const rows = frm['doc'][child_table_name];
164 if (this.first_row_is_empty(rows)){
165 frm['doc'][child_table_name] = rows.splice(1);
166 }
167 return rows;
168 },
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530169});
170
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530171erpnext.utils.map_current_doc = function(opts) {
172 if(opts.get_query_filters) {
173 opts.get_query = function() {
174 return {filters: opts.get_query_filters};
175 }
176 }
177 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530178 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530179 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530180 if(!cur_frm.doc.items[0].item_code) {
181 cur_frm.doc.items = cur_frm.doc.items.splice(1);
182 }
bhupen3c7e70f2016-11-30 14:32:11 +0530183
184 // find the doctype of the items table
185 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530186
bhupen3c7e70f2016-11-30 14:32:11 +0530187 // find the link fieldname from items table for the given
188 // source_doctype
189 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530190 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530191 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
192
Nabin Hait802b4352017-01-09 15:32:20 +0530193 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530194 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530195 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530196
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530197 $.each(cur_frm.doc.items, function(i, d) {
198 opts.source_name.forEach(function(src) {
199 if(d[link_fieldname]==src) {
200 already_set = true;
201 if (item_qty_map[d.item_code])
202 item_qty_map[d.item_code] += flt(d.qty);
203 else
204 item_qty_map[d.item_code] = flt(d.qty);
205 }
206 });
207 });
208
209 if(already_set) {
210 opts.source_name.forEach(function(src) {
211 frappe.model.with_doc(opts.source_doctype, src, function(r) {
212 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
213 $.each(source_doc.items || [], function(i, row) {
214 if(row.qty > flt(item_qty_map[row.item_code])) {
215 already_set = false;
216 return false;
217 }
218 })
219 })
220
221 if(already_set) {
222 frappe.msgprint(__("You have already selected items from {0} {1}",
223 [opts.source_doctype, src]));
224 return;
225 }
226
227 })
Nabin Hait802b4352017-01-09 15:32:20 +0530228 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530229 }
230
231 return frappe.call({
232 // Sometimes we hit the limit for URL length of a GET request
233 // as we send the full target_doc. Hence this is a POST request.
234 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530235 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530236 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530237 "method": opts.method,
238 "source_names": opts.source_name,
239 "target_doc": cur_frm.doc,
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530240 },
241 callback: function(r) {
242 if(!r.exc) {
243 var doc = frappe.model.sync(r.message);
244 cur_frm.refresh();
245 }
246 }
247 });
248 }
249 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530250 var d = new frappe.ui.form.MultiSelectDialog({
251 doctype: opts.source_doctype,
252 target: opts.target,
253 date_field: opts.date_field || undefined,
254 setters: opts.setters,
255 get_query: opts.get_query,
256 action: function(selections, args) {
257 let values = selections;
258 if(values.length === 0){
259 frappe.msgprint(__("Please select Quotations"))
260 return;
261 }
262 opts.source_name = values;
263 opts.setters = args;
264 d.dialog.hide();
265 _map();
266 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530267 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530268 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530269 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530270 _map();
271 }
272}
273
Rushabh Mehta180e4352016-07-26 17:57:42 +0530274frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530275 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530276 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530277 } else {
278 return value;
279 }
280}
281
282frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530283 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530284 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530285 } else {
286 return value;
287 }
288}
289
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530290// add description on posting time
291$(document).on('app_ready', function() {
292 if(!frappe.datetime.is_timezone_same()) {
293 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
294 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
295 frappe.ui.form.on(d, "onload", function(frm) {
296 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530297 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530298 });
299 });
300 }
akshay14384c22016-07-28 13:53:17 +0530301});