blob: efb04d1c093727d29f045539fcf6c99a63a5e724 [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
Makarand Bauskare6712c12017-10-25 12:17:40 +053040 stale_rate_allowed: () => {
41 return cint(frappe.boot.sysdefaults.allow_stale) || 1;
42 },
43
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053044 setup_serial_no: function() {
45 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053046 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
47 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053048
Pratik Vyasb52618c2014-04-14 16:25:30 +053049 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053050 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053051 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053052 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053053
Rushabh Mehta62030e02013-08-14 18:37:28 +053054 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053055 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053056 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053057 fields: [
58 {
59 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053060 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053061 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053062 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053063 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053064 return {
65 filters: {
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053066 item_code:grid_row.doc.item_code ,
Nabin Haitb7c0c552015-08-04 12:18:12 +053067 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053068 }
69 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053070 }
71 },
72 {
73 "fieldtype": "Button",
Kanchan Chauhan69818702017-02-27 12:53:05 +053074 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053075 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053076 }
77 ]
78 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053079
Rushabh Mehta62030e02013-08-14 18:37:28 +053080 d.get_input("add").on("click", function() {
81 var serial_no = d.get_value("serial_no");
82 if(serial_no) {
83 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053084 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053085 }
86 d.hide();
87 return false;
88 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053089
Rushabh Mehta62030e02013-08-14 18:37:28 +053090 d.show();
91 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053092 }
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) {
99 var info = frm.doc.__onload.dashboard_info;
100 frm.dashboard.add_indicator(__('Annual Billing: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530101 [format_currency(info.billing_this_year, info.currency)]), 'blue');
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530102 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530103 [format_currency(info.total_unpaid, info.currency)]),
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530104 info.total_unpaid ? 'orange' : 'green');
105 }
106 },
107
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530108 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
109 var d = locals[dt][dn];
110 if(d[fieldname]){
111 var cl = doc[table_fieldname] || [];
112 for(var i = 0; i < cl.length; i++) {
113 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
114 }
115 }
116 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530117 },
118
119 get_terms: function(tc_name, doc, callback) {
120 if(tc_name) {
121 return frappe.call({
122 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
123 args: {
124 template_name: tc_name,
125 doc: doc
126 },
127 callback: function(r) {
128 callback(r)
129 }
130 });
131 }
tundebabzy1a947db2017-08-29 13:48:27 +0100132 },
133
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530134 make_subscription: function(doctype, docname) {
135 frappe.call({
rohitwaghchauredd092e32017-09-15 16:16:36 +0530136 method: "erpnext.accounts.doctype.subscription.subscription.make_subscription",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530137 args: {
138 doctype: doctype,
139 docname: docname
140 },
141 callback: function(r) {
142 var doclist = frappe.model.sync(r.message);
143 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
144 }
145 })
146 },
147
tundebabzy1a947db2017-08-29 13:48:27 +0100148 /**
149 * Checks if the first row of a given child table is empty
150 * @param child_table - Child table Doctype
151 * @return {Boolean}
152 **/
153 first_row_is_empty: function(child_table){
154 if($.isArray(child_table) && child_table.length > 0) {
155 return !child_table[0].item_code;
156 }
157 return false;
158 },
159
160 /**
161 * Removes the first row of a child table if it is empty
162 * @param {_Frm} frm - The current form
163 * @param {String} child_table_name - The child table field name
164 * @return {Boolean}
165 **/
166 remove_empty_first_row: function(frm, child_table_name){
167 const rows = frm['doc'][child_table_name];
168 if (this.first_row_is_empty(rows)){
169 frm['doc'][child_table_name] = rows.splice(1);
170 }
171 return rows;
172 },
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530173});
174
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530175erpnext.utils.map_current_doc = function(opts) {
176 if(opts.get_query_filters) {
177 opts.get_query = function() {
178 return {filters: opts.get_query_filters};
179 }
180 }
181 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530182 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530183 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530184 if(!cur_frm.doc.items[0].item_code) {
185 cur_frm.doc.items = cur_frm.doc.items.splice(1);
186 }
bhupen3c7e70f2016-11-30 14:32:11 +0530187
188 // find the doctype of the items table
189 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530190
bhupen3c7e70f2016-11-30 14:32:11 +0530191 // find the link fieldname from items table for the given
192 // source_doctype
193 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530194 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530195 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
196
Nabin Hait802b4352017-01-09 15:32:20 +0530197 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530198 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530199 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530200
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530201 $.each(cur_frm.doc.items, function(i, d) {
202 opts.source_name.forEach(function(src) {
203 if(d[link_fieldname]==src) {
204 already_set = true;
205 if (item_qty_map[d.item_code])
206 item_qty_map[d.item_code] += flt(d.qty);
207 else
208 item_qty_map[d.item_code] = flt(d.qty);
209 }
210 });
211 });
212
213 if(already_set) {
214 opts.source_name.forEach(function(src) {
215 frappe.model.with_doc(opts.source_doctype, src, function(r) {
216 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
217 $.each(source_doc.items || [], function(i, row) {
218 if(row.qty > flt(item_qty_map[row.item_code])) {
219 already_set = false;
220 return false;
221 }
222 })
223 })
224
225 if(already_set) {
226 frappe.msgprint(__("You have already selected items from {0} {1}",
227 [opts.source_doctype, src]));
228 return;
229 }
230
231 })
Nabin Hait802b4352017-01-09 15:32:20 +0530232 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530233 }
234
235 return frappe.call({
236 // Sometimes we hit the limit for URL length of a GET request
237 // as we send the full target_doc. Hence this is a POST request.
238 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530239 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530240 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530241 "method": opts.method,
242 "source_names": opts.source_name,
243 "target_doc": cur_frm.doc,
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530244 },
245 callback: function(r) {
246 if(!r.exc) {
247 var doc = frappe.model.sync(r.message);
248 cur_frm.refresh();
249 }
250 }
251 });
252 }
253 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530254 var d = new frappe.ui.form.MultiSelectDialog({
255 doctype: opts.source_doctype,
256 target: opts.target,
257 date_field: opts.date_field || undefined,
258 setters: opts.setters,
259 get_query: opts.get_query,
260 action: function(selections, args) {
261 let values = selections;
262 if(values.length === 0){
263 frappe.msgprint(__("Please select Quotations"))
264 return;
265 }
266 opts.source_name = values;
267 opts.setters = args;
268 d.dialog.hide();
269 _map();
270 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530271 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530272 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530273 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530274 _map();
275 }
276}
277
Rushabh Mehta180e4352016-07-26 17:57:42 +0530278frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530279 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530280 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530281 } else {
282 return value;
283 }
284}
285
286frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530287 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530288 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530289 } else {
290 return value;
291 }
292}
293
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530294// add description on posting time
295$(document).on('app_ready', function() {
296 if(!frappe.datetime.is_timezone_same()) {
297 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
298 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
299 frappe.ui.form.on(d, "onload", function(frm) {
300 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530301 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530302 });
303 });
304 }
akshay14384c22016-07-28 13:53:17 +0530305});