blob: cb9c23bb628f83da58b70fd58b43c37277e06162 [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() {
74 if(me.frm.doc.is_subcontracted == "Yes") {
75 return{
76 query: "erpnext.controllers.queries.item_query",
77 filters:{ 'is_sub_contracted_item': 1 }
78 }
79 } else {
80 return{
81 query: "erpnext.controllers.queries.item_query",
82 filters: {'is_purchase_item': 1}
83 }
84 }
85 });
86 },
87
88 refresh: function(doc) {
89 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
90
91 this.frm.toggle_display("supplier_name",
92 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
93
94 if(this.frm.doc.docstatus==0 &&
95 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
96 this.set_from_product_bundle();
97 }
98
99 this._super();
100 },
101
102 supplier: function() {
103 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530104 erpnext.utils.get_party_details(this.frm, null, null, function(){
105 me.apply_price_list();
106 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530107 },
108
109 supplier_address: function() {
110 erpnext.utils.get_address_display(this.frm);
111 },
112
113 buying_price_list: function() {
114 this.apply_price_list();
115 },
116
117 price_list_rate: function(doc, cdt, cdn) {
118 var item = frappe.get_doc(cdt, cdn);
119 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
120
Manas Solankie5e87f72018-05-28 20:07:08 +0530121 let item_rate = item.price_list_rate;
122 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
123 item_rate = item.blanket_order_rate;
124 }
Nabin Haita39f3242019-01-21 18:50:50 +0530125 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
Shreya Shah0631aed2018-08-14 10:51:48 +0530126 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530127
128 this.calculate_taxes_and_totals();
129 },
130
131 discount_percentage: function(doc, cdt, cdn) {
132 this.price_list_rate(doc, cdt, cdn);
133 },
134
135 qty: function(doc, cdt, cdn) {
136 var item = frappe.get_doc(cdt, cdn);
137 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
138 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
139
140 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
141
142 if(!item.rejected_qty && item.qty) {
143 item.received_qty = item.qty;
144 }
145
146 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
147 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
148 }
149
150 this._super(doc, cdt, cdn);
151 },
152
153 received_qty: function(doc, cdt, cdn) {
154 this.calculate_accepted_qty(doc, cdt, cdn)
155 },
156
157 rejected_qty: function(doc, cdt, cdn) {
158 this.calculate_accepted_qty(doc, cdt, cdn)
159 },
160
161 calculate_accepted_qty: function(doc, cdt, cdn){
162 var item = frappe.get_doc(cdt, cdn);
163 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
164
165 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
166
167 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
168 this.qty(doc, cdt, cdn);
169 },
170
171 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
172 if(!item || !fieldnames) { return }
173
174 var is_negative_qty = false;
175 for(var i = 0; i<fieldnames.length; i++) {
176 if(item[fieldnames[i]] < 0){
177 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
178 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
179 is_negative_qty = true;
180 break;
181 }
182 }
183
184 return is_negative_qty
185 },
186
187 warehouse: function(doc, cdt, cdn) {
188 var item = frappe.get_doc(cdt, cdn);
189 if(item.item_code && item.warehouse) {
190 return this.frm.call({
191 method: "erpnext.stock.get_item_details.get_bin_details",
192 child: item,
193 args: {
194 item_code: item.item_code,
195 warehouse: item.warehouse
196 }
197 });
198 }
199 },
200
201 project: function(doc, cdt, cdn) {
202 var item = frappe.get_doc(cdt, cdn);
203 if(item.project) {
204 $.each(this.frm.doc["items"] || [],
205 function(i, other_item) {
206 if(!other_item.project) {
207 other_item.project = item.project;
208 refresh_field("project", other_item.name, other_item.parentfield);
209 }
210 });
211 }
212 },
213
214 category: function(doc, cdt, cdn) {
215 // should be the category field of tax table
216 if(cdt != doc.doctype) {
217 this.calculate_taxes_and_totals();
218 }
219 },
220 add_deduct_tax: function(doc, cdt, cdn) {
221 this.calculate_taxes_and_totals();
222 },
223
224 set_from_product_bundle: function() {
225 var me = this;
226 this.frm.add_custom_button(__("Product Bundle"), function() {
227 erpnext.buying.get_items_from_product_bundle(me.frm);
228 }, __("Get items from"));
229 },
230
231 shipping_address: function(){
232 var me = this;
233 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530234 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530235 },
236
237 tc_name: function() {
238 this.get_terms();
239 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200240
Shreya Shah95d93912018-10-08 14:17:37 +0530241 link_to_mrs: function() {
242 var my_items = [];
243 for (var i in cur_frm.doc.items) {
244 if(!cur_frm.doc.items[i].material_request){
245 my_items.push(cur_frm.doc.items[i].item_code);
246 }
247 }
248 frappe.call({
249 method: "erpnext.buying.utils.get_linked_material_requests",
250 args:{
251 items: my_items
252 },
253 callback: function(r) {
254 if(!r.message) {
255 frappe.throw(__("No pending Material Requests found to link for the given items."))
256 }
257 else {
258 var i = 0;
259 var item_length = cur_frm.doc.items.length;
260 while (i < item_length) {
261 var qty = cur_frm.doc.items[i].qty;
262 (r.message[0] || []).forEach(function(d) {
263 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
264 {
265 cur_frm.doc.items[i].material_request = d.mr_name;
266 cur_frm.doc.items[i].material_request_item = d.mr_item;
267 var my_qty = Math.min(qty, d.qty);
268 qty = qty - my_qty;
269 d.qty = d.qty - my_qty;
270 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
271 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530272
Shreya Shah95d93912018-10-08 14:17:37 +0530273 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
274 if (qty > 0)
275 {
276 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
277 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
278 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530279
Shreya Shah95d93912018-10-08 14:17:37 +0530280 for (var key in cur_frm.doc.items[i])
281 {
282 newrow[key] = cur_frm.doc.items[i][key];
283 }
Nabin Haita39f3242019-01-21 18:50:50 +0530284
Shreya Shah95d93912018-10-08 14:17:37 +0530285 newrow.idx = item_length;
286 newrow["stock_qty"] = newrow.conversion_factor*qty;
287 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530288
Shreya Shah95d93912018-10-08 14:17:37 +0530289 newrow["material_request"] = "";
290 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530291
Shreya Shah95d93912018-10-08 14:17:37 +0530292 }
293 }
294 });
295 i++;
296 }
297 refresh_field("items");
298 //cur_frm.save();
299 }
300 }
301 });
302 },
303
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200304 update_auto_repeat_reference: function(doc) {
305 if (doc.auto_repeat) {
306 frappe.call({
307 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530308 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200309 docname: doc.auto_repeat,
310 reference:doc.name
311 },
312 callback: function(r){
313 if (r.message=="success") {
314 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
315 } else {
316 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
317 }
318 }
319 })
320 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530321 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530322});
323
324cur_frm.add_fetch('project', 'cost_center', 'cost_center');
325
326erpnext.buying.get_default_bom = function(frm) {
327 $.each(frm.doc["items"] || [], function(i, d) {
328 if (d.item_code && d.bom === "") {
329 return frappe.call({
330 type: "GET",
331 method: "erpnext.stock.get_item_details.get_default_bom",
332 args: {
333 "item_code": d.item_code,
334 },
335 callback: function(r) {
336 if(r) {
337 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
338 }
339 }
340 })
341 }
342 });
343}
344
345erpnext.buying.get_items_from_product_bundle = function(frm) {
346 var dialog = new frappe.ui.Dialog({
347 title: __("Get Items from Product Bundle"),
348 fields: [
349 {
350 "fieldtype": "Link",
351 "label": __("Product Bundle"),
352 "fieldname": "product_bundle",
353 "options":"Product Bundle",
354 "reqd": 1
355 },
356 {
357 "fieldtype": "Currency",
358 "label": __("Quantity"),
359 "fieldname": "quantity",
360 "reqd": 1,
361 "default": 1
362 },
363 {
364 "fieldtype": "Button",
365 "label": __("Get Items"),
366 "fieldname": "get_items",
367 "cssClass": "btn-primary"
368 }
369 ]
370 });
371
372 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530373 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530374 if(!args) return;
375 dialog.hide();
376 return frappe.call({
377 type: "GET",
378 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
379 args: {
380 args: {
381 item_code: args.product_bundle,
382 quantity: args.quantity,
383 parenttype: frm.doc.doctype,
384 parent: frm.doc.name,
385 supplier: frm.doc.supplier,
386 currency: frm.doc.currency,
387 conversion_rate: frm.doc.conversion_rate,
388 price_list: frm.doc.buying_price_list,
389 price_list_currency: frm.doc.price_list_currency,
390 plc_conversion_rate: frm.doc.plc_conversion_rate,
391 company: frm.doc.company,
392 is_subcontracted: frm.doc.is_subcontracted,
393 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
394 ignore_pricing_rule: frm.doc.ignore_pricing_rule
395 }
396 },
397 freeze: true,
398 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100399 const first_row_is_empty = function(child_table){
400 if($.isArray(child_table) && child_table.length > 0) {
401 return !child_table[0].item_code;
402 }
403 return false;
404 };
405
406 const remove_empty_first_row = function(frm){
407 if (first_row_is_empty(frm.doc.items)){
408 frm.doc.items = frm.doc.items.splice(1);
409 }
410 };
411
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530412 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100413 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530414 for ( var i=0; i< r.message.length; i++ ) {
415 var d = frm.add_child("items");
416 var item = r.message[i];
417 for ( var key in item) {
418 if ( !is_null(item[key]) ) {
419 d[key] = item[key];
420 }
421 }
422 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
423 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
424 }
425 }
426 frm.refresh_field("items");
427 }
428 }
429 })
430 });
431 dialog.show();
432}