blob: b0e08cc6f2658dd9c7e7f4ee716c39aa51f4fc83 [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
Faris Ansari1fe891b2021-04-23 08:04:00 +053012erpnext.buying.BuyingController = class BuyingController extends erpnext.TransactionController {
13 setup() {
14 super.setup();
15 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053016
Faris Ansari1fe891b2021-04-23 08:04:00 +053017 onload(doc, cdt, cdn) {
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053018 this.setup_queries(doc, cdt, cdn);
Faris Ansari1fe891b2021-04-23 08:04:00 +053019 super.onload();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053020
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 */
Faris Ansari1fe891b2021-04-23 08:04:00 +053051 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053052
Faris Ansari1fe891b2021-04-23 08:04:00 +053053 setup_queries(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);
Deepesh Garg3f376cc2022-06-02 13:57:54 +053077 erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype);
Deepesh Garg67600772020-06-13 22:40:23 +053078
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053079 if(this.frm.fields_dict.supplier) {
80 this.frm.set_query("supplier", function() {
81 return{ query: "erpnext.controllers.queries.supplier_query" }});
82 }
83
84 this.frm.set_query("item_code", "items", function() {
Sagar Sharma409df262022-04-19 14:57:31 +053085 if (me.frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +053086 var filters = {'supplier': me.frm.doc.supplier};
87 if (me.frm.doc.is_old_subcontracting_flow) {
88 filters["is_sub_contracted_item"] = 1;
89 }
90 else {
91 filters["is_stock_item"] = 0;
92 }
93
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053094 return{
95 query: "erpnext.controllers.queries.item_query",
s-aga-r6d89b2f2022-06-18 15:46:59 +053096 filters: filters
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053097 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080098 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080099 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530100 return{
101 query: "erpnext.controllers.queries.item_query",
Ankush Menat0e875f52022-05-16 12:14:07 +0530102 filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1, 'has_variants': 0}
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530103 }
104 }
105 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530106
107
108 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
109 const row = locals[cdt][cdn];
110 return {
111 query: "erpnext.controllers.queries.item_manufacturer_query",
112 filters:{ 'item_code': row.item_code }
113 }
114 });
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530115
116 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
117 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
118 return me.set_query_for_item_tax_template(doc, cdt, cdn)
119 });
120 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530121 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530122
Faris Ansari1fe891b2021-04-23 08:04:00 +0530123 refresh(doc) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530124 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
125
126 this.frm.toggle_display("supplier_name",
127 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
128
129 if(this.frm.doc.docstatus==0 &&
130 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
131 this.set_from_product_bundle();
132 }
133
Rohit Waghchaure110e1522021-06-15 17:29:52 +0530134 this.toggle_subcontracting_fields();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530135 super.refresh();
Rohit Waghchauref0dfea12021-06-20 10:20:35 +0530136 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530137
Rohit Waghchauref0dfea12021-06-20 10:20:35 +0530138 toggle_subcontracting_fields() {
Rohit Waghchaure110e1522021-06-15 17:29:52 +0530139 if (in_list(['Purchase Receipt', 'Purchase Invoice'], this.frm.doc.doctype)) {
140 this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty',
141 'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM');
142
143 this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1);
144 this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1);
145 }
Rohit Waghchauref0dfea12021-06-20 10:20:35 +0530146 }
Rohit Waghchaure110e1522021-06-15 17:29:52 +0530147
Rohit Waghchauref0dfea12021-06-20 10:20:35 +0530148 supplier() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530149 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530150 erpnext.utils.get_party_details(this.frm, null, null, function(){
151 me.apply_price_list();
152 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530153 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530154
Faris Ansari1fe891b2021-04-23 08:04:00 +0530155 supplier_address() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530156 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500157 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Faris Ansari1fe891b2021-04-23 08:04:00 +0530158 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530159
Faris Ansari1fe891b2021-04-23 08:04:00 +0530160 buying_price_list() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530161 this.apply_price_list();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530162 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530163
Faris Ansari1fe891b2021-04-23 08:04:00 +0530164 discount_percentage(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530165 var item = frappe.get_doc(cdt, cdn);
166 item.discount_amount = 0.0;
167 this.price_list_rate(doc, cdt, cdn);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530168 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530169
Faris Ansari1fe891b2021-04-23 08:04:00 +0530170 discount_amount(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530171 var item = frappe.get_doc(cdt, cdn);
172 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530173 this.price_list_rate(doc, cdt, cdn);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530174 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530175
Faris Ansari1fe891b2021-04-23 08:04:00 +0530176 qty(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530177 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
marination5fb5a752021-11-08 13:22:57 +0530178 this.calculate_received_qty(doc, cdt, cdn)
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530179 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530180 super.qty(doc, cdt, cdn);
181 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530182
marination5fb5a752021-11-08 13:22:57 +0530183 rejected_qty(doc, cdt, cdn) {
184 this.calculate_received_qty(doc, cdt, cdn)
185 }
186
187 calculate_received_qty(doc, cdt, cdn){
188 var item = frappe.get_doc(cdt, cdn);
189 frappe.model.round_floats_in(item, ["qty", "rejected_qty"]);
190
191 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "rejected_qty"])){ return }
192
193 let received_qty = flt(item.qty + item.rejected_qty, precision("received_qty", item));
194 let received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(received_qty);
195
196 frappe.model.set_value(cdt, cdn, "received_qty", received_qty);
197 frappe.model.set_value(cdt, cdn, "received_stock_qty", received_stock_qty);
198 }
199
Faris Ansari1fe891b2021-04-23 08:04:00 +0530200 batch_no(doc, cdt, cdn) {
201 super.batch_no(doc, cdt, cdn);
202 }
Deepesh Gargbd9bdc52021-01-28 12:05:57 +0530203
Faris Ansari1fe891b2021-04-23 08:04:00 +0530204 validate_negative_quantity(cdt, cdn, item, fieldnames){
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530205 if(!item || !fieldnames) { return }
206
207 var is_negative_qty = false;
208 for(var i = 0; i<fieldnames.length; i++) {
209 if(item[fieldnames[i]] < 0){
Mohammad Hasnain Mohsin Rajan90e33e52020-11-25 15:37:54 +0530210 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]));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530211 is_negative_qty = true;
212 break;
213 }
214 }
215
216 return is_negative_qty
Faris Ansari1fe891b2021-04-23 08:04:00 +0530217 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530218
Faris Ansari1fe891b2021-04-23 08:04:00 +0530219 warehouse(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530220 var item = frappe.get_doc(cdt, cdn);
221 if(item.item_code && item.warehouse) {
222 return this.frm.call({
223 method: "erpnext.stock.get_item_details.get_bin_details",
224 child: item,
225 args: {
226 item_code: item.item_code,
Anupam4980dd62021-03-15 11:37:46 +0530227 warehouse: item.warehouse,
s-aga-rc716dcc2022-12-29 12:34:18 +0530228 company: doc.company,
229 include_child_warehouses: true
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530230 }
231 });
232 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530233 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530234
Faris Ansari1fe891b2021-04-23 08:04:00 +0530235 project(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530236 var item = frappe.get_doc(cdt, cdn);
237 if(item.project) {
238 $.each(this.frm.doc["items"] || [],
239 function(i, other_item) {
240 if(!other_item.project) {
241 other_item.project = item.project;
242 refresh_field("project", other_item.name, other_item.parentfield);
243 }
244 });
245 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530246 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530247
Faris Ansari1fe891b2021-04-23 08:04:00 +0530248 rejected_warehouse(doc, cdt) {
marination8a30cb22020-04-23 00:18:21 +0530249 // trigger autofill_warehouse only if parent rejected_warehouse field is triggered
250 if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) {
251 this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse);
252 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530253 }
marination8a30cb22020-04-23 00:18:21 +0530254
Faris Ansari1fe891b2021-04-23 08:04:00 +0530255 category(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530256 // should be the category field of tax table
257 if(cdt != doc.doctype) {
258 this.calculate_taxes_and_totals();
259 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530260 }
261 add_deduct_tax(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530262 this.calculate_taxes_and_totals();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530263 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530264
Faris Ansari1fe891b2021-04-23 08:04:00 +0530265 set_from_product_bundle() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530266 var me = this;
267 this.frm.add_custom_button(__("Product Bundle"), function() {
268 erpnext.buying.get_items_from_product_bundle(me.frm);
marinatione14fdf42020-10-30 17:06:00 +0530269 }, __("Get Items From"));
Faris Ansari1fe891b2021-04-23 08:04:00 +0530270 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530271
Faris Ansari1fe891b2021-04-23 08:04:00 +0530272 shipping_address(){
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530273 var me = this;
274 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530275 "shipping_address_display", true);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530276 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530277
Faris Ansari1fe891b2021-04-23 08:04:00 +0530278 billing_address() {
Deepesh Garg67600772020-06-13 22:40:23 +0530279 erpnext.utils.get_address_display(this.frm, "billing_address",
280 "billing_address_display", true);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530281 }
Deepesh Garg67600772020-06-13 22:40:23 +0530282
Faris Ansari1fe891b2021-04-23 08:04:00 +0530283 tc_name() {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530284 this.get_terms();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530285 }
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200286
Faris Ansari1fe891b2021-04-23 08:04:00 +0530287 update_auto_repeat_reference(doc) {
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200288 if (doc.auto_repeat) {
289 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530290 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530291 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200292 docname: doc.auto_repeat,
293 reference:doc.name
294 },
295 callback: function(r){
296 if (r.message=="success") {
297 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
298 } else {
299 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
300 }
301 }
302 })
303 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530304 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530305
Faris Ansari1fe891b2021-04-23 08:04:00 +0530306 manufacturer(doc, cdt, cdn) {
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530307 const row = locals[cdt][cdn];
308
309 if(row.manufacturer) {
310 frappe.call({
311 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
312 args: {
313 'item_code': row.item_code,
314 'manufacturer': row.manufacturer
315 },
316 callback: function(r) {
317 if (r.message) {
318 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
319 }
320 }
321 });
322 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530323 }
marination3e7a8ab2020-04-07 21:00:57 +0530324
Faris Ansari1fe891b2021-04-23 08:04:00 +0530325 manufacturer_part_no(doc, cdt, cdn) {
marination3e7a8ab2020-04-07 21:00:57 +0530326 const row = locals[cdt][cdn];
327
328 if (row.manufacturer_part_no) {
329 frappe.model.get_value('Item Manufacturer',
330 {
331 'item_code': row.item_code,
332 'manufacturer': row.manufacturer,
333 'manufacturer_part_no': row.manufacturer_part_no
334 },
335 'name',
336 function(data) {
337 if (!data) {
338 let msg = {
339 message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]),
340 title: __("Invalid Part Number")
341 }
342 frappe.throw(msg);
343 }
344 });
345
346 }
347 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530348};
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530349
350cur_frm.add_fetch('project', 'cost_center', 'cost_center');
351
marinationa1196882020-09-24 15:22:46 +0530352erpnext.buying.link_to_mrs = function(frm) {
marinationa1196882020-09-24 15:22:46 +0530353 frappe.call({
354 method: "erpnext.buying.utils.get_linked_material_requests",
355 args:{
marination359778e2020-10-16 16:47:23 +0530356 items: frm.doc.items.map((item) => item.item_code)
marinationa1196882020-09-24 15:22:46 +0530357 },
358 callback: function(r) {
marination359778e2020-10-16 16:47:23 +0530359 if (!r.message || r.message.length == 0) {
360 frappe.throw({
361 message: __("No pending Material Requests found to link for the given items."),
362 title: __("Note")
363 });
marinationa1196882020-09-24 15:22:46 +0530364 }
marination359778e2020-10-16 16:47:23 +0530365
366 var item_length = frm.doc.items.length;
367 for (let item of frm.doc.items) {
368 var qty = item.qty;
369 (r.message[0] || []).forEach(function(d) {
370 if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item)
371 {
372 item.material_request = d.mr_name;
373 item.material_request_item = d.mr_item;
374 var my_qty = Math.min(qty, d.qty);
375 qty = qty - my_qty;
376 d.qty = d.qty - my_qty;
377 item.stock_qty = my_qty*item.conversion_factor;
378 item.qty = my_qty;
379
380 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")");
381 if (qty > 0)
marinationa1196882020-09-24 15:22:46 +0530382 {
marination359778e2020-10-16 16:47:23 +0530383 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
384 var newrow = frappe.model.add_child(frm.doc, item.doctype, "items");
385 item_length++;
marinationa1196882020-09-24 15:22:46 +0530386
marination359778e2020-10-16 16:47:23 +0530387 for (var key in item)
marinationa1196882020-09-24 15:22:46 +0530388 {
marination359778e2020-10-16 16:47:23 +0530389 newrow[key] = item[key];
marinationa1196882020-09-24 15:22:46 +0530390 }
marination359778e2020-10-16 16:47:23 +0530391
392 newrow.idx = item_length;
393 newrow["stock_qty"] = newrow.conversion_factor*qty;
394 newrow["qty"] = qty;
395
396 newrow["material_request"] = "";
397 newrow["material_request_item"] = "";
398
marinationa1196882020-09-24 15:22:46 +0530399 }
marination359778e2020-10-16 16:47:23 +0530400 }
401 });
marinationa1196882020-09-24 15:22:46 +0530402 }
marination359778e2020-10-16 16:47:23 +0530403 refresh_field("items");
marinationa1196882020-09-24 15:22:46 +0530404 }
405 });
406}
407
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530408erpnext.buying.get_default_bom = function(frm) {
409 $.each(frm.doc["items"] || [], function(i, d) {
410 if (d.item_code && d.bom === "") {
411 return frappe.call({
412 type: "GET",
413 method: "erpnext.stock.get_item_details.get_default_bom",
414 args: {
415 "item_code": d.item_code,
416 },
417 callback: function(r) {
418 if(r) {
419 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
420 }
421 }
422 })
423 }
424 });
425}
426
427erpnext.buying.get_items_from_product_bundle = function(frm) {
428 var dialog = new frappe.ui.Dialog({
429 title: __("Get Items from Product Bundle"),
430 fields: [
431 {
432 "fieldtype": "Link",
433 "label": __("Product Bundle"),
434 "fieldname": "product_bundle",
435 "options":"Product Bundle",
436 "reqd": 1
437 },
438 {
439 "fieldtype": "Currency",
440 "label": __("Quantity"),
441 "fieldname": "quantity",
442 "reqd": 1,
443 "default": 1
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530444 }
Marica9942acd2020-04-21 13:13:39 +0530445 ],
446 primary_action_label: 'Get Items',
447 primary_action(args){
448 if(!args) return;
449 dialog.hide();
450 return frappe.call({
451 type: "GET",
452 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530453 args: {
marination4c677ea2022-01-25 23:52:52 +0530454 row: {
Marica9942acd2020-04-21 13:13:39 +0530455 item_code: args.product_bundle,
456 quantity: args.quantity,
457 parenttype: frm.doc.doctype,
458 parent: frm.doc.name,
459 supplier: frm.doc.supplier,
460 currency: frm.doc.currency,
461 conversion_rate: frm.doc.conversion_rate,
462 price_list: frm.doc.buying_price_list,
463 price_list_currency: frm.doc.price_list_currency,
464 plc_conversion_rate: frm.doc.plc_conversion_rate,
465 company: frm.doc.company,
466 is_subcontracted: frm.doc.is_subcontracted,
467 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
468 ignore_pricing_rule: frm.doc.ignore_pricing_rule,
469 doctype: frm.doc.doctype
tundebabzyf35710d2017-08-16 08:55:18 +0100470 }
Marica9942acd2020-04-21 13:13:39 +0530471 },
472 freeze: true,
473 callback: function(r) {
474 const first_row_is_empty = function(child_table){
475 if($.isArray(child_table) && child_table.length > 0) {
476 return !child_table[0].item_code;
477 }
478 return false;
479 };
tundebabzyf35710d2017-08-16 08:55:18 +0100480
Marica9942acd2020-04-21 13:13:39 +0530481 const remove_empty_first_row = function(frm){
482 if (first_row_is_empty(frm.doc.items)){
483 frm.doc.items = frm.doc.items.splice(1);
484 }
485 };
tundebabzyf35710d2017-08-16 08:55:18 +0100486
Marica9942acd2020-04-21 13:13:39 +0530487 if(!r.exc && r.message) {
488 remove_empty_first_row(frm);
Afshaneb4b2aa2020-09-01 15:01:32 +0530489 for (var i=0; i< r.message.length; i++) {
Marica9942acd2020-04-21 13:13:39 +0530490 var d = frm.add_child("items");
491 var item = r.message[i];
Afshaneb4b2aa2020-09-01 15:01:32 +0530492 for (var key in item) {
493 if (!is_null(item[key]) && key !== "doctype") {
Marica9942acd2020-04-21 13:13:39 +0530494 d[key] = item[key];
495 }
496 }
497 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
498 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530499 }
500 }
Marica9942acd2020-04-21 13:13:39 +0530501 frm.refresh_field("items");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530502 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530503 }
Marica9942acd2020-04-21 13:13:39 +0530504 })
505 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530506 });
Marica9942acd2020-04-21 13:13:39 +0530507
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530508 dialog.show();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530509}