blob: ece82844be58daeb62e8b3fc65bf43bc9353f6d9 [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();
Rushabh Mehta5a39d412016-07-20 22:53:23 +053036 if(!grid_row || !grid_row.fields_dict.serial_no ||
Anand Doshif83cf582013-08-20 15:36:10 +053037 grid_row.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"})
Rushabh Mehta62030e02013-08-14 18:37:28 +053042 .appendTo(grid_row.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",
50 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053051 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053052 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053053 return {
54 filters: {
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053055 item_code:grid_row.doc.item_code ,
Nabin Haitb7c0c552015-08-04 12:18:12 +053056 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053057 }
58 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053059 }
60 },
61 {
62 "fieldtype": "Button",
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053064 }
65 ]
66 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053067
Rushabh Mehta62030e02013-08-14 18:37:28 +053068 d.get_input("add").on("click", function() {
69 var serial_no = d.get_value("serial_no");
70 if(serial_no) {
71 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
72 grid_row.fields_dict.serial_no.set_model_value(val.trim());
73 }
74 d.hide();
75 return false;
76 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053077
Rushabh Mehta62030e02013-08-14 18:37:28 +053078 d.show();
79 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053080 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053081});
Rushabh Mehta7d368752014-11-24 14:16:47 +053082
Nabin Haitb072c742014-12-16 12:49:13 +053083
84$.extend(erpnext.utils, {
Nabin Hait915778f2015-10-30 14:43:13 +053085 clear_address_and_contact: function(frm) {
86 $(frm.fields_dict['address_html'].wrapper).html("");
87 frm.fields_dict['contact_html'] && $(frm.fields_dict['contact_html'].wrapper).html("");
88 },
Rushabh Mehta621283c2016-04-21 19:00:34 +053089
Rushabh Mehta7d368752014-11-24 14:16:47 +053090 render_address_and_contact: function(frm) {
91 // render address
92 $(frm.fields_dict['address_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +053093 .html(frappe.render_template("address_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +053094 cur_frm.doc.__onload))
95 .find(".btn-address").on("click", function() {
Rushabh Mehtaaa60fd92016-06-01 11:46:33 +053096 frappe.new_doc("Address");
Rushabh Mehta7d368752014-11-24 14:16:47 +053097 });
98
99 // render contact
100 if(frm.fields_dict['contact_html']) {
101 $(frm.fields_dict['contact_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +0530102 .html(frappe.render_template("contact_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +0530103 cur_frm.doc.__onload))
104 .find(".btn-contact").on("click", function() {
Rushabh Mehtaaa60fd92016-06-01 11:46:33 +0530105 frappe.new_doc("Contact");
Rushabh Mehta7d368752014-11-24 14:16:47 +0530106 }
107 );
108 }
Anand Doshi06ad3082015-09-23 12:46:59 +0530109 },
110
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530111 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
112 var d = locals[dt][dn];
113 if(d[fieldname]){
114 var cl = doc[table_fieldname] || [];
115 for(var i = 0; i < cl.length; i++) {
116 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
117 }
118 }
119 refresh_field(table_fieldname);
Rushabh Mehta7d368752014-11-24 14:16:47 +0530120 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530121});
122
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530123erpnext.utils.map_current_doc = function(opts) {
124 if(opts.get_query_filters) {
125 opts.get_query = function() {
126 return {filters: opts.get_query_filters};
127 }
128 }
129 var _map = function() {
130 // remove first item row if empty
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530131 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530132 if(!cur_frm.doc.items[0].item_code) {
133 cur_frm.doc.items = cur_frm.doc.items.splice(1);
134 }
135 }
136
137 return frappe.call({
138 // Sometimes we hit the limit for URL length of a GET request
139 // as we send the full target_doc. Hence this is a POST request.
140 type: "POST",
141 method: opts.method,
142 args: {
143 "source_name": opts.source_name,
144 "target_doc": cur_frm.doc
145 },
146 callback: function(r) {
147 if(!r.exc) {
148 var doc = frappe.model.sync(r.message);
149 cur_frm.refresh();
150 }
151 }
152 });
153 }
154 if(opts.source_doctype) {
155 var d = new frappe.ui.Dialog({
156 title: __("Get From ") + __(opts.source_doctype),
157 fields: [
158 {
159 fieldtype: "Link",
160 label: __(opts.source_doctype),
161 fieldname: opts.source_doctype,
162 options: opts.source_doctype,
163 get_query: opts.get_query,
164 reqd:1
165 },
166 ]
167 });
168 d.set_primary_action(__('Get Items'), function() {
169 var values = d.get_values();
170 if(!values)
171 return;
172 opts.source_name = values[opts.source_doctype];
173 d.hide();
174 _map();
175 })
176 d.show();
177 } else if(opts.source_name) {
178 _map();
179 }
180}
181
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530182// add description on posting time
183$(document).on('app_ready', function() {
184 if(!frappe.datetime.is_timezone_same()) {
185 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
186 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
187 frappe.ui.form.on(d, "onload", function(frm) {
188 cur_frm.set_df_property("posting_time", "description",
189 sys_defaults.time_zone);
190 });
191 });
192 }
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530193});