blob: f409f617f37b9fc41a423f224ee6dc9b6401ebc9 [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
Anand Doshi3543f302013-05-24 19:25:01 +05303
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05304erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05305 setup: function() {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05306 this._super();
Rohit Waghchaure6ab63032017-09-01 15:28:44 +05307 frappe.flags.hide_serial_batch_dialog = false;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05308 frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
9 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053010 var has_margin_field = frappe.meta.has_field(cdt, 'margin_type');
11
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053012 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
13
14 if(item.price_list_rate) {
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053015 if(item.rate > item.price_list_rate && has_margin_field) {
16 // if rate is greater than price_list_rate, set margin
17 // or set discount
18 item.discount_percentage = 0;
Nabin Haite72c98d2017-05-24 09:01:44 +053019 item.margin_type = 'Amount';
Rushabh Mehta36311022017-06-20 09:30:53 +053020 item.margin_rate_or_amount = flt(item.rate - item.price_list_rate,
Nabin Haite72c98d2017-05-24 09:01:44 +053021 precision("margin_rate_or_amount", item));
22 item.rate_with_margin = item.rate;
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053023 } else {
Rushabh Mehta36311022017-06-20 09:30:53 +053024 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053025 precision("discount_percentage", item));
26 item.margin_type = '';
27 item.margin_rate_or_amount = 0;
28 item.rate_with_margin = 0;
29 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053030 } else {
31 item.discount_percentage = 0.0;
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053032 item.margin_type = '';
33 item.margin_rate_or_amount = 0;
34 item.rate_with_margin = 0;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053035 }
Saif Ur Rehman147fa732018-11-26 18:14:06 +050036 item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate);
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053037
38 cur_frm.cscript.set_gross_profit(item);
39 cur_frm.cscript.calculate_taxes_and_totals();
Vishal Dhayaguded42242d2017-11-29 16:09:59 +053040
Rushabh Mehta30dc9a12017-11-17 14:31:09 +053041 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053042
Vishal Dhayaguded42242d2017-11-29 16:09:59 +053043
44
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053045 frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
46 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053047 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053048
49 frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
50 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053051 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053052
53 frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
54 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053055 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053056
57 frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
58 cur_frm.cscript.set_dynamic_labels();
59 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053060 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053061
62 frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
63 if(frm.doc.additional_discount_percentage) {
64 frm.trigger("additional_discount_percentage");
65 } else {
66 cur_frm.cscript.calculate_taxes_and_totals();
67 }
Rushabh Mehta764aa922016-05-02 13:28:46 +053068 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053069
70 frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053071 if(!frm.doc.apply_discount_on) {
72 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
Rushabh Mehta233a19a2017-07-07 13:05:11 +053073 return;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053074 }
75
76 frm.via_discount_percentage = true;
77
78 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
79 // Reset discount amount and net / grand total
Rushabh Mehta6705ab32017-07-07 13:14:08 +053080 frm.doc.discount_amount = 0;
81 frm.cscript.calculate_taxes_and_totals();
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053082 }
83
84 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
85 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
86 precision("discount_amount"));
87
Rushabh Mehta233a19a2017-07-07 13:05:11 +053088 frm.set_value("discount_amount", discount_amount)
89 .then(() => delete frm.via_discount_percentage);
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053090 });
91
92 frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
93 frm.cscript.set_dynamic_labels();
94
95 if (!frm.via_discount_percentage) {
Rushabh Mehta233a19a2017-07-07 13:05:11 +053096 frm.doc.additional_discount_percentage = 0;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053097 }
98
99 frm.cscript.calculate_taxes_and_totals();
100 });
Makarand Bauskarc196d742017-06-02 17:28:40 +0530101
Saif60ec88b2018-11-13 12:07:53 +0500102 frappe.ui.form.on(this.frm.doctype + " Item", {
103 items_add: function(frm, cdt, cdn) {
104 var item = frappe.get_doc(cdt, cdn);
105 if(!item.warehouse && frm.doc.set_warehouse) {
106 item.warehouse = frm.doc.set_warehouse;
107 }
108 }
109 });
110
Makarand Bauskarc196d742017-06-02 17:28:40 +0530111 var me = this;
112 if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
113 this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530114 return me.set_query_for_batch(doc, cdt, cdn);
Makarand Bauskarc196d742017-06-02 17:28:40 +0530115 });
116 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530117
tundebabzy590bd0a2017-12-25 07:10:19 +0100118 if(
Faris Ansari404f39d2018-05-14 15:46:45 +0530119 this.frm.docstatus < 2
Manas Solankia7f55892018-04-02 10:32:00 +0530120 && this.frm.fields_dict["payment_terms_template"]
tundebabzy590bd0a2017-12-25 07:10:19 +0100121 && this.frm.fields_dict["payment_schedule"]
122 && this.frm.doc.payment_terms_template
123 && !this.frm.doc.payment_schedule.length
124 ){
tundedf3a1752017-09-11 11:02:57 +0100125 this.frm.trigger("payment_terms_template");
126 }
127
Nabin Haitdd38a262014-12-26 13:15:21 +0530128 if(this.frm.fields_dict["taxes"]) {
129 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +0530130 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530131
Nabin Haitdd38a262014-12-26 13:15:21 +0530132 if(this.frm.fields_dict["items"]) {
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530133 this["items_remove"] = this.calculate_net_weight;
Anand Doshi1e4fb682013-08-23 12:56:33 +0530134 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530135
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530136 if(this.frm.fields_dict["recurring_print_format"]) {
137 this.frm.set_query("recurring_print_format", function(doc) {
138 return{
139 filters: [
140 ['Print Format', 'doc_type', '=', cur_frm.doctype],
141 ]
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530142 };
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530143 });
144 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530145
Nabin Hait1d218422015-07-17 15:19:02 +0530146 if(this.frm.fields_dict["return_against"]) {
147 this.frm.set_query("return_against", function(doc) {
148 var filters = {
149 "docstatus": 1,
150 "is_return": 0,
151 "company": doc.company
152 };
153 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
154 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530155
Nabin Hait1d218422015-07-17 15:19:02 +0530156 return {
157 filters: filters
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530158 };
Nabin Hait1d218422015-07-17 15:19:02 +0530159 });
160 }
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530161 },
162 onload: function() {
163 var me = this;
Rushabh Mehta6b537922017-03-14 16:59:11 +0530164
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530165 if(this.frm.doc.__islocal) {
166 var currency = frappe.defaults.get_user_default("currency");
167
168 let set_value = (fieldname, value) => {
169 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
170 return me.frm.set_value(fieldname, value);
171 }
172 };
173
174 return frappe.run_serially([
175 () => set_value('currency', currency),
176 () => set_value('price_list_currency', currency),
177 () => set_value('status', 'Draft'),
178 () => set_value('is_subcontracted', 'No'),
179 () => {
180 if(this.frm.doc.company && !this.frm.doc.amended_from) {
181 this.frm.trigger("company");
182 }
183 }
184 ]);
185 }
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530186 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530187
Rohit Waghchaureb4a59932018-11-26 20:02:01 +0530188 is_return: function() {
189 if(!this.frm.doc.is_return && this.frm.doc.return_against) {
190 this.frm.set_value('return_against', '');
191 }
192 },
193
Nabin Hait8af429d2016-11-16 17:21:59 +0530194 setup_quality_inspection: function() {
195 if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) {
196 return;
197 }
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530198 var me = this;
Rushabh Mehta6b537922017-03-14 16:59:11 +0530199 var inspection_type = in_list(["Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)
Nabin Hait8af429d2016-11-16 17:21:59 +0530200 ? "Incoming" : "Outgoing";
Rushabh Mehta6b537922017-03-14 16:59:11 +0530201
Manas Solankia8d1bc72016-11-15 14:48:23 +0530202 var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection");
Nabin Hait8af429d2016-11-16 17:21:59 +0530203 quality_inspection_field.get_route_options_for_new_doc = function(row) {
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530204 if(me.frm.is_new()) return;
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530205 return {
206 "inspection_type": inspection_type,
Manas Solanki34feab12016-11-16 14:36:28 +0530207 "reference_type": me.frm.doc.doctype,
Manas Solankidbc25112016-11-16 15:06:08 +0530208 "reference_name": me.frm.doc.name,
Nabin Hait8af429d2016-11-16 17:21:59 +0530209 "item_code": row.doc.item_code,
210 "description": row.doc.description,
211 "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null,
212 "batch_no": row.doc.batch_no
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530213 }
214 }
Rushabh Mehta6b537922017-03-14 16:59:11 +0530215
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530216 this.frm.set_query("quality_inspection", "items", function(doc, cdt, cdn) {
217 var d = locals[cdt][cdn];
218 return {
219 filters: {
220 docstatus: 1,
221 inspection_type: inspection_type,
rohitwaghchaure4e8fdf72018-02-22 11:03:48 +0530222 reference_name: doc.name,
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530223 item_code: d.item_code
224 }
225 }
226 });
Anand Doshi3543f302013-05-24 19:25:01 +0530227 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530228
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530229 make_payment_request: function() {
Rohit Waghchaure210f4982018-10-03 15:55:51 +0530230 var me = this;
231 const payment_request_type = (in_list(['Sales Order', 'Sales Invoice'], this.frm.doc.doctype))
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530232 ? "Inward" : "Outward";
233
234 frappe.call({
235 method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request",
236 args: {
237 dt: me.frm.doc.doctype,
238 dn: me.frm.doc.name,
239 recipient_id: me.frm.doc.contact_email,
240 payment_request_type: payment_request_type,
241 party_type: payment_request_type == 'Outward' ? "Supplier" : "Customer",
242 party: payment_request_type == 'Outward' ? me.frm.doc.supplier : me.frm.doc.customer
243 },
244 callback: function(r) {
245 if(!r.exc){
246 var doc = frappe.model.sync(r.message);
247 frappe.set_route("Form", r.message.doctype, r.message.name);
248 }
249 }
250 })
251 },
252
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530253 onload_post_render: function() {
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530254 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
255 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530256 frappe.after_ajax(() => this.apply_default_taxes());
Anand Doshi70f57eb2015-11-27 14:37:40 +0530257 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +0530258 && !this.frm.doc.is_pos) {
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530259 frappe.after_ajax(() => this.calculate_taxes_and_totals());
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530260 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530261 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
Rushabh Mehta203cc962016-04-07 15:25:43 +0530262 this.setup_item_selector();
Rushabh Mehtab6843f22014-06-04 13:10:41 +0530263 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530264 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530265
Anand Doshi3543f302013-05-24 19:25:01 +0530266 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +0530267 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +0530268 erpnext.hide_company();
Nabin Hait1f996c32013-07-29 19:00:53 +0530269 this.set_dynamic_labels();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530270 this.setup_sms();
Rohit Waghchauref2705672018-06-12 12:20:52 +0530271 this.setup_quality_inspection();
Zarrar69224152018-11-13 11:11:32 +0530272 this.frm.fields_dict["scan_barcode"] && this.frm.fields_dict["scan_barcode"].set_value("");
273 this.frm.fields_dict["scan_barcode"] && this.frm.fields_dict["scan_barcode"].set_new_description("");
274 },
275
276 scan_barcode: function() {
277 let scan_barcode_field = this.frm.fields_dict["scan_barcode"];
278
279 let show_description = function(idx, item_code, exist=null) {
280 if(exist) {
281 scan_barcode_field.set_new_description(__('Row : ') + idx + ' ' +
282 item_code + __(' Qty increased by 1'));
283 } else {
284 scan_barcode_field.set_new_description(__('New row : ') + idx + ' ' +
285 item_code + __(' Created'));
286 }
287 }
288
289 if(this.frm.doc.scan_barcode) {
290 frappe.call({
291 method: "erpnext.selling.page.point_of_sale.point_of_sale.search_serial_or_batch_or_barcode_number",
292 args: { search_value: this.frm.doc.scan_barcode }
293 }).then(r => {
294
295 if(r && r.message && r.message.item_code) {
296 let child = "";
297 let add_row_index = -1;
298 let cur_grid= this.frm.fields_dict["items"].grid;
299
300 this.frm.doc.items.map(d => {
301 if(d.item_code==r.message.item_code){
302 add_row_index = d.idx;
303 return;
304 } else if(!d.item_code && add_row_index==-1) {
305 add_row_index = d.idx;
306 }
307 });
308
309 if(add_row_index == -1) {
310 child = frappe.model.add_child(this.frm.doc, cur_grid.doctype, "items", add_row_index);
311 } else {
312 child = cur_grid.get_grid_row(add_row_index-1).doc;
313 }
314 show_description(child.idx, r.message.item_code, child.item_code);
315
316 frappe.model.set_value(child.doctype, child.name, {
317 "item_code": r.message.item_code,
318 "qty": (child.qty || 0) + 1
319 });
320 }
321 else{
322 scan_barcode_field.set_new_description(this.frm.doc.scan_barcode +__(' does not exist!'));
323 }
324 });
325 scan_barcode_field.set_value("");
326 }
327 return false;
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530328 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530329
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530330 apply_default_taxes: function() {
331 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530332 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
333 me.frm.doc.name);
334
rohitwaghchaure1f39feb2018-01-05 12:25:45 +0530335 if (!this.frm.doc.taxes_and_charges && this.frm.doc.taxes) {
336 return;
337 }
338
339 if (taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530340 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530341 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
342 args: {
rohitwaghchaure505661b2017-12-11 14:52:28 +0530343 "master_doctype": taxes_and_charges_field.options,
Nabin Haita2426fc2018-01-15 17:45:46 +0530344 "tax_template": me.frm.doc.taxes_and_charges,
rohitwaghchaure505661b2017-12-11 14:52:28 +0530345 "company": me.frm.doc.company
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530346 },
347 callback: function(r) {
Nabin Haita2426fc2018-01-15 17:45:46 +0530348 if(!r.exc && r.message) {
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530349 frappe.run_serially([
350 () => {
351 // directly set in doc, so as not to call triggers
rohitwaghchaure1bc47fa2017-12-16 10:49:58 +0530352 if(r.message.taxes_and_charges) {
353 me.frm.doc.taxes_and_charges = r.message.taxes_and_charges;
354 }
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530355
356 // set taxes table
rohitwaghchaure1bc47fa2017-12-16 10:49:58 +0530357 if(r.message.taxes) {
358 me.frm.set_value("taxes", r.message.taxes);
359 }
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530360 },
Saif3c9155e2018-10-18 17:27:56 +0500361 () => me.set_dynamic_labels(),
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530362 () => me.calculate_taxes_and_totals()
363 ]);
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530364 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530365 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530366 });
367 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530368 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530369
370 setup_sms: function() {
371 var me = this;
Nabin Hait7132bd32016-04-13 15:19:37 +0530372 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530373 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530374 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
375 }
376 },
377
378 send_sms: function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530379 var sms_man = new erpnext.SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530380 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530381
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530382 barcode: function(doc, cdt, cdn) {
383 var d = locals[cdt][cdn];
384 if(d.barcode=="" || d.barcode==null) {
385 // barcode cleared, remove item
386 d.item_code = "";
387 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530388 this.item_code(doc, cdt, cdn, true);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530389 },
390
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530391 item_code: function(doc, cdt, cdn, from_barcode) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530392 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530393 var item = frappe.get_doc(cdt, cdn);
pratu16x75f389c92017-06-26 13:38:24 +0530394 var update_stock = 0, show_batch_dialog = 0;
rohitwaghchaure5999ade2017-08-09 17:24:13 +0530395 if(['Sales Invoice'].includes(this.frm.doc.doctype)) {
pratu16x7577e4c42017-06-23 11:39:03 +0530396 update_stock = cint(me.frm.doc.update_stock);
397 show_batch_dialog = update_stock;
pratu16x7ae450fc2017-06-26 15:31:46 +0530398
399 } else if((this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return) ||
400 this.frm.doc.doctype === 'Delivery Note') {
pratu16x789d001c2017-06-26 17:45:49 +0530401 show_batch_dialog = 1;
pratu16x7577e4c42017-06-23 11:39:03 +0530402 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530403 // clear barcode if setting item (else barcode will take priority)
404 if(!from_barcode) {
405 item.barcode = null;
406 }
Nabin Hait139dc7b2014-02-12 14:53:18 +0530407 if(item.item_code || item.barcode || item.serial_no) {
408 if(!this.validate_company_and_party()) {
pratu16x7577e4c42017-06-23 11:39:03 +0530409 this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530410 } else {
411 return this.frm.call({
412 method: "erpnext.stock.get_item_details.get_item_details",
413 child: item,
414 args: {
415 args: {
416 item_code: item.item_code,
417 barcode: item.barcode,
418 serial_no: item.serial_no,
419 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530420 customer: me.frm.doc.customer,
421 supplier: me.frm.doc.supplier,
422 currency: me.frm.doc.currency,
pratu16x7577e4c42017-06-23 11:39:03 +0530423 update_stock: update_stock,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530424 conversion_rate: me.frm.doc.conversion_rate,
Faris Ansariab74ca72017-05-30 12:54:42 +0530425 price_list: me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530426 price_list_currency: me.frm.doc.price_list_currency,
427 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
428 company: me.frm.doc.company,
429 order_type: me.frm.doc.order_type,
430 is_pos: cint(me.frm.doc.is_pos),
431 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530432 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530433 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530434 doctype: me.frm.doc.doctype,
435 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530436 project: item.project || me.frm.doc.project,
mbauskarba412422017-04-19 12:35:51 +0530437 qty: item.qty || 1,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530438 stock_qty: item.stock_qty,
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530439 conversion_factor: item.conversion_factor,
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530440 weight_per_unit: item.weight_per_unit,
441 weight_uom: item.weight_uom,
Nabin Haite45ec662018-08-01 17:44:34 +0530442 uom : item.uom,
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400443 pos_profile: me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '',
Saif Ur Rehman67786682018-12-27 02:11:07 +0500444 cost_center: item.cost_center,
445 tax_category: me.frm.doc.tax_category,
446 item_tax_template: item.item_tax_template
Nabin Hait139dc7b2014-02-12 14:53:18 +0530447 }
448 },
Anand Doshi13945092014-09-21 19:45:49 +0530449
Nabin Hait139dc7b2014-02-12 14:53:18 +0530450 callback: function(r) {
451 if(!r.exc) {
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530452 frappe.run_serially([
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500453 () => {
454 var d = locals[cdt][cdn];
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500455 me.add_taxes_from_item_tax_template(d.item_tax_rate);
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500456 },
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530457 () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn),
458 () => me.toggle_conversion_factor(item),
459 () => {
460 if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) {
461 var d = locals[cdt][cdn];
462 $.each(r.message, function(k, v) {
463 if(!d[k]) d[k] = v;
464 });
Rohit Waghchaure6ab63032017-09-01 15:28:44 +0530465
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530466 erpnext.show_serial_batch_selector(me.frm, d, (item) => {
467 me.frm.script_manager.trigger('qty', item.doctype, item.name);
468 });
469 }
470 },
471 () => me.conversion_factor(doc, cdt, cdn, true)
472 ]);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530473 }
474 }
475 });
476 }
477 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530478 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530479
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500480 add_taxes_from_item_tax_template: function(item_tax_map) {
481 let me = this;
482
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500483 if(item_tax_map && cint(frappe.defaults.get_default("add_taxes_from_item_tax_template"))) {
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500484 if(typeof (item_tax_map) == "string") {
485 item_tax_map = JSON.parse(item_tax_map);
486 }
487
488 $.each(item_tax_map, function(tax, rate) {
489 let found = (me.frm.doc.taxes || []).find(d => d.account_head === tax);
490 if(!found) {
491 let child = frappe.model.add_child(me.frm.doc, "taxes");
492 child.charge_type = "On Net Total";
493 child.account_head = tax;
494 child.rate = 0;
495 }
496 });
497 }
498 },
499
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530500 serial_no: function(doc, cdt, cdn) {
501 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530502 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530503
504 if (item.serial_no) {
505 if (!item.item_code) {
Rushabh Mehtab66edd12017-03-30 21:21:56 +0530506 this.frm.trigger("item_code", cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530507 }
508 else {
Nabin Hait79c21912017-07-12 14:10:43 +0530509 var valid_serial_nos = [];
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530510
511 // Replacing all occurences of comma with carriage return
512 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
513
514 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530515
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530516 // Trim each string and push unique string to new list
517 for (var x=0; x<=serial_nos.length - 1; x++) {
Nabin Hait79c21912017-07-12 14:10:43 +0530518 if (serial_nos[x].trim() != "" && valid_serial_nos.indexOf(serial_nos[x].trim()) == -1) {
519 valid_serial_nos.push(serial_nos[x].trim());
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530520 }
521 }
522
523 // Add the new list to the serial no. field in grid with each in new line
Nabin Hait79c21912017-07-12 14:10:43 +0530524 item.serial_no = valid_serial_nos.join('\n');
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530525
526 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haited376ca2018-05-03 16:24:52 +0530527 if(!doc.is_return && cint(user_defaults.set_qty_in_transactions_based_on_serial_no_input)) {
Rushabh Mehta6b537922017-03-14 16:59:11 +0530528 frappe.model.set_value(item.doctype, item.name,
Nabin Hait79c21912017-07-12 14:10:43 +0530529 "qty", valid_serial_nos.length / item.conversion_factor);
530 frappe.model.set_value(item.doctype, item.name, "stock_qty", valid_serial_nos.length);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530531 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530532 }
533 }
534 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530535
Anand Doshi923d41d2013-05-28 17:23:36 +0530536 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530537 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530538 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530539
Anand Doshi3543f302013-05-24 19:25:01 +0530540 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530541 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530542 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530543 if(me.frm.doc.company && me.frm.fields_dict.currency) {
544 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530545 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
tundebabzy5510d072017-09-29 10:51:59 +0100546
Nabin Hait1f10d692017-10-02 13:20:51 +0530547 if (!me.frm.doc.currency) {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530548 me.frm.set_value("currency", company_currency);
549 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530550
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530551 if (me.frm.doc.currency == company_currency) {
552 me.frm.set_value("conversion_rate", 1.0);
553 }
554 if (me.frm.doc.price_list_currency == company_currency) {
555 me.frm.set_value('plc_conversion_rate', 1.0);
556 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530557 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530558 if(me.frm.fields_dict.letter_head) {
559 me.frm.set_value("letter_head", company_doc.default_letter_head);
560 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530561 }
Rohit Waghchaure585448b2016-12-30 15:41:55 +0530562 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice" && frappe.meta.has_field(me.frm.doc.doctype, "tc_name")) {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530563 me.frm.set_value("tc_name", company_doc.default_terms);
564 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530565
rohitwaghchaure505661b2017-12-11 14:52:28 +0530566 frappe.run_serially([
567 () => me.frm.script_manager.trigger("currency"),
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +0500568 () => me.update_item_tax_map(),
rohitwaghchaure505661b2017-12-11 14:52:28 +0530569 () => me.apply_default_taxes(),
570 () => me.apply_pricing_rule()
571 ]);
Nabin Haitdd38a262014-12-26 13:15:21 +0530572 }
Anand Doshi3543f302013-05-24 19:25:01 +0530573 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530574
Nabin Haita731ad42016-02-04 17:21:28 +0530575 var set_party_account = function(set_pricing) {
576 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
577 if(me.frm.doc.doctype=="Sales Invoice") {
578 var party_type = "Customer";
579 var party_account_field = 'debit_to';
580 } else {
581 var party_type = "Supplier";
582 var party_account_field = 'credit_to';
583 }
Anand Doshi39a28912016-02-15 11:42:18 +0530584
Nabin Haitef864402016-02-10 14:47:25 +0530585 var party = me.frm.doc[frappe.model.scrub(party_type)];
Faris Ansarif7717b92018-02-08 11:09:54 +0530586 if(party && me.frm.doc.company) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530587 return frappe.call({
588 method: "erpnext.accounts.party.get_party_account",
589 args: {
590 company: me.frm.doc.company,
591 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530592 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530593 },
594 callback: function(r) {
595 if(!r.exc && r.message) {
596 me.frm.set_value(party_account_field, r.message);
597 set_pricing();
598 }
Nabin Haita731ad42016-02-04 17:21:28 +0530599 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530600 });
Nabin Haitef864402016-02-10 14:47:25 +0530601 } else {
602 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530603 }
Nabin Haita731ad42016-02-04 17:21:28 +0530604 } else {
605 set_pricing();
606 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530607
Nabin Haita731ad42016-02-04 17:21:28 +0530608 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530609
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530610 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
611 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530612
613 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530614 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
Faris Ansariab74ca72017-05-30 12:54:42 +0530615 erpnext.utils.get_shipping_address(this.frm, function(){
616 set_party_account(set_pricing);
617 })
618 } else {
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530619 set_party_account(set_pricing);
620 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530621
622 if(this.frm.doc.company) {
623 erpnext.last_selected_company = this.frm.doc.company;
624 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530625 },
626
627 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530628 if (this.frm.doc.transaction_date) {
629 this.frm.transaction_date = this.frm.doc.transaction_date;
Nabin Hait288a18e2016-12-08 15:36:23 +0530630 frappe.ui.form.trigger(this.frm.doc.doctype, "currency");
Anand Doshi87da6622015-10-19 14:59:32 +0530631 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530632 },
633
634 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530635 var me = this;
636 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530637 this.frm.posting_date = this.frm.doc.posting_date;
638
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530639 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530640 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
641 return frappe.call({
642 method: "erpnext.accounts.party.get_due_date",
643 args: {
644 "posting_date": me.frm.doc.posting_date,
645 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
Shreya Shah3a9eec22018-02-16 13:05:21 +0530646 "bill_date": me.frm.doc.bill_date,
Nabin Hait40e92b62015-07-09 17:12:23 +0530647 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
648 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530649 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530650 callback: function(r, rt) {
651 if(r.message) {
Nabin Haitcfa9d1a2018-01-29 16:07:21 +0530652 me.frm.doc.due_date = r.message;
653 refresh_field("due_date");
Nabin Hait87d70272016-12-08 14:43:11 +0530654 frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
tundebabzy650a1ee2018-01-02 07:32:30 +0100655 me.recalculate_terms();
Nabin Hait40e92b62015-07-09 17:12:23 +0530656 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530657 }
658 })
Nabin Hait87d70272016-12-08 14:43:11 +0530659 } else {
660 frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
Nabin Hait40e92b62015-07-09 17:12:23 +0530661 }
662 }
Anand Doshi3543f302013-05-24 19:25:01 +0530663 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530664
tundebabzyda221672018-01-09 06:28:44 +0100665 due_date: function() {
666 // due_date is to be changed, payment terms template and/or payment schedule must
667 // be removed as due_date is automatically changed based on payment terms
rohitwaghchaure52beb772018-01-29 17:40:44 +0530668 if (this.frm.doc.due_date && !this.frm.updating_party_details && !this.frm.doc.is_pos) {
Saurabh3959aeb2018-01-23 15:40:29 +0530669 if (this.frm.doc.payment_terms_template ||
670 (this.frm.doc.payment_schedule && this.frm.doc.payment_schedule.length)) {
tundebabzyda221672018-01-09 06:28:44 +0100671 var message1 = "";
672 var message2 = "";
673 var final_message = "Please clear the ";
674
675 if (this.frm.doc.payment_terms_template) {
676 message1 = "selected Payment Terms Template";
677 final_message = final_message + message1;
678 }
679
Nabin Hait2bfa1802018-01-17 14:39:05 +0530680 if ((this.frm.doc.payment_schedule || []).length) {
tundebabzyda221672018-01-09 06:28:44 +0100681 message2 = "Payment Schedule Table";
682 if (message1.length !== 0) message2 = " and " + message2;
683 final_message = final_message + message2;
684 }
tundebabzyda221672018-01-09 06:28:44 +0100685 frappe.msgprint(final_message);
686 }
Nabin Haitcfa9d1a2018-01-29 16:07:21 +0530687 }
tundebabzyda221672018-01-09 06:28:44 +0100688 },
689
Shreya Shah3a9eec22018-02-16 13:05:21 +0530690 bill_date: function() {
691 this.posting_date();
692 },
693
tundebabzy650a1ee2018-01-02 07:32:30 +0100694 recalculate_terms: function() {
695 const doc = this.frm.doc;
tundebabzy650a1ee2018-01-02 07:32:30 +0100696 if (doc.payment_terms_template) {
697 this.payment_terms_template();
698 } else if (doc.payment_schedule) {
699 const me = this;
tundebabzy650a1ee2018-01-02 07:32:30 +0100700 doc.payment_schedule.forEach(
701 function(term) {
702 if (term.payment_term) {
703 me.payment_term(doc, term.doctype, term.name);
704 } else {
705 frappe.model.set_value(
706 term.doctype, term.name, 'due_date',
707 doc.posting_date || doc.transaction_date
708 );
709 }
710 }
711 );
712 }
713 },
714
Anand Doshi3543f302013-05-24 19:25:01 +0530715 get_company_currency: function() {
716 return erpnext.get_currency(this.frm.doc.company);
717 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530718
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530719 contact_person: function() {
720 erpnext.utils.get_contact_details(this.frm);
721 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530722
Anand Doshi3543f302013-05-24 19:25:01 +0530723 currency: function() {
Chude Osiegbu256ec172016-09-19 03:53:37 +0100724 /* manqala 19/09/2016: let the translation date be whichever of the transaction_date or posting_date is available */
Nabin Hait288a18e2016-12-08 15:36:23 +0530725 var transaction_date = this.frm.doc.transaction_date || this.frm.doc.posting_date;
Chude Osiegbu256ec172016-09-19 03:53:37 +0100726 /* end manqala */
Anand Doshi61a2f682013-06-21 17:55:31 +0530727 var me = this;
728 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530729 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530730 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Rushabh Mehta76b13412016-07-18 14:30:18 +0530731 if(this.frm.doc.currency && this.frm.doc.currency !== company_currency
Nabin Hait39505072016-07-07 11:11:32 +0530732 && !this.frm.doc.ignore_pricing_rule) {
Shreya0db85062018-05-15 11:25:46 +0530733
Nabin Hait288a18e2016-12-08 15:36:23 +0530734 this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530735 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530736 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530737 });
738 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530739 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530740 }
741 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530742
Anand Doshi61a2f682013-06-21 17:55:31 +0530743 conversion_rate: function() {
tundebabzyab5b0302017-09-21 10:20:39 +0100744 const me = this.frm;
Anand Doshi535d8332013-07-19 13:51:07 +0530745 if(this.frm.doc.currency === this.get_company_currency()) {
746 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530747 }
748 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530749 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530750 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530751 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530752
Nabin Haita3dd72a2014-05-28 12:49:20 +0530753 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530754 if(this.frm.doc.ignore_pricing_rule) {
755 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530756 } else if (!this.in_apply_price_list){
Nabin Haita9c9b692017-08-31 19:40:09 +0530757 this.set_actual_charges_based_on_currency();
Anand Doshi51f722d2014-07-04 15:44:26 +0530758 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530759 }
760
Nabin Haita3dd72a2014-05-28 12:49:20 +0530761 }
tundebabzyab5b0302017-09-21 10:20:39 +0100762 // Make read only if Accounts Settings doesn't allow stale rates
tundebabzybc7c3872017-11-28 18:10:05 +0100763 this.frm.set_df_property("conversion_rate", "read_only", erpnext.stale_rate_allowed() ? 0 : 1);
Anand Doshi3543f302013-05-24 19:25:01 +0530764 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530765
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530766 shipping_rule: function() {
767 var me = this;
768 if(this.frm.doc.shipping_rule) {
769 return this.frm.call({
770 doc: this.frm.doc,
771 method: "apply_shipping_rule",
772 callback: function(r) {
773 if(!r.exc) {
774 me.calculate_taxes_and_totals();
775 }
776 }
777 }).fail(() => this.frm.set_value('shipping_rule', ''));
778 }
Shreya39adc8f2018-01-22 13:08:05 +0530779 else {
780 me.calculate_taxes_and_totals();
781 }
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530782 },
783
Nabin Haita9c9b692017-08-31 19:40:09 +0530784 set_actual_charges_based_on_currency: function() {
785 var me = this;
786 $.each(this.frm.doc.taxes || [], function(i, d) {
787 if(d.charge_type == "Actual") {
788 frappe.model.set_value(d.doctype, d.name, "tax_amount",
789 flt(d.tax_amount) / flt(me.frm.doc.conversion_rate));
790 }
791 });
792 },
793
Nabin Hait288a18e2016-12-08 15:36:23 +0530794 get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) {
Shreya3f778522018-05-15 16:59:20 +0530795 var args;
796 if (["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"].includes(this.frm.doctype)) {
797 args = "for_selling";
Shreya0db85062018-05-15 11:25:46 +0530798 }
Shreya3f778522018-05-15 16:59:20 +0530799 else if (["Purchase Order", "Purchase Receipt", "Purchase Invoice"].includes(this.frm.doctype)) {
800 args = "for_buying";
801 }
802
Nabin Hait17179ee2017-05-06 13:23:12 +0530803 if (!transaction_date || !from_currency || !to_currency) return;
Nabin Haitbdfb0702015-10-28 15:38:08 +0530804 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530805 method: "erpnext.setup.utils.get_exchange_rate",
806 args: {
Nabin Hait288a18e2016-12-08 15:36:23 +0530807 transaction_date: transaction_date,
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530808 from_currency: from_currency,
Shreya0db85062018-05-15 11:25:46 +0530809 to_currency: to_currency,
810 args: args
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530811 },
812 callback: function(r) {
813 callback(flt(r.message));
814 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530815 });
816 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530817
Anand Doshi3543f302013-05-24 19:25:01 +0530818 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530819 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530820 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530821
822 var company_currency = this.get_company_currency();
823 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
824 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Chude Osiegbufbf27b52016-09-07 03:06:08 +0100825 this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency,
Nabin Haitec361ea2015-05-11 18:14:45 +0530826 function(exchange_rate) {
827 me.frm.set_value("plc_conversion_rate", exchange_rate);
828 });
829 } else {
830 this.plc_conversion_rate();
831 }
Anand Doshi3543f302013-05-24 19:25:01 +0530832 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530833
Anand Doshi3543f302013-05-24 19:25:01 +0530834 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530835 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
836 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530837 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530838 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530839 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530840 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530841 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530842
Nabin Haitec361ea2015-05-11 18:14:45 +0530843 if(!this.in_apply_price_list) {
tundebabzy6e90f492018-02-12 10:48:57 +0100844 this.apply_price_list(null, true);
Anand Doshi61a2f682013-06-21 17:55:31 +0530845 }
Anand Doshi3543f302013-05-24 19:25:01 +0530846 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530847
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530848 uom: function(doc, cdt, cdn) {
849 var me = this;
850 var item = frappe.get_doc(cdt, cdn);
851 if(item.item_code && item.uom) {
852 return this.frm.call({
853 method: "erpnext.stock.get_item_details.get_conversion_factor",
854 child: item,
855 args: {
856 item_code: item.item_code,
857 uom: item.uom
858 },
859 callback: function(r) {
860 if(!r.exc) {
861 me.conversion_factor(me.frm.doc, cdt, cdn);
862 }
863 }
864 });
865 }
866 },
867
Nabin Hait3257aee2017-05-06 12:47:14 +0530868 conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) {
Rohit Waghchaure4d4fb6d2018-05-15 22:12:44 +0530869 if(doc.doctype != 'Material Request' && frappe.meta.get_docfield(cdt, "stock_qty", cdn)) {
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530870 var item = frappe.get_doc(cdt, cdn);
871 frappe.model.round_floats_in(item, ["qty", "conversion_factor"]);
872 item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530873 item.total_weight = flt(item.stock_qty * item.weight_per_unit);
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530874 refresh_field("stock_qty", item.name, item.parentfield);
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530875 refresh_field("total_weight", item.name, item.parentfield);
mbauskar287fe812017-04-10 19:15:57 +0530876 this.toggle_conversion_factor(item);
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530877 this.calculate_net_weight();
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530878 if (!dont_fetch_price_list_rate &&
879 frappe.meta.has_field(doc.doctype, "price_list_currency")) {
880 this.apply_price_list(item, true);
881 }
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530882 }
883 },
884
mbauskar287fe812017-04-10 19:15:57 +0530885 toggle_conversion_factor: function(item) {
886 // toggle read only property for conversion factor field if the uom and stock uom are same
Rushabh Mehta36311022017-06-20 09:30:53 +0530887 if(this.frm.get_field('items').grid.fields_map.conversion_factor) {
888 this.frm.fields_dict.items.grid.toggle_enable("conversion_factor",
Javier Wong24690952017-12-29 16:36:47 +0800889 ((item.uom != item.stock_uom) && !frappe.meta.get_docfield(cur_frm.fields_dict.items.grid.doctype, "conversion_factor").read_only)? true: false);
Rushabh Mehta36311022017-06-20 09:30:53 +0530890 }
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530891
mbauskar287fe812017-04-10 19:15:57 +0530892 },
893
Anand Doshi3543f302013-05-24 19:25:01 +0530894 qty: function(doc, cdt, cdn) {
Nabin Hait3257aee2017-05-06 12:47:14 +0530895 this.conversion_factor(doc, cdt, cdn, true);
896 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530897 },
898
Zarrare83ff382018-09-21 15:45:40 +0530899 service_stop_date: function(frm, cdt, cdn) {
900 var child = locals[cdt][cdn];
901
902 if(child.service_stop_date) {
903 let start_date = Date.parse(child.service_start_date);
904 let end_date = Date.parse(child.service_end_date);
905 let stop_date = Date.parse(child.service_stop_date);
906
907 if(stop_date < start_date) {
908 frappe.model.set_value(cdt, cdn, "service_stop_date", "");
909 frappe.throw(__("Service Stop Date cannot be before Service Start Date"));
910 } else if (stop_date > end_date) {
911 frappe.model.set_value(cdt, cdn, "service_stop_date", "");
912 frappe.throw(__("Service Stop Date cannot be after Service End Date"));
913 }
914 }
915 },
916
917 service_start_date: function(frm, cdt, cdn) {
918 var child = locals[cdt][cdn];
919
920 if(child.service_start_date) {
921 frappe.call({
922 "method": "erpnext.stock.get_item_details.calculate_service_end_date",
923 args: {"args": child},
924 callback: function(r) {
925 frappe.model.set_value(cdt, cdn, "service_end_date", r.message.service_end_date);
926 }
927 })
928 }
929 },
930
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530931 calculate_net_weight: function(){
932 /* Calculate Total Net Weight then further applied shipping rule to calculate shipping charges.*/
933 var me = this;
934 this.frm.doc.total_net_weight= 0.0;
935
936 $.each(this.frm.doc["items"] || [], function(i, item) {
937 me.frm.doc.total_net_weight += flt(item.total_weight);
938 });
939 refresh_field("total_net_weight");
940 this.shipping_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530941 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530942
Anand Doshi61a2f682013-06-21 17:55:31 +0530943 set_dynamic_labels: function() {
944 // What TODO? should we make price list system non-mandatory?
945 this.frm.toggle_reqd("plc_conversion_rate",
946 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530947
Saif3c9155e2018-10-18 17:27:56 +0500948 var company_currency = this.get_company_currency();
949 this.change_form_labels(company_currency);
950 this.change_grid_labels(company_currency);
951 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530952 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530953
Nabin Hait613d0812015-02-23 11:58:15 +0530954 change_form_labels: function(company_currency) {
955 var me = this;
Nabin Hait613d0812015-02-23 11:58:15 +0530956
Rohit Waghchaureb1b32882016-10-21 19:31:54 +0530957 this.frm.set_currency_labels(["base_total", "base_net_total", "base_total_taxes_and_charges",
Nabin Hait613d0812015-02-23 11:58:15 +0530958 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
959 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rushabh Mehta6b537922017-03-14 16:59:11 +0530960 "base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost",
Nabin Hait2e4de832017-09-19 14:53:16 +0530961 "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost",
962 "base_rounding_adjustment"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530963
Rohit Waghchaureb1b32882016-10-21 19:31:54 +0530964 this.frm.set_currency_labels(["total", "net_total", "total_taxes_and_charges", "discount_amount",
Nabin Hait613d0812015-02-23 11:58:15 +0530965 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait2e4de832017-09-19 14:53:16 +0530966 "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost",
967 "scrap_material_cost", "rounding_adjustment", "raw_material_cost",
968 "total_cost"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530969
Nabin Hait2e4de832017-09-19 14:53:16 +0530970 this.frm.set_currency_labels(["outstanding_amount", "total_advance"],
971 this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530972
973 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
Nabin Hait2e4de832017-09-19 14:53:16 +0530974 + " = [?] " + company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530975
976 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Nabin Hait2e4de832017-09-19 14:53:16 +0530977 cur_frm.set_df_property("plc_conversion_rate", "description", "1 "
978 + this.frm.doc.price_list_currency + " = [?] " + company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530979 }
980
981 // toggle fields
Nabin Hait2e4de832017-09-19 14:53:16 +0530982 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total",
983 "base_total_taxes_and_charges", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530984 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
Nabin Hait2e4de832017-09-19 14:53:16 +0530985 "base_paid_amount", "base_write_off_amount", "base_operating_cost", "base_raw_material_cost",
986 "base_total_cost", "base_scrap_material_cost", "base_rounding_adjustment"],
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530987 this.frm.doc.currency != company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530988
989 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
990 this.frm.doc.price_list_currency != company_currency);
991
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530992 var show = cint(cur_frm.doc.discount_amount) ||
Nabin Hait903c42a2015-02-24 15:24:49 +0530993 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
994
995 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
996 cur_frm.toggle_display("net_total", show);
997
998 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
999 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
1000
Nabin Hait613d0812015-02-23 11:58:15 +05301001 },
1002
1003 change_grid_labels: function(company_currency) {
1004 var me = this;
Nabin Hait613d0812015-02-23 11:58:15 +05301005
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301006 this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
1007 company_currency, "items");
1008
1009 this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
1010 this.frm.doc.currency, "items");
1011
1012 if(this.frm.fields_dict["operations"]) {
1013 this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations");
1014 this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations");
1015
1016 var item_grid = this.frm.fields_dict["operations"].grid;
1017 $.each(["base_operating_cost", "base_hour_rate"], function(i, fname) {
1018 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1019 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301020 });
1021 }
1022
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301023 if(this.frm.fields_dict["scrap_items"]) {
1024 this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items");
1025 this.frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "scrap_items");
Nabin Hait613d0812015-02-23 11:58:15 +05301026
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301027 var item_grid = this.frm.fields_dict["scrap_items"].grid;
1028 $.each(["base_rate", "base_amount"], function(i, fname) {
1029 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1030 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
1031 });
1032 }
Nabin Hait613d0812015-02-23 11:58:15 +05301033
1034 if(this.frm.fields_dict["taxes"]) {
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301035 this.frm.set_currency_labels(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
Nabin Hait613d0812015-02-23 11:58:15 +05301036
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301037 this.frm.set_currency_labels(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
Nabin Hait613d0812015-02-23 11:58:15 +05301038 }
1039
1040 if(this.frm.fields_dict["advances"]) {
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301041 this.frm.set_currency_labels(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +05301042 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +05301043 }
1044
1045 // toggle columns
1046 var item_grid = this.frm.fields_dict["items"].grid;
1047 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
1048 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1049 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
1050 });
1051
1052 var show = (cint(cur_frm.doc.discount_amount)) ||
1053 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
1054
1055 $.each(["net_rate", "net_amount"], function(i, fname) {
1056 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1057 item_grid.set_column_disp(fname, show);
1058 });
1059
1060 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
1061 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1062 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
1063 });
1064
1065 // set labels
1066 var $wrapper = $(this.frm.wrapper);
Nabin Hait613d0812015-02-23 11:58:15 +05301067 },
1068
Anand Doshi923d41d2013-05-28 17:23:36 +05301069 recalculate: function() {
1070 this.calculate_taxes_and_totals();
1071 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301072
Anand Doshi923d41d2013-05-28 17:23:36 +05301073 recalculate_values: function() {
1074 this.calculate_taxes_and_totals();
1075 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301076
Anand Doshid0b00722013-05-28 18:54:48 +05301077 calculate_charges: function() {
1078 this.calculate_taxes_and_totals();
1079 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301080
Nabin Hait444f9562014-06-20 15:59:49 +05301081 ignore_pricing_rule: function() {
Nabin Hait8c9fb762017-04-28 14:23:03 +05301082 if(this.frm.doc.ignore_pricing_rule) {
1083 var me = this;
1084 var item_list = [];
Rushabh Mehta36311022017-06-20 09:30:53 +05301085
Nabin Hait8c9fb762017-04-28 14:23:03 +05301086 $.each(this.frm.doc["items"] || [], function(i, d) {
1087 if (d.item_code) {
1088 item_list.push({
1089 "doctype": d.doctype,
1090 "name": d.name,
1091 "pricing_rule": d.pricing_rule
1092 })
1093 }
1094 });
1095 return this.frm.call({
1096 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules",
1097 args: { item_list: item_list },
1098 callback: function(r) {
1099 if (!r.exc && r.message) {
1100 me._set_values_for_item_list(r.message);
1101 me.calculate_taxes_and_totals();
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301102 if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on");
Nabin Hait8c9fb762017-04-28 14:23:03 +05301103 }
1104 }
1105 });
1106 } else {
1107 this.apply_pricing_rule();
1108 }
Nabin Hait444f9562014-06-20 15:59:49 +05301109 },
1110
1111 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +05301112 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +05301113 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301114 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +05301115 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
1116 return;
1117 }
Anand Doshidffec8f2014-07-01 17:45:15 +05301118 return this.frm.call({
1119 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +05301120 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +05301121 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +05301122 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +05301123 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +05301124 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +05301125 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
Rohit Waghchauredeaee7c2016-09-22 14:02:53 +05301126 if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on")
Anand Doshidffec8f2014-07-01 17:45:15 +05301127 }
1128 }
1129 });
1130 },
1131
1132 _get_args: function(item) {
1133 var me = this;
1134 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301135 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +05301136 "customer": me.frm.doc.customer,
1137 "customer_group": me.frm.doc.customer_group,
1138 "territory": me.frm.doc.territory,
1139 "supplier": me.frm.doc.supplier,
Zlash652e080982018-04-19 18:37:53 +05301140 "supplier_group": me.frm.doc.supplier_group,
Nabin Hait444f9562014-06-20 15:59:49 +05301141 "currency": me.frm.doc.currency,
1142 "conversion_rate": me.frm.doc.conversion_rate,
1143 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +05301144 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +05301145 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
1146 "company": me.frm.doc.company,
1147 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
1148 "campaign": me.frm.doc.campaign,
1149 "sales_partner": me.frm.doc.sales_partner,
1150 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301151 "doctype": me.frm.doc.doctype,
Rohit Waghchaure5ecbaa42017-03-28 12:09:07 +05301152 "name": me.frm.doc.name,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +05301153 "is_return": cint(me.frm.doc.is_return),
1154 "update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0,
rohitwaghchaurec037dc72017-11-28 16:11:15 +05301155 "conversion_factor": me.frm.doc.conversion_factor,
1156 "pos_profile": me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : ''
Nabin Hait444f9562014-06-20 15:59:49 +05301157 };
Nabin Hait444f9562014-06-20 15:59:49 +05301158 },
1159
1160 _get_item_list: function(item) {
1161 var item_list = [];
1162 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +05301163 if (d.item_code) {
1164 item_list.push({
1165 "doctype": d.doctype,
1166 "name": d.name,
1167 "item_code": d.item_code,
1168 "item_group": d.item_group,
1169 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +05301170 "qty": d.qty,
Nabin Haite45ec662018-08-01 17:44:34 +05301171 "uom": d.uom,
Anand Doshi39a28912016-02-15 11:42:18 +05301172 "parenttype": d.parenttype,
Rohit Waghchaure43b50802016-10-24 14:04:46 +05301173 "parent": d.parent,
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +05301174 "pricing_rule": d.pricing_rule,
1175 "warehouse": d.warehouse,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +05301176 "serial_no": d.serial_no,
Rohit Waghchaureb12f2102017-07-12 13:33:59 +05301177 "discount_percentage": d.discount_percentage || 0.0,
mbauskar287fe812017-04-10 19:15:57 +05301178 "conversion_factor": d.conversion_factor || 1.0
Anand Doshi39edcbc2014-07-28 16:00:19 +05301179 });
mbauskarc79415e2016-01-18 16:32:24 +05301180
1181 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +05301182 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301183 item_list[0]["margin_type"] = d.margin_type;
1184 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount;
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301185 }
Anand Doshi39edcbc2014-07-28 16:00:19 +05301186 }
Nabin Hait444f9562014-06-20 15:59:49 +05301187 };
Nabin Haita3dd72a2014-05-28 12:49:20 +05301188
Nabin Hait444f9562014-06-20 15:59:49 +05301189 if (item) {
1190 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +05301191 } else {
Nabin Hait96339342015-01-21 17:22:11 +05301192 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +05301193 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +05301194 });
1195 }
Nabin Hait444f9562014-06-20 15:59:49 +05301196 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +05301197 },
1198
Anand Doshidffec8f2014-07-01 17:45:15 +05301199 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +05301200 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +05301201 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +05301202 for(var i=0, l=children.length; i<l; i++) {
1203 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +05301204 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +05301205 for(var k in d) {
1206 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +05301207 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +05301208 if(k=="price_list_rate") {
1209 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
1210 }
mbauskar287fe812017-04-10 19:15:57 +05301211 frappe.model.set_value(d.doctype, d.name, k, v);
Anand Doshidffec8f2014-07-01 17:45:15 +05301212 }
Anand Doshidea3fb02015-02-05 15:05:58 +05301213 }
1214
Nabin Hait49a27292014-12-17 11:21:32 +05301215 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +05301216 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +05301217 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
1218 }
Anand Doshidea3fb02015-02-05 15:05:58 +05301219 }
Nabin Hait7d8fa802014-12-30 15:35:45 +05301220
Anand Doshidea3fb02015-02-05 15:05:58 +05301221 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +05301222 },
1223
tundebabzy6e90f492018-02-12 10:48:57 +01001224 apply_price_list: function(item, reset_plc_conversion) {
1225 // We need to reset plc_conversion_rate sometimes because the call to
1226 // `erpnext.stock.get_item_details.apply_price_list` is sensitive to its value
1227 if (!reset_plc_conversion) {
1228 this.frm.set_value("plc_conversion_rate", "");
1229 }
1230
Anand Doshidffec8f2014-07-01 17:45:15 +05301231 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +05301232 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301233 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +05301234 return;
1235 }
Nabin Hait37b047d2015-02-23 16:01:33 +05301236
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301237 if (me.in_apply_price_list == true) return;
1238
1239 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +05301240 return this.frm.call({
1241 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +05301242 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +05301243 callback: function(r) {
1244 if (!r.exc) {
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301245 frappe.run_serially([
1246 () => me.frm.set_value("price_list_currency", r.message.parent.price_list_currency),
1247 () => me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate),
1248 () => {
1249 if(args.items.length) {
1250 me._set_values_for_item_list(r.message.children);
1251 }
1252 },
1253 () => { me.in_apply_price_list = false; }
1254 ]);
Nabin Hait37b047d2015-02-23 16:01:33 +05301255
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301256 } else {
1257 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +05301258 }
1259 }
Faris Ansari404f39d2018-05-14 15:46:45 +05301260 }).always(() => {
1261 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +05301262 });
1263 },
1264
Nabin Hait139dc7b2014-02-12 14:53:18 +05301265 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +05301266 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +05301267 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +05301268
Nabin Hait0f231062014-02-20 11:27:47 +05301269 $.each(["company", "customer"], function(i, fieldname) {
Nabin Haitae07f222017-06-02 13:57:08 +05301270 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname) && me.frm.doc.doctype != "Purchase Order") {
Nabin Hait139dc7b2014-02-12 14:53:18 +05301271 if (!me.frm.doc[fieldname]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301272 frappe.msgprint(__("Please specify") + ": " +
Rushabh Mehtaff938022014-04-15 18:40:00 +05301273 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +05301274 ". " + __("It is needed to fetch Item Details."));
Faris Ansariab74ca72017-05-30 12:54:42 +05301275 valid = false;
Nabin Hait139dc7b2014-02-12 14:53:18 +05301276 }
Anand Doshi3543f302013-05-24 19:25:01 +05301277 }
1278 });
1279 return valid;
1280 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301281
Anand Doshibf3e54a2013-06-05 14:11:32 +05301282 get_terms: function() {
1283 var me = this;
Makarand Bauskar157c3342017-05-26 21:32:33 +05301284
1285 erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function(r) {
1286 if(!r.exc) {
1287 me.frm.set_value("terms", r.message);
1288 }
1289 });
Anand Doshibf3e54a2013-06-05 14:11:32 +05301290 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301291
1292 taxes_and_charges: function() {
1293 var me = this;
1294 if(this.frm.doc.taxes_and_charges) {
1295 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +05301296 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
1297 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +05301298 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
1299 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +05301300 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +05301301 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301302 callback: function(r) {
1303 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +05301304 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301305 me.calculate_taxes_and_totals();
1306 }
1307 }
1308 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +05301309 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301310 },
1311
Saif Ur Rehman67786682018-12-27 02:11:07 +05001312 tax_category: function() {
1313 var me = this;
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +05001314 if(me.frm.updating_party_details) return;
1315
Saif Ur Rehmane414a922018-12-28 04:59:42 +05001316 frappe.run_serially([
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001317 () => this.update_item_tax_map(),
Saif Ur Rehmane414a922018-12-28 04:59:42 +05001318 () => erpnext.utils.set_taxes(this.frm, "tax_category"),
1319 ]);
Saif Ur Rehman67786682018-12-27 02:11:07 +05001320 },
1321
1322 item_tax_template: function(doc, cdt, cdn) {
1323 var me = this;
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +05001324 if(me.frm.updating_party_details) return;
1325
Saif Ur Rehman67786682018-12-27 02:11:07 +05001326 var item = frappe.get_doc(cdt, cdn);
1327
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001328 if(item.item_tax_template) {
1329 return this.frm.call({
1330 method: "erpnext.stock.get_item_details.get_item_tax_map",
1331 args: {
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001332 company: me.frm.doc.company,
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001333 item_tax_template: item.item_tax_template,
1334 as_json: true
1335 },
1336 callback: function(r) {
1337 if(!r.exc) {
1338 item.item_tax_rate = r.message;
1339 me.add_taxes_from_item_tax_template(item.item_tax_rate);
1340 me.calculate_taxes_and_totals();
1341 }
Saif Ur Rehman67786682018-12-27 02:11:07 +05001342 }
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001343 });
1344 } else {
1345 item.item_tax_rate = "{}";
1346 me.calculate_taxes_and_totals();
1347 }
Saif Ur Rehman67786682018-12-27 02:11:07 +05001348 },
1349
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001350 update_item_tax_map: function() {
1351 var me = this;
1352 var item_codes = [];
1353 $.each(this.frm.doc.items || [], function(i, item) {
1354 if(item.item_code) {
1355 item_codes.push(item.item_code);
1356 }
1357 });
1358
1359 if(item_codes.length) {
1360 return this.frm.call({
1361 method: "erpnext.stock.get_item_details.get_item_tax_info",
1362 args: {
1363 company: me.frm.doc.company,
1364 tax_category: cstr(me.frm.doc.tax_category),
1365 item_codes: item_codes
1366 },
1367 callback: function(r) {
1368 if(!r.exc) {
1369 $.each(me.frm.doc.items || [], function(i, item) {
1370 if(item.item_code && r.message.hasOwnProperty(item.item_code)) {
1371 item.item_tax_template = r.message[item.item_code].item_tax_template;
1372 item.item_tax_rate = r.message[item.item_code].item_tax_rate;
1373 me.add_taxes_from_item_tax_template(item.item_tax_rate);
1374 } else {
1375 item.item_tax_template = "";
1376 item.item_tax_rate = "{}";
1377 }
1378 });
1379 me.calculate_taxes_and_totals();
1380 }
1381 }
1382 });
1383 }
1384 },
1385
Anand Doshi13945092014-09-21 19:45:49 +05301386 is_recurring: function() {
1387 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +05301388 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
1389 frappe.msgprint(__("Please set recurring after saving"));
1390 this.frm.set_value('is_recurring', 0);
1391 return;
1392 }
1393
Anand Doshi13945092014-09-21 19:45:49 +05301394 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +05301395 if(!this.frm.doc.recurring_id) {
1396 this.frm.set_value('recurring_id', this.frm.doc.name);
1397 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301398
Anand Doshi13945092014-09-21 19:45:49 +05301399 var owner_email = this.frm.doc.owner=="Administrator"
1400 ? frappe.user_info("Administrator").email
1401 : this.frm.doc.owner;
1402
1403 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
1404 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
1405 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
1406 }
1407
1408 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
1409 },
1410
1411 from_date: function() {
1412 // set to_date
1413 if(this.frm.doc.from_date) {
1414 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
1415 'Yearly': 12};
1416
1417 var months = recurring_type_map[this.frm.doc.recurring_type];
1418 if(months) {
1419 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
1420 months);
1421 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
1422 refresh_field('to_date');
1423 }
1424 }
Saurabhd3135532016-02-25 18:59:20 +05301425 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301426
Saurabh2d7af632016-02-26 11:09:20 +05301427 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +05301428 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301429 var rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +05301430 item.gross_profit = flt(((rate - item.valuation_rate) * item.stock_qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +05301431 }
Rushabh Mehta203cc962016-04-07 15:25:43 +05301432 },
1433
1434 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +05301435 // TODO: remove item selector
1436
1437 return;
Faris Ansariab74ca72017-05-30 12:54:42 +05301438 // if(!this.item_selector) {
1439 // this.item_selector = new erpnext.ItemSelector({frm: this.frm});
1440 // }
Nabin Hait28a05282016-06-27 17:41:39 +05301441 },
Rushabh Mehta76b13412016-07-18 14:30:18 +05301442
Nabin Hait28a05282016-06-27 17:41:39 +05301443 get_advances: function() {
1444 if(!this.frm.is_return) {
1445 return this.frm.call({
1446 method: "set_advances",
1447 doc: this.frm.doc,
1448 callback: function(r, rt) {
1449 refresh_field("advances");
1450 }
1451 })
1452 }
Nabin Hait05aefbb2016-06-28 19:42:19 +05301453 },
Rushabh Mehta76b13412016-07-18 14:30:18 +05301454
Nabin Hait05aefbb2016-06-28 19:42:19 +05301455 make_payment_entry: function() {
1456 return frappe.call({
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301457 method: cur_frm.cscript.get_method_for_payment(),
Nabin Hait05aefbb2016-06-28 19:42:19 +05301458 args: {
1459 "dt": cur_frm.doc.doctype,
1460 "dn": cur_frm.doc.name
1461 },
1462 callback: function(r) {
1463 var doclist = frappe.model.sync(r.message);
1464 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Nabin Hait56548cb2016-07-01 15:58:39 +05301465 // cur_frm.refresh_fields()
Nabin Hait05aefbb2016-06-28 19:42:19 +05301466 }
1467 });
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301468 },
1469
1470 get_method_for_payment: function(){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301471 var method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry";
Rohit Waghchaure7d529892016-10-06 14:35:04 +05301472 if(cur_frm.doc.__onload && cur_frm.doc.__onload.make_payment_via_journal_entry){
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301473 if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301474 method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice";
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301475 }else {
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301476 method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order";
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301477 }
1478 }
1479
1480 return method
Manas Solankicc902412016-11-10 19:15:11 +05301481 },
Makarand Bauskarc196d742017-06-02 17:28:40 +05301482
1483 set_query_for_batch: function(doc, cdt, cdn) {
1484 // Show item's batches in the dropdown of batch no
1485
1486 var me = this;
1487 var item = frappe.get_doc(cdt, cdn);
1488
1489 if(!item.item_code) {
1490 frappe.throw(__("Please enter Item Code to get batch no"));
Rushabh Mehta36311022017-06-20 09:30:53 +05301491 } else if (doc.doctype == "Purchase Receipt" ||
Makarand Bauskarc196d742017-06-02 17:28:40 +05301492 (doc.doctype == "Purchase Invoice" && doc.update_stock)) {
1493
1494 return {
1495 filters: {'item': item.item_code}
1496 }
1497 } else {
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301498 let filters = {
Makarand Bauskarc196d742017-06-02 17:28:40 +05301499 'item_code': item.item_code,
1500 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(),
1501 }
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301502 if (item.warehouse) filters["warehouse"] = item.warehouse;
Makarand Bauskarc196d742017-06-02 17:28:40 +05301503
KanchanChauhan0fe455a2017-11-17 20:08:15 +05301504 return {
Makarand Bauskarc196d742017-06-02 17:28:40 +05301505 query : "erpnext.controllers.queries.get_batch_no",
1506 filters: filters
1507 }
1508 }
1509 },
Nabin Hait92759692017-08-15 08:23:51 +05301510
1511 payment_terms_template: function() {
1512 var me = this;
tundebabzy8f44d1c2018-01-05 08:16:50 +01001513 const doc = this.frm.doc;
1514 if(doc.payment_terms_template && doc.doctype !== 'Delivery Note') {
Shreya Shah3a9eec22018-02-16 13:05:21 +05301515 var posting_date = doc.posting_date || doc.transaction_date;
Nabin Hait92759692017-08-15 08:23:51 +05301516 frappe.call({
1517 method: "erpnext.controllers.accounts_controller.get_payment_terms",
1518 args: {
tundebabzy8f44d1c2018-01-05 08:16:50 +01001519 terms_template: doc.payment_terms_template,
rohitwaghchaureda941af2018-01-17 16:23:04 +05301520 posting_date: posting_date,
Shreya Shah3a9eec22018-02-16 13:05:21 +05301521 grand_total: doc.rounded_total || doc.grand_total,
1522 bill_date: doc.bill_date
Nabin Hait92759692017-08-15 08:23:51 +05301523 },
1524 callback: function(r) {
1525 if(r.message && !r.exc) {
1526 me.frm.set_value("payment_schedule", r.message);
1527 }
1528 }
1529 })
1530 }
1531 },
1532
1533 payment_term: function(doc, cdt, cdn) {
1534 var row = locals[cdt][cdn];
1535 if(row.payment_term) {
1536 frappe.call({
1537 method: "erpnext.controllers.accounts_controller.get_payment_term_details",
1538 args: {
1539 term: row.payment_term,
Shreya Shah3a9eec22018-02-16 13:05:21 +05301540 bill_date: this.frm.doc.bill_date,
Nabin Hait92759692017-08-15 08:23:51 +05301541 posting_date: this.frm.doc.posting_date || this.frm.doc.transaction_date,
Nabin Hait0551f7b2017-11-21 19:58:16 +05301542 grand_total: this.frm.doc.rounded_total || this.frm.doc.grand_total
Nabin Hait92759692017-08-15 08:23:51 +05301543 },
1544 callback: function(r) {
1545 if(r.message && !r.exc) {
1546 for (var d in r.message) {
1547 frappe.model.set_value(cdt, cdn, d, r.message[d]);
1548 }
1549 }
1550 }
1551 })
1552 }
Manas Solankie5e87f72018-05-28 20:07:08 +05301553 },
1554
1555 blanket_order: function(doc, cdt, cdn) {
1556 var me = this;
1557 var item = locals[cdt][cdn];
1558 if (item.blanket_order && (item.parenttype=="Sales Order" || item.parenttype=="Purchase Order")) {
1559 frappe.call({
1560 method: "erpnext.stock.get_item_details.get_blanket_order_details",
1561 args: {
1562 args:{
1563 item_code: item.item_code,
1564 customer: doc.customer,
1565 supplier: doc.supplier,
1566 company: doc.company,
1567 transaction_date: doc.transaction_date,
1568 blanket_order: item.blanket_order
1569 }
1570 },
1571 callback: function(r) {
1572 if (!r.message) {
Manas Solanki2df1b312018-05-29 12:56:14 +05301573 frappe.throw(__("Invalid Blanket Order for the selected Customer and Item"));
Manas Solankie5e87f72018-05-28 20:07:08 +05301574 } else {
1575 frappe.run_serially([
1576 () => frappe.model.set_value(cdt, cdn, "blanket_order_rate", r.message.blanket_order_rate),
1577 () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn)
1578 ]);
1579 }
1580 }
1581 })
1582 }
Saif60ec88b2018-11-13 12:07:53 +05001583 },
1584
1585 set_warehouse: function() {
1586 var me = this;
1587 if(this.frm.doc.set_warehouse) {
1588 $.each(this.frm.doc.items || [], function(i, item) {
1589 frappe.model.set_value(me.frm.doctype + " Item", item.name, "warehouse", me.frm.doc.set_warehouse);
1590 });
1591 }
Nabin Hait92759692017-08-15 08:23:51 +05301592 }
Nabin Hait6e740792017-05-06 12:45:16 +05301593});
pratu16x7ae450fc2017-06-26 15:31:46 +05301594
rohitwaghchaure2b95a542017-12-14 16:06:23 +05301595erpnext.show_serial_batch_selector = function(frm, d, callback, on_close, show_dialog) {
pratu16x7ae450fc2017-06-26 15:31:46 +05301596 frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
pratu16x789d001c2017-06-26 17:45:49 +05301597 new erpnext.SerialNoBatchSelector({
pratu16x7ae450fc2017-06-26 15:31:46 +05301598 frm: frm,
1599 item: d,
1600 warehouse_details: {
1601 type: "Warehouse",
1602 name: d.warehouse
1603 },
rohitwaghchaure2b95a542017-12-14 16:06:23 +05301604 callback: callback,
1605 on_close: on_close
Rohit Waghchaure6ab63032017-09-01 15:28:44 +05301606 }, show_dialog);
pratu16x7ae450fc2017-06-26 15:31:46 +05301607 });
1608}