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"); |
| 5 | |
| 6 | cur_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 | |
| 10 | cur_frm.email_field = "contact_email"; |
| 11 | |
| 12 | erpnext.buying.BuyingController = erpnext.TransactionController.extend({ |
| 13 | setup: function() { |
| 14 | this._super(); |
| 15 | }, |
| 16 | |
| 17 | onload: function() { |
| 18 | this.setup_queries(); |
| 19 | this._super(); |
| 20 | |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 21 | this.frm.set_query('shipping_rule', function() { |
| 22 | return { |
| 23 | filters: { |
| 24 | "shipping_rule_type": "Buying" |
| 25 | } |
| 26 | }; |
| 27 | }); |
| 28 | |
rohitwaghchaure | 8356d4b | 2018-08-09 15:51:11 +0530 | [diff] [blame] | 29 | if (this.frm.doc.__islocal |
| 30 | && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) { |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 31 | this.frm.set_value("disable_rounded_total", cint(frappe.sys_defaults.disable_rounded_total)); |
| 32 | } |
| 33 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 34 | /* eslint-disable */ |
| 35 | // no idea where me is coming from |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 36 | if(this.frm.get_field('shipping_address')) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 37 | this.frm.set_query("shipping_address", function() { |
| 38 | if(me.frm.doc.customer) { |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 39 | return { |
KanchanChauhan | 1dc26b1 | 2017-06-13 15:26:35 +0530 | [diff] [blame] | 40 | query: 'frappe.contacts.doctype.address.address.address_query', |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 41 | filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer } |
| 42 | }; |
| 43 | } else |
| 44 | return erpnext.queries.company_address_query(me.frm.doc) |
| 45 | }); |
| 46 | } |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 47 | /* eslint-enable */ |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 48 | }, |
| 49 | |
| 50 | setup_queries: function() { |
| 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 | }); |
| 59 | } |
| 60 | |
| 61 | me.frm.set_query('supplier', erpnext.queries.supplier); |
| 62 | me.frm.set_query('contact_person', erpnext.queries.contact_query); |
| 63 | me.frm.set_query('supplier_address', erpnext.queries.address_query); |
| 64 | |
| 65 | if(this.frm.fields_dict.supplier) { |
| 66 | this.frm.set_query("supplier", function() { |
| 67 | return{ query: "erpnext.controllers.queries.supplier_query" }}); |
| 68 | } |
| 69 | |
| 70 | this.frm.set_query("item_code", "items", function() { |
| 71 | if(me.frm.doc.is_subcontracted == "Yes") { |
| 72 | return{ |
| 73 | query: "erpnext.controllers.queries.item_query", |
| 74 | filters:{ 'is_sub_contracted_item': 1 } |
| 75 | } |
| 76 | } else { |
| 77 | return{ |
| 78 | query: "erpnext.controllers.queries.item_query", |
| 79 | filters: {'is_purchase_item': 1} |
| 80 | } |
| 81 | } |
| 82 | }); |
| 83 | }, |
| 84 | |
| 85 | refresh: function(doc) { |
| 86 | frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; |
| 87 | |
| 88 | this.frm.toggle_display("supplier_name", |
| 89 | (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier)); |
| 90 | |
| 91 | if(this.frm.doc.docstatus==0 && |
| 92 | (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) { |
| 93 | this.set_from_product_bundle(); |
| 94 | } |
| 95 | |
| 96 | this._super(); |
| 97 | }, |
| 98 | |
| 99 | supplier: function() { |
| 100 | var me = this; |
| 101 | erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()}); |
| 102 | }, |
| 103 | |
| 104 | supplier_address: function() { |
| 105 | erpnext.utils.get_address_display(this.frm); |
| 106 | }, |
| 107 | |
| 108 | buying_price_list: function() { |
| 109 | this.apply_price_list(); |
| 110 | }, |
| 111 | |
| 112 | price_list_rate: function(doc, cdt, cdn) { |
| 113 | var item = frappe.get_doc(cdt, cdn); |
| 114 | frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]); |
| 115 | |
Shreya Shah | 0631aed | 2018-08-14 10:51:48 +0530 | [diff] [blame] | 116 | item.discount_amount = flt(item.price_list_rate) * flt(item.discount_percentage) / 100; |
| 117 | item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item)); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 118 | |
| 119 | this.calculate_taxes_and_totals(); |
| 120 | }, |
| 121 | |
| 122 | discount_percentage: function(doc, cdt, cdn) { |
| 123 | this.price_list_rate(doc, cdt, cdn); |
| 124 | }, |
| 125 | |
| 126 | qty: function(doc, cdt, cdn) { |
| 127 | var item = frappe.get_doc(cdt, cdn); |
| 128 | if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) { |
| 129 | frappe.model.round_floats_in(item, ["qty", "received_qty"]); |
| 130 | |
| 131 | if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return } |
| 132 | |
| 133 | if(!item.rejected_qty && item.qty) { |
| 134 | item.received_qty = item.qty; |
| 135 | } |
| 136 | |
| 137 | frappe.model.round_floats_in(item, ["qty", "received_qty"]); |
| 138 | item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item)); |
| 139 | } |
| 140 | |
| 141 | this._super(doc, cdt, cdn); |
| 142 | }, |
| 143 | |
| 144 | received_qty: function(doc, cdt, cdn) { |
| 145 | this.calculate_accepted_qty(doc, cdt, cdn) |
| 146 | }, |
| 147 | |
| 148 | rejected_qty: function(doc, cdt, cdn) { |
| 149 | this.calculate_accepted_qty(doc, cdt, cdn) |
| 150 | }, |
| 151 | |
| 152 | calculate_accepted_qty: function(doc, cdt, cdn){ |
| 153 | var item = frappe.get_doc(cdt, cdn); |
| 154 | frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]); |
| 155 | |
| 156 | if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return } |
| 157 | |
| 158 | item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item)); |
| 159 | this.qty(doc, cdt, cdn); |
| 160 | }, |
| 161 | |
| 162 | validate_negative_quantity: function(cdt, cdn, item, fieldnames){ |
| 163 | if(!item || !fieldnames) { return } |
| 164 | |
| 165 | var is_negative_qty = false; |
| 166 | for(var i = 0; i<fieldnames.length; i++) { |
| 167 | if(item[fieldnames[i]] < 0){ |
| 168 | frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}", |
| 169 | [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code])); |
| 170 | is_negative_qty = true; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return is_negative_qty |
| 176 | }, |
| 177 | |
| 178 | warehouse: function(doc, cdt, cdn) { |
| 179 | var item = frappe.get_doc(cdt, cdn); |
| 180 | if(item.item_code && item.warehouse) { |
| 181 | return this.frm.call({ |
| 182 | method: "erpnext.stock.get_item_details.get_bin_details", |
| 183 | child: item, |
| 184 | args: { |
| 185 | item_code: item.item_code, |
| 186 | warehouse: item.warehouse |
| 187 | } |
| 188 | }); |
| 189 | } |
| 190 | }, |
| 191 | |
| 192 | project: function(doc, cdt, cdn) { |
| 193 | var item = frappe.get_doc(cdt, cdn); |
| 194 | if(item.project) { |
| 195 | $.each(this.frm.doc["items"] || [], |
| 196 | function(i, other_item) { |
| 197 | if(!other_item.project) { |
| 198 | other_item.project = item.project; |
| 199 | refresh_field("project", other_item.name, other_item.parentfield); |
| 200 | } |
| 201 | }); |
| 202 | } |
| 203 | }, |
| 204 | |
| 205 | category: function(doc, cdt, cdn) { |
| 206 | // should be the category field of tax table |
| 207 | if(cdt != doc.doctype) { |
| 208 | this.calculate_taxes_and_totals(); |
| 209 | } |
| 210 | }, |
| 211 | add_deduct_tax: function(doc, cdt, cdn) { |
| 212 | this.calculate_taxes_and_totals(); |
| 213 | }, |
| 214 | |
| 215 | set_from_product_bundle: function() { |
| 216 | var me = this; |
| 217 | this.frm.add_custom_button(__("Product Bundle"), function() { |
| 218 | erpnext.buying.get_items_from_product_bundle(me.frm); |
| 219 | }, __("Get items from")); |
| 220 | }, |
| 221 | |
| 222 | shipping_address: function(){ |
| 223 | var me = this; |
| 224 | erpnext.utils.get_address_display(this.frm, "shipping_address", |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 225 | "shipping_address_display", true); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 226 | }, |
| 227 | |
| 228 | tc_name: function() { |
| 229 | this.get_terms(); |
Shreya Shah | 4fb9230 | 2018-10-08 14:18:03 +0530 | [diff] [blame] | 230 | }, |
| 231 | |
| 232 | link_to_mrs: function() { |
| 233 | var my_items = []; |
| 234 | for (var i in cur_frm.doc.items) { |
| 235 | if(!cur_frm.doc.items[i].material_request){ |
| 236 | my_items.push(cur_frm.doc.items[i].item_code); |
| 237 | } |
| 238 | } |
| 239 | frappe.call({ |
| 240 | method: "erpnext.buying.utils.get_linked_material_requests", |
| 241 | args:{ |
| 242 | items: my_items |
| 243 | }, |
| 244 | callback: function(r) { |
| 245 | if(!r.message) { |
| 246 | frappe.throw(__("No pending Material Requests found to link for the given items.")) |
| 247 | } |
| 248 | else { |
| 249 | var i = 0; |
| 250 | var item_length = cur_frm.doc.items.length; |
| 251 | while (i < item_length) { |
| 252 | var qty = cur_frm.doc.items[i].qty; |
| 253 | (r.message[0] || []).forEach(function(d) { |
| 254 | if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item) |
| 255 | { |
| 256 | cur_frm.doc.items[i].material_request = d.mr_name; |
| 257 | cur_frm.doc.items[i].material_request_item = d.mr_item; |
| 258 | var my_qty = Math.min(qty, d.qty); |
| 259 | qty = qty - my_qty; |
| 260 | d.qty = d.qty - my_qty; |
| 261 | cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor; |
| 262 | cur_frm.doc.items[i].qty = my_qty; |
| 263 | |
| 264 | frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")"); |
| 265 | if (qty > 0) |
| 266 | { |
| 267 | frappe.msgprint("Splitting " + qty + " units of " + d.item_code); |
| 268 | var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items"); |
| 269 | item_length++; |
| 270 | |
| 271 | for (var key in cur_frm.doc.items[i]) |
| 272 | { |
| 273 | newrow[key] = cur_frm.doc.items[i][key]; |
| 274 | } |
| 275 | |
| 276 | newrow.idx = item_length; |
| 277 | newrow["stock_qty"] = newrow.conversion_factor*qty; |
| 278 | newrow["qty"] = qty; |
| 279 | |
| 280 | newrow["material_request"] = ""; |
| 281 | newrow["material_request_item"] = ""; |
| 282 | |
| 283 | } |
| 284 | } |
| 285 | }); |
| 286 | i++; |
| 287 | } |
| 288 | refresh_field("items"); |
| 289 | //cur_frm.save(); |
| 290 | } |
| 291 | } |
| 292 | }); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 293 | } |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 294 | }); |
| 295 | |
| 296 | cur_frm.add_fetch('project', 'cost_center', 'cost_center'); |
| 297 | |
| 298 | erpnext.buying.get_default_bom = function(frm) { |
| 299 | $.each(frm.doc["items"] || [], function(i, d) { |
| 300 | if (d.item_code && d.bom === "") { |
| 301 | return frappe.call({ |
| 302 | type: "GET", |
| 303 | method: "erpnext.stock.get_item_details.get_default_bom", |
| 304 | args: { |
| 305 | "item_code": d.item_code, |
| 306 | }, |
| 307 | callback: function(r) { |
| 308 | if(r) { |
| 309 | frappe.model.set_value(d.doctype, d.name, "bom", r.message); |
| 310 | } |
| 311 | } |
| 312 | }) |
| 313 | } |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | erpnext.buying.get_items_from_product_bundle = function(frm) { |
| 318 | var dialog = new frappe.ui.Dialog({ |
| 319 | title: __("Get Items from Product Bundle"), |
| 320 | fields: [ |
| 321 | { |
| 322 | "fieldtype": "Link", |
| 323 | "label": __("Product Bundle"), |
| 324 | "fieldname": "product_bundle", |
| 325 | "options":"Product Bundle", |
| 326 | "reqd": 1 |
| 327 | }, |
| 328 | { |
| 329 | "fieldtype": "Currency", |
| 330 | "label": __("Quantity"), |
| 331 | "fieldname": "quantity", |
| 332 | "reqd": 1, |
| 333 | "default": 1 |
| 334 | }, |
| 335 | { |
| 336 | "fieldtype": "Button", |
| 337 | "label": __("Get Items"), |
| 338 | "fieldname": "get_items", |
| 339 | "cssClass": "btn-primary" |
| 340 | } |
| 341 | ] |
| 342 | }); |
| 343 | |
| 344 | dialog.fields_dict.get_items.$input.click(function() { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 345 | var args = dialog.get_values(); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 346 | if(!args) return; |
| 347 | dialog.hide(); |
| 348 | return frappe.call({ |
| 349 | type: "GET", |
| 350 | method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle", |
| 351 | args: { |
| 352 | args: { |
| 353 | item_code: args.product_bundle, |
| 354 | quantity: args.quantity, |
| 355 | parenttype: frm.doc.doctype, |
| 356 | parent: frm.doc.name, |
| 357 | supplier: frm.doc.supplier, |
| 358 | currency: frm.doc.currency, |
| 359 | conversion_rate: frm.doc.conversion_rate, |
| 360 | price_list: frm.doc.buying_price_list, |
| 361 | price_list_currency: frm.doc.price_list_currency, |
| 362 | plc_conversion_rate: frm.doc.plc_conversion_rate, |
| 363 | company: frm.doc.company, |
| 364 | is_subcontracted: frm.doc.is_subcontracted, |
| 365 | transaction_date: frm.doc.transaction_date || frm.doc.posting_date, |
| 366 | ignore_pricing_rule: frm.doc.ignore_pricing_rule |
| 367 | } |
| 368 | }, |
| 369 | freeze: true, |
| 370 | callback: function(r) { |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 371 | const first_row_is_empty = function(child_table){ |
| 372 | if($.isArray(child_table) && child_table.length > 0) { |
| 373 | return !child_table[0].item_code; |
| 374 | } |
| 375 | return false; |
| 376 | }; |
| 377 | |
| 378 | const remove_empty_first_row = function(frm){ |
| 379 | if (first_row_is_empty(frm.doc.items)){ |
| 380 | frm.doc.items = frm.doc.items.splice(1); |
| 381 | } |
| 382 | }; |
| 383 | |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 384 | if(!r.exc && r.message) { |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 385 | remove_empty_first_row(frm); |
Rushabh Mehta | f0b4562 | 2017-03-31 12:53:05 +0530 | [diff] [blame] | 386 | for ( var i=0; i< r.message.length; i++ ) { |
| 387 | var d = frm.add_child("items"); |
| 388 | var item = r.message[i]; |
| 389 | for ( var key in item) { |
| 390 | if ( !is_null(item[key]) ) { |
| 391 | d[key] = item[key]; |
| 392 | } |
| 393 | } |
| 394 | if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) { |
| 395 | frm.script_manager.trigger("price_list_rate", d.doctype, d.name); |
| 396 | } |
| 397 | } |
| 398 | frm.refresh_field("items"); |
| 399 | } |
| 400 | } |
| 401 | }) |
| 402 | }); |
| 403 | dialog.show(); |
| 404 | } |
tundebabzy | f35710d | 2017-08-16 08:55:18 +0100 | [diff] [blame] | 405 | |