blob: 97c823d0535aa9184381c39e0e6a717d3b96abf7 [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
Aditya Hase48585c92019-06-18 19:11:17 +0530147 if (item.discount_amount) {
148 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
149 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530150
151 this.calculate_taxes_and_totals();
152 },
153
154 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530155 var item = frappe.get_doc(cdt, cdn);
156 item.discount_amount = 0.0;
157 this.price_list_rate(doc, cdt, cdn);
158 },
159
160 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530161 var item = frappe.get_doc(cdt, cdn);
162 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530163 this.price_list_rate(doc, cdt, cdn);
164 },
165
166 qty: function(doc, cdt, cdn) {
167 var item = frappe.get_doc(cdt, cdn);
168 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
169 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
170
171 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
172
173 if(!item.rejected_qty && item.qty) {
174 item.received_qty = item.qty;
175 }
176
177 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
178 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
179 }
180
181 this._super(doc, cdt, cdn);
182 },
183
184 received_qty: function(doc, cdt, cdn) {
185 this.calculate_accepted_qty(doc, cdt, cdn)
186 },
187
188 rejected_qty: function(doc, cdt, cdn) {
189 this.calculate_accepted_qty(doc, cdt, cdn)
190 },
191
192 calculate_accepted_qty: function(doc, cdt, cdn){
193 var item = frappe.get_doc(cdt, cdn);
194 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
195
196 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
197
198 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
199 this.qty(doc, cdt, cdn);
200 },
201
202 validate_negative_quantity: function(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}",
209 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
210 is_negative_qty = true;
211 break;
212 }
213 }
214
215 return is_negative_qty
216 },
217
218 warehouse: function(doc, cdt, cdn) {
219 var item = frappe.get_doc(cdt, cdn);
220 if(item.item_code && item.warehouse) {
221 return this.frm.call({
222 method: "erpnext.stock.get_item_details.get_bin_details",
223 child: item,
224 args: {
225 item_code: item.item_code,
226 warehouse: item.warehouse
227 }
228 });
229 }
230 },
231
232 project: function(doc, cdt, cdn) {
233 var item = frappe.get_doc(cdt, cdn);
234 if(item.project) {
235 $.each(this.frm.doc["items"] || [],
236 function(i, other_item) {
237 if(!other_item.project) {
238 other_item.project = item.project;
239 refresh_field("project", other_item.name, other_item.parentfield);
240 }
241 });
242 }
243 },
244
245 category: function(doc, cdt, cdn) {
246 // should be the category field of tax table
247 if(cdt != doc.doctype) {
248 this.calculate_taxes_and_totals();
249 }
250 },
251 add_deduct_tax: function(doc, cdt, cdn) {
252 this.calculate_taxes_and_totals();
253 },
254
255 set_from_product_bundle: function() {
256 var me = this;
257 this.frm.add_custom_button(__("Product Bundle"), function() {
258 erpnext.buying.get_items_from_product_bundle(me.frm);
259 }, __("Get items from"));
260 },
261
262 shipping_address: function(){
263 var me = this;
264 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530265 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530266 },
267
268 tc_name: function() {
269 this.get_terms();
270 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200271
Shreya Shah95d93912018-10-08 14:17:37 +0530272 link_to_mrs: function() {
273 var my_items = [];
274 for (var i in cur_frm.doc.items) {
275 if(!cur_frm.doc.items[i].material_request){
276 my_items.push(cur_frm.doc.items[i].item_code);
277 }
278 }
279 frappe.call({
280 method: "erpnext.buying.utils.get_linked_material_requests",
281 args:{
282 items: my_items
283 },
284 callback: function(r) {
285 if(!r.message) {
286 frappe.throw(__("No pending Material Requests found to link for the given items."))
287 }
288 else {
289 var i = 0;
290 var item_length = cur_frm.doc.items.length;
291 while (i < item_length) {
292 var qty = cur_frm.doc.items[i].qty;
293 (r.message[0] || []).forEach(function(d) {
294 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
295 {
296 cur_frm.doc.items[i].material_request = d.mr_name;
297 cur_frm.doc.items[i].material_request_item = d.mr_item;
298 var my_qty = Math.min(qty, d.qty);
299 qty = qty - my_qty;
300 d.qty = d.qty - my_qty;
301 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
302 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530303
Shreya Shah95d93912018-10-08 14:17:37 +0530304 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
305 if (qty > 0)
306 {
307 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
308 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
309 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530310
Shreya Shah95d93912018-10-08 14:17:37 +0530311 for (var key in cur_frm.doc.items[i])
312 {
313 newrow[key] = cur_frm.doc.items[i][key];
314 }
Nabin Haita39f3242019-01-21 18:50:50 +0530315
Shreya Shah95d93912018-10-08 14:17:37 +0530316 newrow.idx = item_length;
317 newrow["stock_qty"] = newrow.conversion_factor*qty;
318 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530319
Shreya Shah95d93912018-10-08 14:17:37 +0530320 newrow["material_request"] = "";
321 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530322
Shreya Shah95d93912018-10-08 14:17:37 +0530323 }
324 }
325 });
326 i++;
327 }
328 refresh_field("items");
329 //cur_frm.save();
330 }
331 }
332 });
333 },
334
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200335 update_auto_repeat_reference: function(doc) {
336 if (doc.auto_repeat) {
337 frappe.call({
338 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530339 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200340 docname: doc.auto_repeat,
341 reference:doc.name
342 },
343 callback: function(r){
344 if (r.message=="success") {
345 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
346 } else {
347 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
348 }
349 }
350 })
351 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530352 },
353
354 manufacturer: function(doc, cdt, cdn) {
355 const row = locals[cdt][cdn];
356
357 if(row.manufacturer) {
358 frappe.call({
359 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
360 args: {
361 'item_code': row.item_code,
362 'manufacturer': row.manufacturer
363 },
364 callback: function(r) {
365 if (r.message) {
366 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
367 }
368 }
369 });
370 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530371 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530372});
373
374cur_frm.add_fetch('project', 'cost_center', 'cost_center');
375
376erpnext.buying.get_default_bom = function(frm) {
377 $.each(frm.doc["items"] || [], function(i, d) {
378 if (d.item_code && d.bom === "") {
379 return frappe.call({
380 type: "GET",
381 method: "erpnext.stock.get_item_details.get_default_bom",
382 args: {
383 "item_code": d.item_code,
384 },
385 callback: function(r) {
386 if(r) {
387 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
388 }
389 }
390 })
391 }
392 });
393}
394
395erpnext.buying.get_items_from_product_bundle = function(frm) {
396 var dialog = new frappe.ui.Dialog({
397 title: __("Get Items from Product Bundle"),
398 fields: [
399 {
400 "fieldtype": "Link",
401 "label": __("Product Bundle"),
402 "fieldname": "product_bundle",
403 "options":"Product Bundle",
404 "reqd": 1
405 },
406 {
407 "fieldtype": "Currency",
408 "label": __("Quantity"),
409 "fieldname": "quantity",
410 "reqd": 1,
411 "default": 1
412 },
413 {
414 "fieldtype": "Button",
415 "label": __("Get Items"),
416 "fieldname": "get_items",
417 "cssClass": "btn-primary"
418 }
419 ]
420 });
421
422 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530423 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530424 if(!args) return;
425 dialog.hide();
426 return frappe.call({
427 type: "GET",
428 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
429 args: {
430 args: {
431 item_code: args.product_bundle,
432 quantity: args.quantity,
433 parenttype: frm.doc.doctype,
434 parent: frm.doc.name,
435 supplier: frm.doc.supplier,
436 currency: frm.doc.currency,
437 conversion_rate: frm.doc.conversion_rate,
438 price_list: frm.doc.buying_price_list,
439 price_list_currency: frm.doc.price_list_currency,
440 plc_conversion_rate: frm.doc.plc_conversion_rate,
441 company: frm.doc.company,
442 is_subcontracted: frm.doc.is_subcontracted,
443 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
444 ignore_pricing_rule: frm.doc.ignore_pricing_rule
445 }
446 },
447 freeze: true,
448 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100449 const first_row_is_empty = function(child_table){
450 if($.isArray(child_table) && child_table.length > 0) {
451 return !child_table[0].item_code;
452 }
453 return false;
454 };
455
456 const remove_empty_first_row = function(frm){
457 if (first_row_is_empty(frm.doc.items)){
458 frm.doc.items = frm.doc.items.splice(1);
459 }
460 };
461
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530462 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100463 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530464 for ( var i=0; i< r.message.length; i++ ) {
465 var d = frm.add_child("items");
466 var item = r.message[i];
467 for ( var key in item) {
468 if ( !is_null(item[key]) ) {
469 d[key] = item[key];
470 }
471 }
472 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
473 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
474 }
475 }
476 frm.refresh_field("items");
477 }
478 }
479 })
480 });
481 dialog.show();
482}