blob: 7e61f03af9c9a5576ce92203e03ad6c6cb7d0d23 [file] [log] [blame]
Rushabh Mehtaf0b45622017-03-31 12:53:05 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2// License: GNU General Public License v3. See license.txt
3
4frappe.provide("erpnext.buying");
5
6cur_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
10cur_frm.email_field = "contact_email";
11
12erpnext.buying.BuyingController = erpnext.TransactionController.extend({
13 setup: function() {
14 this._super();
15 },
16
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053017 onload: function(doc, cdt, cdn) {
18 this.setup_queries(doc, cdt, cdn);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053019 this._super();
20
Rushabh Mehta30dc9a12017-11-17 14:31:09 +053021 this.frm.set_query('shipping_rule', function() {
22 return {
23 filters: {
24 "shipping_rule_type": "Buying"
25 }
26 };
27 });
28
rohitwaghchaure8356d4b2018-08-09 15:51:11 +053029 if (this.frm.doc.__islocal
30 && frappe.meta.has_field(this.frm.doc.doctype, "disable_rounded_total")) {
Nabin Haita39f3242019-01-21 18:50:50 +053031
32 var df = frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total");
33 var disable = df.default || cint(frappe.sys_defaults.disable_rounded_total);
34 this.frm.set_value("disable_rounded_total", disable);
rohitwaghchaureae4ff5a2018-08-01 18:09:51 +053035 }
36
Faris Ansariab74ca72017-05-30 12:54:42 +053037 /* eslint-disable */
38 // no idea where me is coming from
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053039 if(this.frm.get_field('shipping_address')) {
Faris Ansariab74ca72017-05-30 12:54:42 +053040 this.frm.set_query("shipping_address", function() {
41 if(me.frm.doc.customer) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053042 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053043 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053044 filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
45 };
46 } else
47 return erpnext.queries.company_address_query(me.frm.doc)
48 });
49 }
Faris Ansariab74ca72017-05-30 12:54:42 +053050 /* eslint-enable */
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053051 },
52
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053053 setup_queries: function(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053054 var me = this;
55
56 if(this.frm.fields_dict.buying_price_list) {
57 this.frm.set_query("buying_price_list", function() {
58 return{
59 filters: { 'buying': 1 }
60 }
61 });
62 }
63
64 me.frm.set_query('supplier', erpnext.queries.supplier);
65 me.frm.set_query('contact_person', erpnext.queries.contact_query);
66 me.frm.set_query('supplier_address', erpnext.queries.address_query);
67
68 if(this.frm.fields_dict.supplier) {
69 this.frm.set_query("supplier", function() {
70 return{ query: "erpnext.controllers.queries.supplier_query" }});
71 }
72
73 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080074 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053075 return{
76 query: "erpnext.controllers.queries.item_query",
77 filters:{ 'is_sub_contracted_item': 1 }
78 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080079 }
80 else if (me.frm.doc.material_request_type == "Customer Provided") {
81 return{
82 query: "erpnext.controllers.queries.item_query",
83 filters:{ 'customer': me.frm.doc.customer }
84 }
85 }
86 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053087 return{
88 query: "erpnext.controllers.queries.item_query",
89 filters: {'is_purchase_item': 1}
90 }
91 }
92 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053093
94
95 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
96 const row = locals[cdt][cdn];
97 return {
98 query: "erpnext.controllers.queries.item_manufacturer_query",
99 filters:{ 'item_code': row.item_code }
100 }
101 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530102 },
103
104 refresh: function(doc) {
105 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
106
107 this.frm.toggle_display("supplier_name",
108 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
109
110 if(this.frm.doc.docstatus==0 &&
111 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
112 this.set_from_product_bundle();
113 }
114
115 this._super();
116 },
117
118 supplier: function() {
119 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530120 erpnext.utils.get_party_details(this.frm, null, null, function(){
121 me.apply_price_list();
122 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530123 },
124
125 supplier_address: function() {
126 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500127 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530128 },
129
130 buying_price_list: function() {
131 this.apply_price_list();
132 },
133
134 price_list_rate: function(doc, cdt, cdn) {
135 var item = frappe.get_doc(cdt, cdn);
136 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
137
Manas Solankie5e87f72018-05-28 20:07:08 +0530138 let item_rate = item.price_list_rate;
139 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
140 item_rate = item.blanket_order_rate;
141 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530142
143 if (item.discount_percentage) {
144 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
145 }
146
Shreya Shah0631aed2018-08-14 10:51:48 +0530147 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530148
149 this.calculate_taxes_and_totals();
150 },
151
152 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530153 var item = frappe.get_doc(cdt, cdn);
154 item.discount_amount = 0.0;
155 this.price_list_rate(doc, cdt, cdn);
156 },
157
158 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530159 var item = frappe.get_doc(cdt, cdn);
160 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530161 this.price_list_rate(doc, cdt, cdn);
162 },
163
164 qty: function(doc, cdt, cdn) {
165 var item = frappe.get_doc(cdt, cdn);
166 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
167 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
168
169 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
170
171 if(!item.rejected_qty && item.qty) {
172 item.received_qty = item.qty;
173 }
174
175 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
176 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
177 }
178
179 this._super(doc, cdt, cdn);
180 },
181
182 received_qty: function(doc, cdt, cdn) {
183 this.calculate_accepted_qty(doc, cdt, cdn)
184 },
185
186 rejected_qty: function(doc, cdt, cdn) {
187 this.calculate_accepted_qty(doc, cdt, cdn)
188 },
189
190 calculate_accepted_qty: function(doc, cdt, cdn){
191 var item = frappe.get_doc(cdt, cdn);
192 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
193
194 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
195
196 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
197 this.qty(doc, cdt, cdn);
198 },
199
200 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
201 if(!item || !fieldnames) { return }
202
203 var is_negative_qty = false;
204 for(var i = 0; i<fieldnames.length; i++) {
205 if(item[fieldnames[i]] < 0){
206 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
207 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
208 is_negative_qty = true;
209 break;
210 }
211 }
212
213 return is_negative_qty
214 },
215
216 warehouse: function(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 }
226 });
227 }
228 },
229
230 project: function(doc, cdt, cdn) {
231 var item = frappe.get_doc(cdt, cdn);
232 if(item.project) {
233 $.each(this.frm.doc["items"] || [],
234 function(i, other_item) {
235 if(!other_item.project) {
236 other_item.project = item.project;
237 refresh_field("project", other_item.name, other_item.parentfield);
238 }
239 });
240 }
241 },
242
243 category: function(doc, cdt, cdn) {
244 // should be the category field of tax table
245 if(cdt != doc.doctype) {
246 this.calculate_taxes_and_totals();
247 }
248 },
249 add_deduct_tax: function(doc, cdt, cdn) {
250 this.calculate_taxes_and_totals();
251 },
252
253 set_from_product_bundle: function() {
254 var me = this;
255 this.frm.add_custom_button(__("Product Bundle"), function() {
256 erpnext.buying.get_items_from_product_bundle(me.frm);
257 }, __("Get items from"));
258 },
259
260 shipping_address: function(){
261 var me = this;
262 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530263 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530264 },
265
266 tc_name: function() {
267 this.get_terms();
268 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200269
Shreya Shah95d93912018-10-08 14:17:37 +0530270 link_to_mrs: function() {
271 var my_items = [];
272 for (var i in cur_frm.doc.items) {
273 if(!cur_frm.doc.items[i].material_request){
274 my_items.push(cur_frm.doc.items[i].item_code);
275 }
276 }
277 frappe.call({
278 method: "erpnext.buying.utils.get_linked_material_requests",
279 args:{
280 items: my_items
281 },
282 callback: function(r) {
283 if(!r.message) {
284 frappe.throw(__("No pending Material Requests found to link for the given items."))
285 }
286 else {
287 var i = 0;
288 var item_length = cur_frm.doc.items.length;
289 while (i < item_length) {
290 var qty = cur_frm.doc.items[i].qty;
291 (r.message[0] || []).forEach(function(d) {
292 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
293 {
294 cur_frm.doc.items[i].material_request = d.mr_name;
295 cur_frm.doc.items[i].material_request_item = d.mr_item;
296 var my_qty = Math.min(qty, d.qty);
297 qty = qty - my_qty;
298 d.qty = d.qty - my_qty;
299 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
300 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530301
Shreya Shah95d93912018-10-08 14:17:37 +0530302 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
303 if (qty > 0)
304 {
305 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
306 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
307 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530308
Shreya Shah95d93912018-10-08 14:17:37 +0530309 for (var key in cur_frm.doc.items[i])
310 {
311 newrow[key] = cur_frm.doc.items[i][key];
312 }
Nabin Haita39f3242019-01-21 18:50:50 +0530313
Shreya Shah95d93912018-10-08 14:17:37 +0530314 newrow.idx = item_length;
315 newrow["stock_qty"] = newrow.conversion_factor*qty;
316 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530317
Shreya Shah95d93912018-10-08 14:17:37 +0530318 newrow["material_request"] = "";
319 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530320
Shreya Shah95d93912018-10-08 14:17:37 +0530321 }
322 }
323 });
324 i++;
325 }
326 refresh_field("items");
327 //cur_frm.save();
328 }
329 }
330 });
331 },
332
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200333 update_auto_repeat_reference: function(doc) {
334 if (doc.auto_repeat) {
335 frappe.call({
336 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530337 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200338 docname: doc.auto_repeat,
339 reference:doc.name
340 },
341 callback: function(r){
342 if (r.message=="success") {
343 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
344 } else {
345 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
346 }
347 }
348 })
349 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530350 },
351
352 manufacturer: function(doc, cdt, cdn) {
353 const row = locals[cdt][cdn];
354
355 if(row.manufacturer) {
356 frappe.call({
357 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
358 args: {
359 'item_code': row.item_code,
360 'manufacturer': row.manufacturer
361 },
362 callback: function(r) {
363 if (r.message) {
364 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
365 }
366 }
367 });
368 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530369 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530370});
371
372cur_frm.add_fetch('project', 'cost_center', 'cost_center');
373
374erpnext.buying.get_default_bom = function(frm) {
375 $.each(frm.doc["items"] || [], function(i, d) {
376 if (d.item_code && d.bom === "") {
377 return frappe.call({
378 type: "GET",
379 method: "erpnext.stock.get_item_details.get_default_bom",
380 args: {
381 "item_code": d.item_code,
382 },
383 callback: function(r) {
384 if(r) {
385 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
386 }
387 }
388 })
389 }
390 });
391}
392
393erpnext.buying.get_items_from_product_bundle = function(frm) {
394 var dialog = new frappe.ui.Dialog({
395 title: __("Get Items from Product Bundle"),
396 fields: [
397 {
398 "fieldtype": "Link",
399 "label": __("Product Bundle"),
400 "fieldname": "product_bundle",
401 "options":"Product Bundle",
402 "reqd": 1
403 },
404 {
405 "fieldtype": "Currency",
406 "label": __("Quantity"),
407 "fieldname": "quantity",
408 "reqd": 1,
409 "default": 1
410 },
411 {
412 "fieldtype": "Button",
413 "label": __("Get Items"),
414 "fieldname": "get_items",
415 "cssClass": "btn-primary"
416 }
417 ]
418 });
419
420 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530421 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530422 if(!args) return;
423 dialog.hide();
424 return frappe.call({
425 type: "GET",
426 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
427 args: {
428 args: {
429 item_code: args.product_bundle,
430 quantity: args.quantity,
431 parenttype: frm.doc.doctype,
432 parent: frm.doc.name,
433 supplier: frm.doc.supplier,
434 currency: frm.doc.currency,
435 conversion_rate: frm.doc.conversion_rate,
436 price_list: frm.doc.buying_price_list,
437 price_list_currency: frm.doc.price_list_currency,
438 plc_conversion_rate: frm.doc.plc_conversion_rate,
439 company: frm.doc.company,
440 is_subcontracted: frm.doc.is_subcontracted,
441 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
442 ignore_pricing_rule: frm.doc.ignore_pricing_rule
443 }
444 },
445 freeze: true,
446 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100447 const first_row_is_empty = function(child_table){
448 if($.isArray(child_table) && child_table.length > 0) {
449 return !child_table[0].item_code;
450 }
451 return false;
452 };
453
454 const remove_empty_first_row = function(frm){
455 if (first_row_is_empty(frm.doc.items)){
456 frm.doc.items = frm.doc.items.splice(1);
457 }
458 };
459
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530460 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100461 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530462 for ( var i=0; i< r.message.length; i++ ) {
463 var d = frm.add_child("items");
464 var item = r.message[i];
465 for ( var key in item) {
466 if ( !is_null(item[key]) ) {
467 d[key] = item[key];
468 }
469 }
470 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
471 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
472 }
473 }
474 frm.refresh_field("items");
475 }
476 }
477 })
478 });
479 dialog.show();
480}