blob: b5a8b75770608c2c251c9ca13eb459ba49f2282e [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");
Deepesh Garg8ccb8e32023-07-23 18:50:44 +05305// cur_frm.add_fetch('project', 'cost_center', 'cost_center');
Rushabh Mehtaf0b45622017-03-31 12:53:05 +05306
Deepesh Garg8ccb8e32023-07-23 18:50:44 +05307erpnext.buying = {
8 setup_buying_controller: function() {
9 erpnext.buying.BuyingController = class BuyingController extends erpnext.TransactionController {
10 setup() {
11 super.setup();
rohitwaghchauredd4769e2023-09-20 18:09:03 +053012 this.toggle_enable_for_stock_uom("allow_to_edit_stock_uom_qty_for_purchase");
Deepesh Gargcdc9b622023-07-23 22:44:18 +053013 this.frm.email_field = "contact_email";
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053014 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053015
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053016 onload(doc, cdt, cdn) {
17 this.setup_queries(doc, cdt, cdn);
18 super.onload();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053019
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053020 this.frm.set_query('shipping_rule', function() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053021 return {
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053022 filters: {
23 "shipping_rule_type": "Buying"
24 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053025 };
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053026 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053027
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053028 if (this.frm.doc.__islocal
29 && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053030
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053031 var df = frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total");
32 var disable = cint(df.default) || cint(frappe.sys_defaults.disable_rounded_total);
33 this.frm.set_value("disable_rounded_total", disable);
s-aga-r6d89b2f2022-06-18 15:46:59 +053034 }
35
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053036
37 // no idea where me is coming from
38 if(this.frm.get_field('shipping_address')) {
creative-paramuca0c3eb2023-12-08 11:05:09 +053039 this.frm.set_query("shipping_address", () => {
40 if(this.frm.doc.customer) {
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053041 return {
42 query: 'frappe.contacts.doctype.address.address.address_query',
creative-paramuca0c3eb2023-12-08 11:05:09 +053043 filters: { link_doctype: 'Customer', link_name: this.frm.doc.customer }
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053044 };
45 } else
creative-paramuca0c3eb2023-12-08 11:05:09 +053046 return erpnext.queries.company_address_query(this.frm.doc)
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053047 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053048 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080049 }
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053050
51 setup_queries(doc, cdt, cdn) {
52 var me = this;
53
54 if(this.frm.fields_dict.buying_price_list) {
55 this.frm.set_query("buying_price_list", function() {
56 return{
57 filters: { 'buying': 1 }
58 }
59 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053060 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053061
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053062 if(this.frm.fields_dict.tc_name) {
63 this.frm.set_query("tc_name", function() {
64 return{
65 filters: { 'buying': 1 }
66 }
67 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053068 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053069
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053070 me.frm.set_query('supplier', erpnext.queries.supplier);
71 me.frm.set_query('contact_person', erpnext.queries.contact_query);
72 me.frm.set_query('supplier_address', erpnext.queries.address_query);
73
74 me.frm.set_query('billing_address', erpnext.queries.company_address_query);
75 erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype);
76
Deepesh Garg8ccb8e32023-07-23 18:50:44 +053077 this.frm.set_query("item_code", "items", function() {
78 if (me.frm.doc.is_subcontracted) {
79 var filters = {'supplier': me.frm.doc.supplier};
80 if (me.frm.doc.is_old_subcontracting_flow) {
81 filters["is_sub_contracted_item"] = 1;
82 }
83 else {
84 filters["is_stock_item"] = 0;
85 }
86
87 return{
88 query: "erpnext.controllers.queries.item_query",
89 filters: filters
90 }
91 }
92 else {
93 return{
94 query: "erpnext.controllers.queries.item_query",
95 filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1, 'has_variants': 0}
96 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053097 }
98 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053099
marination8a30cb22020-04-23 00:18:21 +0530100
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530101 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
102 const row = locals[cdt][cdn];
103 return {
104 query: "erpnext.controllers.queries.item_manufacturer_query",
105 filters:{ 'item_code': row.item_code }
106 }
107 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530108
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530109 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
110 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
111 return me.set_query_for_item_tax_template(doc, cdt, cdn)
112 });
113 }
114 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530115
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530116 refresh(doc) {
117 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530118
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530119 this.frm.toggle_display("supplier_name",
120 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
Deepesh Garg67600772020-06-13 22:40:23 +0530121
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530122 if(this.frm.doc.docstatus==0 &&
123 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
124 this.set_from_product_bundle();
125 }
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200126
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530127 this.toggle_subcontracting_fields();
128 super.refresh();
129 }
130
131 toggle_subcontracting_fields() {
barredterrad2387512024-03-19 12:03:36 +0100132 if (['Purchase Receipt', 'Purchase Invoice'].includes(this.frm.doc.doctype)) {
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530133 this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty',
134 'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM');
135
136 this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1);
137 this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1);
138 }
139 }
140
141 supplier() {
142 var me = this;
143 erpnext.utils.get_party_details(this.frm, null, null, function(){
144 me.apply_price_list();
145 });
146 }
147
148 supplier_address() {
149 erpnext.utils.get_address_display(this.frm);
150 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
151 }
152
153 buying_price_list() {
154 this.apply_price_list();
155 }
156
157 discount_percentage(doc, cdt, cdn) {
158 var item = frappe.get_doc(cdt, cdn);
159 item.discount_amount = 0.0;
160 this.price_list_rate(doc, cdt, cdn);
161 }
162
163 discount_amount(doc, cdt, cdn) {
164 var item = frappe.get_doc(cdt, cdn);
165 item.discount_percentage = 0.0;
166 this.price_list_rate(doc, cdt, cdn);
167 }
168
169 qty(doc, cdt, cdn) {
170 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
171 this.calculate_received_qty(doc, cdt, cdn)
172 }
173 super.qty(doc, cdt, cdn);
174 }
175
176 rejected_qty(doc, cdt, cdn) {
177 this.calculate_received_qty(doc, cdt, cdn)
178 }
179
180 calculate_received_qty(doc, cdt, cdn){
181 var item = frappe.get_doc(cdt, cdn);
182 frappe.model.round_floats_in(item, ["qty", "rejected_qty"]);
183
184 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "rejected_qty"])){ return }
185
186 let received_qty = flt(item.qty + item.rejected_qty, precision("received_qty", item));
187 let received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(received_qty);
188
189 frappe.model.set_value(cdt, cdn, "received_qty", received_qty);
190 frappe.model.set_value(cdt, cdn, "received_stock_qty", received_stock_qty);
191 }
192
193 batch_no(doc, cdt, cdn) {
194 super.batch_no(doc, cdt, cdn);
195 }
196
197 validate_negative_quantity(cdt, cdn, item, fieldnames){
198 if(!item || !fieldnames) { return }
199
200 var is_negative_qty = false;
201 for(var i = 0; i<fieldnames.length; i++) {
202 if(item[fieldnames[i]] < 0){
203 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}", [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
204 is_negative_qty = true;
205 break;
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200206 }
207 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530208
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530209 return is_negative_qty
210 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530211
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530212 warehouse(doc, cdt, cdn) {
213 var item = frappe.get_doc(cdt, cdn);
214 if(item.item_code && item.warehouse) {
215 return this.frm.call({
216 method: "erpnext.stock.get_item_details.get_bin_details",
217 child: item,
218 args: {
219 item_code: item.item_code,
220 warehouse: item.warehouse,
221 company: doc.company,
222 include_child_warehouses: true
marination3e7a8ab2020-04-07 21:00:57 +0530223 }
Rohit Waghchaurebc75a7e2022-10-10 13:28:19 +0530224 });
225 }
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530226 }
Rohit Waghchauree6143ab2023-03-13 14:51:43 +0530227
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530228 project(doc, cdt, cdn) {
229 var item = frappe.get_doc(cdt, cdn);
230 if(item.project) {
231 $.each(this.frm.doc["items"] || [],
232 function(i, other_item) {
233 if(!other_item.project) {
234 other_item.project = item.project;
235 refresh_field("project", other_item.name, other_item.parentfield);
Rohit Waghchauree6143ab2023-03-13 14:51:43 +0530236 }
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530237 });
238 }
239 }
240
241 rejected_warehouse(doc, cdt) {
242 // trigger autofill_warehouse only if parent rejected_warehouse field is triggered
243 if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) {
244 this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse);
245 }
246 }
247
248 category(doc, cdt, cdn) {
249 // should be the category field of tax table
250 if(cdt != doc.doctype) {
251 this.calculate_taxes_and_totals();
252 }
253 }
254 add_deduct_tax(doc, cdt, cdn) {
255 this.calculate_taxes_and_totals();
256 }
257
258 set_from_product_bundle() {
259 var me = this;
260 this.frm.add_custom_button(__("Product Bundle"), function() {
261 erpnext.buying.get_items_from_product_bundle(me.frm);
262 }, __("Get Items From"));
263 }
264
265 shipping_address(){
266 var me = this;
267 erpnext.utils.get_address_display(this.frm, "shipping_address",
268 "shipping_address_display", true);
269 }
270
271 billing_address() {
272 erpnext.utils.get_address_display(this.frm, "billing_address",
273 "billing_address_display", true);
274 }
275
276 tc_name() {
277 this.get_terms();
278 }
279
280 update_auto_repeat_reference(doc) {
281 if (doc.auto_repeat) {
282 frappe.call({
283 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
284 args:{
285 docname: doc.auto_repeat,
286 reference:doc.name
287 },
288 callback: function(r){
289 if (r.message=="success") {
290 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
291 } else {
292 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
293 }
294 }
295 })
296 }
297 }
298
299 manufacturer(doc, cdt, cdn) {
300 const row = locals[cdt][cdn];
301
302 if(row.manufacturer) {
303 frappe.call({
304 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
305 args: {
306 'item_code': row.item_code,
307 'manufacturer': row.manufacturer
308 },
309 callback: function(r) {
310 if (r.message) {
311 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
312 }
313 }
Rohit Waghchauree6143ab2023-03-13 14:51:43 +0530314 });
315 }
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530316 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530317
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530318 manufacturer_part_no(doc, cdt, cdn) {
319 const row = locals[cdt][cdn];
320
321 if (row.manufacturer_part_no) {
322 frappe.model.get_value('Item Manufacturer',
323 {
324 'item_code': row.item_code,
325 'manufacturer': row.manufacturer,
326 'manufacturer_part_no': row.manufacturer_part_no
327 },
328 'name',
329 function(data) {
330 if (!data) {
331 let msg = {
332 message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]),
333 title: __("Invalid Part Number")
334 }
335 frappe.throw(msg);
336 }
337 }
338 );
339 }
340 }
341
342 add_serial_batch_bundle(doc, cdt, cdn) {
343 let item = locals[cdt][cdn];
344 let me = this;
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530345
346 frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"])
347 .then((r) => {
348 if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
349 item.has_serial_no = r.message.has_serial_no;
350 item.has_batch_no = r.message.has_batch_no;
351 item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward";
352 item.is_rejected = false;
353
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530354 new erpnext.SerialBatchPackageSelector(
355 me.frm, item, (r) => {
356 if (r) {
357 let qty = Math.abs(r.total_qty);
358 if (doc.is_return) {
359 qty = qty * -1;
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530360 }
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530361
362 let update_values = {
363 "serial_and_batch_bundle": r.name,
364 "use_serial_batch_fields": 0,
365 "qty": qty / flt(item.conversion_factor || 1, precision("conversion_factor", item))
366 }
367
368 if (r.warehouse) {
369 update_values["warehouse"] = r.warehouse;
370 }
371
372 frappe.model.set_value(item.doctype, item.name, update_values);
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530373 }
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530374 }
375 );
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530376 }
377 });
378 }
379
380 add_serial_batch_for_rejected_qty(doc, cdt, cdn) {
381 let item = locals[cdt][cdn];
382 let me = this;
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530383
384 frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"])
385 .then((r) => {
386 if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) {
387 item.has_serial_no = r.message.has_serial_no;
388 item.has_batch_no = r.message.has_batch_no;
Rohit Waghchaured73b1f32024-03-30 10:46:56 +0530389 item.type_of_transaction = item.rejected_qty > 0 ? "Inward" : "Outward";
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530390 item.is_rejected = true;
391
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530392 new erpnext.SerialBatchPackageSelector(
393 me.frm, item, (r) => {
394 if (r) {
395 let qty = Math.abs(r.total_qty);
396 if (doc.is_return) {
397 qty = qty * -1;
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530398 }
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530399
400 let update_values = {
401 "serial_and_batch_bundle": r.name,
402 "use_serial_batch_fields": 0,
403 "rejected_qty": qty / flt(item.conversion_factor || 1, precision("conversion_factor", item))
404 }
405
406 if (r.warehouse) {
407 update_values["rejected_warehouse"] = r.warehouse;
408 }
409
410 frappe.model.set_value(item.doctype, item.name, update_values);
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530411 }
Rohit Waghchaure0d9d42a2024-03-27 11:52:06 +0530412 }
413 );
Deepesh Garg8ccb8e32023-07-23 18:50:44 +0530414 }
415 });
416 }
417 };
418 }
419}
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530420
marinationa1196882020-09-24 15:22:46 +0530421erpnext.buying.link_to_mrs = function(frm) {
marinationa1196882020-09-24 15:22:46 +0530422 frappe.call({
423 method: "erpnext.buying.utils.get_linked_material_requests",
424 args:{
marination359778e2020-10-16 16:47:23 +0530425 items: frm.doc.items.map((item) => item.item_code)
marinationa1196882020-09-24 15:22:46 +0530426 },
427 callback: function(r) {
marination359778e2020-10-16 16:47:23 +0530428 if (!r.message || r.message.length == 0) {
429 frappe.throw({
430 message: __("No pending Material Requests found to link for the given items."),
431 title: __("Note")
432 });
marinationa1196882020-09-24 15:22:46 +0530433 }
marination359778e2020-10-16 16:47:23 +0530434
435 var item_length = frm.doc.items.length;
436 for (let item of frm.doc.items) {
437 var qty = item.qty;
438 (r.message[0] || []).forEach(function(d) {
439 if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item)
440 {
441 item.material_request = d.mr_name;
442 item.material_request_item = d.mr_item;
443 var my_qty = Math.min(qty, d.qty);
444 qty = qty - my_qty;
445 d.qty = d.qty - my_qty;
446 item.stock_qty = my_qty*item.conversion_factor;
447 item.qty = my_qty;
448
449 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")");
450 if (qty > 0)
marinationa1196882020-09-24 15:22:46 +0530451 {
marination359778e2020-10-16 16:47:23 +0530452 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
453 var newrow = frappe.model.add_child(frm.doc, item.doctype, "items");
454 item_length++;
marinationa1196882020-09-24 15:22:46 +0530455
marination359778e2020-10-16 16:47:23 +0530456 for (var key in item)
marinationa1196882020-09-24 15:22:46 +0530457 {
marination359778e2020-10-16 16:47:23 +0530458 newrow[key] = item[key];
marinationa1196882020-09-24 15:22:46 +0530459 }
marination359778e2020-10-16 16:47:23 +0530460
461 newrow.idx = item_length;
462 newrow["stock_qty"] = newrow.conversion_factor*qty;
463 newrow["qty"] = qty;
464
465 newrow["material_request"] = "";
466 newrow["material_request_item"] = "";
467
marinationa1196882020-09-24 15:22:46 +0530468 }
marination359778e2020-10-16 16:47:23 +0530469 }
470 });
marinationa1196882020-09-24 15:22:46 +0530471 }
marination359778e2020-10-16 16:47:23 +0530472 refresh_field("items");
marinationa1196882020-09-24 15:22:46 +0530473 }
474 });
475}
476
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530477erpnext.buying.get_default_bom = function(frm) {
478 $.each(frm.doc["items"] || [], function(i, d) {
479 if (d.item_code && d.bom === "") {
480 return frappe.call({
481 type: "GET",
482 method: "erpnext.stock.get_item_details.get_default_bom",
483 args: {
484 "item_code": d.item_code,
485 },
486 callback: function(r) {
487 if(r) {
488 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
489 }
490 }
491 })
492 }
493 });
494}
495
496erpnext.buying.get_items_from_product_bundle = function(frm) {
497 var dialog = new frappe.ui.Dialog({
498 title: __("Get Items from Product Bundle"),
499 fields: [
500 {
501 "fieldtype": "Link",
502 "label": __("Product Bundle"),
503 "fieldname": "product_bundle",
504 "options":"Product Bundle",
505 "reqd": 1
506 },
507 {
508 "fieldtype": "Currency",
509 "label": __("Quantity"),
510 "fieldname": "quantity",
511 "reqd": 1,
512 "default": 1
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530513 }
Marica9942acd2020-04-21 13:13:39 +0530514 ],
515 primary_action_label: 'Get Items',
516 primary_action(args){
517 if(!args) return;
518 dialog.hide();
519 return frappe.call({
520 type: "GET",
521 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530522 args: {
marination4c677ea2022-01-25 23:52:52 +0530523 row: {
Marica9942acd2020-04-21 13:13:39 +0530524 item_code: args.product_bundle,
525 quantity: args.quantity,
526 parenttype: frm.doc.doctype,
527 parent: frm.doc.name,
528 supplier: frm.doc.supplier,
529 currency: frm.doc.currency,
530 conversion_rate: frm.doc.conversion_rate,
531 price_list: frm.doc.buying_price_list,
532 price_list_currency: frm.doc.price_list_currency,
533 plc_conversion_rate: frm.doc.plc_conversion_rate,
534 company: frm.doc.company,
535 is_subcontracted: frm.doc.is_subcontracted,
536 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
537 ignore_pricing_rule: frm.doc.ignore_pricing_rule,
538 doctype: frm.doc.doctype
tundebabzyf35710d2017-08-16 08:55:18 +0100539 }
Marica9942acd2020-04-21 13:13:39 +0530540 },
541 freeze: true,
542 callback: function(r) {
543 const first_row_is_empty = function(child_table){
544 if($.isArray(child_table) && child_table.length > 0) {
545 return !child_table[0].item_code;
546 }
547 return false;
548 };
tundebabzyf35710d2017-08-16 08:55:18 +0100549
Marica9942acd2020-04-21 13:13:39 +0530550 const remove_empty_first_row = function(frm){
551 if (first_row_is_empty(frm.doc.items)){
552 frm.doc.items = frm.doc.items.splice(1);
553 }
554 };
tundebabzyf35710d2017-08-16 08:55:18 +0100555
Marica9942acd2020-04-21 13:13:39 +0530556 if(!r.exc && r.message) {
557 remove_empty_first_row(frm);
Afshaneb4b2aa2020-09-01 15:01:32 +0530558 for (var i=0; i< r.message.length; i++) {
Marica9942acd2020-04-21 13:13:39 +0530559 var d = frm.add_child("items");
560 var item = r.message[i];
Afshaneb4b2aa2020-09-01 15:01:32 +0530561 for (var key in item) {
562 if (!is_null(item[key]) && key !== "doctype") {
Marica9942acd2020-04-21 13:13:39 +0530563 d[key] = item[key];
564 }
565 }
566 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
567 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530568 }
569 }
Marica9942acd2020-04-21 13:13:39 +0530570 frm.refresh_field("items");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530571 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530572 }
Marica9942acd2020-04-21 13:13:39 +0530573 })
574 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530575 });
Marica9942acd2020-04-21 13:13:39 +0530576
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530577 dialog.show();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530578}