blob: 3d4c4a645958fcec5f17687c850d873081b93912 [file] [log] [blame]
Rushabh Mehtaf0b45622017-03-31 12:53:05 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2// License: GNU General Public License v3. See license.txt
3
4frappe.provide("erpnext.buying");
5
6cur_frm.cscript.tax_table = "Purchase Taxes and Charges";
7
8{% include 'erpnext/accounts/doctype/purchase_taxes_and_charges_template/purchase_taxes_and_charges_template.js' %}
9
10cur_frm.email_field = "contact_email";
11
12erpnext.buying.BuyingController = erpnext.TransactionController.extend({
13 setup: function() {
14 this._super();
15 },
16
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053017 onload: function(doc, cdt, cdn) {
18 this.setup_queries(doc, cdt, cdn);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053019 this._super();
20
Rushabh Mehta30dc9a12017-11-17 14:31:09 +053021 this.frm.set_query('shipping_rule', function() {
22 return {
23 filters: {
24 "shipping_rule_type": "Buying"
25 }
26 };
27 });
28
rohitwaghchaure8356d4b2018-08-09 15:51:11 +053029 if (this.frm.doc.__islocal
30 && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) {
Nabin Haita39f3242019-01-21 18:50:50 +053031
32 var df = frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total");
marinationca6dbad2019-12-11 12:10:05 +053033 var disable = cint(df.default) || cint(frappe.sys_defaults.disable_rounded_total);
Nabin Haita39f3242019-01-21 18:50:50 +053034 this.frm.set_value("disable_rounded_total", disable);
rohitwaghchaureae4ff5a2018-08-01 18:09:51 +053035 }
36
Faris Ansariab74ca72017-05-30 12:54:42 +053037 /* eslint-disable */
38 // no idea where me is coming from
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053039 if(this.frm.get_field('shipping_address')) {
Faris Ansariab74ca72017-05-30 12:54:42 +053040 this.frm.set_query("shipping_address", function() {
41 if(me.frm.doc.customer) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053042 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053043 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053044 filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
45 };
46 } else
47 return erpnext.queries.company_address_query(me.frm.doc)
48 });
49 }
Faris Ansariab74ca72017-05-30 12:54:42 +053050 /* eslint-enable */
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053051 },
52
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053053 setup_queries: function(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053054 var me = this;
55
56 if(this.frm.fields_dict.buying_price_list) {
57 this.frm.set_query("buying_price_list", function() {
58 return{
59 filters: { 'buying': 1 }
60 }
61 });
62 }
63
karthikeyan57fc60212019-07-04 22:46:16 +053064 if(this.frm.fields_dict.tc_name) {
65 this.frm.set_query("tc_name", function() {
66 return{
67 filters: { 'buying': 1 }
68 }
69 });
70 }
71
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053072 me.frm.set_query('supplier', erpnext.queries.supplier);
73 me.frm.set_query('contact_person', erpnext.queries.contact_query);
74 me.frm.set_query('supplier_address', erpnext.queries.address_query);
75
76 if(this.frm.fields_dict.supplier) {
77 this.frm.set_query("supplier", function() {
78 return{ query: "erpnext.controllers.queries.supplier_query" }});
79 }
80
81 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080082 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053083 return{
84 query: "erpnext.controllers.queries.item_query",
85 filters:{ 'is_sub_contracted_item': 1 }
86 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080087 }
88 else if (me.frm.doc.material_request_type == "Customer Provided") {
89 return{
90 query: "erpnext.controllers.queries.item_query",
91 filters:{ 'customer': me.frm.doc.customer }
92 }
93 }
94 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053095 return{
96 query: "erpnext.controllers.queries.item_query",
97 filters: {'is_purchase_item': 1}
98 }
99 }
100 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530101
102
103 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
104 const row = locals[cdt][cdn];
105 return {
106 query: "erpnext.controllers.queries.item_manufacturer_query",
107 filters:{ 'item_code': row.item_code }
108 }
109 });
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530110
111 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
112 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
113 return me.set_query_for_item_tax_template(doc, cdt, cdn)
114 });
115 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530116 },
117
118 refresh: function(doc) {
119 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
120
121 this.frm.toggle_display("supplier_name",
122 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
123
124 if(this.frm.doc.docstatus==0 &&
125 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
126 this.set_from_product_bundle();
127 }
128
129 this._super();
130 },
131
132 supplier: function() {
133 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530134 erpnext.utils.get_party_details(this.frm, null, null, function(){
135 me.apply_price_list();
136 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530137 },
138
139 supplier_address: function() {
140 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500141 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530142 },
143
144 buying_price_list: function() {
145 this.apply_price_list();
146 },
147
148 price_list_rate: function(doc, cdt, cdn) {
149 var item = frappe.get_doc(cdt, cdn);
rohitwaghchaure4870e952019-07-11 10:03:35 +0530150
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530151 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
152
Manas Solankie5e87f72018-05-28 20:07:08 +0530153 let item_rate = item.price_list_rate;
154 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
155 item_rate = item.blanket_order_rate;
156 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530157
158 if (item.discount_percentage) {
159 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
160 }
161
Aditya Hase48585c92019-06-18 19:11:17 +0530162 if (item.discount_amount) {
163 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
rohitwaghchaure4870e952019-07-11 10:03:35 +0530164 } else {
165 item.rate = item_rate;
Aditya Hase48585c92019-06-18 19:11:17 +0530166 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530167
168 this.calculate_taxes_and_totals();
169 },
170
171 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530172 var item = frappe.get_doc(cdt, cdn);
173 item.discount_amount = 0.0;
174 this.price_list_rate(doc, cdt, cdn);
175 },
176
177 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530178 var item = frappe.get_doc(cdt, cdn);
179 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530180 this.price_list_rate(doc, cdt, cdn);
181 },
182
183 qty: function(doc, cdt, cdn) {
184 var item = frappe.get_doc(cdt, cdn);
185 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
186 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
187
188 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
189
190 if(!item.rejected_qty && item.qty) {
191 item.received_qty = item.qty;
192 }
193
194 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
195 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
196 }
197
198 this._super(doc, cdt, cdn);
199 },
200
201 received_qty: function(doc, cdt, cdn) {
202 this.calculate_accepted_qty(doc, cdt, cdn)
203 },
204
205 rejected_qty: function(doc, cdt, cdn) {
206 this.calculate_accepted_qty(doc, cdt, cdn)
207 },
208
209 calculate_accepted_qty: function(doc, cdt, cdn){
210 var item = frappe.get_doc(cdt, cdn);
211 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
212
213 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
214
215 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
216 this.qty(doc, cdt, cdn);
217 },
218
219 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
220 if(!item || !fieldnames) { return }
221
222 var is_negative_qty = false;
223 for(var i = 0; i<fieldnames.length; i++) {
224 if(item[fieldnames[i]] < 0){
225 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
226 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
227 is_negative_qty = true;
228 break;
229 }
230 }
231
232 return is_negative_qty
233 },
234
235 warehouse: function(doc, cdt, cdn) {
236 var item = frappe.get_doc(cdt, cdn);
237 if(item.item_code && item.warehouse) {
238 return this.frm.call({
239 method: "erpnext.stock.get_item_details.get_bin_details",
240 child: item,
241 args: {
242 item_code: item.item_code,
243 warehouse: item.warehouse
244 }
245 });
246 }
247 },
248
249 project: function(doc, cdt, cdn) {
250 var item = frappe.get_doc(cdt, cdn);
251 if(item.project) {
252 $.each(this.frm.doc["items"] || [],
253 function(i, other_item) {
254 if(!other_item.project) {
255 other_item.project = item.project;
256 refresh_field("project", other_item.name, other_item.parentfield);
257 }
258 });
259 }
260 },
261
262 category: function(doc, cdt, cdn) {
263 // should be the category field of tax table
264 if(cdt != doc.doctype) {
265 this.calculate_taxes_and_totals();
266 }
267 },
268 add_deduct_tax: function(doc, cdt, cdn) {
269 this.calculate_taxes_and_totals();
270 },
271
272 set_from_product_bundle: function() {
273 var me = this;
274 this.frm.add_custom_button(__("Product Bundle"), function() {
275 erpnext.buying.get_items_from_product_bundle(me.frm);
276 }, __("Get items from"));
277 },
278
279 shipping_address: function(){
280 var me = this;
281 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530282 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530283 },
284
285 tc_name: function() {
286 this.get_terms();
287 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200288
Shreya Shah95d93912018-10-08 14:17:37 +0530289 link_to_mrs: function() {
290 var my_items = [];
291 for (var i in cur_frm.doc.items) {
292 if(!cur_frm.doc.items[i].material_request){
293 my_items.push(cur_frm.doc.items[i].item_code);
294 }
295 }
296 frappe.call({
297 method: "erpnext.buying.utils.get_linked_material_requests",
298 args:{
299 items: my_items
300 },
301 callback: function(r) {
Rucha Mahabal2818b5b2019-09-16 15:16:38 +0530302 if(!r.message || r.message.length == 0) {
Shreya Shah95d93912018-10-08 14:17:37 +0530303 frappe.throw(__("No pending Material Requests found to link for the given items."))
304 }
305 else {
306 var i = 0;
307 var item_length = cur_frm.doc.items.length;
308 while (i < item_length) {
309 var qty = cur_frm.doc.items[i].qty;
310 (r.message[0] || []).forEach(function(d) {
311 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
312 {
313 cur_frm.doc.items[i].material_request = d.mr_name;
314 cur_frm.doc.items[i].material_request_item = d.mr_item;
315 var my_qty = Math.min(qty, d.qty);
316 qty = qty - my_qty;
317 d.qty = d.qty - my_qty;
318 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
319 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530320
Shreya Shah95d93912018-10-08 14:17:37 +0530321 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
322 if (qty > 0)
323 {
324 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
325 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
326 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530327
Shreya Shah95d93912018-10-08 14:17:37 +0530328 for (var key in cur_frm.doc.items[i])
329 {
330 newrow[key] = cur_frm.doc.items[i][key];
331 }
Nabin Haita39f3242019-01-21 18:50:50 +0530332
Shreya Shah95d93912018-10-08 14:17:37 +0530333 newrow.idx = item_length;
334 newrow["stock_qty"] = newrow.conversion_factor*qty;
335 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530336
Shreya Shah95d93912018-10-08 14:17:37 +0530337 newrow["material_request"] = "";
338 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530339
Shreya Shah95d93912018-10-08 14:17:37 +0530340 }
341 }
342 });
343 i++;
344 }
345 refresh_field("items");
346 //cur_frm.save();
347 }
348 }
349 });
350 },
351
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200352 update_auto_repeat_reference: function(doc) {
353 if (doc.auto_repeat) {
354 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530355 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530356 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200357 docname: doc.auto_repeat,
358 reference:doc.name
359 },
360 callback: function(r){
361 if (r.message=="success") {
362 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
363 } else {
364 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
365 }
366 }
367 })
368 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530369 },
370
371 manufacturer: function(doc, cdt, cdn) {
372 const row = locals[cdt][cdn];
373
374 if(row.manufacturer) {
375 frappe.call({
376 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
377 args: {
378 'item_code': row.item_code,
379 'manufacturer': row.manufacturer
380 },
381 callback: function(r) {
382 if (r.message) {
383 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
384 }
385 }
386 });
387 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530388 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530389});
390
391cur_frm.add_fetch('project', 'cost_center', 'cost_center');
392
393erpnext.buying.get_default_bom = function(frm) {
394 $.each(frm.doc["items"] || [], function(i, d) {
395 if (d.item_code && d.bom === "") {
396 return frappe.call({
397 type: "GET",
398 method: "erpnext.stock.get_item_details.get_default_bom",
399 args: {
400 "item_code": d.item_code,
401 },
402 callback: function(r) {
403 if(r) {
404 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
405 }
406 }
407 })
408 }
409 });
410}
411
412erpnext.buying.get_items_from_product_bundle = function(frm) {
413 var dialog = new frappe.ui.Dialog({
414 title: __("Get Items from Product Bundle"),
415 fields: [
416 {
417 "fieldtype": "Link",
418 "label": __("Product Bundle"),
419 "fieldname": "product_bundle",
420 "options":"Product Bundle",
421 "reqd": 1
422 },
423 {
424 "fieldtype": "Currency",
425 "label": __("Quantity"),
426 "fieldname": "quantity",
427 "reqd": 1,
428 "default": 1
429 },
430 {
431 "fieldtype": "Button",
432 "label": __("Get Items"),
433 "fieldname": "get_items",
434 "cssClass": "btn-primary"
435 }
436 ]
437 });
438
439 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530440 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530441 if(!args) return;
442 dialog.hide();
443 return frappe.call({
444 type: "GET",
445 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
446 args: {
447 args: {
448 item_code: args.product_bundle,
449 quantity: args.quantity,
450 parenttype: frm.doc.doctype,
451 parent: frm.doc.name,
452 supplier: frm.doc.supplier,
453 currency: frm.doc.currency,
454 conversion_rate: frm.doc.conversion_rate,
455 price_list: frm.doc.buying_price_list,
456 price_list_currency: frm.doc.price_list_currency,
457 plc_conversion_rate: frm.doc.plc_conversion_rate,
458 company: frm.doc.company,
459 is_subcontracted: frm.doc.is_subcontracted,
460 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
Anurag Mishra24c3b4e2019-08-09 13:27:59 +0530461 ignore_pricing_rule: frm.doc.ignore_pricing_rule,
462 doctype: frm.doc.doctype
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530463 }
464 },
465 freeze: true,
466 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100467 const first_row_is_empty = function(child_table){
468 if($.isArray(child_table) && child_table.length > 0) {
469 return !child_table[0].item_code;
470 }
471 return false;
472 };
473
474 const remove_empty_first_row = function(frm){
475 if (first_row_is_empty(frm.doc.items)){
476 frm.doc.items = frm.doc.items.splice(1);
477 }
478 };
479
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530480 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100481 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530482 for ( var i=0; i< r.message.length; i++ ) {
483 var d = frm.add_child("items");
484 var item = r.message[i];
485 for ( var key in item) {
486 if ( !is_null(item[key]) ) {
487 d[key] = item[key];
488 }
489 }
490 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
491 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
492 }
493 }
494 frm.refresh_field("items");
495 }
496 }
497 })
498 });
499 dialog.show();
500}