blob: 076b39c1c8f2b2d8417b9df3529f7a85939a3533 [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;
Nabin Haite45ec662018-08-01 17:44:34 +053096 erpnext.utils.get_party_details(this.frm, null, null, function(){
97 me.apply_price_list();
98 });
Rushabh Mehtaf0b45622017-03-31 12:53:05 +053099 },
100
101 supplier_address: function() {
102 erpnext.utils.get_address_display(this.frm);
103 },
104
105 buying_price_list: function() {
106 this.apply_price_list();
107 },
108
109 price_list_rate: function(doc, cdt, cdn) {
110 var item = frappe.get_doc(cdt, cdn);
111 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
112
Manas Solankie5e87f72018-05-28 20:07:08 +0530113 let item_rate = item.price_list_rate;
114 if (doc.doctype == "Purchase Order" && item.blanket_order_rate) {
115 item_rate = item.blanket_order_rate;
116 }
117 item.rate = flt(item_rate * (1 - item.discount_percentage / 100.0), precision("rate", item));
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530118
119 this.calculate_taxes_and_totals();
120 },
121
122 discount_percentage: function(doc, cdt, cdn) {
123 this.price_list_rate(doc, cdt, cdn);
124 },
125
126 qty: function(doc, cdt, cdn) {
127 var item = frappe.get_doc(cdt, cdn);
128 if ((doc.doctype == "Purchase Receipt") || (doc.doctype == "Purchase Invoice" && (doc.update_stock || doc.is_return))) {
129 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
130
131 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["qty", "received_qty"])){ return }
132
133 if(!item.rejected_qty && item.qty) {
134 item.received_qty = item.qty;
135 }
136
137 frappe.model.round_floats_in(item, ["qty", "received_qty"]);
138 item.rejected_qty = flt(item.received_qty - item.qty, precision("rejected_qty", item));
139 }
140
141 this._super(doc, cdt, cdn);
142 },
143
144 received_qty: function(doc, cdt, cdn) {
145 this.calculate_accepted_qty(doc, cdt, cdn)
146 },
147
148 rejected_qty: function(doc, cdt, cdn) {
149 this.calculate_accepted_qty(doc, cdt, cdn)
150 },
151
152 calculate_accepted_qty: function(doc, cdt, cdn){
153 var item = frappe.get_doc(cdt, cdn);
154 frappe.model.round_floats_in(item, ["received_qty", "rejected_qty"]);
155
156 if(!doc.is_return && this.validate_negative_quantity(cdt, cdn, item, ["received_qty", "rejected_qty"])){ return }
157
158 item.qty = flt(item.received_qty - item.rejected_qty, precision("qty", item));
159 this.qty(doc, cdt, cdn);
160 },
161
162 validate_negative_quantity: function(cdt, cdn, item, fieldnames){
163 if(!item || !fieldnames) { return }
164
165 var is_negative_qty = false;
166 for(var i = 0; i<fieldnames.length; i++) {
167 if(item[fieldnames[i]] < 0){
168 frappe.msgprint(__("Row #{0}: {1} can not be negative for item {2}",
169 [item.idx,__(frappe.meta.get_label(cdt, fieldnames[i], cdn)), item.item_code]));
170 is_negative_qty = true;
171 break;
172 }
173 }
174
175 return is_negative_qty
176 },
177
178 warehouse: function(doc, cdt, cdn) {
179 var item = frappe.get_doc(cdt, cdn);
180 if(item.item_code && item.warehouse) {
181 return this.frm.call({
182 method: "erpnext.stock.get_item_details.get_bin_details",
183 child: item,
184 args: {
185 item_code: item.item_code,
186 warehouse: item.warehouse
187 }
188 });
189 }
190 },
191
192 project: function(doc, cdt, cdn) {
193 var item = frappe.get_doc(cdt, cdn);
194 if(item.project) {
195 $.each(this.frm.doc["items"] || [],
196 function(i, other_item) {
197 if(!other_item.project) {
198 other_item.project = item.project;
199 refresh_field("project", other_item.name, other_item.parentfield);
200 }
201 });
202 }
203 },
204
205 category: function(doc, cdt, cdn) {
206 // should be the category field of tax table
207 if(cdt != doc.doctype) {
208 this.calculate_taxes_and_totals();
209 }
210 },
211 add_deduct_tax: function(doc, cdt, cdn) {
212 this.calculate_taxes_and_totals();
213 },
214
215 set_from_product_bundle: function() {
216 var me = this;
217 this.frm.add_custom_button(__("Product Bundle"), function() {
218 erpnext.buying.get_items_from_product_bundle(me.frm);
219 }, __("Get items from"));
220 },
221
222 shipping_address: function(){
223 var me = this;
224 erpnext.utils.get_address_display(this.frm, "shipping_address",
Faris Ansariab74ca72017-05-30 12:54:42 +0530225 "shipping_address_display", true);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530226 },
227
228 tc_name: function() {
229 this.get_terms();
230 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200231
232 update_auto_repeat_reference: function(doc) {
233 if (doc.auto_repeat) {
234 frappe.call({
235 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
236 args:{
237 docname: doc.auto_repeat,
238 reference:doc.name
239 },
240 callback: function(r){
241 if (r.message=="success") {
242 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
243 } else {
244 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
245 }
246 }
247 })
248 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530249 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530250});
251
252cur_frm.add_fetch('project', 'cost_center', 'cost_center');
253
254erpnext.buying.get_default_bom = function(frm) {
255 $.each(frm.doc["items"] || [], function(i, d) {
256 if (d.item_code && d.bom === "") {
257 return frappe.call({
258 type: "GET",
259 method: "erpnext.stock.get_item_details.get_default_bom",
260 args: {
261 "item_code": d.item_code,
262 },
263 callback: function(r) {
264 if(r) {
265 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
266 }
267 }
268 })
269 }
270 });
271}
272
273erpnext.buying.get_items_from_product_bundle = function(frm) {
274 var dialog = new frappe.ui.Dialog({
275 title: __("Get Items from Product Bundle"),
276 fields: [
277 {
278 "fieldtype": "Link",
279 "label": __("Product Bundle"),
280 "fieldname": "product_bundle",
281 "options":"Product Bundle",
282 "reqd": 1
283 },
284 {
285 "fieldtype": "Currency",
286 "label": __("Quantity"),
287 "fieldname": "quantity",
288 "reqd": 1,
289 "default": 1
290 },
291 {
292 "fieldtype": "Button",
293 "label": __("Get Items"),
294 "fieldname": "get_items",
295 "cssClass": "btn-primary"
296 }
297 ]
298 });
299
300 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530301 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530302 if(!args) return;
303 dialog.hide();
304 return frappe.call({
305 type: "GET",
306 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
307 args: {
308 args: {
309 item_code: args.product_bundle,
310 quantity: args.quantity,
311 parenttype: frm.doc.doctype,
312 parent: frm.doc.name,
313 supplier: frm.doc.supplier,
314 currency: frm.doc.currency,
315 conversion_rate: frm.doc.conversion_rate,
316 price_list: frm.doc.buying_price_list,
317 price_list_currency: frm.doc.price_list_currency,
318 plc_conversion_rate: frm.doc.plc_conversion_rate,
319 company: frm.doc.company,
320 is_subcontracted: frm.doc.is_subcontracted,
321 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
322 ignore_pricing_rule: frm.doc.ignore_pricing_rule
323 }
324 },
325 freeze: true,
326 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100327 const first_row_is_empty = function(child_table){
328 if($.isArray(child_table) && child_table.length > 0) {
329 return !child_table[0].item_code;
330 }
331 return false;
332 };
333
334 const remove_empty_first_row = function(frm){
335 if (first_row_is_empty(frm.doc.items)){
336 frm.doc.items = frm.doc.items.splice(1);
337 }
338 };
339
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530340 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100341 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530342 for ( var i=0; i< r.message.length; i++ ) {
343 var d = frm.add_child("items");
344 var item = r.message[i];
345 for ( var key in item) {
346 if ( !is_null(item[key]) ) {
347 d[key] = item[key];
348 }
349 }
350 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
351 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
352 }
353 }
354 frm.refresh_field("items");
355 }
356 }
357 })
358 });
359 dialog.show();
360}
tundebabzyf35710d2017-08-16 08:55:18 +0100361