Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | frappe.provide("erpnext.buying"); |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 5 | // cur_frm.add_fetch('project', 'cost_center', 'cost_center'); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 6 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 7 | erpnext.buying = { |
| 8 | setup_buying_controller: function() { |
| 9 | erpnext.buying.BuyingController = class BuyingController extends erpnext.TransactionController { |
| 10 | setup() { |
| 11 | super.setup(); |
Deepesh Garg | cdc9b62 | 2023-07-23 22:44:18 +0530 | [diff] [blame] | 12 | this.frm.email_field = "contact_email"; |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 13 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 14 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 15 | onload(doc, cdt, cdn) { |
| 16 | this.setup_queries(doc, cdt, cdn); |
| 17 | super.onload(); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 18 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 19 | this.frm.set_query('shipping_rule', function() { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 20 | return { |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 21 | filters: { |
| 22 | "shipping_rule_type": "Buying" |
| 23 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 24 | }; |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 25 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 26 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 27 | if (this.frm.doc.__islocal |
| 28 | && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 29 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 30 | var df = frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total"); |
| 31 | var disable = cint(df.default) || cint(frappe.sys_defaults.disable_rounded_total); |
| 32 | this.frm.set_value("disable_rounded_total", disable); |
s-aga-r | 6d89b2f | 2022-06-18 15:46:59 +0530 | [diff] [blame] | 33 | } |
| 34 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 35 | |
| 36 | // no idea where me is coming from |
| 37 | if(this.frm.get_field('shipping_address')) { |
| 38 | this.frm.set_query("shipping_address", function() { |
| 39 | if(me.frm.doc.customer) { |
| 40 | return { |
| 41 | query: 'frappe.contacts.doctype.address.address.address_query', |
| 42 | filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer } |
| 43 | }; |
| 44 | } else |
| 45 | return erpnext.queries.company_address_query(me.frm.doc) |
| 46 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 47 | } |
Doridel Cahanap | 2b14d6a | 2018-11-13 14:37:16 +0800 | [diff] [blame] | 48 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 49 | |
| 50 | setup_queries(doc, cdt, cdn) { |
| 51 | var me = this; |
| 52 | |
| 53 | if(this.frm.fields_dict.buying_price_list) { |
| 54 | this.frm.set_query("buying_price_list", function() { |
| 55 | return{ |
| 56 | filters: { 'buying': 1 } |
| 57 | } |
| 58 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 59 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 60 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 61 | if(this.frm.fields_dict.tc_name) { |
| 62 | this.frm.set_query("tc_name", function() { |
| 63 | return{ |
| 64 | filters: { 'buying': 1 } |
| 65 | } |
| 66 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 67 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 68 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 69 | me.frm.set_query('supplier', erpnext.queries.supplier); |
| 70 | me.frm.set_query('contact_person', erpnext.queries.contact_query); |
| 71 | me.frm.set_query('supplier_address', erpnext.queries.address_query); |
| 72 | |
| 73 | me.frm.set_query('billing_address', erpnext.queries.company_address_query); |
| 74 | erpnext.accounts.dimensions.setup_dimension_filters(me.frm, me.frm.doctype); |
| 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() { |
| 82 | if (me.frm.doc.is_subcontracted) { |
| 83 | var filters = {'supplier': me.frm.doc.supplier}; |
| 84 | if (me.frm.doc.is_old_subcontracting_flow) { |
| 85 | filters["is_sub_contracted_item"] = 1; |
| 86 | } |
| 87 | else { |
| 88 | filters["is_stock_item"] = 0; |
| 89 | } |
| 90 | |
| 91 | return{ |
| 92 | query: "erpnext.controllers.queries.item_query", |
| 93 | filters: filters |
| 94 | } |
| 95 | } |
| 96 | else { |
| 97 | return{ |
| 98 | query: "erpnext.controllers.queries.item_query", |
| 99 | filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1, 'has_variants': 0} |
| 100 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 101 | } |
| 102 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 103 | |
marination | 8a30cb2 | 2020-04-23 00:18:21 +0530 | [diff] [blame] | 104 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 105 | this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) { |
| 106 | const row = locals[cdt][cdn]; |
| 107 | return { |
| 108 | query: "erpnext.controllers.queries.item_manufacturer_query", |
| 109 | filters:{ 'item_code': row.item_code } |
| 110 | } |
| 111 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 112 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 113 | if(this.frm.fields_dict["items"].grid.get_field('item_code')) { |
| 114 | this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) { |
| 115 | return me.set_query_for_item_tax_template(doc, cdt, cdn) |
| 116 | }); |
| 117 | } |
| 118 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 119 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 120 | refresh(doc) { |
| 121 | frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 122 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 123 | this.frm.toggle_display("supplier_name", |
| 124 | (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier)); |
Deepesh Garg | 6760077 | 2020-06-13 22:40:23 +0530 | [diff] [blame] | 125 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 126 | if(this.frm.doc.docstatus==0 && |
| 127 | (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) { |
| 128 | this.set_from_product_bundle(); |
| 129 | } |
Charles-Henri Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 130 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 131 | this.toggle_subcontracting_fields(); |
| 132 | super.refresh(); |
| 133 | } |
| 134 | |
| 135 | toggle_subcontracting_fields() { |
| 136 | if (in_list(['Purchase Receipt', 'Purchase Invoice'], this.frm.doc.doctype)) { |
| 137 | this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty', |
| 138 | 'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM'); |
| 139 | |
| 140 | this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1); |
| 141 | this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | supplier() { |
| 146 | var me = this; |
| 147 | erpnext.utils.get_party_details(this.frm, null, null, function(){ |
| 148 | me.apply_price_list(); |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | supplier_address() { |
| 153 | erpnext.utils.get_address_display(this.frm); |
| 154 | erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address"); |
| 155 | } |
| 156 | |
| 157 | buying_price_list() { |
| 158 | this.apply_price_list(); |
| 159 | } |
| 160 | |
| 161 | discount_percentage(doc, cdt, cdn) { |
| 162 | var item = frappe.get_doc(cdt, cdn); |
| 163 | item.discount_amount = 0.0; |
| 164 | this.price_list_rate(doc, cdt, cdn); |
| 165 | } |
| 166 | |
| 167 | discount_amount(doc, cdt, cdn) { |
| 168 | var item = frappe.get_doc(cdt, cdn); |
| 169 | item.discount_percentage = 0.0; |
| 170 | this.price_list_rate(doc, cdt, cdn); |
| 171 | } |
| 172 | |
| 173 | qty(doc, cdt, cdn) { |
| 174 | if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) { |
| 175 | this.calculate_received_qty(doc, cdt, cdn) |
| 176 | } |
| 177 | super.qty(doc, cdt, cdn); |
| 178 | } |
| 179 | |
| 180 | rejected_qty(doc, cdt, cdn) { |
| 181 | this.calculate_received_qty(doc, cdt, cdn) |
| 182 | } |
| 183 | |
| 184 | calculate_received_qty(doc, cdt, cdn){ |
| 185 | var item = frappe.get_doc(cdt, cdn); |
| 186 | frappe.model.round_floats_in(item, ["qty", "rejected_qty"]); |
| 187 | |
| 188 | if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "rejected_qty"])){ return } |
| 189 | |
| 190 | let received_qty = flt(item.qty + item.rejected_qty, precision("received_qty", item)); |
| 191 | let received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(received_qty); |
| 192 | |
| 193 | frappe.model.set_value(cdt, cdn, "received_qty", received_qty); |
| 194 | frappe.model.set_value(cdt, cdn, "received_stock_qty", received_stock_qty); |
| 195 | } |
| 196 | |
| 197 | batch_no(doc, cdt, cdn) { |
| 198 | super.batch_no(doc, cdt, cdn); |
| 199 | } |
| 200 | |
| 201 | validate_negative_quantity(cdt, cdn, item, fieldnames){ |
| 202 | if(!item || !fieldnames) { return } |
| 203 | |
| 204 | var is_negative_qty = false; |
| 205 | for(var i = 0; i<fieldnames.length; i++) { |
| 206 | if(item[fieldnames[i]] < 0){ |
| 207 | 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])); |
| 208 | is_negative_qty = true; |
| 209 | break; |
Charles-Henri Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 210 | } |
| 211 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 212 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 213 | return is_negative_qty |
| 214 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 215 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 216 | warehouse(doc, cdt, cdn) { |
| 217 | var item = frappe.get_doc(cdt, cdn); |
| 218 | if(item.item_code && item.warehouse) { |
| 219 | return this.frm.call({ |
| 220 | method: "erpnext.stock.get_item_details.get_bin_details", |
| 221 | child: item, |
| 222 | args: { |
| 223 | item_code: item.item_code, |
| 224 | warehouse: item.warehouse, |
| 225 | company: doc.company, |
| 226 | include_child_warehouses: true |
marination | 3e7a8ab | 2020-04-07 21:00:57 +0530 | [diff] [blame] | 227 | } |
Rohit Waghchaure | bc75a7e | 2022-10-10 13:28:19 +0530 | [diff] [blame] | 228 | }); |
| 229 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 230 | } |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 231 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 232 | project(doc, cdt, cdn) { |
| 233 | var item = frappe.get_doc(cdt, cdn); |
| 234 | if(item.project) { |
| 235 | $.each(this.frm.doc["items"] || [], |
| 236 | function(i, other_item) { |
| 237 | if(!other_item.project) { |
| 238 | other_item.project = item.project; |
| 239 | refresh_field("project", other_item.name, other_item.parentfield); |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 240 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 241 | }); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | rejected_warehouse(doc, cdt) { |
| 246 | // trigger autofill_warehouse only if parent rejected_warehouse field is triggered |
| 247 | if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) { |
| 248 | this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | category(doc, cdt, cdn) { |
| 253 | // should be the category field of tax table |
| 254 | if(cdt != doc.doctype) { |
| 255 | this.calculate_taxes_and_totals(); |
| 256 | } |
| 257 | } |
| 258 | add_deduct_tax(doc, cdt, cdn) { |
| 259 | this.calculate_taxes_and_totals(); |
| 260 | } |
| 261 | |
| 262 | set_from_product_bundle() { |
| 263 | var me = this; |
| 264 | this.frm.add_custom_button(__("Product Bundle"), function() { |
| 265 | erpnext.buying.get_items_from_product_bundle(me.frm); |
| 266 | }, __("Get Items From")); |
| 267 | } |
| 268 | |
| 269 | shipping_address(){ |
| 270 | var me = this; |
| 271 | erpnext.utils.get_address_display(this.frm, "shipping_address", |
| 272 | "shipping_address_display", true); |
| 273 | } |
| 274 | |
| 275 | billing_address() { |
| 276 | erpnext.utils.get_address_display(this.frm, "billing_address", |
| 277 | "billing_address_display", true); |
| 278 | } |
| 279 | |
| 280 | tc_name() { |
| 281 | this.get_terms(); |
| 282 | } |
| 283 | |
| 284 | update_auto_repeat_reference(doc) { |
| 285 | if (doc.auto_repeat) { |
| 286 | frappe.call({ |
| 287 | method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference", |
| 288 | args:{ |
| 289 | docname: doc.auto_repeat, |
| 290 | reference:doc.name |
| 291 | }, |
| 292 | callback: function(r){ |
| 293 | if (r.message=="success") { |
| 294 | frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'}); |
| 295 | } else { |
| 296 | frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'}); |
| 297 | } |
| 298 | } |
| 299 | }) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | manufacturer(doc, cdt, cdn) { |
| 304 | const row = locals[cdt][cdn]; |
| 305 | |
| 306 | if(row.manufacturer) { |
| 307 | frappe.call({ |
| 308 | method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no", |
| 309 | args: { |
| 310 | 'item_code': row.item_code, |
| 311 | 'manufacturer': row.manufacturer |
| 312 | }, |
| 313 | callback: function(r) { |
| 314 | if (r.message) { |
| 315 | frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message); |
| 316 | } |
| 317 | } |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 318 | }); |
| 319 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 320 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 321 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 322 | manufacturer_part_no(doc, cdt, cdn) { |
| 323 | const row = locals[cdt][cdn]; |
| 324 | |
| 325 | if (row.manufacturer_part_no) { |
| 326 | frappe.model.get_value('Item Manufacturer', |
| 327 | { |
| 328 | 'item_code': row.item_code, |
| 329 | 'manufacturer': row.manufacturer, |
| 330 | 'manufacturer_part_no': row.manufacturer_part_no |
| 331 | }, |
| 332 | 'name', |
| 333 | function(data) { |
| 334 | if (!data) { |
| 335 | let msg = { |
| 336 | message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]), |
| 337 | title: __("Invalid Part Number") |
| 338 | } |
| 339 | frappe.throw(msg); |
| 340 | } |
| 341 | } |
| 342 | ); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | add_serial_batch_bundle(doc, cdt, cdn) { |
| 347 | let item = locals[cdt][cdn]; |
| 348 | let me = this; |
| 349 | let path = "assets/erpnext/js/utils/serial_no_batch_selector.js"; |
| 350 | |
| 351 | frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"]) |
| 352 | .then((r) => { |
| 353 | if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) { |
| 354 | item.has_serial_no = r.message.has_serial_no; |
| 355 | item.has_batch_no = r.message.has_batch_no; |
| 356 | item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward"; |
| 357 | item.is_rejected = false; |
| 358 | |
| 359 | frappe.require(path, function() { |
| 360 | new erpnext.SerialBatchPackageSelector( |
| 361 | me.frm, item, (r) => { |
| 362 | if (r) { |
| 363 | let update_values = { |
| 364 | "serial_and_batch_bundle": r.name, |
| 365 | "qty": Math.abs(r.total_qty) |
| 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); |
| 373 | } |
| 374 | } |
| 375 | ); |
| 376 | }); |
| 377 | } |
| 378 | }); |
| 379 | } |
| 380 | |
| 381 | add_serial_batch_for_rejected_qty(doc, cdt, cdn) { |
| 382 | let item = locals[cdt][cdn]; |
| 383 | let me = this; |
| 384 | let path = "assets/erpnext/js/utils/serial_no_batch_selector.js"; |
| 385 | |
| 386 | frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"]) |
| 387 | .then((r) => { |
| 388 | if (r.message && (r.message.has_batch_no || r.message.has_serial_no)) { |
| 389 | item.has_serial_no = r.message.has_serial_no; |
| 390 | item.has_batch_no = r.message.has_batch_no; |
| 391 | item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward"; |
| 392 | item.is_rejected = true; |
| 393 | |
| 394 | frappe.require(path, function() { |
| 395 | new erpnext.SerialBatchPackageSelector( |
| 396 | me.frm, item, (r) => { |
| 397 | if (r) { |
| 398 | let update_values = { |
| 399 | "serial_and_batch_bundle": r.name, |
| 400 | "rejected_qty": Math.abs(r.total_qty) |
| 401 | } |
| 402 | |
| 403 | if (r.warehouse) { |
| 404 | update_values["rejected_warehouse"] = r.warehouse; |
| 405 | } |
| 406 | |
| 407 | frappe.model.set_value(item.doctype, item.name, update_values); |
| 408 | } |
| 409 | } |
| 410 | ); |
| 411 | }); |
| 412 | } |
| 413 | }); |
| 414 | } |
| 415 | }; |
| 416 | } |
| 417 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 418 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 419 | erpnext.buying.link_to_mrs = function(frm) { |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 420 | frappe.call({ |
| 421 | method: "erpnext.buying.utils.get_linked_material_requests", |
| 422 | args:{ |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 423 | items: frm.doc.items.map((item) => item.item_code) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 424 | }, |
| 425 | callback: function(r) { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 426 | if (!r.message || r.message.length == 0) { |
| 427 | frappe.throw({ |
| 428 | message: __("No pending Material Requests found to link for the given items."), |
| 429 | title: __("Note") |
| 430 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 431 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 432 | |
| 433 | var item_length = frm.doc.items.length; |
| 434 | for (let item of frm.doc.items) { |
| 435 | var qty = item.qty; |
| 436 | (r.message[0] || []).forEach(function(d) { |
| 437 | if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item) |
| 438 | { |
| 439 | item.material_request = d.mr_name; |
| 440 | item.material_request_item = d.mr_item; |
| 441 | var my_qty = Math.min(qty, d.qty); |
| 442 | qty = qty - my_qty; |
| 443 | d.qty = d.qty - my_qty; |
| 444 | item.stock_qty = my_qty*item.conversion_factor; |
| 445 | item.qty = my_qty; |
| 446 | |
| 447 | frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")"); |
| 448 | if (qty > 0) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 449 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 450 | frappe.msgprint("Splitting " + qty + " units of " + d.item_code); |
| 451 | var newrow = frappe.model.add_child(frm.doc, item.doctype, "items"); |
| 452 | item_length++; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 453 | |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 454 | for (var key in item) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 455 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 456 | newrow[key] = item[key]; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 457 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 458 | |
| 459 | newrow.idx = item_length; |
| 460 | newrow["stock_qty"] = newrow.conversion_factor*qty; |
| 461 | newrow["qty"] = qty; |
| 462 | |
| 463 | newrow["material_request"] = ""; |
| 464 | newrow["material_request_item"] = ""; |
| 465 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 466 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 467 | } |
| 468 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 469 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 470 | refresh_field("items"); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 471 | } |
| 472 | }); |
| 473 | } |
| 474 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 475 | erpnext.buying.get_default_bom = function(frm) { |
| 476 | $.each(frm.doc["items"] || [], function(i, d) { |
| 477 | if (d.item_code && d.bom === "") { |
| 478 | return frappe.call({ |
| 479 | type: "GET", |
| 480 | method: "erpnext.stock.get_item_details.get_default_bom", |
| 481 | args: { |
| 482 | "item_code": d.item_code, |
| 483 | }, |
| 484 | callback: function(r) { |
| 485 | if(r) { |
| 486 | frappe.model.set_value(d.doctype, d.name, "bom", r.message); |
| 487 | } |
| 488 | } |
| 489 | }) |
| 490 | } |
| 491 | }); |
| 492 | } |
| 493 | |
| 494 | erpnext.buying.get_items_from_product_bundle = function(frm) { |
| 495 | var dialog = new frappe.ui.Dialog({ |
| 496 | title: __("Get Items from Product Bundle"), |
| 497 | fields: [ |
| 498 | { |
| 499 | "fieldtype": "Link", |
| 500 | "label": __("Product Bundle"), |
| 501 | "fieldname": "product_bundle", |
| 502 | "options":"Product Bundle", |
| 503 | "reqd": 1 |
| 504 | }, |
| 505 | { |
| 506 | "fieldtype": "Currency", |
| 507 | "label": __("Quantity"), |
| 508 | "fieldname": "quantity", |
| 509 | "reqd": 1, |
| 510 | "default": 1 |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 511 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 512 | ], |
| 513 | primary_action_label: 'Get Items', |
| 514 | primary_action(args){ |
| 515 | if(!args) return; |
| 516 | dialog.hide(); |
| 517 | return frappe.call({ |
| 518 | type: "GET", |
| 519 | method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle", |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 520 | args: { |
marination | 4c677ea | 2022-01-25 23:52:52 +0530 | [diff] [blame] | 521 | row: { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 522 | item_code: args.product_bundle, |
| 523 | quantity: args.quantity, |
| 524 | parenttype: frm.doc.doctype, |
| 525 | parent: frm.doc.name, |
| 526 | supplier: frm.doc.supplier, |
| 527 | currency: frm.doc.currency, |
| 528 | conversion_rate: frm.doc.conversion_rate, |
| 529 | price_list: frm.doc.buying_price_list, |
| 530 | price_list_currency: frm.doc.price_list_currency, |
| 531 | plc_conversion_rate: frm.doc.plc_conversion_rate, |
| 532 | company: frm.doc.company, |
| 533 | is_subcontracted: frm.doc.is_subcontracted, |
| 534 | transaction_date: frm.doc.transaction_date || frm.doc.posting_date, |
| 535 | ignore_pricing_rule: frm.doc.ignore_pricing_rule, |
| 536 | doctype: frm.doc.doctype |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 537 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 538 | }, |
| 539 | freeze: true, |
| 540 | callback: function(r) { |
| 541 | const first_row_is_empty = function(child_table){ |
| 542 | if($.isArray(child_table) && child_table.length > 0) { |
| 543 | return !child_table[0].item_code; |
| 544 | } |
| 545 | return false; |
| 546 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 547 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 548 | const remove_empty_first_row = function(frm){ |
| 549 | if (first_row_is_empty(frm.doc.items)){ |
| 550 | frm.doc.items = frm.doc.items.splice(1); |
| 551 | } |
| 552 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 553 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 554 | if(!r.exc && r.message) { |
| 555 | remove_empty_first_row(frm); |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 556 | for (var i=0; i< r.message.length; i++) { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 557 | var d = frm.add_child("items"); |
| 558 | var item = r.message[i]; |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 559 | for (var key in item) { |
| 560 | if (!is_null(item[key]) && key !== "doctype") { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 561 | d[key] = item[key]; |
| 562 | } |
| 563 | } |
| 564 | if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) { |
| 565 | frm.script_manager.trigger("price_list_rate", d.doctype, d.name); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 566 | } |
| 567 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 568 | frm.refresh_field("items"); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 569 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 570 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 571 | }) |
| 572 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 573 | }); |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 574 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 575 | dialog.show(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 576 | } |