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