blob: bf6941b34752893ba92a9d7b014e531c2340f2b4 [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
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053034 setup_serial_no: function() {
35 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053036 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
37 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053038
Pratik Vyasb52618c2014-04-14 16:25:30 +053039 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053040 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053041 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053042 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053043
Rushabh Mehta62030e02013-08-14 18:37:28 +053044 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053045 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053046 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053047 fields: [
48 {
49 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053050 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053051 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053052 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053053 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053054 return {
55 filters: {
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053056 item_code:grid_row.doc.item_code ,
Nabin Haitb7c0c552015-08-04 12:18:12 +053057 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053058 }
59 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053060 }
61 },
62 {
63 "fieldtype": "Button",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053064 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053065 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053066 }
67 ]
68 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053069
Rushabh Mehta62030e02013-08-14 18:37:28 +053070 d.get_input("add").on("click", function() {
71 var serial_no = d.get_value("serial_no");
72 if(serial_no) {
73 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053074 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053075 }
76 d.hide();
77 return false;
78 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053079
Rushabh Mehta62030e02013-08-14 18:37:28 +053080 d.show();
81 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053082 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053083});
Rushabh Mehta7d368752014-11-24 14:16:47 +053084
Nabin Haitb072c742014-12-16 12:49:13 +053085
86$.extend(erpnext.utils, {
Nabin Hait915778f2015-10-30 14:43:13 +053087 clear_address_and_contact: function(frm) {
88 $(frm.fields_dict['address_html'].wrapper).html("");
89 frm.fields_dict['contact_html'] && $(frm.fields_dict['contact_html'].wrapper).html("");
90 },
Rushabh Mehta621283c2016-04-21 19:00:34 +053091
Rushabh Mehta7d368752014-11-24 14:16:47 +053092 render_address_and_contact: function(frm) {
93 // render address
94 $(frm.fields_dict['address_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +053095 .html(frappe.render_template("address_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +053096 cur_frm.doc.__onload))
97 .find(".btn-address").on("click", function() {
Rushabh Mehtaaa60fd92016-06-01 11:46:33 +053098 frappe.new_doc("Address");
Rushabh Mehta7d368752014-11-24 14:16:47 +053099 });
100
101 // render contact
102 if(frm.fields_dict['contact_html']) {
103 $(frm.fields_dict['contact_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +0530104 .html(frappe.render_template("contact_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +0530105 cur_frm.doc.__onload))
106 .find(".btn-contact").on("click", function() {
Rushabh Mehtaaa60fd92016-06-01 11:46:33 +0530107 frappe.new_doc("Contact");
Rushabh Mehta7d368752014-11-24 14:16:47 +0530108 }
109 );
110 }
Anand Doshi06ad3082015-09-23 12:46:59 +0530111 },
112
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530113 set_party_dashboard_indicators: function(frm) {
114 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
115 var info = frm.doc.__onload.dashboard_info;
116 frm.dashboard.add_indicator(__('Annual Billing: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530117 [format_currency(info.billing_this_year, info.currency)]), 'blue');
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530118 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
Rohit Waghchaurec857f372016-12-05 17:09:30 +0530119 [format_currency(info.total_unpaid, info.currency)]),
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530120 info.total_unpaid ? 'orange' : 'green');
121 }
122 },
123
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530124 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
125 var d = locals[dt][dn];
126 if(d[fieldname]){
127 var cl = doc[table_fieldname] || [];
128 for(var i = 0; i < cl.length; i++) {
129 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
130 }
131 }
132 refresh_field(table_fieldname);
Rushabh Mehta7d368752014-11-24 14:16:47 +0530133 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530134});
135
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530136erpnext.utils.map_current_doc = function(opts) {
137 if(opts.get_query_filters) {
138 opts.get_query = function() {
139 return {filters: opts.get_query_filters};
140 }
141 }
142 var _map = function() {
143 // remove first item row if empty
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530144 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530145 if(!cur_frm.doc.items[0].item_code) {
146 cur_frm.doc.items = cur_frm.doc.items.splice(1);
147 }
bhupen3c7e70f2016-11-30 14:32:11 +0530148
149 // find the doctype of the items table
150 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
151
152 // find the link fieldname from items table for the given
153 // source_doctype
154 var link_fieldname = null;
155 frappe.get_meta(items_doctype).fields.forEach(function(d) {
156 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
157
Nabin Hait802b4352017-01-09 15:32:20 +0530158 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530159 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530160 var item_qty_map = {};
161
bhupen3c7e70f2016-11-30 14:32:11 +0530162 $.each(cur_frm.doc.items, function(i, d) {
163 if(d[link_fieldname]==opts.source_name) {
164 already_set = true;
Nabin Hait802b4352017-01-09 15:32:20 +0530165 if (item_qty_map[d.item_code])
166 item_qty_map[d.item_code] += flt(d.qty);
167 else
168 item_qty_map[d.item_code] = flt(d.qty);
bhupen3c7e70f2016-11-30 14:32:11 +0530169 }
170 });
Nabin Hait802b4352017-01-09 15:32:20 +0530171
bhupen3c7e70f2016-11-30 14:32:11 +0530172 if(already_set) {
Nabin Hait802b4352017-01-09 15:32:20 +0530173 frappe.model.with_doc(opts.source_doctype, opts.source_name, function(r) {
174 var source_doc = frappe.model.get_doc(opts.source_doctype, opts.source_name);
175 $.each(source_doc.items || [], function(i, row) {
176 if(row.qty > flt(item_qty_map[row.item_code])) {
177 already_set = false;
178 return false;
179 }
180 })
181 })
bhupen3c7e70f2016-11-30 14:32:11 +0530182
Nabin Hait802b4352017-01-09 15:32:20 +0530183 if(already_set) {
184 frappe.msgprint(__("You have already selected items from {0} {1}",
185 [opts.source_doctype, opts.source_name]));
186 return;
187 }
188 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530189 }
190
bhupen3c7e70f2016-11-30 14:32:11 +0530191
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530192 return frappe.call({
193 // Sometimes we hit the limit for URL length of a GET request
194 // as we send the full target_doc. Hence this is a POST request.
195 type: "POST",
196 method: opts.method,
197 args: {
198 "source_name": opts.source_name,
199 "target_doc": cur_frm.doc
200 },
201 callback: function(r) {
202 if(!r.exc) {
203 var doc = frappe.model.sync(r.message);
204 cur_frm.refresh();
205 }
206 }
207 });
208 }
209 if(opts.source_doctype) {
210 var d = new frappe.ui.Dialog({
211 title: __("Get From ") + __(opts.source_doctype),
212 fields: [
213 {
214 fieldtype: "Link",
215 label: __(opts.source_doctype),
216 fieldname: opts.source_doctype,
217 options: opts.source_doctype,
218 get_query: opts.get_query,
219 reqd:1
220 },
221 ]
222 });
223 d.set_primary_action(__('Get Items'), function() {
224 var values = d.get_values();
225 if(!values)
226 return;
227 opts.source_name = values[opts.source_doctype];
228 d.hide();
229 _map();
230 })
231 d.show();
232 } else if(opts.source_name) {
233 _map();
234 }
235}
236
Rushabh Mehta180e4352016-07-26 17:57:42 +0530237frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530238 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530239 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530240 } else {
241 return value;
242 }
243}
244
245frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530246 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530247 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530248 } else {
249 return value;
250 }
251}
252
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530253// add description on posting time
254$(document).on('app_ready', function() {
255 if(!frappe.datetime.is_timezone_same()) {
256 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
257 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
258 frappe.ui.form.on(d, "onload", function(frm) {
259 cur_frm.set_df_property("posting_time", "description",
260 sys_defaults.time_zone);
261 });
262 });
263 }
akshay14384c22016-07-28 13:53:17 +0530264});