blob: f2fe3fe72b4e499a7eea4fd524a7e43dac2e288a [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) {
Nabin Hait593242f2019-04-05 19:35:02 +0530150 var item = frappe.get_doc(cdt, cdn);
151 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530152 this.price_list_rate(doc, cdt, cdn);
153 },
154
155 qty: function(doc, cdt, cdn) {
156 var item = frappe.get_doc(cdt, cdn);
157 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
158 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
159
160 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
161
162 if(!item.rejected_qty && item.qty) {
163 item.received_qty = item.qty;
164 }
165
166 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
167 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
168 }
169
170 this._super(doc, cdt, cdn);
171 },
172
173 received_qty: function(doc, cdt, cdn) {
174 this.calculate_accepted_qty(doc, cdt, cdn)
175 },
176
177 rejected_qty: function(doc, cdt, cdn) {
178 this.calculate_accepted_qty(doc, cdt, cdn)
179 },
180
181 calculate_accepted_qty: function(doc, cdt, cdn){
182 var item = frappe.get_doc(cdt, cdn);
183 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
184
185 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
186
187 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
188 this.qty(doc, cdt, cdn);
189 },
190
191 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
192 if(!item || !fieldnames) { return }
193
194 var is_negative_qty = false;
195 for(var i = 0; i<fieldnames.length; i++) {
196 if(item[fieldnames[i]] < 0){
197 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
198 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
199 is_negative_qty = true;
200 break;
201 }
202 }
203
204 return is_negative_qty
205 },
206
207 warehouse: function(doc, cdt, cdn) {
208 var item = frappe.get_doc(cdt, cdn);
209 if(item.item_code && item.warehouse) {
210 return this.frm.call({
211 method: "erpnext.stock.get_item_details.get_bin_details",
212 child: item,
213 args: {
214 item_code: item.item_code,
215 warehouse: item.warehouse
216 }
217 });
218 }
219 },
220
221 project: function(doc, cdt, cdn) {
222 var item = frappe.get_doc(cdt, cdn);
223 if(item.project) {
224 $.each(this.frm.doc["items"] || [],
225 function(i, other_item) {
226 if(!other_item.project) {
227 other_item.project = item.project;
228 refresh_field("project", other_item.name, other_item.parentfield);
229 }
230 });
231 }
232 },
233
234 category: function(doc, cdt, cdn) {
235 // should be the category field of tax table
236 if(cdt != doc.doctype) {
237 this.calculate_taxes_and_totals();
238 }
239 },
240 add_deduct_tax: function(doc, cdt, cdn) {
241 this.calculate_taxes_and_totals();
242 },
243
244 set_from_product_bundle: function() {
245 var me = this;
246 this.frm.add_custom_button(__("Product Bundle"), function() {
247 erpnext.buying.get_items_from_product_bundle(me.frm);
248 }, __("Get items from"));
249 },
250
251 shipping_address: function(){
252 var me = this;
253 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530254 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530255 },
256
257 tc_name: function() {
258 this.get_terms();
259 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200260
Shreya Shah95d93912018-10-08 14:17:37 +0530261 link_to_mrs: function() {
262 var my_items = [];
263 for (var i in cur_frm.doc.items) {
264 if(!cur_frm.doc.items[i].material_request){
265 my_items.push(cur_frm.doc.items[i].item_code);
266 }
267 }
268 frappe.call({
269 method: "erpnext.buying.utils.get_linked_material_requests",
270 args:{
271 items: my_items
272 },
273 callback: function(r) {
274 if(!r.message) {
275 frappe.throw(__("No pending Material Requests found to link for the given items."))
276 }
277 else {
278 var i = 0;
279 var item_length = cur_frm.doc.items.length;
280 while (i < item_length) {
281 var qty = cur_frm.doc.items[i].qty;
282 (r.message[0] || []).forEach(function(d) {
283 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
284 {
285 cur_frm.doc.items[i].material_request = d.mr_name;
286 cur_frm.doc.items[i].material_request_item = d.mr_item;
287 var my_qty = Math.min(qty, d.qty);
288 qty = qty - my_qty;
289 d.qty = d.qty - my_qty;
290 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
291 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530292
Shreya Shah95d93912018-10-08 14:17:37 +0530293 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
294 if (qty > 0)
295 {
296 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
297 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
298 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530299
Shreya Shah95d93912018-10-08 14:17:37 +0530300 for (var key in cur_frm.doc.items[i])
301 {
302 newrow[key] = cur_frm.doc.items[i][key];
303 }
Nabin Haita39f3242019-01-21 18:50:50 +0530304
Shreya Shah95d93912018-10-08 14:17:37 +0530305 newrow.idx = item_length;
306 newrow["stock_qty"] = newrow.conversion_factor*qty;
307 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530308
Shreya Shah95d93912018-10-08 14:17:37 +0530309 newrow["material_request"] = "";
310 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530311
Shreya Shah95d93912018-10-08 14:17:37 +0530312 }
313 }
314 });
315 i++;
316 }
317 refresh_field("items");
318 //cur_frm.save();
319 }
320 }
321 });
322 },
323
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200324 update_auto_repeat_reference: function(doc) {
325 if (doc.auto_repeat) {
326 frappe.call({
327 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530328 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200329 docname: doc.auto_repeat,
330 reference:doc.name
331 },
332 callback: function(r){
333 if (r.message=="success") {
334 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
335 } else {
336 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
337 }
338 }
339 })
340 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530341 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530342});
343
344cur_frm.add_fetch('project', 'cost_center', 'cost_center');
345
346erpnext.buying.get_default_bom = function(frm) {
347 $.each(frm.doc["items"] || [], function(i, d) {
348 if (d.item_code && d.bom === "") {
349 return frappe.call({
350 type: "GET",
351 method: "erpnext.stock.get_item_details.get_default_bom",
352 args: {
353 "item_code": d.item_code,
354 },
355 callback: function(r) {
356 if(r) {
357 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
358 }
359 }
360 })
361 }
362 });
363}
364
365erpnext.buying.get_items_from_product_bundle = function(frm) {
366 var dialog = new frappe.ui.Dialog({
367 title: __("Get Items from Product Bundle"),
368 fields: [
369 {
370 "fieldtype": "Link",
371 "label": __("Product Bundle"),
372 "fieldname": "product_bundle",
373 "options":"Product Bundle",
374 "reqd": 1
375 },
376 {
377 "fieldtype": "Currency",
378 "label": __("Quantity"),
379 "fieldname": "quantity",
380 "reqd": 1,
381 "default": 1
382 },
383 {
384 "fieldtype": "Button",
385 "label": __("Get Items"),
386 "fieldname": "get_items",
387 "cssClass": "btn-primary"
388 }
389 ]
390 });
391
392 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530393 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530394 if(!args) return;
395 dialog.hide();
396 return frappe.call({
397 type: "GET",
398 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
399 args: {
400 args: {
401 item_code: args.product_bundle,
402 quantity: args.quantity,
403 parenttype: frm.doc.doctype,
404 parent: frm.doc.name,
405 supplier: frm.doc.supplier,
406 currency: frm.doc.currency,
407 conversion_rate: frm.doc.conversion_rate,
408 price_list: frm.doc.buying_price_list,
409 price_list_currency: frm.doc.price_list_currency,
410 plc_conversion_rate: frm.doc.plc_conversion_rate,
411 company: frm.doc.company,
412 is_subcontracted: frm.doc.is_subcontracted,
413 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
414 ignore_pricing_rule: frm.doc.ignore_pricing_rule
415 }
416 },
417 freeze: true,
418 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100419 const first_row_is_empty = function(child_table){
420 if($.isArray(child_table) && child_table.length > 0) {
421 return !child_table[0].item_code;
422 }
423 return false;
424 };
425
426 const remove_empty_first_row = function(frm){
427 if (first_row_is_empty(frm.doc.items)){
428 frm.doc.items = frm.doc.items.splice(1);
429 }
430 };
431
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530432 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100433 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530434 for ( var i=0; i< r.message.length; i++ ) {
435 var d = frm.add_child("items");
436 var item = r.message[i];
437 for ( var key in item) {
438 if ( !is_null(item[key]) ) {
439 d[key] = item[key];
440 }
441 }
442 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
443 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
444 }
445 }
446 frm.refresh_field("items");
447 }
448 }
449 })
450 });
451 dialog.show();
452}