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