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