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')) { |
creative-paramu | ca0c3eb | 2023-12-08 11:05:09 +0530 | [diff] [blame] | 39 | this.frm.set_query("shipping_address", () => { |
| 40 | if(this.frm.doc.customer) { |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 41 | return { |
| 42 | query: 'frappe.contacts.doctype.address.address.address_query', |
creative-paramu | ca0c3eb | 2023-12-08 11:05:09 +0530 | [diff] [blame] | 43 | filters: { link_doctype: 'Customer', link_name: this.frm.doc.customer } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 44 | }; |
| 45 | } else |
creative-paramu | ca0c3eb | 2023-12-08 11:05:09 +0530 | [diff] [blame] | 46 | return erpnext.queries.company_address_query(this.frm.doc) |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 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 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 77 | 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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 97 | } |
| 98 | }); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 99 | |
marination | 8a30cb2 | 2020-04-23 00:18:21 +0530 | [diff] [blame] | 100 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 101 | 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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 108 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 109 | 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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 115 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 116 | refresh(doc) { |
| 117 | frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 118 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 119 | this.frm.toggle_display("supplier_name", |
| 120 | (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] | 121 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 122 | 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 Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 126 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 127 | this.toggle_subcontracting_fields(); |
| 128 | super.refresh(); |
| 129 | } |
| 130 | |
| 131 | toggle_subcontracting_fields() { |
barredterra | d238751 | 2024-03-19 12:03:36 +0100 | [diff] [blame] | 132 | if (['Purchase Receipt', 'Purchase Invoice'].includes(this.frm.doc.doctype)) { |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 133 | 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 Decultot | 8b22310 | 2018-06-28 10:57:58 +0200 | [diff] [blame] | 206 | } |
| 207 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 208 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 209 | return is_negative_qty |
| 210 | } |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 211 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 212 | 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 |
marination | 3e7a8ab | 2020-04-07 21:00:57 +0530 | [diff] [blame] | 223 | } |
Rohit Waghchaure | bc75a7e | 2022-10-10 13:28:19 +0530 | [diff] [blame] | 224 | }); |
| 225 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 226 | } |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 227 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 228 | 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 Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 236 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 237 | }); |
| 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 Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 314 | }); |
| 315 | } |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 316 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 317 | |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 318 | 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 Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 345 | |
| 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 Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 354 | 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 Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 360 | } |
Rohit Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 361 | |
| 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 Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 373 | } |
Rohit Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 374 | } |
| 375 | ); |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 376 | } |
| 377 | }); |
| 378 | } |
| 379 | |
| 380 | add_serial_batch_for_rejected_qty(doc, cdt, cdn) { |
| 381 | let item = locals[cdt][cdn]; |
| 382 | let me = this; |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 383 | |
| 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; |
| 389 | item.type_of_transaction = item.qty > 0 ? "Inward" : "Outward"; |
| 390 | item.is_rejected = true; |
| 391 | |
Rohit Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 392 | 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 Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 398 | } |
Rohit Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 399 | |
| 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 Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 411 | } |
Rohit Waghchaure | 0d9d42a | 2024-03-27 11:52:06 +0530 | [diff] [blame] | 412 | } |
| 413 | ); |
Deepesh Garg | 8ccb8e3 | 2023-07-23 18:50:44 +0530 | [diff] [blame] | 414 | } |
| 415 | }); |
| 416 | } |
| 417 | }; |
| 418 | } |
| 419 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 420 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 421 | erpnext.buying.link_to_mrs = function(frm) { |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 422 | frappe.call({ |
| 423 | method: "erpnext.buying.utils.get_linked_material_requests", |
| 424 | args:{ |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 425 | items: frm.doc.items.map((item) => item.item_code) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 426 | }, |
| 427 | callback: function(r) { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 428 | 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 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 433 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 434 | |
| 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) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 451 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 452 | frappe.msgprint("Splitting " + qty + " units of " + d.item_code); |
| 453 | var newrow = frappe.model.add_child(frm.doc, item.doctype, "items"); |
| 454 | item_length++; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 455 | |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 456 | for (var key in item) |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 457 | { |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 458 | newrow[key] = item[key]; |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 459 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 460 | |
| 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 | |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 468 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 469 | } |
| 470 | }); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 471 | } |
marination | 359778e | 2020-10-16 16:47:23 +0530 | [diff] [blame] | 472 | refresh_field("items"); |
marination | a119688 | 2020-09-24 15:22:46 +0530 | [diff] [blame] | 473 | } |
| 474 | }); |
| 475 | } |
| 476 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 477 | erpnext.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 | |
| 496 | erpnext.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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 513 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 514 | ], |
| 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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 522 | args: { |
marination | 4c677ea | 2022-01-25 23:52:52 +0530 | [diff] [blame] | 523 | row: { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 524 | 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 |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 539 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 540 | }, |
| 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 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 549 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 550 | 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 | }; |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 555 | |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 556 | if(!r.exc && r.message) { |
| 557 | remove_empty_first_row(frm); |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 558 | for (var i=0; i< r.message.length; i++) { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 559 | var d = frm.add_child("items"); |
| 560 | var item = r.message[i]; |
Afshan | eb4b2aa | 2020-09-01 15:01:32 +0530 | [diff] [blame] | 561 | for (var key in item) { |
| 562 | if (!is_null(item[key]) && key !== "doctype") { |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 563 | 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 Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 568 | } |
| 569 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 570 | frm.refresh_field("items"); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 571 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 572 | } |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 573 | }) |
| 574 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 575 | }); |
Marica | 9942acd | 2020-04-21 13:13:39 +0530 | [diff] [blame] | 576 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 577 | dialog.show(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 578 | } |