blob: 2822ae850a05132e371d2a22b20645512c39643e [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
rohitwaghchaureae4ff5a2018-08-01 18:09:51 +053029 if (this.frm.doc.__islocal) {
30 this.frm.set_value("disable_rounded_total", cint(frappe.sys_defaults.disable_rounded_total));
31 }
32
Faris Ansariab74ca72017-05-30 12:54:42 +053033 /* eslint-disable */
34 // no idea where me is coming from
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053035 if(this.frm.get_field('shipping_address')) {
Faris Ansariab74ca72017-05-30 12:54:42 +053036 this.frm.set_query("shipping_address", function() {
37 if(me.frm.doc.customer) {
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053038 return {
KanchanChauhan1dc26b12017-06-13 15:26:35 +053039 query: 'frappe.contacts.doctype.address.address.address_query',
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053040 filters: { link_doctype: 'Customer', link_name: me.frm.doc.customer }
41 };
42 } else
43 return erpnext.queries.company_address_query(me.frm.doc)
44 });
45 }
Faris Ansariab74ca72017-05-30 12:54:42 +053046 /* eslint-enable */
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053047 },
48
49 setup_queries: function() {
50 var me = this;
51
52 if(this.frm.fields_dict.buying_price_list) {
53 this.frm.set_query("buying_price_list", function() {
54 return{
55 filters: { 'buying': 1 }
56 }
57 });
58 }
59
60 me.frm.set_query('supplier', erpnext.queries.supplier);
61 me.frm.set_query('contact_person', erpnext.queries.contact_query);
62 me.frm.set_query('supplier_address', erpnext.queries.address_query);
63
64 if(this.frm.fields_dict.supplier) {
65 this.frm.set_query("supplier", function() {
66 return{ query: "erpnext.controllers.queries.supplier_query" }});
67 }
68
69 this.frm.set_query("item_code", "items", function() {
70 if(me.frm.doc.is_subcontracted == "Yes") {
71 return{
72 query: "erpnext.controllers.queries.item_query",
73 filters:{ 'is_sub_contracted_item': 1 }
74 }
75 } else {
76 return{
77 query: "erpnext.controllers.queries.item_query",
78 filters: {'is_purchase_item': 1}
79 }
80 }
81 });
82 },
83
84 refresh: function(doc) {
85 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'};
86
87 this.frm.toggle_display("supplier_name",
88 (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier));
89
90 if(this.frm.doc.docstatus==0 &&
91 (this.frm.doctype==="Purchase Order" || this.frm.doctype==="Material Request")) {
92 this.set_from_product_bundle();
93 }
94
95 this._super();
96 },
97
98 supplier: function() {
99 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530100 erpnext.utils.get_party_details(this.frm, null, null, function(){
101 me.apply_price_list();
102 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530103 },
104
105 supplier_address: function() {
106 erpnext.utils.get_address_display(this.frm);
107 },
108
109 buying_price_list: function() {
110 this.apply_price_list();
111 },
112
113 price_list_rate: function(doc, cdt, cdn) {
114 var item = frappe.get_doc(cdt, cdn);
115 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
116
Manas Solankie5e87f72018-05-28 20:07:08 +0530117 let item_rate = item.price_list_rate;
118 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
119 item_rate = item.blanket_order_rate;
120 }
121 item.rate = flt(item_rate * (1 - item.discount_percentage / 100.0), precision("rate", item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530122
123 this.calculate_taxes_and_totals();
124 },
125
126 discount_percentage: function(doc, cdt, cdn) {
127 this.price_list_rate(doc, cdt, cdn);
128 },
129
130 qty: function(doc, cdt, cdn) {
131 var item = frappe.get_doc(cdt, cdn);
132 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
133 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
134
135 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
136
137 if(!item.rejected_qty && item.qty) {
138 item.received_qty = item.qty;
139 }
140
141 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
142 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
143 }
144
145 this._super(doc, cdt, cdn);
146 },
147
148 received_qty: function(doc, cdt, cdn) {
149 this.calculate_accepted_qty(doc, cdt, cdn)
150 },
151
152 rejected_qty: function(doc, cdt, cdn) {
153 this.calculate_accepted_qty(doc, cdt, cdn)
154 },
155
156 calculate_accepted_qty: function(doc, cdt, cdn){
157 var item = frappe.get_doc(cdt, cdn);
158 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
159
160 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
161
162 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
163 this.qty(doc, cdt, cdn);
164 },
165
166 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
167 if(!item || !fieldnames) { return }
168
169 var is_negative_qty = false;
170 for(var i = 0; i<fieldnames.length; i++) {
171 if(item[fieldnames[i]] < 0){
172 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
173 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
174 is_negative_qty = true;
175 break;
176 }
177 }
178
179 return is_negative_qty
180 },
181
182 warehouse: function(doc, cdt, cdn) {
183 var item = frappe.get_doc(cdt, cdn);
184 if(item.item_code && item.warehouse) {
185 return this.frm.call({
186 method: "erpnext.stock.get_item_details.get_bin_details",
187 child: item,
188 args: {
189 item_code: item.item_code,
190 warehouse: item.warehouse
191 }
192 });
193 }
194 },
195
196 project: function(doc, cdt, cdn) {
197 var item = frappe.get_doc(cdt, cdn);
198 if(item.project) {
199 $.each(this.frm.doc["items"] || [],
200 function(i, other_item) {
201 if(!other_item.project) {
202 other_item.project = item.project;
203 refresh_field("project", other_item.name, other_item.parentfield);
204 }
205 });
206 }
207 },
208
209 category: function(doc, cdt, cdn) {
210 // should be the category field of tax table
211 if(cdt != doc.doctype) {
212 this.calculate_taxes_and_totals();
213 }
214 },
215 add_deduct_tax: function(doc, cdt, cdn) {
216 this.calculate_taxes_and_totals();
217 },
218
219 set_from_product_bundle: function() {
220 var me = this;
221 this.frm.add_custom_button(__("Product Bundle"), function() {
222 erpnext.buying.get_items_from_product_bundle(me.frm);
223 }, __("Get items from"));
224 },
225
226 shipping_address: function(){
227 var me = this;
228 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530229 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530230 },
231
232 tc_name: function() {
233 this.get_terms();
234 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200235
236 update_auto_repeat_reference: function(doc) {
237 if (doc.auto_repeat) {
238 frappe.call({
239 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
240 args:{
241 docname: doc.auto_repeat,
242 reference:doc.name
243 },
244 callback: function(r){
245 if (r.message=="success") {
246 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
247 } else {
248 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
249 }
250 }
251 })
252 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530253 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530254});
255
256cur_frm.add_fetch('project', 'cost_center', 'cost_center');
257
258erpnext.buying.get_default_bom = function(frm) {
259 $.each(frm.doc["items"] || [], function(i, d) {
260 if (d.item_code && d.bom === "") {
261 return frappe.call({
262 type: "GET",
263 method: "erpnext.stock.get_item_details.get_default_bom",
264 args: {
265 "item_code": d.item_code,
266 },
267 callback: function(r) {
268 if(r) {
269 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
270 }
271 }
272 })
273 }
274 });
275}
276
277erpnext.buying.get_items_from_product_bundle = function(frm) {
278 var dialog = new frappe.ui.Dialog({
279 title: __("Get Items from Product Bundle"),
280 fields: [
281 {
282 "fieldtype": "Link",
283 "label": __("Product Bundle"),
284 "fieldname": "product_bundle",
285 "options":"Product Bundle",
286 "reqd": 1
287 },
288 {
289 "fieldtype": "Currency",
290 "label": __("Quantity"),
291 "fieldname": "quantity",
292 "reqd": 1,
293 "default": 1
294 },
295 {
296 "fieldtype": "Button",
297 "label": __("Get Items"),
298 "fieldname": "get_items",
299 "cssClass": "btn-primary"
300 }
301 ]
302 });
303
304 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530305 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530306 if(!args) return;
307 dialog.hide();
308 return frappe.call({
309 type: "GET",
310 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
311 args: {
312 args: {
313 item_code: args.product_bundle,
314 quantity: args.quantity,
315 parenttype: frm.doc.doctype,
316 parent: frm.doc.name,
317 supplier: frm.doc.supplier,
318 currency: frm.doc.currency,
319 conversion_rate: frm.doc.conversion_rate,
320 price_list: frm.doc.buying_price_list,
321 price_list_currency: frm.doc.price_list_currency,
322 plc_conversion_rate: frm.doc.plc_conversion_rate,
323 company: frm.doc.company,
324 is_subcontracted: frm.doc.is_subcontracted,
325 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
326 ignore_pricing_rule: frm.doc.ignore_pricing_rule
327 }
328 },
329 freeze: true,
330 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100331 const first_row_is_empty = function(child_table){
332 if($.isArray(child_table) && child_table.length > 0) {
333 return !child_table[0].item_code;
334 }
335 return false;
336 };
337
338 const remove_empty_first_row = function(frm){
339 if (first_row_is_empty(frm.doc.items)){
340 frm.doc.items = frm.doc.items.splice(1);
341 }
342 };
343
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530344 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100345 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530346 for ( var i=0; i< r.message.length; i++ ) {
347 var d = frm.add_child("items");
348 var item = r.message[i];
349 for ( var key in item) {
350 if ( !is_null(item[key]) ) {
351 d[key] = item[key];
352 }
353 }
354 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
355 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
356 }
357 }
358 frm.refresh_field("items");
359 }
360 }
361 })
362 });
363 dialog.show();
364}
tundebabzyf35710d2017-08-16 08:55:18 +0100365