blob: 5c9f5d7da43be2bdfeba86f19e1b1355e91260f9 [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");
marinationca6dbad2019-12-11 12:10:05 +053033 var disable = cint(df.default) || cint(frappe.sys_defaults.disable_rounded_total);
Nabin Haita39f3242019-01-21 18:50:50 +053034 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
Deepesh Garg67600772020-06-13 22:40:23 +053076 me.frm.set_query('billing_address', erpnext.queries.company_address_query);
77
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053078 if(this.frm.fields_dict.supplier) {
79 this.frm.set_query("supplier", function() {
80 return{ query: "erpnext.controllers.queries.supplier_query" }});
81 }
82
83 this.frm.set_query("item_code", "items", function() {
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080084 if (me.frm.doc.is_subcontracted == "Yes") {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053085 return{
86 query: "erpnext.controllers.queries.item_query",
noahjacobca2fb472021-05-12 16:25:07 +053087 filters:{ 'supplier': me.frm.doc.supplier, 'is_sub_contracted_item': 1 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053088 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080089 }
Doridel Cahanap2b14d6a2018-11-13 14:37:16 +080090 else {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053091 return{
92 query: "erpnext.controllers.queries.item_query",
noahjacobca2fb472021-05-12 16:25:07 +053093 filters: { 'supplier': me.frm.doc.supplier, 'is_purchase_item': 1 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053094 }
95 }
96 });
Rohit Waghchaure3cf24362019-06-02 16:03:05 +053097
98
99 this.frm.set_query("manufacturer", "items", function(doc, cdt, cdn) {
100 const row = locals[cdt][cdn];
101 return {
102 query: "erpnext.controllers.queries.item_manufacturer_query",
103 filters:{ 'item_code': row.item_code }
104 }
105 });
Deepesh Gargef0d26c2020-01-06 15:34:15 +0530106
107 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
108 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
109 return me.set_query_for_item_tax_template(doc, cdt, cdn)
110 });
111 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530112 },
113
114 refresh: function(doc) {
115 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
116
117 this.frm.toggle_display("supplier_name",
118 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
119
120 if(this.frm.doc.docstatus==0 &&
121 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
122 this.set_from_product_bundle();
123 }
124
Rohit Waghchaure5cc3f142021-06-15 17:29:52 +0530125 this.toggle_subcontracting_fields();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530126 this._super();
127 },
128
Rohit Waghchaure5cc3f142021-06-15 17:29:52 +0530129 toggle_subcontracting_fields: function() {
130 if (in_list(['Purchase Receipt', 'Purchase Invoice'], this.frm.doc.doctype)) {
131 this.frm.fields_dict.supplied_items.grid.update_docfield_property('consumed_qty',
132 'read_only', this.frm.doc.__onload && this.frm.doc.__onload.backflush_based_on === 'BOM');
133
134 this.frm.set_df_property('supplied_items', 'cannot_add_rows', 1);
135 this.frm.set_df_property('supplied_items', 'cannot_delete_rows', 1);
136 }
137 },
138
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530139 supplier: function() {
140 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530141 erpnext.utils.get_party_details(this.frm, null, null, function(){
142 me.apply_price_list();
143 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530144 },
145
146 supplier_address: function() {
147 erpnext.utils.get_address_display(this.frm);
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500148 erpnext.utils.set_taxes_from_address(this.frm, "supplier_address", "supplier_address", "supplier_address");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530149 },
150
151 buying_price_list: function() {
152 this.apply_price_list();
153 },
154
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530155 discount_percentage: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530156 var item = frappe.get_doc(cdt, cdn);
157 item.discount_amount = 0.0;
158 this.price_list_rate(doc, cdt, cdn);
159 },
160
161 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530162 var item = frappe.get_doc(cdt, cdn);
163 item.discount_percentage = 0.0;
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530164 this.price_list_rate(doc, cdt, cdn);
165 },
166
167 qty: function(doc, cdt, cdn) {
168 var item = frappe.get_doc(cdt, cdn);
169 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
170 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
171
172 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
173
174 if(!item.rejected_qty && item.qty) {
175 item.received_qty = item.qty;
176 }
177
178 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
179 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
marinationd6596a12020-11-02 15:07:48 +0530180 item.received_stock_qty = flt(item.conversion_factor, precision("conversion_factor", item)) * flt(item.received_qty);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530181 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530182 this._super(doc, cdt, cdn);
183 },
184
Deepesh Gargbd9bdc52021-01-28 12:05:57 +0530185 batch_no: function(doc, cdt, cdn) {
186 this._super(doc, cdt, cdn);
187 },
188
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530189 received_qty: function(doc, cdt, cdn) {
190 this.calculate_accepted_qty(doc, cdt, cdn)
191 },
192
193 rejected_qty: function(doc, cdt, cdn) {
194 this.calculate_accepted_qty(doc, cdt, cdn)
195 },
196
197 calculate_accepted_qty: function(doc, cdt, cdn){
198 var item = frappe.get_doc(cdt, cdn);
199 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
200
201 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
202
203 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
204 this.qty(doc, cdt, cdn);
205 },
206
207 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
208 if(!item || !fieldnames) { return }
209
210 var is_negative_qty = false;
211 for(var i = 0; i<fieldnames.length; i++) {
212 if(item[fieldnames[i]] < 0){
Mohammad Hasnain Mohsin Rajan90e33e52020-11-25 15:37:54 +0530213 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}", [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530214 is_negative_qty = true;
215 break;
216 }
217 }
218
219 return is_negative_qty
220 },
221
222 warehouse: function(doc, cdt, cdn) {
223 var item = frappe.get_doc(cdt, cdn);
224 if(item.item_code && item.warehouse) {
225 return this.frm.call({
226 method: "erpnext.stock.get_item_details.get_bin_details",
227 child: item,
228 args: {
229 item_code: item.item_code,
Anupam4980dd62021-03-15 11:37:46 +0530230 warehouse: item.warehouse,
231 company: doc.company
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530232 }
233 });
234 }
235 },
236
237 project: function(doc, cdt, cdn) {
238 var item = frappe.get_doc(cdt, cdn);
239 if(item.project) {
240 $.each(this.frm.doc["items"] || [],
241 function(i, other_item) {
242 if(!other_item.project) {
243 other_item.project = item.project;
244 refresh_field("project", other_item.name, other_item.parentfield);
245 }
246 });
247 }
248 },
249
marination8a30cb22020-04-23 00:18:21 +0530250 rejected_warehouse: function(doc, cdt) {
251 // trigger autofill_warehouse only if parent rejected_warehouse field is triggered
252 if (["Purchase Invoice", "Purchase Receipt"].includes(cdt)) {
253 this.autofill_warehouse(doc.items, "rejected_warehouse", doc.rejected_warehouse);
254 }
255 },
256
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530257 category: function(doc, cdt, cdn) {
258 // should be the category field of tax table
259 if(cdt != doc.doctype) {
260 this.calculate_taxes_and_totals();
261 }
262 },
263 add_deduct_tax: function(doc, cdt, cdn) {
264 this.calculate_taxes_and_totals();
265 },
266
267 set_from_product_bundle: function() {
268 var me = this;
269 this.frm.add_custom_button(__("Product Bundle"), function() {
270 erpnext.buying.get_items_from_product_bundle(me.frm);
marinatione14fdf42020-10-30 17:06:00 +0530271 }, __("Get Items From"));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530272 },
273
274 shipping_address: function(){
275 var me = this;
276 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530277 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530278 },
279
Deepesh Garg67600772020-06-13 22:40:23 +0530280 billing_address: function() {
281 erpnext.utils.get_address_display(this.frm, "billing_address",
282 "billing_address_display", true);
283 },
284
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530285 tc_name: function() {
286 this.get_terms();
287 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200288
289 update_auto_repeat_reference: function(doc) {
290 if (doc.auto_repeat) {
291 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530292 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
Nabin Haita39f3242019-01-21 18:50:50 +0530293 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200294 docname: doc.auto_repeat,
295 reference:doc.name
296 },
297 callback: function(r){
298 if (r.message=="success") {
299 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
300 } else {
301 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
302 }
303 }
304 })
305 }
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530306 },
307
308 manufacturer: function(doc, cdt, cdn) {
309 const row = locals[cdt][cdn];
310
311 if(row.manufacturer) {
312 frappe.call({
313 method: "erpnext.stock.doctype.item_manufacturer.item_manufacturer.get_item_manufacturer_part_no",
314 args: {
315 'item_code': row.item_code,
316 'manufacturer': row.manufacturer
317 },
318 callback: function(r) {
319 if (r.message) {
320 frappe.model.set_value(cdt, cdn, 'manufacturer_part_no', r.message);
321 }
322 }
323 });
324 }
marination3e7a8ab2020-04-07 21:00:57 +0530325 },
326
327 manufacturer_part_no: function(doc, cdt, cdn) {
328 const row = locals[cdt][cdn];
329
330 if (row.manufacturer_part_no) {
331 frappe.model.get_value('Item Manufacturer',
332 {
333 'item_code': row.item_code,
334 'manufacturer': row.manufacturer,
335 'manufacturer_part_no': row.manufacturer_part_no
336 },
337 'name',
338 function(data) {
339 if (!data) {
340 let msg = {
341 message: __("Manufacturer Part Number <b>{0}</b> is invalid", [row.manufacturer_part_no]),
342 title: __("Invalid Part Number")
343 }
344 frappe.throw(msg);
345 }
346 });
347
348 }
349 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530350});
351
352cur_frm.add_fetch('project', 'cost_center', 'cost_center');
353
marinationa1196882020-09-24 15:22:46 +0530354erpnext.buying.link_to_mrs = function(frm) {
marinationa1196882020-09-24 15:22:46 +0530355 frappe.call({
356 method: "erpnext.buying.utils.get_linked_material_requests",
357 args:{
marination359778e2020-10-16 16:47:23 +0530358 items: frm.doc.items.map((item) => item.item_code)
marinationa1196882020-09-24 15:22:46 +0530359 },
360 callback: function(r) {
marination359778e2020-10-16 16:47:23 +0530361 if (!r.message || r.message.length == 0) {
362 frappe.throw({
363 message: __("No pending Material Requests found to link for the given items."),
364 title: __("Note")
365 });
marinationa1196882020-09-24 15:22:46 +0530366 }
marination359778e2020-10-16 16:47:23 +0530367
368 var item_length = frm.doc.items.length;
369 for (let item of frm.doc.items) {
370 var qty = item.qty;
371 (r.message[0] || []).forEach(function(d) {
372 if (d.qty > 0 && qty > 0 && item.item_code == d.item_code && !item.material_request_item)
373 {
374 item.material_request = d.mr_name;
375 item.material_request_item = d.mr_item;
376 var my_qty = Math.min(qty, d.qty);
377 qty = qty - my_qty;
378 d.qty = d.qty - my_qty;
379 item.stock_qty = my_qty*item.conversion_factor;
380 item.qty = my_qty;
381
382 frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + item.idx + ")");
383 if (qty > 0)
marinationa1196882020-09-24 15:22:46 +0530384 {
marination359778e2020-10-16 16:47:23 +0530385 frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
386 var newrow = frappe.model.add_child(frm.doc, item.doctype, "items");
387 item_length++;
marinationa1196882020-09-24 15:22:46 +0530388
marination359778e2020-10-16 16:47:23 +0530389 for (var key in item)
marinationa1196882020-09-24 15:22:46 +0530390 {
marination359778e2020-10-16 16:47:23 +0530391 newrow[key] = item[key];
marinationa1196882020-09-24 15:22:46 +0530392 }
marination359778e2020-10-16 16:47:23 +0530393
394 newrow.idx = item_length;
395 newrow["stock_qty"] = newrow.conversion_factor*qty;
396 newrow["qty"] = qty;
397
398 newrow["material_request"] = "";
399 newrow["material_request_item"] = "";
400
marinationa1196882020-09-24 15:22:46 +0530401 }
marination359778e2020-10-16 16:47:23 +0530402 }
403 });
marinationa1196882020-09-24 15:22:46 +0530404 }
marination359778e2020-10-16 16:47:23 +0530405 refresh_field("items");
marinationa1196882020-09-24 15:22:46 +0530406 }
407 });
408}
409
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530410erpnext.buying.get_default_bom = function(frm) {
411 $.each(frm.doc["items"] || [], function(i, d) {
412 if (d.item_code && d.bom === "") {
413 return frappe.call({
414 type: "GET",
415 method: "erpnext.stock.get_item_details.get_default_bom",
416 args: {
417 "item_code": d.item_code,
418 },
419 callback: function(r) {
420 if(r) {
421 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
422 }
423 }
424 })
425 }
426 });
427}
428
429erpnext.buying.get_items_from_product_bundle = function(frm) {
430 var dialog = new frappe.ui.Dialog({
431 title: __("Get Items from Product Bundle"),
432 fields: [
433 {
434 "fieldtype": "Link",
435 "label": __("Product Bundle"),
436 "fieldname": "product_bundle",
437 "options":"Product Bundle",
438 "reqd": 1
439 },
440 {
441 "fieldtype": "Currency",
442 "label": __("Quantity"),
443 "fieldname": "quantity",
444 "reqd": 1,
445 "default": 1
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530446 }
Marica9942acd2020-04-21 13:13:39 +0530447 ],
448 primary_action_label: 'Get Items',
449 primary_action(args){
450 if(!args) return;
451 dialog.hide();
452 return frappe.call({
453 type: "GET",
454 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530455 args: {
Marica9942acd2020-04-21 13:13:39 +0530456 args: {
457 item_code: args.product_bundle,
458 quantity: args.quantity,
459 parenttype: frm.doc.doctype,
460 parent: frm.doc.name,
461 supplier: frm.doc.supplier,
462 currency: frm.doc.currency,
463 conversion_rate: frm.doc.conversion_rate,
464 price_list: frm.doc.buying_price_list,
465 price_list_currency: frm.doc.price_list_currency,
466 plc_conversion_rate: frm.doc.plc_conversion_rate,
467 company: frm.doc.company,
468 is_subcontracted: frm.doc.is_subcontracted,
469 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
470 ignore_pricing_rule: frm.doc.ignore_pricing_rule,
471 doctype: frm.doc.doctype
tundebabzyf35710d2017-08-16 08:55:18 +0100472 }
Marica9942acd2020-04-21 13:13:39 +0530473 },
474 freeze: true,
475 callback: function(r) {
476 const first_row_is_empty = function(child_table){
477 if($.isArray(child_table) && child_table.length > 0) {
478 return !child_table[0].item_code;
479 }
480 return false;
481 };
tundebabzyf35710d2017-08-16 08:55:18 +0100482
Marica9942acd2020-04-21 13:13:39 +0530483 const remove_empty_first_row = function(frm){
484 if (first_row_is_empty(frm.doc.items)){
485 frm.doc.items = frm.doc.items.splice(1);
486 }
487 };
tundebabzyf35710d2017-08-16 08:55:18 +0100488
Marica9942acd2020-04-21 13:13:39 +0530489 if(!r.exc && r.message) {
490 remove_empty_first_row(frm);
Afshaneb4b2aa2020-09-01 15:01:32 +0530491 for (var i=0; i< r.message.length; i++) {
Marica9942acd2020-04-21 13:13:39 +0530492 var d = frm.add_child("items");
493 var item = r.message[i];
Afshaneb4b2aa2020-09-01 15:01:32 +0530494 for (var key in item) {
495 if (!is_null(item[key]) && key !== "doctype") {
Marica9942acd2020-04-21 13:13:39 +0530496 d[key] = item[key];
497 }
498 }
499 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
500 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530501 }
502 }
Marica9942acd2020-04-21 13:13:39 +0530503 frm.refresh_field("items");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530504 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530505 }
Marica9942acd2020-04-21 13:13:39 +0530506 })
507 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530508 });
Marica9942acd2020-04-21 13:13:39 +0530509
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530510 dialog.show();
marinationfac40352020-12-07 21:35:49 +0530511}