blob: 3ec27fcc7f9ece16343f4e1b1df332f025ddfbaf [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
17 onload: function() {
18 this.setup_queries();
19 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
53 setup_queries: function() {
54 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 });
93 },
94
95 refresh: function(doc) {
96 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
97
98 this.frm.toggle_display("supplier_name",
99 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
100
101 if(this.frm.doc.docstatus==0 &&
102 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
103 this.set_from_product_bundle();
104 }
105
106 this._super();
107 },
108
109 supplier: function() {
110 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530111 erpnext.utils.get_party_details(this.frm, null, null, function(){
112 me.apply_price_list();
113 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530114 },
115
116 supplier_address: function() {
117 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500118 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530119 },
120
121 buying_price_list: function() {
122 this.apply_price_list();
123 },
124
125 price_list_rate: function(doc, cdt, cdn) {
126 var item = frappe.get_doc(cdt, cdn);
127 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
128
Manas Solankie5e87f72018-05-28 20:07:08 +0530129 let item_rate = item.price_list_rate;
130 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
131 item_rate = item.blanket_order_rate;
132 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530133
134 if (item.discount_percentage) {
135 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
136 }
137
Shreya Shah0631aed2018-08-14 10:51:48 +0530138 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530139
140 this.calculate_taxes_and_totals();
141 },
142
143 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530144 var item = frappe.get_doc(cdt, cdn);
145 item.discount_amount = 0.0;
146 this.price_list_rate(doc, cdt, cdn);
147 },
148
149 discount_amount: function(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530150 this.price_list_rate(doc, cdt, cdn);
151 },
152
153 qty: function(doc, cdt, cdn) {
154 var item = frappe.get_doc(cdt, cdn);
155 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
156 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
157
158 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
159
160 if(!item.rejected_qty && item.qty) {
161 item.received_qty = item.qty;
162 }
163
164 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
165 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
166 }
167
168 this._super(doc, cdt, cdn);
169 },
170
171 received_qty: function(doc, cdt, cdn) {
172 this.calculate_accepted_qty(doc, cdt, cdn)
173 },
174
175 rejected_qty: function(doc, cdt, cdn) {
176 this.calculate_accepted_qty(doc, cdt, cdn)
177 },
178
179 calculate_accepted_qty: function(doc, cdt, cdn){
180 var item = frappe.get_doc(cdt, cdn);
181 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
182
183 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
184
185 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
186 this.qty(doc, cdt, cdn);
187 },
188
189 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
190 if(!item || !fieldnames) { return }
191
192 var is_negative_qty = false;
193 for(var i = 0; i<fieldnames.length; i++) {
194 if(item[fieldnames[i]] < 0){
195 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
196 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
197 is_negative_qty = true;
198 break;
199 }
200 }
201
202 return is_negative_qty
203 },
204
205 warehouse: function(doc, cdt, cdn) {
206 var item = frappe.get_doc(cdt, cdn);
207 if(item.item_code && item.warehouse) {
208 return this.frm.call({
209 method: "erpnext.stock.get_item_details.get_bin_details",
210 child: item,
211 args: {
212 item_code: item.item_code,
213 warehouse: item.warehouse
214 }
215 });
216 }
217 },
218
219 project: function(doc, cdt, cdn) {
220 var item = frappe.get_doc(cdt, cdn);
221 if(item.project) {
222 $.each(this.frm.doc["items"] || [],
223 function(i, other_item) {
224 if(!other_item.project) {
225 other_item.project = item.project;
226 refresh_field("project", other_item.name, other_item.parentfield);
227 }
228 });
229 }
230 },
231
232 category: function(doc, cdt, cdn) {
233 // should be the category field of tax table
234 if(cdt != doc.doctype) {
235 this.calculate_taxes_and_totals();
236 }
237 },
238 add_deduct_tax: function(doc, cdt, cdn) {
239 this.calculate_taxes_and_totals();
240 },
241
242 set_from_product_bundle: function() {
243 var me = this;
244 this.frm.add_custom_button(__("Product Bundle"), function() {
245 erpnext.buying.get_items_from_product_bundle(me.frm);
246 }, __("Get items from"));
247 },
248
249 shipping_address: function(){
250 var me = this;
251 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530252 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530253 },
254
255 tc_name: function() {
256 this.get_terms();
257 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200258
Shreya Shah95d93912018-10-08 14:17:37 +0530259 link_to_mrs: function() {
260 var my_items = [];
261 for (var i in cur_frm.doc.items) {
262 if(!cur_frm.doc.items[i].material_request){
263 my_items.push(cur_frm.doc.items[i].item_code);
264 }
265 }
266 frappe.call({
267 method: "erpnext.buying.utils.get_linked_material_requests",
268 args:{
269 items: my_items
270 },
271 callback: function(r) {
272 if(!r.message) {
273 frappe.throw(__("No pending Material Requests found to link for the given items."))
274 }
275 else {
276 var i = 0;
277 var item_length = cur_frm.doc.items.length;
278 while (i < item_length) {
279 var qty = cur_frm.doc.items[i].qty;
280 (r.message[0] || []).forEach(function(d) {
281 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
282 {
283 cur_frm.doc.items[i].material_request = d.mr_name;
284 cur_frm.doc.items[i].material_request_item = d.mr_item;
285 var my_qty = Math.min(qty, d.qty);
286 qty = qty - my_qty;
287 d.qty = d.qty - my_qty;
288 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
289 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530290
Shreya Shah95d93912018-10-08 14:17:37 +0530291 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
292 if (qty > 0)
293 {
294 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
295 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
296 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530297
Shreya Shah95d93912018-10-08 14:17:37 +0530298 for (var key in cur_frm.doc.items[i])
299 {
300 newrow[key] = cur_frm.doc.items[i][key];
301 }
Nabin Haita39f3242019-01-21 18:50:50 +0530302
Shreya Shah95d93912018-10-08 14:17:37 +0530303 newrow.idx = item_length;
304 newrow["stock_qty"] = newrow.conversion_factor*qty;
305 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530306
Shreya Shah95d93912018-10-08 14:17:37 +0530307 newrow["material_request"] = "";
308 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530309
Shreya Shah95d93912018-10-08 14:17:37 +0530310 }
311 }
312 });
313 i++;
314 }
315 refresh_field("items");
316 //cur_frm.save();
317 }
318 }
319 });
320 },
321
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200322 update_auto_repeat_reference: function(doc) {
323 if (doc.auto_repeat) {
324 frappe.call({
325 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530326 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200327 docname: doc.auto_repeat,
328 reference:doc.name
329 },
330 callback: function(r){
331 if (r.message=="success") {
332 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
333 } else {
334 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
335 }
336 }
337 })
338 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530339 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530340});
341
342cur_frm.add_fetch('project', 'cost_center', 'cost_center');
343
344erpnext.buying.get_default_bom = function(frm) {
345 $.each(frm.doc["items"] || [], function(i, d) {
346 if (d.item_code && d.bom === "") {
347 return frappe.call({
348 type: "GET",
349 method: "erpnext.stock.get_item_details.get_default_bom",
350 args: {
351 "item_code": d.item_code,
352 },
353 callback: function(r) {
354 if(r) {
355 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
356 }
357 }
358 })
359 }
360 });
361}
362
363erpnext.buying.get_items_from_product_bundle = function(frm) {
364 var dialog = new frappe.ui.Dialog({
365 title: __("Get Items from Product Bundle"),
366 fields: [
367 {
368 "fieldtype": "Link",
369 "label": __("Product Bundle"),
370 "fieldname": "product_bundle",
371 "options":"Product Bundle",
372 "reqd": 1
373 },
374 {
375 "fieldtype": "Currency",
376 "label": __("Quantity"),
377 "fieldname": "quantity",
378 "reqd": 1,
379 "default": 1
380 },
381 {
382 "fieldtype": "Button",
383 "label": __("Get Items"),
384 "fieldname": "get_items",
385 "cssClass": "btn-primary"
386 }
387 ]
388 });
389
390 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530391 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530392 if(!args) return;
393 dialog.hide();
394 return frappe.call({
395 type: "GET",
396 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
397 args: {
398 args: {
399 item_code: args.product_bundle,
400 quantity: args.quantity,
401 parenttype: frm.doc.doctype,
402 parent: frm.doc.name,
403 supplier: frm.doc.supplier,
404 currency: frm.doc.currency,
405 conversion_rate: frm.doc.conversion_rate,
406 price_list: frm.doc.buying_price_list,
407 price_list_currency: frm.doc.price_list_currency,
408 plc_conversion_rate: frm.doc.plc_conversion_rate,
409 company: frm.doc.company,
410 is_subcontracted: frm.doc.is_subcontracted,
411 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
412 ignore_pricing_rule: frm.doc.ignore_pricing_rule
413 }
414 },
415 freeze: true,
416 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100417 const first_row_is_empty = function(child_table){
418 if($.isArray(child_table) && child_table.length > 0) {
419 return !child_table[0].item_code;
420 }
421 return false;
422 };
423
424 const remove_empty_first_row = function(frm){
425 if (first_row_is_empty(frm.doc.items)){
426 frm.doc.items = frm.doc.items.splice(1);
427 }
428 };
429
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530430 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100431 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530432 for ( var i=0; i< r.message.length; i++ ) {
433 var d = frm.add_child("items");
434 var item = r.message[i];
435 for ( var key in item) {
436 if ( !is_null(item[key]) ) {
437 d[key] = item[key];
438 }
439 }
440 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
441 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
442 }
443 }
444 frm.refresh_field("items");
445 }
446 }
447 })
448 });
449 dialog.show();
450}