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