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