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