blob: ac48d451b9a23f9c437dd715041eaf0b6df140d4 [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
Deepesh Garg67600772020-06-13 22:40:23 +053076 me.frm.set_query('billing_address', erpnext.queries.company_address_query);
77
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053078 if(this.frm.fields_dict.supplier) {
79 this.frm.set_query("supplier", function() {
80 return{ query: "erpnext.controllers.queries.supplier_query" }});
81 }
82
83 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080084 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053085 return{
86 query: "erpnext.controllers.queries.item_query",
87 filters:{ 'is_sub_contracted_item': 1 }
88 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080089 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080090 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053091 return{
92 query: "erpnext.controllers.queries.item_query",
93 filters: {'is_purchase_item': 1}
94 }
95 }
96 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053097
98
99 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
100 const row = locals[cdt][cdn];
101 return {
102 query: "erpnext.controllers.queries.item_manufacturer_query",
103 filters:{ 'item_code': row.item_code }
104 }
105 });
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530106
107 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
108 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
109 return me.set_query_for_item_tax_template(doc, cdt, cdn)
110 });
111 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530112 },
113
114 refresh: function(doc) {
115 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
116
117 this.frm.toggle_display("supplier_name",
118 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
119
120 if(this.frm.doc.docstatus==0 &&
121 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
122 this.set_from_product_bundle();
123 }
124
125 this._super();
126 },
127
128 supplier: function() {
129 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530130 erpnext.utils.get_party_details(this.frm, null, null, function(){
131 me.apply_price_list();
132 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530133 },
134
135 supplier_address: function() {
136 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500137 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530138 },
139
140 buying_price_list: function() {
141 this.apply_price_list();
142 },
143
144 price_list_rate: function(doc, cdt, cdn) {
145 var item = frappe.get_doc(cdt, cdn);
rohitwaghchaure4870e952019-07-11 10:03:35 +0530146
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530147 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
148
Manas Solankie5e87f72018-05-28 20:07:08 +0530149 let item_rate = item.price_list_rate;
150 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
151 item_rate = item.blanket_order_rate;
152 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530153
154 if (item.discount_percentage) {
155 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
156 }
157
Aditya Hase48585c92019-06-18 19:11:17 +0530158 if (item.discount_amount) {
159 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
rohitwaghchaure4870e952019-07-11 10:03:35 +0530160 } else {
161 item.rate = item_rate;
Aditya Hase48585c92019-06-18 19:11:17 +0530162 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530163
164 this.calculate_taxes_and_totals();
165 },
166
167 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530168 var item = frappe.get_doc(cdt, cdn);
169 item.discount_amount = 0.0;
170 this.price_list_rate(doc, cdt, cdn);
171 },
172
173 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530174 var item = frappe.get_doc(cdt, cdn);
175 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530176 this.price_list_rate(doc, cdt, cdn);
177 },
178
179 qty: function(doc, cdt, cdn) {
180 var item = frappe.get_doc(cdt, cdn);
181 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
182 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
183
184 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
185
186 if(!item.rejected_qty && item.qty) {
187 item.received_qty = item.qty;
188 }
189
190 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
191 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
192 }
193
194 this._super(doc, cdt, cdn);
195 },
196
197 received_qty: function(doc, cdt, cdn) {
198 this.calculate_accepted_qty(doc, cdt, cdn)
199 },
200
201 rejected_qty: function(doc, cdt, cdn) {
202 this.calculate_accepted_qty(doc, cdt, cdn)
203 },
204
205 calculate_accepted_qty: function(doc, cdt, cdn){
206 var item = frappe.get_doc(cdt, cdn);
207 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
208
209 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
210
211 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
212 this.qty(doc, cdt, cdn);
213 },
214
215 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
216 if(!item || !fieldnames) { return }
217
218 var is_negative_qty = false;
219 for(var i = 0; i<fieldnames.length; i++) {
220 if(item[fieldnames[i]] < 0){
221 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
222 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
223 is_negative_qty = true;
224 break;
225 }
226 }
227
228 return is_negative_qty
229 },
230
231 warehouse: function(doc, cdt, cdn) {
232 var item = frappe.get_doc(cdt, cdn);
233 if(item.item_code && item.warehouse) {
234 return this.frm.call({
235 method: "erpnext.stock.get_item_details.get_bin_details",
236 child: item,
237 args: {
238 item_code: item.item_code,
239 warehouse: item.warehouse
240 }
241 });
242 }
243 },
244
245 project: function(doc, cdt, cdn) {
246 var item = frappe.get_doc(cdt, cdn);
247 if(item.project) {
248 $.each(this.frm.doc["items"] || [],
249 function(i, other_item) {
250 if(!other_item.project) {
251 other_item.project = item.project;
252 refresh_field("project", other_item.name, other_item.parentfield);
253 }
254 });
255 }
256 },
257
marination8a30cb22020-04-23 00:18:21 +0530258 rejected_warehouse: function(doc, cdt) {
259 // trigger autofill_warehouse only if parent rejected_warehouse field is triggered
260 if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) {
261 this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse);
262 }
263 },
264
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530265 category: function(doc, cdt, cdn) {
266 // should be the category field of tax table
267 if(cdt != doc.doctype) {
268 this.calculate_taxes_and_totals();
269 }
270 },
271 add_deduct_tax: function(doc, cdt, cdn) {
272 this.calculate_taxes_and_totals();
273 },
274
275 set_from_product_bundle: function() {
276 var me = this;
277 this.frm.add_custom_button(__("Product Bundle"), function() {
278 erpnext.buying.get_items_from_product_bundle(me.frm);
marinatione14fdf42020-10-30 17:06:00 +0530279 }, __("Get Items From"));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530280 },
281
282 shipping_address: function(){
283 var me = this;
284 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530285 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530286 },
287
Deepesh Garg67600772020-06-13 22:40:23 +0530288 billing_address: function() {
289 erpnext.utils.get_address_display(this.frm, "billing_address",
290 "billing_address_display", true);
291 },
292
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530293 tc_name: function() {
294 this.get_terms();
295 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200296
Shreya Shah95d93912018-10-08 14:17:37 +0530297 link_to_mrs: function() {
298 var my_items = [];
299 for (var i in cur_frm.doc.items) {
300 if(!cur_frm.doc.items[i].material_request){
301 my_items.push(cur_frm.doc.items[i].item_code);
302 }
303 }
304 frappe.call({
305 method: "erpnext.buying.utils.get_linked_material_requests",
306 args:{
307 items: my_items
308 },
309 callback: function(r) {
Rucha Mahabal2818b5b2019-09-16 15:16:38 +0530310 if(!r.message || r.message.length == 0) {
Shreya Shah95d93912018-10-08 14:17:37 +0530311 frappe.throw(__("No pending Material Requests found to link for the given items."))
312 }
313 else {
314 var i = 0;
315 var item_length = cur_frm.doc.items.length;
316 while (i < item_length) {
317 var qty = cur_frm.doc.items[i].qty;
318 (r.message[0] || []).forEach(function(d) {
319 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
320 {
321 cur_frm.doc.items[i].material_request = d.mr_name;
322 cur_frm.doc.items[i].material_request_item = d.mr_item;
323 var my_qty = Math.min(qty, d.qty);
324 qty = qty - my_qty;
325 d.qty = d.qty - my_qty;
326 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
327 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530328
Shreya Shah95d93912018-10-08 14:17:37 +0530329 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
330 if (qty > 0)
331 {
332 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
333 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
334 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530335
Shreya Shah95d93912018-10-08 14:17:37 +0530336 for (var key in cur_frm.doc.items[i])
337 {
338 newrow[key] = cur_frm.doc.items[i][key];
339 }
Nabin Haita39f3242019-01-21 18:50:50 +0530340
Shreya Shah95d93912018-10-08 14:17:37 +0530341 newrow.idx = item_length;
342 newrow["stock_qty"] = newrow.conversion_factor*qty;
343 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530344
Shreya Shah95d93912018-10-08 14:17:37 +0530345 newrow["material_request"] = "";
346 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530347
Shreya Shah95d93912018-10-08 14:17:37 +0530348 }
349 }
350 });
351 i++;
352 }
353 refresh_field("items");
354 //cur_frm.save();
355 }
356 }
357 });
358 },
359
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200360 update_auto_repeat_reference: function(doc) {
361 if (doc.auto_repeat) {
362 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530363 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530364 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200365 docname: doc.auto_repeat,
366 reference:doc.name
367 },
368 callback: function(r){
369 if (r.message=="success") {
370 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
371 } else {
372 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
373 }
374 }
375 })
376 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530377 },
378
379 manufacturer: function(doc, cdt, cdn) {
380 const row = locals[cdt][cdn];
381
382 if(row.manufacturer) {
383 frappe.call({
384 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
385 args: {
386 'item_code': row.item_code,
387 'manufacturer': row.manufacturer
388 },
389 callback: function(r) {
390 if (r.message) {
391 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
392 }
393 }
394 });
395 }
marination3e7a8ab2020-04-07 21:00:57 +0530396 },
397
398 manufacturer_part_no: function(doc, cdt, cdn) {
399 const row = locals[cdt][cdn];
400
401 if (row.manufacturer_part_no) {
402 frappe.model.get_value('Item Manufacturer',
403 {
404 'item_code': row.item_code,
405 'manufacturer': row.manufacturer,
406 'manufacturer_part_no': row.manufacturer_part_no
407 },
408 'name',
409 function(data) {
410 if (!data) {
411 let msg = {
412 message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]),
413 title: __("Invalid Part Number")
414 }
415 frappe.throw(msg);
416 }
417 });
418
419 }
420 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530421});
422
423cur_frm.add_fetch('project', 'cost_center', 'cost_center');
424
425erpnext.buying.get_default_bom = function(frm) {
426 $.each(frm.doc["items"] || [], function(i, d) {
427 if (d.item_code && d.bom === "") {
428 return frappe.call({
429 type: "GET",
430 method: "erpnext.stock.get_item_details.get_default_bom",
431 args: {
432 "item_code": d.item_code,
433 },
434 callback: function(r) {
435 if(r) {
436 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
437 }
438 }
439 })
440 }
441 });
442}
443
444erpnext.buying.get_items_from_product_bundle = function(frm) {
445 var dialog = new frappe.ui.Dialog({
446 title: __("Get Items from Product Bundle"),
447 fields: [
448 {
449 "fieldtype": "Link",
450 "label": __("Product Bundle"),
451 "fieldname": "product_bundle",
452 "options":"Product Bundle",
453 "reqd": 1
454 },
455 {
456 "fieldtype": "Currency",
457 "label": __("Quantity"),
458 "fieldname": "quantity",
459 "reqd": 1,
460 "default": 1
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530461 }
Marica9942acd2020-04-21 13:13:39 +0530462 ],
463 primary_action_label: 'Get Items',
464 primary_action(args){
465 if(!args) return;
466 dialog.hide();
467 return frappe.call({
468 type: "GET",
469 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530470 args: {
Marica9942acd2020-04-21 13:13:39 +0530471 args: {
472 item_code: args.product_bundle,
473 quantity: args.quantity,
474 parenttype: frm.doc.doctype,
475 parent: frm.doc.name,
476 supplier: frm.doc.supplier,
477 currency: frm.doc.currency,
478 conversion_rate: frm.doc.conversion_rate,
479 price_list: frm.doc.buying_price_list,
480 price_list_currency: frm.doc.price_list_currency,
481 plc_conversion_rate: frm.doc.plc_conversion_rate,
482 company: frm.doc.company,
483 is_subcontracted: frm.doc.is_subcontracted,
484 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
485 ignore_pricing_rule: frm.doc.ignore_pricing_rule,
486 doctype: frm.doc.doctype
tundebabzyf35710d2017-08-16 08:55:18 +0100487 }
Marica9942acd2020-04-21 13:13:39 +0530488 },
489 freeze: true,
490 callback: function(r) {
491 const first_row_is_empty = function(child_table){
492 if($.isArray(child_table) && child_table.length > 0) {
493 return !child_table[0].item_code;
494 }
495 return false;
496 };
tundebabzyf35710d2017-08-16 08:55:18 +0100497
Marica9942acd2020-04-21 13:13:39 +0530498 const remove_empty_first_row = function(frm){
499 if (first_row_is_empty(frm.doc.items)){
500 frm.doc.items = frm.doc.items.splice(1);
501 }
502 };
tundebabzyf35710d2017-08-16 08:55:18 +0100503
Marica9942acd2020-04-21 13:13:39 +0530504 if(!r.exc && r.message) {
505 remove_empty_first_row(frm);
Afshaneb4b2aa2020-09-01 15:01:32 +0530506 for (var i=0; i< r.message.length; i++) {
Marica9942acd2020-04-21 13:13:39 +0530507 var d = frm.add_child("items");
508 var item = r.message[i];
Afshaneb4b2aa2020-09-01 15:01:32 +0530509 for (var key in item) {
510 if (!is_null(item[key]) && key !== "doctype") {
Marica9942acd2020-04-21 13:13:39 +0530511 d[key] = item[key];
512 }
513 }
514 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
515 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530516 }
517 }
Marica9942acd2020-04-21 13:13:39 +0530518 frm.refresh_field("items");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530519 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530520 }
Marica9942acd2020-04-21 13:13:39 +0530521 })
522 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530523 });
Marica9942acd2020-04-21 13:13:39 +0530524
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530525 dialog.show();
526}
marinationfac40352020-12-07 21:35:49 +0530527
528erpnext.apply_putaway_rule = (frm) => {
529 if (!frm.doc.company) {
530 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")})
531 }
532 if (!frm.doc.items.length) return;
533
534 frappe.call({
535 method: "erpnext.stock.doctype.putaway_rule.putaway_rule.apply_putaway_rule",
536 args: {
537 items: frm.doc.items,
538 company: frm.doc.company
539 },
540 callback: (result) => {
541 if(!result.exc) {
542 if(result.message) {
543 frm.doc.items = result.message;
544 frm.get_field("items").refresh();
545 }
546 }
547 }
548 });
549}