blob: fcccfb462550edd059fd9e6343ddb0eb160bebe4 [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 Waghchaure6087fe12016-04-09 14:31:09 +053072 this.page.set_indicator("Offline", "grey")
73 frappe.call({
74 method:"frappe.handler.ping",
75 callback: function(r){
76 if(r.message){
77 me.connection_status = true;
78 me.page.set_indicator("Online", "green")
79 }
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 Waghchaure9fe40d52016-06-13 21:37:10 +0530107 me.load_data();
108 me.make_customer();
109 me.make_item_list();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530110 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530111 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530112
113 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530114 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530115 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530116 },
117
118 show_unsync_invoice_list: function(){
119 var me = this;
120 this.si_docs = this.get_doc_from_localstorage();
121
122 this.list_dialog = new frappe.ui.Dialog({
123 title: 'Invoice List'
124 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530125
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530126 this.list_dialog.show();
127 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530128 if(this.si_docs.length > 0){
129 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
130 <div class="col-xs-2">Sr</div>\
131 <div class="col-xs-4">Customer</div>\
132 <div class="col-xs-2 text-left">Status</div>\
133 <div class="col-xs-4 text-right">Grand Total</div>\
134 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530135
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530136 $.each(this.si_docs, function(index, data){
137 for(key in data) {
138 $(frappe.render_template("pos_invoice_list", {
139 sr: index + 1,
140 name: key,
141 customer: data[key].customer,
142 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
143 data: me.get_doctype_status(data[key])
144 })).appendTo($(me.list_body));
145 }
146 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530147
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530148 $(this.list_body).find('.list-row').click(function() {
149 me.name = $(this).attr('invoice-name')
150 doc_data = me.get_invoice_doc(me.si_docs)
151 if(doc_data){
152 me.frm.doc = doc_data[0][me.name];
153 me.set_missing_values();
154 me.refresh();
155 me.disable_input_field();
156 me.list_dialog.hide();
157 }
158 })
159 }else{
160 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
161 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530162 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530163
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530164 get_doctype_status: function(doc){
165 if(doc.outstanding_amount == 0){
166 return {status: "Paid", indicator: "green"}
167 }else if(doc.docstatus == 0){
168 return {status: "Draft", indicator: "red"}
169 }else if(doc.paid_amount >= 0){
170 return {status: "Unpaid", indicator: "orange"}
171 }
172 },
173
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530174 set_missing_values: function(){
175 var me = this;
176 doc = JSON.parse(localStorage.getItem('doc'))
177 if(this.frm.doc.payments.length == 0){
178 this.frm.doc.payments = doc.payments;
179 }
180 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530181
182 get_invoice_doc: function(si_docs){
183 var me = this;
184 this.si_docs = this.get_doc_from_localstorage();
185
186 return $.grep(this.si_docs, function(data){
187 for(key in data){
188 return key == me.name
189 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530190 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530191 },
192
193 get_data_from_server: function(callback){
194 var me = this;
195 frappe.call({
196 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
197 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530198 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530199 callback: function(r){
200 window.items = r.message.items;
201 window.customers = r.message.customers;
202 window.pricing_rules = r.message.pricing_rules;
203 window.meta = r.message.meta;
204 window.print_template = r.message.print_template;
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530205 me.default_customer = r.message.default_customer || null;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530206 me.write_off_account = r.message.write_off_account;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530207 localStorage.setItem('doc', JSON.stringify(r.message.doc));
208 if(callback){
209 callback();
210 }
211 }
212 })
213 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530214
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530215 save_previous_entry : function(){
216 if(this.frm.doc.items.length > 0){
217 this.create_invoice()
218 }
219 },
220
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530221 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530222 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530223 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530224 this.name = '';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530225 this.load_data();
226 this.setup();
227 },
228
229 load_data: function(){
230 this.items = window.items;
231 this.customers = window.customers;
232 this.pricing_rules = window.pricing_rules;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530233 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530234
235 $.each(window.meta, function(i, data){
236 frappe.meta.sync(data)
237 })
238
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530239 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530240 {content: window.print_template, title:"POS"})
241 },
242
243 setup: function(){
244 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
245 this.set_transaction_defaults("Customer");
246 this.make();
247 this.set_primary_action();
248 },
249
250 set_transaction_defaults: function(party) {
251 var me = this;
252 this.party = party;
253 this.price_list = (party == "Customer" ?
254 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
255 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
256 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
257 },
258
259 make: function() {
260 this.make_search();
261 this.make_customer();
262 this.make_item_list();
263 this.make_discount_field()
264 },
265
266 make_search: function() {
267 var me = this;
268 this.search = frappe.ui.form.make_control({
269 df: {
270 "fieldtype": "Data",
271 "label": "Item",
272 "fieldname": "pos_item",
273 "placeholder": "Search Item"
274 },
275 parent: this.wrapper.find(".search-area"),
276 only_input: true,
277 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530278
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 this.search.make_input();
280 this.search.$input.on("keyup", function() {
281 setTimeout(function() {
282 me.items = me.get_items();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530283 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 }, 1000);
285 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530286
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530287 this.party_field = frappe.ui.form.make_control({
288 df: {
289 "fieldtype": "Data",
290 "options": this.party,
291 "label": this.party,
292 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530293 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 },
295 parent: this.wrapper.find(".party-area"),
296 only_input: true,
297 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530298
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530299 this.party_field.make_input();
300 },
301
302 make_customer: function() {
303 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530304
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530305 if(this.default_customer){
306 this.party_field.$input.val(this.default_customer);
307 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530308 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530309
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530310 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530311 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530312 source: function (request, response) {
313 me.customer_data = me.get_customers(request.term)
314 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530315 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530316 customer_group: data.customer_group, territory: data.territory}
317 }))
318 },
319 change: function(event, ui){
320 if(ui.item){
321 me.frm.doc.customer = ui.item.label;
322 me.frm.doc.customer_name = ui.item.customer_name;
323 me.frm.doc.customer_group = ui.item.customer_group;
324 me.frm.doc.territory = ui.item.territory;
325 }else{
326 me.frm.doc.customer = me.party_field.$input.val();
327 }
328 me.refresh();
329 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530330 }).on("focus", function(){
331 setTimeout(function() {
332 if(!me.party_field.$input.val()) {
333 me.party_field.$input.autocomplete( "search", " " );
334 }
335 }, 500);
336 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530337 },
338
339 get_customers: function(key){
340 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530341 key = key.toLowerCase().trim()
342 if(key){
343 return $.grep(this.customers, function(data) {
344 if(data.name.toLowerCase().match(key)
345 || data.customer_name.toLowerCase().match(key)
346 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
347 return data
348 }
349 })
350 }else{
351 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
352 return customers.slice(0, 20)
353 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530354 },
355
356 make_item_list: function() {
357 var me = this;
358 if(!this.price_list) {
359 msgprint(__("Price List not found or disabled"));
360 return;
361 }
362
363 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530364
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530365 var $wrap = me.wrapper.find(".item-list");
366 me.wrapper.find(".item-list").empty();
367
368 if (this.items) {
369 $.each(this.items, function(index, obj) {
370 if(index < 16){
371 $(frappe.render_template("pos_item", {
372 item_code: obj.name,
373 item_price: format_currency(obj.price_list_rate, obj.currency),
374 item_name: obj.name===obj.item_name ? "" : obj.item_name,
375 item_image: obj.image ? "url('" + obj.image + "')" : null,
376 color: frappe.get_palette(obj.item_name),
377 abbr: frappe.get_abbr(obj.item_name)
378 })).tooltip().appendTo($wrap);
379 }
380 });
381 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530382
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530383 if(this.items.length == 1
384 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530385 this.search.$input.val("");
386 this.add_to_cart();
387 }
388
389 // if form is local then allow this function
390 $(me.wrapper).find("div.pos-item").on("click", function() {
391 me.customer_validate();
392 if(me.frm.doc.docstatus==0) {
393 me.items = me.get_items($(this).attr("data-item-code"))
394 me.add_to_cart();
395 }
396 });
397 },
398
399 get_items: function(item_code){
400 // To search item as per the key enter
401
402 var me = this;
403 this.item_serial_no = {}
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530404
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530405 if(item_code){
406 return $.grep(window.items, function(item){
407 if(item.item_code == item_code ){
408 return true
409 }
410 })
411 }
412
413 key = this.search.$input.val().toLowerCase();
414
415 if(key){
416 return $.grep(window.items, function(item){
417 if( (item.item_code.toLowerCase().match(key)) ||
418 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key)) ){
419 return true
420 }else if(item.barcode){
421 return item.barcode == me.search.$input.val()
422 } else if (in_list(item.serial_nos, me.search.$input.val())){
423 me.item_serial_no[item.item_code] = me.search.$input.val()
424 return true
425 }
426 })
427 }else{
428 return window.items;
429 }
430 },
431
432 update_qty: function() {
433 var me = this;
434
435 $(this.wrapper).find(".pos-item-qty").on("change", function(){
436 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530437 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530438 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530439
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530440 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
441 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
442 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530443 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530444 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530445
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530446 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
447 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
448 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530449 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530450 })
451 },
452
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530453 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530454 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530455
456 $(this.wrapper).find(".pos-item-rate").on("change", function(){
457 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
458 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
459 })
460 },
461
462 update_qty_rate_against_item_code: function(item_code, field, value){
463 var me = this;
464 if(value < 0){
465 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530466 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530467
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530468 this.remove_item = []
469 $.each(this.frm.doc["items"] || [], function(i, d) {
470 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530471 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530472 d.amount = flt(d.rate) * flt(d.qty);
473 if(d.qty==0){
474 me.remove_item.push(d.idx)
475 }
476 }
477 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530478
479 if(field == 'qty'){
480 this.remove_zero_qty_item();
481 }
482
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530483 this.refresh();
484 },
485
486 remove_zero_qty_item: function(){
487 var me = this;
488 idx = 0
489 this.items = []
490 idx = 0
491 $.each(this.frm.doc["items"] || [], function(i, d) {
492 if(!in_list(me.remove_item, d.idx)){
493 d.idx = idx;
494 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530495 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530496 }
497 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530498
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530499 this.frm.doc["items"] = this.items;
500 },
501
502 make_discount_field: function(){
503 var me = this;
504
505 this.wrapper.find('input.discount-percentage').on("change", function() {
506 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
507 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530508
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530509 if(me.frm.doc.apply_discount_on == 'Net Total'){
510 total = me.frm.doc.net_total
511 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530512
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530513 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
514 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
515 me.refresh();
516 });
517
518 this.wrapper.find('input.discount-amount').on("change", function() {
519 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
520 me.frm.doc.additional_discount_percentage = 0.0;
521 me.wrapper.find('input.discount-percentage').val(0);
522 me.refresh();
523 });
524 },
525
526 customer_validate: function(){
527 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530528
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530529 if(!this.frm.doc.customer){
530 frappe.throw(__("Please select customer"))
531 }
532 },
533
534 add_to_cart: function() {
535 var me = this;
536 var caught = false;
537 var no_of_items = me.wrapper.find(".pos-bill-item").length;
538
539 this.validate_serial_no()
540 this.validate_warehouse();
541
542 if (no_of_items != 0) {
543 $.each(this.frm.doc["items"] || [], function(i, d) {
544 if (d.item_code == me.items[0].item_code) {
545 caught = true;
546 d.qty += 1;
547 d.amount = flt(d.rate) * flt(d.qty)
548 if(me.item_serial_no.length){
549 d.serial_no += '\n' + me.item_serial_no[d.item_code]
550 }
551 }
552 });
553 }
554
555 // if item not found then add new item
556 if (!caught)
557 this.add_new_item_to_grid();
558
559 this.refresh();
560 },
561
562 add_new_item_to_grid: function() {
563 var me = this;
564 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
565 this.child.item_code = this.items[0].item_code;
566 this.child.item_name = this.items[0].item_name;
567 this.child.stock_uom = this.items[0].stock_uom;
568 this.child.description = this.items[0].description;
569 this.child.qty = 1;
570 this.child.item_group = this.items[0].item_group;
571 this.child.cost_center = this.items[0].cost_center;
572 this.child.income_account = this.items[0].income_account;
573 this.child.warehouse = this.items[0].default_warehouse;
574 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
575 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
576 this.child.actual_qty = this.items[0].actual_qty;
577 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530578 this.child.serial_no = this.item_serial_no[this.child.item_code];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530579 },
580
581 refresh: function() {
582 var me = this;
583 this.refresh_fields();
584 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530585 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530586 this.set_primary_action();
587 },
588 refresh_fields: function() {
589 this.apply_pricing_rule();
590 this.discount_amount_applied = false;
591 this._calculate_taxes_and_totals();
592 this.calculate_discount_amount();
593 this.show_items_in_item_cart();
594 this.set_taxes();
595 this.calculate_outstanding_amount();
596 this.set_totals();
597 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530598
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530599 get_company_currency: function() {
600 return erpnext.get_currency(this.frm.doc.company);
601 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530602
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530603 show_item_wise_taxes: function(){
604 return null;
605 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530606
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530607 show_items_in_item_cart: function() {
608 var me = this;
609 var $items = this.wrapper.find(".items").empty();
610 me.frm.doc.net_total = 0.0
611 $.each(this.frm.doc.items|| [], function(i, d) {
612 $(frappe.render_template("pos_bill_item", {
613 item_code: d.item_code,
614 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
615 qty: d.qty,
616 actual_qty: d.actual_qty,
617 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530618 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530619 amount: format_currency(d.amount, me.frm.doc.currency)
620 })).appendTo($items);
621 });
622
623 this.wrapper.find("input.pos-item-qty").on("focus", function() {
624 $(this).select();
625 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530626
627 this.wrapper.find("input.pos-item-rate").on("focus", function() {
628 $(this).select();
629 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530630 },
631
632 set_taxes: function(){
633 var me = this;
634 me.frm.doc.total_taxes_and_charges = 0.0
635
636 var taxes = this.frm.doc.taxes || [];
637 $(this.wrapper)
638 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
639 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530640
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 $.each(taxes, function(i, d) {
642 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
643 $(frappe.render_template("pos_tax_row", {
644 description: d.description,
645 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
646 me.frm.doc.currency)
647 })).appendTo(me.wrapper.find(".tax-table"));
648 }
649 });
650 },
651
652 set_totals: function() {
653 var me = this;
654 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
655 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
656 },
657
658 set_primary_action: function() {
659 var me = this;
660
661 if (this.frm.doc.docstatus==0 && this.frm.doc.outstanding_amount > 0) {
662 this.page.set_primary_action(__("Pay"), function() {
663 me.validate()
664 me.create_invoice();
665 me.make_payment();
666 });
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530667 }else if(this.frm.doc.docstatus == 0 && this.frm.doc.items.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530668 this.page.set_primary_action(__("Submit"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530669 me.validate()
670 me.create_invoice();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530671 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530672 })
673 }else if(this.frm.doc.docstatus == 1){
674 this.page.set_primary_action(__("Print"), function() {
675 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530676 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530677 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530678 }else {
679 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530680 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530681
682 this.page.set_secondary_action(__("New"), function() {
683 me.save_previous_entry();
684 me.create_new();
Saurabh9a6c6662016-06-27 16:51:44 +0530685 }).addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530686 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530687
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530688 print_document: function(html){
689 var w = window.open();
690 w.document.write(html);
691 w.document.close();
692 setTimeout(function(){
693 w.print();
694 w.close();
695 }, 1000)
696 },
697
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530698 write_off_amount: function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530699 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530700 var value = 0.0;
701
702 if(this.frm.doc.outstanding_amount > 0){
703 dialog = new frappe.ui.Dialog({
704 title: 'Write Off Amount',
705 fields: [
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530706 {fieldtype: "Check", fieldname: "write_off_amount", label: __("Write off Outstanding Amount")},
707 {fieldtype: "Link", options:"Account", default:this.write_off_account, fieldname: "write_off_account",
708 label: __("Write off Account"), get_query: function() {
709 return {
710 filters: {'is_group': 0, 'report_type': 'Profit and Loss'}
711 }
712 }}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530713 ]
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530714 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530715
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530716 dialog.show();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530717
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530718 dialog.fields_dict.write_off_amount.$input.change(function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530719 write_off_amount = dialog.get_values().write_off_amount;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530720 me.frm.doc.write_off_outstanding_amount_automatically = write_off_amount;
721 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;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530722 me.frm.doc.write_off_account = (write_off_amount==1) ? dialog.get_values().write_off_account : '';
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530723 me.frm.doc.write_off_amount = flt(me.frm.doc.base_write_off_amount * me.frm.doc.conversion_rate, precision("write_off_amount"))
724 me.calculate_outstanding_amount();
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530725 me.set_primary_action();
726 })
727
728 dialog.fields_dict.write_off_account.$input.change(function(){
729 me.frm.doc.write_off_account = dialog.get_values().write_off_account;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530730 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530731
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530732 dialog.set_primary_action(__("Submit"), function(){
733 dialog.hide()
734 me.submit_invoice()
735 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530736 }else{
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530737 this.submit_invoice()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530738 }
739 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530740
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530741 submit_invoice: function(){
742 var me = this;
743 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
744 me.change_status();
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530745 frappe.msgprint(__("Sales invoice submitted sucessfully."))
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530746 })
747 },
748
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530749 change_status: function(){
750 if(this.frm.doc.docstatus == 0){
751 this.frm.doc.docstatus = 1;
752 this.update_invoice();
753 this.disable_input_field();
754 }
755 },
756
757 disable_input_field: function(){
758 var pointer_events = 'inherit'
759 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530760
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530761 if(this.frm.doc.docstatus == 1){
762 pointer_events = 'none'
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530763 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530764 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530765
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530766 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
767 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
768 this.set_primary_action();
769 },
770
771 create_invoice: function(){
772 var me = this;
773 var invoice_data = {}
774 this.si_docs = this.get_doc_from_localstorage();
775 if(this.name){
776 this.update_invoice()
777 }else{
778 this.name = $.now();
779 invoice_data[this.name] = this.frm.doc
780 this.si_docs.push(invoice_data)
781 this.update_localstorage();
782 this.set_primary_action();
783 }
784 },
785
786 update_invoice: function(){
787 var me = this;
788 this.si_docs = this.get_doc_from_localstorage();
789 $.each(this.si_docs, function(index, data){
790 for(key in data){
791 if(key == me.name){
792 me.si_docs[index][key] = me.frm.doc
793 me.update_localstorage();
794 }
795 }
796 })
797 },
798
799 update_localstorage: function(){
800 try{
801 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
802 }catch(e){
803 frappe.throw(__("LocalStorage is full , did not save"))
804 }
805 },
806
807 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530808 try{
809 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
810 }catch(e){
811 return []
812 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530813 },
814
815 set_interval_for_si_sync: function(){
816 var me = this;
817 setInterval(function(){
818 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530819 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530820 },
821
822 sync_sales_invoice: function(){
823 var me = this;
824 this.si_docs = this.get_submitted_invoice()
825
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530826 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530827 frappe.call({
828 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
829 args: {
830 doc_list: me.si_docs
831 },
832 callback: function(r){
833 if(r.message){
834 me.removed_items = r.message;
835 me.remove_doc_from_localstorage();
836 }
837 }
838 })
839 }
840 },
841
842 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530843 var invoices = [];
844 var index = 1;
845 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530846 if(docs){
847 invoices = $.map(docs, function(data){
848 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530849 if(data[key].docstatus == 1 && index < 50){
850 index++
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530851 return data
852 }
853 }
854 });
855 }
856
857 return invoices
858 },
859
860 remove_doc_from_localstorage: function(){
861 var me = this;
862 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530863 this.new_si_docs = []
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530864 if(this.removed_items){
865 $.each(this.si_docs, function(index, data){
866 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530867 if(!in_list(me.removed_items, key)){
868 me.new_si_docs.push(data)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530869 }
870 }
871 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530872 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530873 this.update_localstorage();
874 }
875 },
876
877 validate: function(){
878 var me = this;
879 this.customer_validate();
880 this.item_validate();
Saurabh9a6c6662016-06-27 16:51:44 +0530881 this.validate_mode_of_payments()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530882 },
883
884 item_validate: function(){
885 if(this.frm.doc.items.length == 0){
886 frappe.throw(__("Select items to save the invoice"))
887 }
888 },
Saurabh9a6c6662016-06-27 16:51:44 +0530889
890 validate_mode_of_payments: function(){
891 if (this.frm.doc.payments.length === 0){
892 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
893 }
894 },
895
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530896 validate_serial_no: function(){
897 var me = this;
898 var item_code = serial_no = '';
899 for (key in this.item_serial_no){
900 item_code = key;
901 serial_no = me.item_serial_no[key]
902 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530903
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530904 if(item_code && serial_no){
905 $.each(this.frm.doc.items, function(index, data){
906 if(data.item_code == item_code){
907 if(in_list(data.serial_no.split('\n'), serial_no)){
908 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
909 'serial_no': serial_no
910 })))
911 }
912 }
913 })
914 }
915 },
916
917 apply_pricing_rule: function(){
918 var me = this;
919 $.each(this.frm.doc["items"], function(n, item) {
920 pricing_rule = me.get_pricing_rule(item)
921 me.validate_pricing_rule(pricing_rule)
922 if(pricing_rule.length){
923 item.margin_type = pricing_rule[0].margin_type;
924 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
925 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
926 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
927 me.apply_pricing_rule_on_item(item)
928 }
929 })
930 },
931
932 get_pricing_rule: function(item){
933 var me = this;
934 return $.grep(this.pricing_rules, function(data){
935 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
936 if(in_list(['Customer', 'Customer Group', 'Territory'], data.applicable_for)){
937 return me.validate_condition(data)
938 }else{
939 return true
940 }
941 }
942 })
943 },
944
945 validate_condition: function(data){
946 //This method check condition based on applicable for
947 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
948 if(in_list(condition[1], condition[0])){
949 return true
950 }
951 },
952
953 get_mapper_for_pricing_rule: function(data){
954 return {
955 'Customer': [data.customer, [this.doc.customer]],
956 'Customer Group': [data.customer_group, [this.doc.customer_group, 'All Customer Groups']],
957 'Territory': [data.territory, [this.doc.territory, 'All Territories']],
958 }
959 },
960
961 validate_pricing_rule: function(pricing_rule){
962 //This method validate duplicate pricing rule
963 var pricing_rule_name = '';
964 var priority = 0;
965 var pricing_rule_list = [];
966 var priority_list = []
967
968 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530969
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530970 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530971 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530972 priority_list.push(data.priority)
973 if(priority <= data.priority){
974 priority = data.priority
975 pricing_rule_list.push(data)
976 }
977 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530978
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530979 count = 0
980 $.each(priority_list, function(index, value){
981 if(value == priority){
982 count++
983 }
984 })
985
986 if(priority == 0 || count > 1){
987 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
988 'pricing_rule': pricing_rule_name
989 })))
990 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530991
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530992 return pricing_rule_list
993 }
994 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530995
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530996 validate_warehouse: function(){
997 if(!this.items[0].default_warehouse){
998 frappe.throw(__("Deafault warehouse is required for selected item"))
999 }
1000 }
1001})