blob: 2162563f22604afaae5609e52ba59569cd63f3d8 [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 Waghchaure6087fe12016-04-09 14:31:09 +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) {
15 wrapper.pos.on_refresh_page()
16}
17
18
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053019erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
Rohit Waghchauree0934d12016-05-11 15:04:57 +053020 init: function(wrapper){
21 this.load = true;
22 this.page = wrapper.page;
23 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053024 this.set_indicator();
25 this.onload();
26 this.make_menu_list();
27 this.set_interval_for_si_sync();
28 this.si_docs = this.get_doc_from_localstorage();
29 },
Rohit Waghchauree0934d12016-05-11 15:04:57 +053030
31 on_refresh_page: function() {
32 var me = this;
33 if(this.load){
34 this.load = false;
35 }else{
36 this.create_new();
37 }
38 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053039
40 check_internet_connection: function(){
41 var me = this;
42 //Check Internet connection after every 30 seconds
43 setInterval(function(){
44 me.set_indicator();
45 }, 30000)
46 },
47
48 set_indicator: function(){
49 var me = this;
50 // navigator.onLine
51 this.page.set_indicator("Offline", "grey")
52 frappe.call({
53 method:"frappe.handler.ping",
54 callback: function(r){
55 if(r.message){
56 me.connection_status = true;
57 me.page.set_indicator("Online", "green")
58 }
59 }
60 })
61 },
62
63 onload: function(){
64 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053065 this.get_data_from_server(function(){
66 me.create_new();
67 });
68
69 this.check_internet_connection();
70 },
71
72 make_menu_list: function(){
73 var me = this;
74
75 this.page.add_menu_item(__("Unsync Records"), function(){
76 me.show_unsync_invoice_list()
77 });
78
79 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053080 me.get_data_from_server(function(){
81 me.load_data()
82 me.make_customer()
83 me.make_item_list()
84 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053085 });
86
87 this.page.add_menu_item(__("New Sales Invoice"), function() {
88 me.create_new()
89 })
90 },
91
92 show_unsync_invoice_list: function(){
93 var me = this;
94 this.si_docs = this.get_doc_from_localstorage();
95
96 this.list_dialog = new frappe.ui.Dialog({
97 title: 'Invoice List'
98 });
99
100 this.list_dialog.show();
101 this.list_body = this.list_dialog.body;
102 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530103 <div class="col-xs-3">Sr</div>\
104 <div class="col-xs-3">Customer</div>\
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530105 <div class="col-xs-3 text-right">Grand Total</div>\
106 <div class="col-xs-3 text-right">Status</div>\
107 </div>')
108
109 $.each(this.si_docs, function(index, data){
110 for(key in data) {
111 $(frappe.render_template("pos_invoice_list", {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530112 sr: index + 1,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530113 name: key,
114 customer: data[key].customer,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530115 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530116 status: (data[key].docstatus == 1) ? 'Submitted' : 'Draft'
117 })).appendTo($(me.list_body));
118 }
119 })
120
121 $(this.list_body).find('.list-row').click(function() {
122 me.name = $(this).attr('invoice-name')
123 doc_data = me.get_invoice_doc(me.si_docs)
124 if(doc_data){
125 me.frm.doc = doc_data[0][me.name];
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530126 me.set_missing_values();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530127 me.refresh();
128 me.disable_input_field();
129 me.list_dialog.hide();
130 }
131 })
132 },
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530133
134 set_missing_values: function(){
135 var me = this;
136 doc = JSON.parse(localStorage.getItem('doc'))
137 if(this.frm.doc.payments.length == 0){
138 this.frm.doc.payments = doc.payments;
139 }
140 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530141
142 get_invoice_doc: function(si_docs){
143 var me = this;
144 this.si_docs = this.get_doc_from_localstorage();
145
146 return $.grep(this.si_docs, function(data){
147 for(key in data){
148 return key == me.name
149 }
150 })
151 },
152
153 get_data_from_server: function(callback){
154 var me = this;
155 frappe.call({
156 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
157 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530158 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530159 callback: function(r){
160 window.items = r.message.items;
161 window.customers = r.message.customers;
162 window.pricing_rules = r.message.pricing_rules;
163 window.meta = r.message.meta;
164 window.print_template = r.message.print_template;
165 localStorage.setItem('doc', JSON.stringify(r.message.doc));
166 if(callback){
167 callback();
168 }
169 }
170 })
171 },
172
173 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530174 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530175 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530176 this.name = '';
177 this.frm.doc = JSON.parse(localStorage.getItem('doc'))
178 this.load_data();
179 this.setup();
180 },
181
182 load_data: function(){
183 this.items = window.items;
184 this.customers = window.customers;
185 this.pricing_rules = window.pricing_rules;
186
187 $.each(window.meta, function(i, data){
188 frappe.meta.sync(data)
189 })
190
191 this.print_template = frappe.render_template("print_template",
192 {content: window.print_template, title:"POS"})
193 },
194
195 setup: function(){
196 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
197 this.set_transaction_defaults("Customer");
198 this.make();
199 this.set_primary_action();
200 },
201
202 set_transaction_defaults: function(party) {
203 var me = this;
204 this.party = party;
205 this.price_list = (party == "Customer" ?
206 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
207 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
208 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
209 },
210
211 make: function() {
212 this.make_search();
213 this.make_customer();
214 this.make_item_list();
215 this.make_discount_field()
216 },
217
218 make_search: function() {
219 var me = this;
220 this.search = frappe.ui.form.make_control({
221 df: {
222 "fieldtype": "Data",
223 "label": "Item",
224 "fieldname": "pos_item",
225 "placeholder": "Search Item"
226 },
227 parent: this.wrapper.find(".search-area"),
228 only_input: true,
229 });
230
231 this.search.make_input();
232 this.search.$input.on("keyup", function() {
233 setTimeout(function() {
234 me.items = me.get_items();
235 me.make_item_list();
236 }, 1000);
237 });
238
239 this.party_field = frappe.ui.form.make_control({
240 df: {
241 "fieldtype": "Data",
242 "options": this.party,
243 "label": this.party,
244 "fieldname": this.party.toLowerCase(),
245 "placeholder": this.party
246 },
247 parent: this.wrapper.find(".party-area"),
248 only_input: true,
249 });
250
251 this.party_field.make_input();
252 },
253
254 make_customer: function() {
255 var me = this;
256
257 if(this.customers.length == 1){
258 this.party_field.$input.val(this.customers[0].name);
259 this.frm.doc.customer = this.customers[0].name;
260 }
261
262 this.party_field.$input.autocomplete({
263 source: function (request, response) {
264 me.customer_data = me.get_customers(request.term)
265 response($.map(me.customer_data, function(data){
266 return {label: data.name, value: data.name,
267 customer_group: data.customer_group, territory: data.territory}
268 }))
269 },
270 change: function(event, ui){
271 if(ui.item){
272 me.frm.doc.customer = ui.item.label;
273 me.frm.doc.customer_name = ui.item.customer_name;
274 me.frm.doc.customer_group = ui.item.customer_group;
275 me.frm.doc.territory = ui.item.territory;
276 }else{
277 me.frm.doc.customer = me.party_field.$input.val();
278 }
279 me.refresh();
280 }
281 })
282 },
283
284 get_customers: function(key){
285 var me = this;
286 key = key.toLowerCase()
287 return $.grep(this.customers, function(data) {
288 if(data.name.toLowerCase().match(key) || data.customer_name.toLowerCase().match(key)
289 || data.customer_group.toLowerCase().match(key)){
290 return data
291 }
292 })
293 },
294
295 make_item_list: function() {
296 var me = this;
297 if(!this.price_list) {
298 msgprint(__("Price List not found or disabled"));
299 return;
300 }
301
302 me.item_timeout = null;
303
304 var $wrap = me.wrapper.find(".item-list");
305 me.wrapper.find(".item-list").empty();
306
307 if (this.items) {
308 $.each(this.items, function(index, obj) {
309 if(index < 16){
310 $(frappe.render_template("pos_item", {
311 item_code: obj.name,
312 item_price: format_currency(obj.price_list_rate, obj.currency),
313 item_name: obj.name===obj.item_name ? "" : obj.item_name,
314 item_image: obj.image ? "url('" + obj.image + "')" : null,
315 color: frappe.get_palette(obj.item_name),
316 abbr: frappe.get_abbr(obj.item_name)
317 })).tooltip().appendTo($wrap);
318 }
319 });
320 }
321
322 if(this.items.length == 1){
323 this.search.$input.val("");
324 this.add_to_cart();
325 }
326
327 // if form is local then allow this function
328 $(me.wrapper).find("div.pos-item").on("click", function() {
329 me.customer_validate();
330 if(me.frm.doc.docstatus==0) {
331 me.items = me.get_items($(this).attr("data-item-code"))
332 me.add_to_cart();
333 }
334 });
335 },
336
337 get_items: function(item_code){
338 // To search item as per the key enter
339
340 var me = this;
341 this.item_serial_no = {}
342
343 if(item_code){
344 return $.grep(window.items, function(item){
345 if(item.item_code == item_code ){
346 return true
347 }
348 })
349 }
350
351 key = this.search.$input.val().toLowerCase();
352
353 if(key){
354 return $.grep(window.items, function(item){
355 if( (item.item_code.toLowerCase().match(key)) ||
356 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key)) ){
357 return true
358 }else if(item.barcode){
359 return item.barcode == me.search.$input.val()
360 } else if (in_list(item.serial_nos, me.search.$input.val())){
361 me.item_serial_no[item.item_code] = me.search.$input.val()
362 return true
363 }
364 })
365 }else{
366 return window.items;
367 }
368 },
369
370 update_qty: function() {
371 var me = this;
372
373 $(this.wrapper).find(".pos-item-qty").on("change", function(){
374 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
375 me.update_qty_against_item_code(item_code, $(this).val());
376 })
377
378 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
379 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
380 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
381 me.update_qty_against_item_code(item_code, qty);
382 })
383
384 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
385 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
386 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
387 me.update_qty_against_item_code(item_code, qty);
388 })
389 },
390
391 update_qty_against_item_code: function(item_code, qty){
392 var me = this;
393 if(qty < 0){
394 frappe.throw(__("Quantity must be positive"));
395 }
396
397 this.remove_item = []
398 $.each(this.frm.doc["items"] || [], function(i, d) {
399 if (d.item_code == item_code) {
400 d.qty = flt(qty);
401 d.amount = flt(d.rate) * flt(d.qty);
402 if(d.qty==0){
403 me.remove_item.push(d.idx)
404 }
405 }
406 });
407 this.remove_zero_qty_item();
408 this.refresh();
409 },
410
411 remove_zero_qty_item: function(){
412 var me = this;
413 idx = 0
414 this.items = []
415 idx = 0
416 $.each(this.frm.doc["items"] || [], function(i, d) {
417 if(!in_list(me.remove_item, d.idx)){
418 d.idx = idx;
419 me.items.push(d);
420 idx++;
421 }
422 });
423
424 this.frm.doc["items"] = this.items;
425 },
426
427 make_discount_field: function(){
428 var me = this;
429
430 this.wrapper.find('input.discount-percentage').on("change", function() {
431 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
432 total = me.frm.doc.grand_total
433
434 if(me.frm.doc.apply_discount_on == 'Net Total'){
435 total = me.frm.doc.net_total
436 }
437
438 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
439 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
440 me.refresh();
441 });
442
443 this.wrapper.find('input.discount-amount').on("change", function() {
444 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
445 me.frm.doc.additional_discount_percentage = 0.0;
446 me.wrapper.find('input.discount-percentage').val(0);
447 me.refresh();
448 });
449 },
450
451 customer_validate: function(){
452 var me = this;
453
454 if(!this.frm.doc.customer){
455 frappe.throw(__("Please select customer"))
456 }
457 },
458
459 add_to_cart: function() {
460 var me = this;
461 var caught = false;
462 var no_of_items = me.wrapper.find(".pos-bill-item").length;
463
464 this.validate_serial_no()
465 this.validate_warehouse();
466
467 if (no_of_items != 0) {
468 $.each(this.frm.doc["items"] || [], function(i, d) {
469 if (d.item_code == me.items[0].item_code) {
470 caught = true;
471 d.qty += 1;
472 d.amount = flt(d.rate) * flt(d.qty)
473 if(me.item_serial_no.length){
474 d.serial_no += '\n' + me.item_serial_no[d.item_code]
475 }
476 }
477 });
478 }
479
480 // if item not found then add new item
481 if (!caught)
482 this.add_new_item_to_grid();
483
484 this.refresh();
485 },
486
487 add_new_item_to_grid: function() {
488 var me = this;
489 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
490 this.child.item_code = this.items[0].item_code;
491 this.child.item_name = this.items[0].item_name;
492 this.child.stock_uom = this.items[0].stock_uom;
493 this.child.description = this.items[0].description;
494 this.child.qty = 1;
495 this.child.item_group = this.items[0].item_group;
496 this.child.cost_center = this.items[0].cost_center;
497 this.child.income_account = this.items[0].income_account;
498 this.child.warehouse = this.items[0].default_warehouse;
499 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
500 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
501 this.child.actual_qty = this.items[0].actual_qty;
502 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
503 this.child.serial_no = this.item_serial_no[this.child.item_code];
504 },
505
506 refresh: function() {
507 var me = this;
508 this.refresh_fields();
509 this.update_qty();
510 this.set_primary_action();
511 },
512 refresh_fields: function() {
513 this.apply_pricing_rule();
514 this.discount_amount_applied = false;
515 this._calculate_taxes_and_totals();
516 this.calculate_discount_amount();
517 this.show_items_in_item_cart();
518 this.set_taxes();
519 this.calculate_outstanding_amount();
520 this.set_totals();
521 },
522
523 get_company_currency: function() {
524 return erpnext.get_currency(this.frm.doc.company);
525 },
526
527 show_item_wise_taxes: function(){
528 return null;
529 },
530
531 show_items_in_item_cart: function() {
532 var me = this;
533 var $items = this.wrapper.find(".items").empty();
534 me.frm.doc.net_total = 0.0
535 $.each(this.frm.doc.items|| [], function(i, d) {
536 $(frappe.render_template("pos_bill_item", {
537 item_code: d.item_code,
538 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
539 qty: d.qty,
540 actual_qty: d.actual_qty,
541 projected_qty: d.projected_qty,
542 rate: format_currency(d.rate, me.frm.doc.currency),
543 amount: format_currency(d.amount, me.frm.doc.currency)
544 })).appendTo($items);
545 });
546
547 this.wrapper.find("input.pos-item-qty").on("focus", function() {
548 $(this).select();
549 });
550 },
551
552 set_taxes: function(){
553 var me = this;
554 me.frm.doc.total_taxes_and_charges = 0.0
555
556 var taxes = this.frm.doc.taxes || [];
557 $(this.wrapper)
558 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
559 .find(".tax-table").empty();
560
561 $.each(taxes, function(i, d) {
562 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
563 $(frappe.render_template("pos_tax_row", {
564 description: d.description,
565 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
566 me.frm.doc.currency)
567 })).appendTo(me.wrapper.find(".tax-table"));
568 }
569 });
570 },
571
572 set_totals: function() {
573 var me = this;
574 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
575 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
576 },
577
578 set_primary_action: function() {
579 var me = this;
580
581 if (this.frm.doc.docstatus==0 && this.frm.doc.outstanding_amount > 0) {
582 this.page.set_primary_action(__("Pay"), function() {
583 me.validate()
584 me.create_invoice();
585 me.make_payment();
586 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530587 }else if(this.frm.doc.docstatus == 0 && this.name){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530588 this.page.set_primary_action(__("Submit"), function() {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530589 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530590 })
591 }else if(this.frm.doc.docstatus == 1){
592 this.page.set_primary_action(__("Print"), function() {
593 html = frappe.render(me.print_template, me.frm.doc)
594 frappe.require("/assets/js/print_format_v3.min.js", function() {
595 w = _p.preview(html);
596 setTimeout(function(){
597 w.print();
598 }, 1000)
599 });
600 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530601 }else {
602 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530603 }
604 },
605
606 write_off_amount: function(){
607 var me = this;
608 var value = 0.0;
609
610 if(this.frm.doc.outstanding_amount > 0){
611 dialog = new frappe.ui.Dialog({
612 title: 'Write Off Amount',
613 fields: [
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530614 {fieldtype: "Check", fieldname: "write_off_amount", label: __("Write of Outstanding Amount")},
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530615 ]
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530616 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530617
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530618 dialog.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530619
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530620 dialog.fields_dict.write_off_amount.$input.change(function(){
621 write_off_amount = dialog.get_values().write_off_amount
622 me.frm.doc.write_off_outstanding_amount_automatically = write_off_amount;
623 me.frm.doc.base_write_off_amount = (write_off_amount==1) ? flt(me.frm.doc.grand_total - me.frm.doc.paid_amount, precision("outstanding_amount")) : 0;
624 me.frm.doc.write_off_amount = flt(me.frm.doc.base_write_off_amount * me.frm.doc.conversion_rate, precision("write_off_amount"))
625 me.calculate_outstanding_amount();
626 })
627
628 dialog.set_primary_action(__("Submit"), function(){
629 dialog.hide()
630 me.submit_invoice()
631 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530632 }else{
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530633 this.submit_invoice()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530634 }
635 },
636
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530637 submit_invoice: function(){
638 var me = this;
639 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
640 me.change_status();
641 })
642 },
643
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530644 change_status: function(){
645 if(this.frm.doc.docstatus == 0){
646 this.frm.doc.docstatus = 1;
647 this.update_invoice();
648 this.disable_input_field();
649 }
650 },
651
652 disable_input_field: function(){
653 var pointer_events = 'inherit'
654 $(this.wrapper).find('input').attr("disabled", false);
655
656 if(this.frm.doc.docstatus == 1){
657 pointer_events = 'none'
658 $(this.wrapper).find('input').attr("disabled", true);
659 }
660
661 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
662 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
663 this.set_primary_action();
664 },
665
666 create_invoice: function(){
667 var me = this;
668 var invoice_data = {}
669 this.si_docs = this.get_doc_from_localstorage();
670 if(this.name){
671 this.update_invoice()
672 }else{
673 this.name = $.now();
674 invoice_data[this.name] = this.frm.doc
675 this.si_docs.push(invoice_data)
676 this.update_localstorage();
677 this.set_primary_action();
678 }
679 },
680
681 update_invoice: function(){
682 var me = this;
683 this.si_docs = this.get_doc_from_localstorage();
684 $.each(this.si_docs, function(index, data){
685 for(key in data){
686 if(key == me.name){
687 me.si_docs[index][key] = me.frm.doc
688 me.update_localstorage();
689 }
690 }
691 })
692 },
693
694 update_localstorage: function(){
695 try{
696 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
697 }catch(e){
698 frappe.throw(__("LocalStorage is full , did not save"))
699 }
700 },
701
702 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530703 try{
704 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
705 }catch(e){
706 return []
707 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 },
709
710 set_interval_for_si_sync: function(){
711 var me = this;
712 setInterval(function(){
713 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530714 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530715 },
716
717 sync_sales_invoice: function(){
718 var me = this;
719 this.si_docs = this.get_submitted_invoice()
720
721 if(this.connection_status && this.si_docs.length){
722 frappe.call({
723 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
724 args: {
725 doc_list: me.si_docs
726 },
727 callback: function(r){
728 if(r.message){
729 me.removed_items = r.message;
730 me.remove_doc_from_localstorage();
731 }
732 }
733 })
734 }
735 },
736
737 get_submitted_invoice: function(){
738 invoices = []
739 docs = this.get_doc_from_localstorage()
740 if(docs){
741 invoices = $.map(docs, function(data){
742 for(key in data){
743 if(data[key].docstatus == 1){
744 return data
745 }
746 }
747 });
748 }
749
750 return invoices
751 },
752
753 remove_doc_from_localstorage: function(){
754 var me = this;
755 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530756 this.new_si_docs = []
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530757 if(this.removed_items){
758 $.each(this.si_docs, function(index, data){
759 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530760 if(!in_list(me.removed_items, key)){
761 me.new_si_docs.push(data)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530762 }
763 }
764 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530765 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530766 this.update_localstorage();
767 }
768 },
769
770 validate: function(){
771 var me = this;
772 this.customer_validate();
773 this.item_validate();
774 },
775
776 item_validate: function(){
777 if(this.frm.doc.items.length == 0){
778 frappe.throw(__("Select items to save the invoice"))
779 }
780 },
781
782 validate_serial_no: function(){
783 var me = this;
784 var item_code = serial_no = '';
785 for (key in this.item_serial_no){
786 item_code = key;
787 serial_no = me.item_serial_no[key]
788 }
789
790 if(item_code && serial_no){
791 $.each(this.frm.doc.items, function(index, data){
792 if(data.item_code == item_code){
793 if(in_list(data.serial_no.split('\n'), serial_no)){
794 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
795 'serial_no': serial_no
796 })))
797 }
798 }
799 })
800 }
801 },
802
803 apply_pricing_rule: function(){
804 var me = this;
805 $.each(this.frm.doc["items"], function(n, item) {
806 pricing_rule = me.get_pricing_rule(item)
807 me.validate_pricing_rule(pricing_rule)
808 if(pricing_rule.length){
809 item.margin_type = pricing_rule[0].margin_type;
810 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
811 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
812 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
813 me.apply_pricing_rule_on_item(item)
814 }
815 })
816 },
817
818 get_pricing_rule: function(item){
819 var me = this;
820 return $.grep(this.pricing_rules, function(data){
821 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
822 if(in_list(['Customer', 'Customer Group', 'Territory'], data.applicable_for)){
823 return me.validate_condition(data)
824 }else{
825 return true
826 }
827 }
828 })
829 },
830
831 validate_condition: function(data){
832 //This method check condition based on applicable for
833 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
834 if(in_list(condition[1], condition[0])){
835 return true
836 }
837 },
838
839 get_mapper_for_pricing_rule: function(data){
840 return {
841 'Customer': [data.customer, [this.doc.customer]],
842 'Customer Group': [data.customer_group, [this.doc.customer_group, 'All Customer Groups']],
843 'Territory': [data.territory, [this.doc.territory, 'All Territories']],
844 }
845 },
846
847 validate_pricing_rule: function(pricing_rule){
848 //This method validate duplicate pricing rule
849 var pricing_rule_name = '';
850 var priority = 0;
851 var pricing_rule_list = [];
852 var priority_list = []
853
854 if(pricing_rule.length > 1){
855
856 $.each(pricing_rule, function(index, data){
857 pricing_rule_name += data.name + ','
858 priority_list.push(data.priority)
859 if(priority <= data.priority){
860 priority = data.priority
861 pricing_rule_list.push(data)
862 }
863 })
864
865 count = 0
866 $.each(priority_list, function(index, value){
867 if(value == priority){
868 count++
869 }
870 })
871
872 if(priority == 0 || count > 1){
873 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
874 'pricing_rule': pricing_rule_name
875 })))
876 }
877
878 return pricing_rule_list
879 }
880 },
881
882 validate_warehouse: function(){
883 if(!this.items[0].default_warehouse){
884 frappe.throw(__("Deafault warehouse is required for selected item"))
885 }
886 }
887})