blob: a80ffa87134caf6f553bb787e5c7a34ac56648a3 [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
Faris Ansariab74ca72017-05-30 12:54:42 +053029 /* eslint-disable */
30 // no idea where me is coming from
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053031 if(this.frm.get_field('shipping_address')) {
Faris Ansariab74ca72017-05-30 12:54:42 +053032 this.frm.set_query("shipping_address", function() {
33 if(me.frm.doc.customer) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053034 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053035 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053036 filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
37 };
38 } else
39 return erpnext.queries.company_address_query(me.frm.doc)
40 });
41 }
Faris Ansariab74ca72017-05-30 12:54:42 +053042 /* eslint-enable */
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053043 },
44
45 setup_queries: function() {
46 var me = this;
47
48 if(this.frm.fields_dict.buying_price_list) {
49 this.frm.set_query("buying_price_list", function() {
50 return{
51 filters: { 'buying': 1 }
52 }
53 });
54 }
55
56 me.frm.set_query('supplier', erpnext.queries.supplier);
57 me.frm.set_query('contact_person', erpnext.queries.contact_query);
58 me.frm.set_query('supplier_address', erpnext.queries.address_query);
59
60 if(this.frm.fields_dict.supplier) {
61 this.frm.set_query("supplier", function() {
62 return{ query: "erpnext.controllers.queries.supplier_query" }});
63 }
64
65 this.frm.set_query("item_code", "items", function() {
66 if(me.frm.doc.is_subcontracted == "Yes") {
67 return{
68 query: "erpnext.controllers.queries.item_query",
69 filters:{ 'is_sub_contracted_item': 1 }
70 }
71 } else {
72 return{
73 query: "erpnext.controllers.queries.item_query",
74 filters: {'is_purchase_item': 1}
75 }
76 }
77 });
78 },
79
80 refresh: function(doc) {
81 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
82
83 this.frm.toggle_display("supplier_name",
84 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
85
86 if(this.frm.doc.docstatus==0 &&
87 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
88 this.set_from_product_bundle();
89 }
90
91 this._super();
92 },
93
94 supplier: function() {
95 var me = this;
96 erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()});
97 },
98
99 supplier_address: function() {
100 erpnext.utils.get_address_display(this.frm);
101 },
102
103 buying_price_list: function() {
104 this.apply_price_list();
105 },
106
107 price_list_rate: function(doc, cdt, cdn) {
108 var item = frappe.get_doc(cdt, cdn);
109 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
110
Manas Solankie5e87f72018-05-28 20:07:08 +0530111 let item_rate = item.price_list_rate;
112 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
113 item_rate = item.blanket_order_rate;
114 }
115 item.rate = flt(item_rate * (1 - item.discount_percentage / 100.0), precision("rate", item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530116
117 this.calculate_taxes_and_totals();
118 },
119
120 discount_percentage: function(doc, cdt, cdn) {
121 this.price_list_rate(doc, cdt, cdn);
122 },
123
124 qty: function(doc, cdt, cdn) {
125 var item = frappe.get_doc(cdt, cdn);
126 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
127 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
128
129 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
130
131 if(!item.rejected_qty && item.qty) {
132 item.received_qty = item.qty;
133 }
134
135 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
136 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
137 }
138
139 this._super(doc, cdt, cdn);
140 },
141
142 received_qty: function(doc, cdt, cdn) {
143 this.calculate_accepted_qty(doc, cdt, cdn)
144 },
145
146 rejected_qty: function(doc, cdt, cdn) {
147 this.calculate_accepted_qty(doc, cdt, cdn)
148 },
149
150 calculate_accepted_qty: function(doc, cdt, cdn){
151 var item = frappe.get_doc(cdt, cdn);
152 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
153
154 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
155
156 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
157 this.qty(doc, cdt, cdn);
158 },
159
160 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
161 if(!item || !fieldnames) { return }
162
163 var is_negative_qty = false;
164 for(var i = 0; i<fieldnames.length; i++) {
165 if(item[fieldnames[i]] < 0){
166 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
167 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
168 is_negative_qty = true;
169 break;
170 }
171 }
172
173 return is_negative_qty
174 },
175
176 warehouse: function(doc, cdt, cdn) {
177 var item = frappe.get_doc(cdt, cdn);
178 if(item.item_code && item.warehouse) {
179 return this.frm.call({
180 method: "erpnext.stock.get_item_details.get_bin_details",
181 child: item,
182 args: {
183 item_code: item.item_code,
184 warehouse: item.warehouse
185 }
186 });
187 }
188 },
189
190 project: function(doc, cdt, cdn) {
191 var item = frappe.get_doc(cdt, cdn);
192 if(item.project) {
193 $.each(this.frm.doc["items"] || [],
194 function(i, other_item) {
195 if(!other_item.project) {
196 other_item.project = item.project;
197 refresh_field("project", other_item.name, other_item.parentfield);
198 }
199 });
200 }
201 },
202
203 category: function(doc, cdt, cdn) {
204 // should be the category field of tax table
205 if(cdt != doc.doctype) {
206 this.calculate_taxes_and_totals();
207 }
208 },
209 add_deduct_tax: function(doc, cdt, cdn) {
210 this.calculate_taxes_and_totals();
211 },
212
213 set_from_product_bundle: function() {
214 var me = this;
215 this.frm.add_custom_button(__("Product Bundle"), function() {
216 erpnext.buying.get_items_from_product_bundle(me.frm);
217 }, __("Get items from"));
218 },
219
220 shipping_address: function(){
221 var me = this;
222 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530223 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530224 },
225
226 tc_name: function() {
227 this.get_terms();
228 },
229 link_to_mrs: function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530230 var my_items = [];
231 for (var i in cur_frm.doc.items) {
232 if(!cur_frm.doc.items[i].material_request){
233 my_items.push(cur_frm.doc.items[i].item_code);
234 }
235 }
236 frappe.call({
237 method: "erpnext.buying.utils.get_linked_material_requests",
238 args:{
239 items: my_items
240 },
241 callback: function(r) {
Sushant Nadkar64907e52018-06-05 10:33:04 +0530242 if(r.exc || !r.message) return;
Nabin Hait9c421612017-07-20 13:32:01 +0530243
Faris Ansariab74ca72017-05-30 12:54:42 +0530244 var i = 0;
245 var item_length = cur_frm.doc.items.length;
246 while (i < item_length) {
247 var qty = cur_frm.doc.items[i].qty;
248 (r.message[0] || []).forEach(function(d) {
249 if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
250 {
251 cur_frm.doc.items[i].material_request = d.mr_name;
252 cur_frm.doc.items[i].material_request_item = d.mr_item;
253 var my_qty = Math.min(qty, d.qty);
254 qty = qty - my_qty;
255 d.qty = d.qty - my_qty;
256 cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
257 cur_frm.doc.items[i].qty = my_qty;
Nabin Haite6d65bc2018-02-14 17:44:06 +0530258
259 frappe.msgprint(__("Assigning {0} to {1} (row {2})",
Shreya Shah8b531c12018-02-15 11:21:49 +0530260 [d.mr_name, d.item_code, cur_frm.doc.items[i].idx]));
Nabin Haite6d65bc2018-02-14 17:44:06 +0530261
Nabin Hait9c421612017-07-20 13:32:01 +0530262 if (qty > 0) {
Shreya Shah8b531c12018-02-15 11:21:49 +0530263 frappe.msgprint(__("Splitting {0} units of {1}", [qty, d.item_code]));
Faris Ansariab74ca72017-05-30 12:54:42 +0530264 var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
265 item_length++;
266
Nabin Hait9c421612017-07-20 13:32:01 +0530267 for (var key in cur_frm.doc.items[i]) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530268 newrow[key] = cur_frm.doc.items[i][key];
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530269 }
270
Faris Ansariab74ca72017-05-30 12:54:42 +0530271 newrow.idx = item_length;
272 newrow["stock_qty"] = newrow.conversion_factor*qty;
273 newrow["qty"] = qty;
Faris Ansariab74ca72017-05-30 12:54:42 +0530274 newrow["material_request"] = "";
275 newrow["material_request_item"] = "";
Faris Ansariab74ca72017-05-30 12:54:42 +0530276 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530277 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530278 });
279 i++;
280 }
281 refresh_field("items");
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530282 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530283 });
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200284 },
285
286 update_auto_repeat_reference: function(doc) {
287 if (doc.auto_repeat) {
288 frappe.call({
289 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
290 args:{
291 docname: doc.auto_repeat,
292 reference:doc.name
293 },
294 callback: function(r){
295 if (r.message=="success") {
296 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
297 } else {
298 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
299 }
300 }
301 })
302 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530303 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530304});
305
306cur_frm.add_fetch('project', 'cost_center', 'cost_center');
307
308erpnext.buying.get_default_bom = function(frm) {
309 $.each(frm.doc["items"] || [], function(i, d) {
310 if (d.item_code && d.bom === "") {
311 return frappe.call({
312 type: "GET",
313 method: "erpnext.stock.get_item_details.get_default_bom",
314 args: {
315 "item_code": d.item_code,
316 },
317 callback: function(r) {
318 if(r) {
319 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
320 }
321 }
322 })
323 }
324 });
325}
326
327erpnext.buying.get_items_from_product_bundle = function(frm) {
328 var dialog = new frappe.ui.Dialog({
329 title: __("Get Items from Product Bundle"),
330 fields: [
331 {
332 "fieldtype": "Link",
333 "label": __("Product Bundle"),
334 "fieldname": "product_bundle",
335 "options":"Product Bundle",
336 "reqd": 1
337 },
338 {
339 "fieldtype": "Currency",
340 "label": __("Quantity"),
341 "fieldname": "quantity",
342 "reqd": 1,
343 "default": 1
344 },
345 {
346 "fieldtype": "Button",
347 "label": __("Get Items"),
348 "fieldname": "get_items",
349 "cssClass": "btn-primary"
350 }
351 ]
352 });
353
354 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530355 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530356 if(!args) return;
357 dialog.hide();
358 return frappe.call({
359 type: "GET",
360 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
361 args: {
362 args: {
363 item_code: args.product_bundle,
364 quantity: args.quantity,
365 parenttype: frm.doc.doctype,
366 parent: frm.doc.name,
367 supplier: frm.doc.supplier,
368 currency: frm.doc.currency,
369 conversion_rate: frm.doc.conversion_rate,
370 price_list: frm.doc.buying_price_list,
371 price_list_currency: frm.doc.price_list_currency,
372 plc_conversion_rate: frm.doc.plc_conversion_rate,
373 company: frm.doc.company,
374 is_subcontracted: frm.doc.is_subcontracted,
375 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
376 ignore_pricing_rule: frm.doc.ignore_pricing_rule
377 }
378 },
379 freeze: true,
380 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100381 const first_row_is_empty = function(child_table){
382 if($.isArray(child_table) && child_table.length > 0) {
383 return !child_table[0].item_code;
384 }
385 return false;
386 };
387
388 const remove_empty_first_row = function(frm){
389 if (first_row_is_empty(frm.doc.items)){
390 frm.doc.items = frm.doc.items.splice(1);
391 }
392 };
393
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530394 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100395 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530396 for ( var i=0; i< r.message.length; i++ ) {
397 var d = frm.add_child("items");
398 var item = r.message[i];
399 for ( var key in item) {
400 if ( !is_null(item[key]) ) {
401 d[key] = item[key];
402 }
403 }
404 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
405 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
406 }
407 }
408 frm.refresh_field("items");
409 }
410 }
411 })
412 });
413 dialog.show();
414}
tundebabzyf35710d2017-08-16 08:55:18 +0100415