blob: 1c4d4103c78d8e721fe2e5605e04b7d988f8a674 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
Rushabh Mehta06fa46f2015-01-29 18:09:11 +05304frappe.pages['pos'].on_page_load = function(wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
11 wrapper.pos = new erpnext.pos.PointOfSale(wrapper)
Rushabh Mehta72e17192014-08-08 15:30:49 +053012}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053013
Rohit Waghchauree0934d12016-05-11 15:04:57 +053014frappe.pages['pos'].refresh = function(wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053015 window.onbeforeunload = function () {
16 return wrapper.pos.beforeunload()
17 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053018 wrapper.pos.on_refresh_page()
19}
20
21
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
Rohit Waghchauree0934d12016-05-11 15:04:57 +053023 init: function(wrapper){
24 this.load = true;
25 this.page = wrapper.page;
26 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053027 this.set_indicator();
28 this.onload();
29 this.make_menu_list();
30 this.set_interval_for_si_sync();
31 this.si_docs = this.get_doc_from_localstorage();
32 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053033
Rohit Waghchauree0934d12016-05-11 15:04:57 +053034 on_refresh_page: function() {
35 var me = this;
36 if(this.load){
37 this.load = false;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053038 }else if(this.connection_status){
39 this.onload();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053040 }else{
41 this.create_new();
42 }
43 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053044
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 beforeunload: function(e){
46 if(this.connection_status == false && frappe.get_route()[0] == "pos"){
47 e = e || window.event;
48
49 // For IE and Firefox prior to version 4
50 if (e) {
51 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
52 return
53 }
54
55 // For Safari
56 return __("You are in offline mode. You will not be able to reload until you have network.");
57 }
58 },
59
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053060 check_internet_connection: function(){
61 var me = this;
62 //Check Internet connection after every 30 seconds
63 setInterval(function(){
64 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053065 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 },
67
68 set_indicator: function(){
69 var me = this;
70 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053071 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053072 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053073 frappe.call({
74 method:"frappe.handler.ping",
75 callback: function(r){
76 if(r.message){
77 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053078 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 }
80 }
81 })
82 },
83
84 onload: function(){
85 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053086 this.get_data_from_server(function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053087 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053089
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053090 this.check_internet_connection();
91 },
92
93 make_menu_list: function(){
94 var me = this;
95
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053096 this.page.add_menu_item(__("New Sales Invoice"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053097 me.save_previous_entry();
98 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053099 })
100
101 this.page.add_menu_item(__("View Offline Records"), function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530102 me.show_unsync_invoice_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530103 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530104
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530105 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530106 me.get_data_from_server(function(){
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530107 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530108 me.make_customer();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530109 me.make_item_list(false);
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530110 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530111 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530112 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530113
114 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530115 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530116 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530117 },
118
119 show_unsync_invoice_list: function(){
120 var me = this;
121 this.si_docs = this.get_doc_from_localstorage();
122
123 this.list_dialog = new frappe.ui.Dialog({
124 title: 'Invoice List'
125 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530126
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530127 this.list_dialog.show();
128 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530129 if(this.si_docs.length > 0){
130 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
131 <div class="col-xs-2">Sr</div>\
132 <div class="col-xs-4">Customer</div>\
133 <div class="col-xs-2 text-left">Status</div>\
134 <div class="col-xs-4 text-right">Grand Total</div>\
135 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530136
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530137 $.each(this.si_docs, function(index, data){
138 for(key in data) {
139 $(frappe.render_template("pos_invoice_list", {
140 sr: index + 1,
141 name: key,
142 customer: data[key].customer,
143 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
144 data: me.get_doctype_status(data[key])
145 })).appendTo($(me.list_body));
146 }
147 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530148
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530149 $(this.list_body).find('.list-row').click(function() {
150 me.name = $(this).attr('invoice-name')
151 doc_data = me.get_invoice_doc(me.si_docs)
152 if(doc_data){
153 me.frm.doc = doc_data[0][me.name];
154 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530155 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530156 me.disable_input_field();
157 me.list_dialog.hide();
158 }
159 })
160 }else{
161 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
162 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530163 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530164
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530165 get_doctype_status: function(doc){
166 if(doc.outstanding_amount == 0){
167 return {status: "Paid", indicator: "green"}
168 }else if(doc.docstatus == 0){
169 return {status: "Draft", indicator: "red"}
170 }else if(doc.paid_amount >= 0){
171 return {status: "Unpaid", indicator: "orange"}
172 }
173 },
174
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530175 set_missing_values: function(){
176 var me = this;
177 doc = JSON.parse(localStorage.getItem('doc'))
178 if(this.frm.doc.payments.length == 0){
179 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530180 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530181 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530182
183 if(this.frm.doc.customer){
184 this.party_field.$input.val(this.frm.doc.customer);
185 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530186
187 if(!this.frm.doc.write_off_account){
188 this.frm.doc.write_off_account = doc.write_off_account
189 }
190
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530191 if(!this.frm.doc.account_for_change_amount){
192 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530193 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530194 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530195
196 get_invoice_doc: function(si_docs){
197 var me = this;
198 this.si_docs = this.get_doc_from_localstorage();
199
200 return $.grep(this.si_docs, function(data){
201 for(key in data){
202 return key == me.name
203 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530204 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530205 },
206
207 get_data_from_server: function(callback){
208 var me = this;
209 frappe.call({
210 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
211 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530212 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530213 callback: function(r){
214 window.items = r.message.items;
215 window.customers = r.message.customers;
216 window.pricing_rules = r.message.pricing_rules;
217 window.meta = r.message.meta;
218 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530219 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530220 localStorage.setItem('doc', JSON.stringify(r.message.doc));
221 if(callback){
222 callback();
223 }
224 }
225 })
226 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530227
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530228 save_previous_entry : function(){
229 if(this.frm.doc.items.length > 0){
230 this.create_invoice()
231 }
232 },
233
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530234 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530235 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530236 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530237 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530238 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530239 this.setup();
240 },
241
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530242 load_data: function(load_doc){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530243 this.items = window.items;
244 this.customers = window.customers;
245 this.pricing_rules = window.pricing_rules;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530246
247 if(load_doc) {
248 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
249 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530250
251 $.each(window.meta, function(i, data){
252 frappe.meta.sync(data)
253 })
254
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530255 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530256 {content: window.print_template, title:"POS"})
257 },
258
259 setup: function(){
260 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
261 this.set_transaction_defaults("Customer");
262 this.make();
263 this.set_primary_action();
264 },
265
266 set_transaction_defaults: function(party) {
267 var me = this;
268 this.party = party;
269 this.price_list = (party == "Customer" ?
270 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
271 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
272 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
273 },
274
275 make: function() {
276 this.make_search();
277 this.make_customer();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530278 this.make_item_list(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 this.make_discount_field()
280 },
281
282 make_search: function() {
283 var me = this;
284 this.search = frappe.ui.form.make_control({
285 df: {
286 "fieldtype": "Data",
287 "label": "Item",
288 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530289 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530290 },
291 parent: this.wrapper.find(".search-area"),
292 only_input: true,
293 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530294
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530295 this.search.make_input();
296 this.search.$input.on("keyup", function() {
297 setTimeout(function() {
298 me.items = me.get_items();
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530299 me.make_item_list(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530300 }, 1000);
301 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530302
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 this.party_field = frappe.ui.form.make_control({
304 df: {
305 "fieldtype": "Data",
306 "options": this.party,
307 "label": this.party,
308 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530309 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530310 },
311 parent: this.wrapper.find(".party-area"),
312 only_input: true,
313 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530314
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530315 this.party_field.make_input();
316 },
317
318 make_customer: function() {
319 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530320
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530321 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530322 this.party_field.$input.val(this.default_customer);
323 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530324 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530325
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530326 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530327 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530328 source: function (request, response) {
329 me.customer_data = me.get_customers(request.term)
330 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530331 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530332 customer_group: data.customer_group, territory: data.territory}
333 }))
334 },
335 change: function(event, ui){
336 if(ui.item){
337 me.frm.doc.customer = ui.item.label;
338 me.frm.doc.customer_name = ui.item.customer_name;
339 me.frm.doc.customer_group = ui.item.customer_group;
340 me.frm.doc.territory = ui.item.territory;
341 }else{
342 me.frm.doc.customer = me.party_field.$input.val();
343 }
344 me.refresh();
345 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530346 }).on("focus", function(){
347 setTimeout(function() {
348 if(!me.party_field.$input.val()) {
349 me.party_field.$input.autocomplete( "search", " " );
350 }
351 }, 500);
352 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 },
354
355 get_customers: function(key){
356 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530357 key = key.toLowerCase().trim()
358 if(key){
359 return $.grep(this.customers, function(data) {
360 if(data.name.toLowerCase().match(key)
361 || data.customer_name.toLowerCase().match(key)
362 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
363 return data
364 }
365 })
366 }else{
367 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
368 return customers.slice(0, 20)
369 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530370 },
371
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530372 make_item_list: function(index_search) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530373 var me = this;
374 if(!this.price_list) {
375 msgprint(__("Price List not found or disabled"));
376 return;
377 }
378
379 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530380
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 var $wrap = me.wrapper.find(".item-list");
382 me.wrapper.find(".item-list").empty();
383
384 if (this.items) {
385 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f1d0122016-10-04 12:18:59 +0530386 if(!index_search || index < 16){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530387 $(frappe.render_template("pos_item", {
388 item_code: obj.name,
389 item_price: format_currency(obj.price_list_rate, obj.currency),
390 item_name: obj.name===obj.item_name ? "" : obj.item_name,
391 item_image: obj.image ? "url('" + obj.image + "')" : null,
392 color: frappe.get_palette(obj.item_name),
393 abbr: frappe.get_abbr(obj.item_name)
394 })).tooltip().appendTo($wrap);
395 }
396 });
397 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530398
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530399 if(this.items.length == 1
400 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530401 this.search.$input.val("");
402 this.add_to_cart();
403 }
404
405 // if form is local then allow this function
406 $(me.wrapper).find("div.pos-item").on("click", function() {
407 me.customer_validate();
408 if(me.frm.doc.docstatus==0) {
409 me.items = me.get_items($(this).attr("data-item-code"))
410 me.add_to_cart();
411 }
412 });
413 },
414
415 get_items: function(item_code){
416 // To search item as per the key enter
417
418 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530419 this.item_serial_no = {};
420 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530421
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530422 if(item_code){
423 return $.grep(window.items, function(item){
424 if(item.item_code == item_code ){
425 return true
426 }
427 })
428 }
429
430 key = this.search.$input.val().toLowerCase();
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530431 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530432
433 if(key){
434 return $.grep(window.items, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530435 if(search_status){
436 if(in_list(item.batch_nos, me.search.$input.val())){
437 search_status = false;
438 return me.item_batch_no[item.item_code] = me.search.$input.val()
439 } else if(in_list(Object.keys(item.serial_nos), me.search.$input.val())) {
440 search_status = false;
441 me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]]
442 return true
443 } else if(item.barcode == me.search.$input.val()) {
444 search_status = false;
445 return item.barcode == me.search.$input.val();
446 } else if((item.item_code.toLowerCase().match(key)) ||
447 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key))) {
448 return true
449 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530450 }
451 })
452 }else{
453 return window.items;
454 }
455 },
456
457 update_qty: function() {
458 var me = this;
459
460 $(this.wrapper).find(".pos-item-qty").on("change", function(){
461 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530462 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530463 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530464
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530465 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
466 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
467 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530468 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530469 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530470
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
472 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
473 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530474 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 })
476 },
477
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530478 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530479 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530480
481 $(this.wrapper).find(".pos-item-rate").on("change", function(){
482 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
483 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
484 })
485 },
486
487 update_qty_rate_against_item_code: function(item_code, field, value){
488 var me = this;
489 if(value < 0){
490 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530492
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530493 this.remove_item = []
494 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530495 if(d.serial_no){
496 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530497 }
498
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530499 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530500 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530501 d.amount = flt(d.rate) * flt(d.qty);
502 if(d.qty==0){
503 me.remove_item.push(d.idx)
504 }
505 }
506 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530507
508 if(field == 'qty'){
509 this.remove_zero_qty_item();
510 }
511
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530512 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530513 },
514
515 remove_zero_qty_item: function(){
516 var me = this;
517 idx = 0
518 this.items = []
519 idx = 0
520 $.each(this.frm.doc["items"] || [], function(i, d) {
521 if(!in_list(me.remove_item, d.idx)){
522 d.idx = idx;
523 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530524 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530525 }
526 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530527
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530528 this.frm.doc["items"] = this.items;
529 },
530
531 make_discount_field: function(){
532 var me = this;
533
534 this.wrapper.find('input.discount-percentage').on("change", function() {
535 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
536 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530537
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530538 if(me.frm.doc.apply_discount_on == 'Net Total'){
539 total = me.frm.doc.net_total
540 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530541
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530542 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
543 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
544 me.refresh();
545 });
546
547 this.wrapper.find('input.discount-amount').on("change", function() {
548 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
549 me.frm.doc.additional_discount_percentage = 0.0;
550 me.wrapper.find('input.discount-percentage').val(0);
551 me.refresh();
552 });
553 },
554
555 customer_validate: function(){
556 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530557 if(!this.frm.doc.customer){
558 frappe.throw(__("Please select customer"))
559 }
560 },
561
562 add_to_cart: function() {
563 var me = this;
564 var caught = false;
565 var no_of_items = me.wrapper.find(".pos-bill-item").length;
566
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530567 this.customer_validate();
568 this.mandatory_batch_no();
569 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530570 this.validate_warehouse();
571
572 if (no_of_items != 0) {
573 $.each(this.frm.doc["items"] || [], function(i, d) {
574 if (d.item_code == me.items[0].item_code) {
575 caught = true;
576 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530577 d.amount = flt(d.rate) * flt(d.qty);
578 if(me.item_serial_no[d.item_code]){
579 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
580 d.warehouse = me.item_serial_no[d.item_code][1]
581 }
582
583 if(me.item_batch_no.length){
584 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530585 }
586 }
587 });
588 }
589
590 // if item not found then add new item
591 if (!caught)
592 this.add_new_item_to_grid();
593
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530594 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530595 },
596
597 add_new_item_to_grid: function() {
598 var me = this;
599 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
600 this.child.item_code = this.items[0].item_code;
601 this.child.item_name = this.items[0].item_name;
602 this.child.stock_uom = this.items[0].stock_uom;
603 this.child.description = this.items[0].description;
604 this.child.qty = 1;
605 this.child.item_group = this.items[0].item_group;
606 this.child.cost_center = this.items[0].cost_center;
607 this.child.income_account = this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530608 this.child.warehouse = (this.item_serial_no[this.child.item_code]
609 ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530610 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
611 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
612 this.child.actual_qty = this.items[0].actual_qty;
613 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530614 this.child.batch_no = this.item_batch_no[this.child.item_code];
615 this.child.serial_no = (this.item_serial_no[this.child.item_code]
616 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaure525900c2016-09-07 15:01:08 +0530617 this.child.item_tax_rate = this.items[0].taxes;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530618 },
619
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530620 update_paid_amount_status: function(update_paid_amount){
621 if(this.name){
622 update_paid_amount = update_paid_amount ? false : true;
623 }
624
625 this.refresh(update_paid_amount);
626 },
627
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530628 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530629 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530630 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530631 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530632 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530633 this.set_primary_action();
634 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530635
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530636 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530637 this.apply_pricing_rule();
638 this.discount_amount_applied = false;
639 this._calculate_taxes_and_totals();
640 this.calculate_discount_amount();
641 this.show_items_in_item_cart();
642 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530643 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530644 this.set_totals();
645 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530646
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530647 get_company_currency: function() {
648 return erpnext.get_currency(this.frm.doc.company);
649 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530650
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530651 show_item_wise_taxes: function(){
652 return null;
653 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530654
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 show_items_in_item_cart: function() {
656 var me = this;
657 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530658 $.each(this.frm.doc.items|| [], function(i, d) {
659 $(frappe.render_template("pos_bill_item", {
660 item_code: d.item_code,
661 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
662 qty: d.qty,
663 actual_qty: d.actual_qty,
664 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530665 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530666 amount: format_currency(d.amount, me.frm.doc.currency)
667 })).appendTo($items);
668 });
669
670 this.wrapper.find("input.pos-item-qty").on("focus", function() {
671 $(this).select();
672 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530673
674 this.wrapper.find("input.pos-item-rate").on("focus", function() {
675 $(this).select();
676 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530677 },
678
679 set_taxes: function(){
680 var me = this;
681 me.frm.doc.total_taxes_and_charges = 0.0
682
683 var taxes = this.frm.doc.taxes || [];
684 $(this.wrapper)
685 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
686 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530687
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530688 $.each(taxes, function(i, d) {
689 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
690 $(frappe.render_template("pos_tax_row", {
691 description: d.description,
692 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
693 me.frm.doc.currency)
694 })).appendTo(me.wrapper.find(".tax-table"));
695 }
696 });
697 },
698
699 set_totals: function() {
700 var me = this;
701 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
702 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
703 },
704
705 set_primary_action: function() {
706 var me = this;
707
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530708 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530709 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530710 me.validate();
711 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530712 me.create_invoice();
713 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530714 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530715 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530716 this.page.set_primary_action(__("Print"), function() {
717 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530718 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530719 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530720 }else {
721 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530722 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530723
724 this.page.set_secondary_action(__("New"), function() {
725 me.save_previous_entry();
726 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530727 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530728 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530729
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530730 print_document: function(html){
731 var w = window.open();
732 w.document.write(html);
733 w.document.close();
734 setTimeout(function(){
735 w.print();
736 w.close();
737 }, 1000)
738 },
739
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530740 submit_invoice: function(){
741 var me = this;
742 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
743 me.change_status();
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530744 frappe.msgprint(__("Sales invoice submitted sucessfully."))
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530745 })
746 },
747
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530748 change_status: function(){
749 if(this.frm.doc.docstatus == 0){
750 this.frm.doc.docstatus = 1;
751 this.update_invoice();
752 this.disable_input_field();
753 }
754 },
755
756 disable_input_field: function(){
757 var pointer_events = 'inherit'
758 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530759
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530760 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530761 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530762 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530763 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530764
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530765 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
766 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
767 this.set_primary_action();
768 },
769
770 create_invoice: function(){
771 var me = this;
772 var invoice_data = {}
773 this.si_docs = this.get_doc_from_localstorage();
774 if(this.name){
775 this.update_invoice()
776 }else{
777 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530778 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530779 this.frm.doc.posting_date = frappe.datetime.get_today();
780 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530781 invoice_data[this.name] = this.frm.doc
782 this.si_docs.push(invoice_data)
783 this.update_localstorage();
784 this.set_primary_action();
785 }
786 },
787
788 update_invoice: function(){
789 var me = this;
790 this.si_docs = this.get_doc_from_localstorage();
791 $.each(this.si_docs, function(index, data){
792 for(key in data){
793 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530794 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530795 me.update_localstorage();
796 }
797 }
798 })
799 },
800
801 update_localstorage: function(){
802 try{
803 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
804 }catch(e){
805 frappe.throw(__("LocalStorage is full , did not save"))
806 }
807 },
808
809 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530810 try{
811 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
812 }catch(e){
813 return []
814 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530815 },
816
817 set_interval_for_si_sync: function(){
818 var me = this;
819 setInterval(function(){
820 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530821 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530822 },
823
824 sync_sales_invoice: function(){
825 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530826 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530827
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530828 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530829 frappe.call({
830 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
831 args: {
832 doc_list: me.si_docs
833 },
834 callback: function(r){
835 if(r.message){
836 me.removed_items = r.message;
837 me.remove_doc_from_localstorage();
838 }
839 }
840 })
841 }
842 },
843
844 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530845 var invoices = [];
846 var index = 1;
847 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530848 if(docs){
849 invoices = $.map(docs, function(data){
850 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530851 if(data[key].docstatus == 1 && index < 50){
852 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530853 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530854 return data
855 }
856 }
857 });
858 }
859
860 return invoices
861 },
862
863 remove_doc_from_localstorage: function(){
864 var me = this;
865 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530866 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530867 if(this.removed_items){
868 $.each(this.si_docs, function(index, data){
869 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530870 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530871 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530872 }
873 }
874 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530875 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530876 this.update_localstorage();
877 }
878 },
879
880 validate: function(){
881 var me = this;
882 this.customer_validate();
883 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530884 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530885 },
886
887 item_validate: function(){
888 if(this.frm.doc.items.length == 0){
889 frappe.throw(__("Select items to save the invoice"))
890 }
891 },
Saurabh9a6c6662016-06-27 16:51:44 +0530892
893 validate_mode_of_payments: function(){
894 if (this.frm.doc.payments.length === 0){
895 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
896 }
897 },
898
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530899 validate_serial_no: function(){
900 var me = this;
901 var item_code = serial_no = '';
902 for (key in this.item_serial_no){
903 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530904 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530905 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530906
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530907 if(item_code && serial_no){
908 $.each(this.frm.doc.items, function(index, data){
909 if(data.item_code == item_code){
910 if(in_list(data.serial_no.split('\n'), serial_no)){
911 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
912 'serial_no': serial_no
913 })))
914 }
915 }
916 })
917 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530918
919 if(this.items[0].has_serial_no && serial_no == ""){
920 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
921 'item': this.items[0].item_code
922 })))
923 }
924 },
925
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530926 validate_serial_no_qty: function(args, item_code, field, value){
927 var me = this;
928 if (args.item_code == item_code && args.serial_no
929 && field == 'qty' && cint(value) != value) {
930 args.qty = 0.0;
931 this.refresh();
932 frappe.throw(__("Serial no item cannot be a fraction"))
933 }
934
935 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
936 args.qty = 0.0;
937 args.serial_no = ''
938 this.refresh();
939 frappe.throw(__("Total nos of serial no is not equal to quantity."))
940 }
941 },
942
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530943 mandatory_batch_no: function(){
944 var me = this;
945 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
946 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
947 'item': this.items[0].item_code
948 })))
949 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530950 },
951
952 apply_pricing_rule: function(){
953 var me = this;
954 $.each(this.frm.doc["items"], function(n, item) {
955 pricing_rule = me.get_pricing_rule(item)
956 me.validate_pricing_rule(pricing_rule)
957 if(pricing_rule.length){
958 item.margin_type = pricing_rule[0].margin_type;
959 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
960 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
961 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
962 me.apply_pricing_rule_on_item(item)
963 }
964 })
965 },
966
967 get_pricing_rule: function(item){
968 var me = this;
969 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530970 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
971 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
972 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
973 return me.validate_condition(data)
974 }else{
975 return true
976 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530977 }
978 }
979 })
980 },
981
982 validate_condition: function(data){
983 //This method check condition based on applicable for
984 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
985 if(in_list(condition[1], condition[0])){
986 return true
987 }
988 },
989
990 get_mapper_for_pricing_rule: function(data){
991 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +0530992 'Customer': [data.customer, [this.frm.doc.customer]],
993 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
994 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530995 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530996 }
997 },
998
999 validate_pricing_rule: function(pricing_rule){
1000 //This method validate duplicate pricing rule
1001 var pricing_rule_name = '';
1002 var priority = 0;
1003 var pricing_rule_list = [];
1004 var priority_list = []
1005
1006 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301007
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301008 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301009 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301010 priority_list.push(data.priority)
1011 if(priority <= data.priority){
1012 priority = data.priority
1013 pricing_rule_list.push(data)
1014 }
1015 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301016
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301017 count = 0
1018 $.each(priority_list, function(index, value){
1019 if(value == priority){
1020 count++
1021 }
1022 })
1023
1024 if(priority == 0 || count > 1){
1025 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1026 'pricing_rule': pricing_rule_name
1027 })))
1028 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301029
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 return pricing_rule_list
1031 }
1032 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301033
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301034 validate_warehouse: function(){
1035 if(!this.items[0].default_warehouse){
1036 frappe.throw(__("Deafault warehouse is required for selected item"))
1037 }
1038 }
1039})