blob: 824b8d98d2bbd13ae885f5f2e579896a40bb94db [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
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053017 onload: function(doc, cdt, cdn) {
18 this.setup_queries(doc, cdt, cdn);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053019 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
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053053 setup_queries: function(doc, cdt, cdn) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053054 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
karthikeyan57fc60212019-07-04 22:46:16 +053064 if(this.frm.fields_dict.tc_name) {
65 this.frm.set_query("tc_name", function() {
66 return{
67 filters: { 'buying': 1 }
68 }
69 });
70 }
71
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053072 me.frm.set_query('supplier', erpnext.queries.supplier);
73 me.frm.set_query('contact_person', erpnext.queries.contact_query);
74 me.frm.set_query('supplier_address', erpnext.queries.address_query);
75
76 if(this.frm.fields_dict.supplier) {
77 this.frm.set_query("supplier", function() {
78 return{ query: "erpnext.controllers.queries.supplier_query" }});
79 }
80
81 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080082 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053083 return{
84 query: "erpnext.controllers.queries.item_query",
85 filters:{ 'is_sub_contracted_item': 1 }
86 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080087 }
88 else if (me.frm.doc.material_request_type == "Customer Provided") {
89 return{
90 query: "erpnext.controllers.queries.item_query",
91 filters:{ 'customer': me.frm.doc.customer }
92 }
93 }
94 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053095 return{
96 query: "erpnext.controllers.queries.item_query",
97 filters: {'is_purchase_item': 1}
98 }
99 }
100 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530101
102
103 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
104 const row = locals[cdt][cdn];
105 return {
106 query: "erpnext.controllers.queries.item_manufacturer_query",
107 filters:{ 'item_code': row.item_code }
108 }
109 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530110 },
111
112 refresh: function(doc) {
113 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
114
115 this.frm.toggle_display("supplier_name",
116 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
117
118 if(this.frm.doc.docstatus==0 &&
119 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
120 this.set_from_product_bundle();
121 }
122
123 this._super();
124 },
125
126 supplier: function() {
127 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530128 erpnext.utils.get_party_details(this.frm, null, null, function(){
129 me.apply_price_list();
130 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530131 },
132
133 supplier_address: function() {
134 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500135 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530136 },
137
138 buying_price_list: function() {
139 this.apply_price_list();
140 },
141
142 price_list_rate: function(doc, cdt, cdn) {
143 var item = frappe.get_doc(cdt, cdn);
144 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
145
Manas Solankie5e87f72018-05-28 20:07:08 +0530146 let item_rate = item.price_list_rate;
147 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
148 item_rate = item.blanket_order_rate;
149 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530150
151 if (item.discount_percentage) {
152 item.discount_amount = flt(item_rate) * flt(item.discount_percentage) / 100;
153 }
154
Aditya Hase48585c92019-06-18 19:11:17 +0530155 if (item.discount_amount) {
156 item.rate = flt((item.price_list_rate) - (item.discount_amount), precision('rate', item));
157 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530158
159 this.calculate_taxes_and_totals();
160 },
161
162 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530163 var item = frappe.get_doc(cdt, cdn);
164 item.discount_amount = 0.0;
165 this.price_list_rate(doc, cdt, cdn);
166 },
167
168 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530169 var item = frappe.get_doc(cdt, cdn);
170 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530171 this.price_list_rate(doc, cdt, cdn);
172 },
173
174 qty: function(doc, cdt, cdn) {
175 var item = frappe.get_doc(cdt, cdn);
176 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
177 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
178
179 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
180
181 if(!item.rejected_qty && item.qty) {
182 item.received_qty = item.qty;
183 }
184
185 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
186 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
187 }
188
189 this._super(doc, cdt, cdn);
190 },
191
192 received_qty: function(doc, cdt, cdn) {
193 this.calculate_accepted_qty(doc, cdt, cdn)
194 },
195
196 rejected_qty: function(doc, cdt, cdn) {
197 this.calculate_accepted_qty(doc, cdt, cdn)
198 },
199
200 calculate_accepted_qty: function(doc, cdt, cdn){
201 var item = frappe.get_doc(cdt, cdn);
202 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
203
204 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
205
206 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
207 this.qty(doc, cdt, cdn);
208 },
209
210 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
211 if(!item || !fieldnames) { return }
212
213 var is_negative_qty = false;
214 for(var i = 0; i<fieldnames.length; i++) {
215 if(item[fieldnames[i]] < 0){
216 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
217 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
218 is_negative_qty = true;
219 break;
220 }
221 }
222
223 return is_negative_qty
224 },
225
226 warehouse: function(doc, cdt, cdn) {
227 var item = frappe.get_doc(cdt, cdn);
228 if(item.item_code && item.warehouse) {
229 return this.frm.call({
230 method: "erpnext.stock.get_item_details.get_bin_details",
231 child: item,
232 args: {
233 item_code: item.item_code,
234 warehouse: item.warehouse
235 }
236 });
237 }
238 },
239
240 project: function(doc, cdt, cdn) {
241 var item = frappe.get_doc(cdt, cdn);
242 if(item.project) {
243 $.each(this.frm.doc["items"] || [],
244 function(i, other_item) {
245 if(!other_item.project) {
246 other_item.project = item.project;
247 refresh_field("project", other_item.name, other_item.parentfield);
248 }
249 });
250 }
251 },
252
253 category: function(doc, cdt, cdn) {
254 // should be the category field of tax table
255 if(cdt != doc.doctype) {
256 this.calculate_taxes_and_totals();
257 }
258 },
259 add_deduct_tax: function(doc, cdt, cdn) {
260 this.calculate_taxes_and_totals();
261 },
262
263 set_from_product_bundle: function() {
264 var me = this;
265 this.frm.add_custom_button(__("Product Bundle"), function() {
266 erpnext.buying.get_items_from_product_bundle(me.frm);
267 }, __("Get items from"));
268 },
269
270 shipping_address: function(){
271 var me = this;
272 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530273 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530274 },
275
276 tc_name: function() {
277 this.get_terms();
278 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200279
Shreya Shah95d93912018-10-08 14:17:37 +0530280 link_to_mrs: function() {
281 var my_items = [];
282 for (var i in cur_frm.doc.items) {
283 if(!cur_frm.doc.items[i].material_request){
284 my_items.push(cur_frm.doc.items[i].item_code);
285 }
286 }
287 frappe.call({
288 method: "erpnext.buying.utils.get_linked_material_requests",
289 args:{
290 items: my_items
291 },
292 callback: function(r) {
293 if(!r.message) {
294 frappe.throw(__("No pending Material Requests found to link for the given items."))
295 }
296 else {
297 var i = 0;
298 var item_length = cur_frm.doc.items.length;
299 while (i < item_length) {
300 var qty = cur_frm.doc.items[i].qty;
301 (r.message[0] || []).forEach(function(d) {
302 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
303 {
304 cur_frm.doc.items[i].material_request = d.mr_name;
305 cur_frm.doc.items[i].material_request_item = d.mr_item;
306 var my_qty = Math.min(qty, d.qty);
307 qty = qty - my_qty;
308 d.qty = d.qty - my_qty;
309 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
310 cur_frm.doc.items[i].qty = my_qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530311
Shreya Shah95d93912018-10-08 14:17:37 +0530312 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
313 if (qty > 0)
314 {
315 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
316 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
317 item_length++;
Nabin Haita39f3242019-01-21 18:50:50 +0530318
Shreya Shah95d93912018-10-08 14:17:37 +0530319 for (var key in cur_frm.doc.items[i])
320 {
321 newrow[key] = cur_frm.doc.items[i][key];
322 }
Nabin Haita39f3242019-01-21 18:50:50 +0530323
Shreya Shah95d93912018-10-08 14:17:37 +0530324 newrow.idx = item_length;
325 newrow["stock_qty"] = newrow.conversion_factor*qty;
326 newrow["qty"] = qty;
Nabin Haita39f3242019-01-21 18:50:50 +0530327
Shreya Shah95d93912018-10-08 14:17:37 +0530328 newrow["material_request"] = "";
329 newrow["material_request_item"] = "";
Nabin Haita39f3242019-01-21 18:50:50 +0530330
Shreya Shah95d93912018-10-08 14:17:37 +0530331 }
332 }
333 });
334 i++;
335 }
336 refresh_field("items");
337 //cur_frm.save();
338 }
339 }
340 });
341 },
342
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200343 update_auto_repeat_reference: function(doc) {
344 if (doc.auto_repeat) {
345 frappe.call({
346 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530347 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200348 docname: doc.auto_repeat,
349 reference:doc.name
350 },
351 callback: function(r){
352 if (r.message=="success") {
353 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
354 } else {
355 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
356 }
357 }
358 })
359 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530360 },
361
362 manufacturer: function(doc, cdt, cdn) {
363 const row = locals[cdt][cdn];
364
365 if(row.manufacturer) {
366 frappe.call({
367 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
368 args: {
369 'item_code': row.item_code,
370 'manufacturer': row.manufacturer
371 },
372 callback: function(r) {
373 if (r.message) {
374 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
375 }
376 }
377 });
378 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530379 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530380});
381
382cur_frm.add_fetch('project', 'cost_center', 'cost_center');
383
384erpnext.buying.get_default_bom = function(frm) {
385 $.each(frm.doc["items"] || [], function(i, d) {
386 if (d.item_code && d.bom === "") {
387 return frappe.call({
388 type: "GET",
389 method: "erpnext.stock.get_item_details.get_default_bom",
390 args: {
391 "item_code": d.item_code,
392 },
393 callback: function(r) {
394 if(r) {
395 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
396 }
397 }
398 })
399 }
400 });
401}
402
403erpnext.buying.get_items_from_product_bundle = function(frm) {
404 var dialog = new frappe.ui.Dialog({
405 title: __("Get Items from Product Bundle"),
406 fields: [
407 {
408 "fieldtype": "Link",
409 "label": __("Product Bundle"),
410 "fieldname": "product_bundle",
411 "options":"Product Bundle",
412 "reqd": 1
413 },
414 {
415 "fieldtype": "Currency",
416 "label": __("Quantity"),
417 "fieldname": "quantity",
418 "reqd": 1,
419 "default": 1
420 },
421 {
422 "fieldtype": "Button",
423 "label": __("Get Items"),
424 "fieldname": "get_items",
425 "cssClass": "btn-primary"
426 }
427 ]
428 });
429
430 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530431 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530432 if(!args) return;
433 dialog.hide();
434 return frappe.call({
435 type: "GET",
436 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
437 args: {
438 args: {
439 item_code: args.product_bundle,
440 quantity: args.quantity,
441 parenttype: frm.doc.doctype,
442 parent: frm.doc.name,
443 supplier: frm.doc.supplier,
444 currency: frm.doc.currency,
445 conversion_rate: frm.doc.conversion_rate,
446 price_list: frm.doc.buying_price_list,
447 price_list_currency: frm.doc.price_list_currency,
448 plc_conversion_rate: frm.doc.plc_conversion_rate,
449 company: frm.doc.company,
450 is_subcontracted: frm.doc.is_subcontracted,
451 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
452 ignore_pricing_rule: frm.doc.ignore_pricing_rule
453 }
454 },
455 freeze: true,
456 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100457 const first_row_is_empty = function(child_table){
458 if($.isArray(child_table) && child_table.length > 0) {
459 return !child_table[0].item_code;
460 }
461 return false;
462 };
463
464 const remove_empty_first_row = function(frm){
465 if (first_row_is_empty(frm.doc.items)){
466 frm.doc.items = frm.doc.items.splice(1);
467 }
468 };
469
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530470 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100471 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530472 for ( var i=0; i< r.message.length; i++ ) {
473 var d = frm.add_child("items");
474 var item = r.message[i];
475 for ( var key in item) {
476 if ( !is_null(item[key]) ) {
477 d[key] = item[key];
478 }
479 }
480 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
481 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
482 }
483 }
484 frm.refresh_field("items");
485 }
486 }
487 })
488 });
489 dialog.show();
490}