blob: 1449e4f5eb3cc5b0f79df6a5ad6bc8435944667a [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")) {
rohitwaghchaureae4ff5a2018-08-01 18:09:51 +053031 this.frm.set_value("disable_rounded_total", cint(frappe.sys_defaults.disable_rounded_total));
32 }
33
Faris Ansariab74ca72017-05-30 12:54:42 +053034 /* eslint-disable */
35 // no idea where me is coming from
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053036 if(this.frm.get_field('shipping_address')) {
Faris Ansariab74ca72017-05-30 12:54:42 +053037 this.frm.set_query("shipping_address", function() {
38 if(me.frm.doc.customer) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053039 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053040 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053041 filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
42 };
43 } else
44 return erpnext.queries.company_address_query(me.frm.doc)
45 });
46 }
Faris Ansariab74ca72017-05-30 12:54:42 +053047 /* eslint-enable */
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053048 },
49
50 setup_queries: function() {
51 var me = this;
52
53 if(this.frm.fields_dict.buying_price_list) {
54 this.frm.set_query("buying_price_list", function() {
55 return{
56 filters: { 'buying': 1 }
57 }
58 });
59 }
60
61 me.frm.set_query('supplier', erpnext.queries.supplier);
62 me.frm.set_query('contact_person', erpnext.queries.contact_query);
63 me.frm.set_query('supplier_address', erpnext.queries.address_query);
64
65 if(this.frm.fields_dict.supplier) {
66 this.frm.set_query("supplier", function() {
67 return{ query: "erpnext.controllers.queries.supplier_query" }});
68 }
69
70 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080071 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053072 return{
73 query: "erpnext.controllers.queries.item_query",
74 filters:{ 'is_sub_contracted_item': 1 }
75 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080076 }
77 else if (me.frm.doc.material_request_type == "Customer Provided") {
78 return{
79 query: "erpnext.controllers.queries.item_query",
80 filters:{ 'customer': me.frm.doc.customer }
81 }
82 }
83 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053084 return{
85 query: "erpnext.controllers.queries.item_query",
86 filters: {'is_purchase_item': 1}
87 }
88 }
89 });
90 },
91
92 refresh: function(doc) {
93 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
94
95 this.frm.toggle_display("supplier_name",
96 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
97
98 if(this.frm.doc.docstatus==0 &&
99 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
100 this.set_from_product_bundle();
101 }
102
103 this._super();
104 },
105
106 supplier: function() {
107 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530108 erpnext.utils.get_party_details(this.frm, null, null, function(){
109 me.apply_price_list();
110 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530111 },
112
113 supplier_address: function() {
114 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500115 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530116 },
117
118 buying_price_list: function() {
119 this.apply_price_list();
120 },
121
122 price_list_rate: function(doc, cdt, cdn) {
123 var item = frappe.get_doc(cdt, cdn);
124 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
125
Manas Solankie5e87f72018-05-28 20:07:08 +0530126 let item_rate = item.price_list_rate;
127 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
128 item_rate = item.blanket_order_rate;
129 }
Nabin Haita804d6d2018-08-29 13:34:58 +0530130 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
Shreya Shah0631aed2018-08-14 10:51:48 +0530131 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530132
133 this.calculate_taxes_and_totals();
134 },
135
136 discount_percentage: function(doc, cdt, cdn) {
137 this.price_list_rate(doc, cdt, cdn);
138 },
139
140 qty: function(doc, cdt, cdn) {
141 var item = frappe.get_doc(cdt, cdn);
142 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
143 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
144
145 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
146
147 if(!item.rejected_qty && item.qty) {
148 item.received_qty = item.qty;
149 }
150
151 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
152 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
153 }
154
155 this._super(doc, cdt, cdn);
156 },
157
158 received_qty: function(doc, cdt, cdn) {
159 this.calculate_accepted_qty(doc, cdt, cdn)
160 },
161
162 rejected_qty: function(doc, cdt, cdn) {
163 this.calculate_accepted_qty(doc, cdt, cdn)
164 },
165
166 calculate_accepted_qty: function(doc, cdt, cdn){
167 var item = frappe.get_doc(cdt, cdn);
168 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
169
170 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
171
172 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
173 this.qty(doc, cdt, cdn);
174 },
175
176 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
177 if(!item || !fieldnames) { return }
178
179 var is_negative_qty = false;
180 for(var i = 0; i<fieldnames.length; i++) {
181 if(item[fieldnames[i]] < 0){
182 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
183 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
184 is_negative_qty = true;
185 break;
186 }
187 }
188
189 return is_negative_qty
190 },
191
192 warehouse: function(doc, cdt, cdn) {
193 var item = frappe.get_doc(cdt, cdn);
194 if(item.item_code && item.warehouse) {
195 return this.frm.call({
196 method: "erpnext.stock.get_item_details.get_bin_details",
197 child: item,
198 args: {
199 item_code: item.item_code,
200 warehouse: item.warehouse
201 }
202 });
203 }
204 },
205
206 project: function(doc, cdt, cdn) {
207 var item = frappe.get_doc(cdt, cdn);
208 if(item.project) {
209 $.each(this.frm.doc["items"] || [],
210 function(i, other_item) {
211 if(!other_item.project) {
212 other_item.project = item.project;
213 refresh_field("project", other_item.name, other_item.parentfield);
214 }
215 });
216 }
217 },
218
219 category: function(doc, cdt, cdn) {
220 // should be the category field of tax table
221 if(cdt != doc.doctype) {
222 this.calculate_taxes_and_totals();
223 }
224 },
225 add_deduct_tax: function(doc, cdt, cdn) {
226 this.calculate_taxes_and_totals();
227 },
228
229 set_from_product_bundle: function() {
230 var me = this;
231 this.frm.add_custom_button(__("Product Bundle"), function() {
232 erpnext.buying.get_items_from_product_bundle(me.frm);
233 }, __("Get items from"));
234 },
235
236 shipping_address: function(){
237 var me = this;
238 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530239 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530240 },
241
242 tc_name: function() {
243 this.get_terms();
244 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200245
Shreya Shah95d93912018-10-08 14:17:37 +0530246 link_to_mrs: function() {
247 var my_items = [];
248 for (var i in cur_frm.doc.items) {
249 if(!cur_frm.doc.items[i].material_request){
250 my_items.push(cur_frm.doc.items[i].item_code);
251 }
252 }
253 frappe.call({
254 method: "erpnext.buying.utils.get_linked_material_requests",
255 args:{
256 items: my_items
257 },
258 callback: function(r) {
259 if(!r.message) {
260 frappe.throw(__("No pending Material Requests found to link for the given items."))
261 }
262 else {
263 var i = 0;
264 var item_length = cur_frm.doc.items.length;
265 while (i < item_length) {
266 var qty = cur_frm.doc.items[i].qty;
267 (r.message[0] || []).forEach(function(d) {
268 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
269 {
270 cur_frm.doc.items[i].material_request = d.mr_name;
271 cur_frm.doc.items[i].material_request_item = d.mr_item;
272 var my_qty = Math.min(qty, d.qty);
273 qty = qty - my_qty;
274 d.qty = d.qty - my_qty;
275 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
276 cur_frm.doc.items[i].qty = my_qty;
277
278 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
279 if (qty > 0)
280 {
281 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
282 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
283 item_length++;
284
285 for (var key in cur_frm.doc.items[i])
286 {
287 newrow[key] = cur_frm.doc.items[i][key];
288 }
289
290 newrow.idx = item_length;
291 newrow["stock_qty"] = newrow.conversion_factor*qty;
292 newrow["qty"] = qty;
293
294 newrow["material_request"] = "";
295 newrow["material_request_item"] = "";
296
297 }
298 }
299 });
300 i++;
301 }
302 refresh_field("items");
303 //cur_frm.save();
304 }
305 }
306 });
307 },
308
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200309 update_auto_repeat_reference: function(doc) {
310 if (doc.auto_repeat) {
311 frappe.call({
312 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
313 args:{
314 docname: doc.auto_repeat,
315 reference:doc.name
316 },
317 callback: function(r){
318 if (r.message=="success") {
319 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
320 } else {
321 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
322 }
323 }
324 })
325 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530326 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530327});
328
329cur_frm.add_fetch('project', 'cost_center', 'cost_center');
330
331erpnext.buying.get_default_bom = function(frm) {
332 $.each(frm.doc["items"] || [], function(i, d) {
333 if (d.item_code && d.bom === "") {
334 return frappe.call({
335 type: "GET",
336 method: "erpnext.stock.get_item_details.get_default_bom",
337 args: {
338 "item_code": d.item_code,
339 },
340 callback: function(r) {
341 if(r) {
342 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
343 }
344 }
345 })
346 }
347 });
348}
349
350erpnext.buying.get_items_from_product_bundle = function(frm) {
351 var dialog = new frappe.ui.Dialog({
352 title: __("Get Items from Product Bundle"),
353 fields: [
354 {
355 "fieldtype": "Link",
356 "label": __("Product Bundle"),
357 "fieldname": "product_bundle",
358 "options":"Product Bundle",
359 "reqd": 1
360 },
361 {
362 "fieldtype": "Currency",
363 "label": __("Quantity"),
364 "fieldname": "quantity",
365 "reqd": 1,
366 "default": 1
367 },
368 {
369 "fieldtype": "Button",
370 "label": __("Get Items"),
371 "fieldname": "get_items",
372 "cssClass": "btn-primary"
373 }
374 ]
375 });
376
377 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530378 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530379 if(!args) return;
380 dialog.hide();
381 return frappe.call({
382 type: "GET",
383 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
384 args: {
385 args: {
386 item_code: args.product_bundle,
387 quantity: args.quantity,
388 parenttype: frm.doc.doctype,
389 parent: frm.doc.name,
390 supplier: frm.doc.supplier,
391 currency: frm.doc.currency,
392 conversion_rate: frm.doc.conversion_rate,
393 price_list: frm.doc.buying_price_list,
394 price_list_currency: frm.doc.price_list_currency,
395 plc_conversion_rate: frm.doc.plc_conversion_rate,
396 company: frm.doc.company,
397 is_subcontracted: frm.doc.is_subcontracted,
398 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
399 ignore_pricing_rule: frm.doc.ignore_pricing_rule
400 }
401 },
402 freeze: true,
403 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100404 const first_row_is_empty = function(child_table){
405 if($.isArray(child_table) && child_table.length > 0) {
406 return !child_table[0].item_code;
407 }
408 return false;
409 };
410
411 const remove_empty_first_row = function(frm){
412 if (first_row_is_empty(frm.doc.items)){
413 frm.doc.items = frm.doc.items.splice(1);
414 }
415 };
416
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530417 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100418 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530419 for ( var i=0; i< r.message.length; i++ ) {
420 var d = frm.add_child("items");
421 var item = r.message[i];
422 for ( var key in item) {
423 if ( !is_null(item[key]) ) {
424 d[key] = item[key];
425 }
426 }
427 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
428 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
429 }
430 }
431 frm.refresh_field("items");
432 }
433 }
434 })
435 });
436 dialog.show();
437}
tundebabzyf35710d2017-08-16 08:55:18 +0100438