blob: e420a4881446cc11fbde8eec12da628fb49409a4 [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 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200229
230 update_auto_repeat_reference: function(doc) {
231 if (doc.auto_repeat) {
232 frappe.call({
233 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
234 args:{
235 docname: doc.auto_repeat,
236 reference:doc.name
237 },
238 callback: function(r){
239 if (r.message=="success") {
240 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
241 } else {
242 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
243 }
244 }
245 })
246 }
Faris Ansariab74ca72017-05-30 12:54:42 +0530247 }
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530248});
249
250cur_frm.add_fetch('project', 'cost_center', 'cost_center');
251
252erpnext.buying.get_default_bom = function(frm) {
253 $.each(frm.doc["items"] || [], function(i, d) {
254 if (d.item_code && d.bom === "") {
255 return frappe.call({
256 type: "GET",
257 method: "erpnext.stock.get_item_details.get_default_bom",
258 args: {
259 "item_code": d.item_code,
260 },
261 callback: function(r) {
262 if(r) {
263 frappe.model.set_value(d.doctype, d.name, "bom", r.message);
264 }
265 }
266 })
267 }
268 });
269}
270
271erpnext.buying.get_items_from_product_bundle = function(frm) {
272 var dialog = new frappe.ui.Dialog({
273 title: __("Get Items from Product Bundle"),
274 fields: [
275 {
276 "fieldtype": "Link",
277 "label": __("Product Bundle"),
278 "fieldname": "product_bundle",
279 "options":"Product Bundle",
280 "reqd": 1
281 },
282 {
283 "fieldtype": "Currency",
284 "label": __("Quantity"),
285 "fieldname": "quantity",
286 "reqd": 1,
287 "default": 1
288 },
289 {
290 "fieldtype": "Button",
291 "label": __("Get Items"),
292 "fieldname": "get_items",
293 "cssClass": "btn-primary"
294 }
295 ]
296 });
297
298 dialog.fields_dict.get_items.$input.click(function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530299 var args = dialog.get_values();
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530300 if(!args) return;
301 dialog.hide();
302 return frappe.call({
303 type: "GET",
304 method: "erpnext.stock.doctype.packed_item.packed_item.get_items_from_product_bundle",
305 args: {
306 args: {
307 item_code: args.product_bundle,
308 quantity: args.quantity,
309 parenttype: frm.doc.doctype,
310 parent: frm.doc.name,
311 supplier: frm.doc.supplier,
312 currency: frm.doc.currency,
313 conversion_rate: frm.doc.conversion_rate,
314 price_list: frm.doc.buying_price_list,
315 price_list_currency: frm.doc.price_list_currency,
316 plc_conversion_rate: frm.doc.plc_conversion_rate,
317 company: frm.doc.company,
318 is_subcontracted: frm.doc.is_subcontracted,
319 transaction_date: frm.doc.transaction_date || frm.doc.posting_date,
320 ignore_pricing_rule: frm.doc.ignore_pricing_rule
321 }
322 },
323 freeze: true,
324 callback: function(r) {
tundebabzyf35710d2017-08-16 08:55:18 +0100325 const first_row_is_empty = function(child_table){
326 if($.isArray(child_table) && child_table.length > 0) {
327 return !child_table[0].item_code;
328 }
329 return false;
330 };
331
332 const remove_empty_first_row = function(frm){
333 if (first_row_is_empty(frm.doc.items)){
334 frm.doc.items = frm.doc.items.splice(1);
335 }
336 };
337
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530338 if(!r.exc && r.message) {
tundebabzyf35710d2017-08-16 08:55:18 +0100339 remove_empty_first_row(frm);
Rushabh Mehtaf0b45622017-03-31 12:53:05 +0530340 for ( var i=0; i< r.message.length; i++ ) {
341 var d = frm.add_child("items");
342 var item = r.message[i];
343 for ( var key in item) {
344 if ( !is_null(item[key]) ) {
345 d[key] = item[key];
346 }
347 }
348 if(frappe.meta.get_docfield(d.doctype, "price_list_rate", d.name)) {
349 frm.script_manager.trigger("price_list_rate", d.doctype, d.name);
350 }
351 }
352 frm.refresh_field("items");
353 }
354 }
355 })
356 });
357 dialog.show();
358}
tundebabzyf35710d2017-08-16 08:55:18 +0100359