blob: f3c8c33ca694ac6970703981de05b7c433ceac70 [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 Waghchaure9fe40d52016-06-13 21:37:10 +0530205 me.write_off_account = r.message.write_off_account;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530206 localStorage.setItem('doc', JSON.stringify(r.message.doc));
207 if(callback){
208 callback();
209 }
210 }
211 })
212 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530213
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530214 save_previous_entry : function(){
215 if(this.frm.doc.items.length > 0){
216 this.create_invoice()
217 }
218 },
219
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530220 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530221 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530222 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530223 this.name = '';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530224 this.load_data();
225 this.setup();
226 },
227
228 load_data: function(){
229 this.items = window.items;
230 this.customers = window.customers;
231 this.pricing_rules = window.pricing_rules;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530232 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530233
234 $.each(window.meta, function(i, data){
235 frappe.meta.sync(data)
236 })
237
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530238 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530239 {content: window.print_template, title:"POS"})
240 },
241
242 setup: function(){
243 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
244 this.set_transaction_defaults("Customer");
245 this.make();
246 this.set_primary_action();
247 },
248
249 set_transaction_defaults: function(party) {
250 var me = this;
251 this.party = party;
252 this.price_list = (party == "Customer" ?
253 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
254 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
255 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
256 },
257
258 make: function() {
259 this.make_search();
260 this.make_customer();
261 this.make_item_list();
262 this.make_discount_field()
263 },
264
265 make_search: function() {
266 var me = this;
267 this.search = frappe.ui.form.make_control({
268 df: {
269 "fieldtype": "Data",
270 "label": "Item",
271 "fieldname": "pos_item",
272 "placeholder": "Search Item"
273 },
274 parent: this.wrapper.find(".search-area"),
275 only_input: true,
276 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530277
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530278 this.search.make_input();
279 this.search.$input.on("keyup", function() {
280 setTimeout(function() {
281 me.items = me.get_items();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530282 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530283 }, 1000);
284 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530285
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 this.party_field = frappe.ui.form.make_control({
287 df: {
288 "fieldtype": "Data",
289 "options": this.party,
290 "label": this.party,
291 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530292 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530293 },
294 parent: this.wrapper.find(".party-area"),
295 only_input: true,
296 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530297
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 this.party_field.make_input();
299 },
300
301 make_customer: function() {
302 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530303
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530304 if(this.customers.length == 1){
305 this.party_field.$input.val(this.customers[0].name);
306 this.frm.doc.customer = this.customers[0].name;
307 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530308
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530309 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530310 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530311 source: function (request, response) {
312 me.customer_data = me.get_customers(request.term)
313 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530314 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530315 customer_group: data.customer_group, territory: data.territory}
316 }))
317 },
318 change: function(event, ui){
319 if(ui.item){
320 me.frm.doc.customer = ui.item.label;
321 me.frm.doc.customer_name = ui.item.customer_name;
322 me.frm.doc.customer_group = ui.item.customer_group;
323 me.frm.doc.territory = ui.item.territory;
324 }else{
325 me.frm.doc.customer = me.party_field.$input.val();
326 }
327 me.refresh();
328 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530329 }).on("focus", function(){
330 setTimeout(function() {
331 if(!me.party_field.$input.val()) {
332 me.party_field.$input.autocomplete( "search", " " );
333 }
334 }, 500);
335 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530336 },
337
338 get_customers: function(key){
339 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530340 key = key.toLowerCase().trim()
341 if(key){
342 return $.grep(this.customers, function(data) {
343 if(data.name.toLowerCase().match(key)
344 || data.customer_name.toLowerCase().match(key)
345 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
346 return data
347 }
348 })
349 }else{
350 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
351 return customers.slice(0, 20)
352 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 },
354
355 make_item_list: function() {
356 var me = this;
357 if(!this.price_list) {
358 msgprint(__("Price List not found or disabled"));
359 return;
360 }
361
362 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530363
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364 var $wrap = me.wrapper.find(".item-list");
365 me.wrapper.find(".item-list").empty();
366
367 if (this.items) {
368 $.each(this.items, function(index, obj) {
369 if(index < 16){
370 $(frappe.render_template("pos_item", {
371 item_code: obj.name,
372 item_price: format_currency(obj.price_list_rate, obj.currency),
373 item_name: obj.name===obj.item_name ? "" : obj.item_name,
374 item_image: obj.image ? "url('" + obj.image + "')" : null,
375 color: frappe.get_palette(obj.item_name),
376 abbr: frappe.get_abbr(obj.item_name)
377 })).tooltip().appendTo($wrap);
378 }
379 });
380 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530381
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530382 if(this.items.length == 1
383 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530384 this.search.$input.val("");
385 this.add_to_cart();
386 }
387
388 // if form is local then allow this function
389 $(me.wrapper).find("div.pos-item").on("click", function() {
390 me.customer_validate();
391 if(me.frm.doc.docstatus==0) {
392 me.items = me.get_items($(this).attr("data-item-code"))
393 me.add_to_cart();
394 }
395 });
396 },
397
398 get_items: function(item_code){
399 // To search item as per the key enter
400
401 var me = this;
402 this.item_serial_no = {}
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530403
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530404 if(item_code){
405 return $.grep(window.items, function(item){
406 if(item.item_code == item_code ){
407 return true
408 }
409 })
410 }
411
412 key = this.search.$input.val().toLowerCase();
413
414 if(key){
415 return $.grep(window.items, function(item){
416 if( (item.item_code.toLowerCase().match(key)) ||
417 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key)) ){
418 return true
419 }else if(item.barcode){
420 return item.barcode == me.search.$input.val()
421 } else if (in_list(item.serial_nos, me.search.$input.val())){
422 me.item_serial_no[item.item_code] = me.search.$input.val()
423 return true
424 }
425 })
426 }else{
427 return window.items;
428 }
429 },
430
431 update_qty: function() {
432 var me = this;
433
434 $(this.wrapper).find(".pos-item-qty").on("change", function(){
435 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530436 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530437 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530438
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530439 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
440 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
441 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530442 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530443 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530444
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530445 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
446 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
447 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530448 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530449 })
450 },
451
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530452 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530453 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530454
455 $(this.wrapper).find(".pos-item-rate").on("change", function(){
456 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
457 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
458 })
459 },
460
461 update_qty_rate_against_item_code: function(item_code, field, value){
462 var me = this;
463 if(value < 0){
464 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530465 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530466
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530467 this.remove_item = []
468 $.each(this.frm.doc["items"] || [], function(i, d) {
469 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530470 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 d.amount = flt(d.rate) * flt(d.qty);
472 if(d.qty==0){
473 me.remove_item.push(d.idx)
474 }
475 }
476 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530477
478 if(field == 'qty'){
479 this.remove_zero_qty_item();
480 }
481
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530482 this.refresh();
483 },
484
485 remove_zero_qty_item: function(){
486 var me = this;
487 idx = 0
488 this.items = []
489 idx = 0
490 $.each(this.frm.doc["items"] || [], function(i, d) {
491 if(!in_list(me.remove_item, d.idx)){
492 d.idx = idx;
493 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530494 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530495 }
496 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530497
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530498 this.frm.doc["items"] = this.items;
499 },
500
501 make_discount_field: function(){
502 var me = this;
503
504 this.wrapper.find('input.discount-percentage').on("change", function() {
505 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
506 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530507
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530508 if(me.frm.doc.apply_discount_on == 'Net Total'){
509 total = me.frm.doc.net_total
510 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530511
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530512 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
513 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
514 me.refresh();
515 });
516
517 this.wrapper.find('input.discount-amount').on("change", function() {
518 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
519 me.frm.doc.additional_discount_percentage = 0.0;
520 me.wrapper.find('input.discount-percentage').val(0);
521 me.refresh();
522 });
523 },
524
525 customer_validate: function(){
526 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530527
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530528 if(!this.frm.doc.customer){
529 frappe.throw(__("Please select customer"))
530 }
531 },
532
533 add_to_cart: function() {
534 var me = this;
535 var caught = false;
536 var no_of_items = me.wrapper.find(".pos-bill-item").length;
537
538 this.validate_serial_no()
539 this.validate_warehouse();
540
541 if (no_of_items != 0) {
542 $.each(this.frm.doc["items"] || [], function(i, d) {
543 if (d.item_code == me.items[0].item_code) {
544 caught = true;
545 d.qty += 1;
546 d.amount = flt(d.rate) * flt(d.qty)
547 if(me.item_serial_no.length){
548 d.serial_no += '\n' + me.item_serial_no[d.item_code]
549 }
550 }
551 });
552 }
553
554 // if item not found then add new item
555 if (!caught)
556 this.add_new_item_to_grid();
557
558 this.refresh();
559 },
560
561 add_new_item_to_grid: function() {
562 var me = this;
563 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
564 this.child.item_code = this.items[0].item_code;
565 this.child.item_name = this.items[0].item_name;
566 this.child.stock_uom = this.items[0].stock_uom;
567 this.child.description = this.items[0].description;
568 this.child.qty = 1;
569 this.child.item_group = this.items[0].item_group;
570 this.child.cost_center = this.items[0].cost_center;
571 this.child.income_account = this.items[0].income_account;
572 this.child.warehouse = this.items[0].default_warehouse;
573 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
574 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
575 this.child.actual_qty = this.items[0].actual_qty;
576 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530577 this.child.serial_no = this.item_serial_no[this.child.item_code];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530578 },
579
580 refresh: function() {
581 var me = this;
582 this.refresh_fields();
583 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530584 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530585 this.set_primary_action();
586 },
587 refresh_fields: function() {
588 this.apply_pricing_rule();
589 this.discount_amount_applied = false;
590 this._calculate_taxes_and_totals();
591 this.calculate_discount_amount();
592 this.show_items_in_item_cart();
593 this.set_taxes();
594 this.calculate_outstanding_amount();
595 this.set_totals();
596 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530597
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530598 get_company_currency: function() {
599 return erpnext.get_currency(this.frm.doc.company);
600 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530601
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530602 show_item_wise_taxes: function(){
603 return null;
604 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530605
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530606 show_items_in_item_cart: function() {
607 var me = this;
608 var $items = this.wrapper.find(".items").empty();
609 me.frm.doc.net_total = 0.0
610 $.each(this.frm.doc.items|| [], function(i, d) {
611 $(frappe.render_template("pos_bill_item", {
612 item_code: d.item_code,
613 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
614 qty: d.qty,
615 actual_qty: d.actual_qty,
616 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530617 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530618 amount: format_currency(d.amount, me.frm.doc.currency)
619 })).appendTo($items);
620 });
621
622 this.wrapper.find("input.pos-item-qty").on("focus", function() {
623 $(this).select();
624 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530625
626 this.wrapper.find("input.pos-item-rate").on("focus", function() {
627 $(this).select();
628 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530629 },
630
631 set_taxes: function(){
632 var me = this;
633 me.frm.doc.total_taxes_and_charges = 0.0
634
635 var taxes = this.frm.doc.taxes || [];
636 $(this.wrapper)
637 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
638 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530639
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530640 $.each(taxes, function(i, d) {
641 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
642 $(frappe.render_template("pos_tax_row", {
643 description: d.description,
644 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
645 me.frm.doc.currency)
646 })).appendTo(me.wrapper.find(".tax-table"));
647 }
648 });
649 },
650
651 set_totals: function() {
652 var me = this;
653 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
654 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
655 },
656
657 set_primary_action: function() {
658 var me = this;
659
660 if (this.frm.doc.docstatus==0 && this.frm.doc.outstanding_amount > 0) {
661 this.page.set_primary_action(__("Pay"), function() {
662 me.validate()
663 me.create_invoice();
664 me.make_payment();
665 });
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530666 }else if(this.frm.doc.docstatus == 0 && this.frm.doc.items.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530667 this.page.set_primary_action(__("Submit"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530668 me.validate()
669 me.create_invoice();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530670 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530671 })
672 }else if(this.frm.doc.docstatus == 1){
673 this.page.set_primary_action(__("Print"), function() {
674 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530675 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530676 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530677 }else {
678 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530679 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530680
681 this.page.set_secondary_action(__("New"), function() {
682 me.save_previous_entry();
683 me.create_new();
684 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530685 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530686
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530687 print_document: function(html){
688 var w = window.open();
689 w.document.write(html);
690 w.document.close();
691 setTimeout(function(){
692 w.print();
693 w.close();
694 }, 1000)
695 },
696
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530697 write_off_amount: function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530698 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530699 var value = 0.0;
700
701 if(this.frm.doc.outstanding_amount > 0){
702 dialog = new frappe.ui.Dialog({
703 title: 'Write Off Amount',
704 fields: [
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530705 {fieldtype: "Check", fieldname: "write_off_amount", label: __("Write off Outstanding Amount")},
706 {fieldtype: "Link", options:"Account", default:this.write_off_account, fieldname: "write_off_account",
707 label: __("Write off Account"), get_query: function() {
708 return {
709 filters: {'is_group': 0, 'report_type': 'Profit and Loss'}
710 }
711 }}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530712 ]
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530713 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530714
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530715 dialog.show();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530716
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530717 dialog.fields_dict.write_off_amount.$input.change(function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530718 write_off_amount = dialog.get_values().write_off_amount;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530719 me.frm.doc.write_off_outstanding_amount_automatically = write_off_amount;
720 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 +0530721 me.frm.doc.write_off_account = (write_off_amount==1) ? dialog.get_values().write_off_account : '';
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530722 me.frm.doc.write_off_amount = flt(me.frm.doc.base_write_off_amount * me.frm.doc.conversion_rate, precision("write_off_amount"))
723 me.calculate_outstanding_amount();
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530724 me.set_primary_action();
725 })
726
727 dialog.fields_dict.write_off_account.$input.change(function(){
728 me.frm.doc.write_off_account = dialog.get_values().write_off_account;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530729 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530730
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530731 dialog.set_primary_action(__("Submit"), function(){
732 dialog.hide()
733 me.submit_invoice()
734 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530735 }else{
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530736 this.submit_invoice()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530737 }
738 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530739
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();
744 })
745 },
746
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530747 change_status: function(){
748 if(this.frm.doc.docstatus == 0){
749 this.frm.doc.docstatus = 1;
750 this.update_invoice();
751 this.disable_input_field();
752 }
753 },
754
755 disable_input_field: function(){
756 var pointer_events = 'inherit'
757 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530758
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530759 if(this.frm.doc.docstatus == 1){
760 pointer_events = 'none'
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530761 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530762 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530763
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530764 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
765 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
766 this.set_primary_action();
767 },
768
769 create_invoice: function(){
770 var me = this;
771 var invoice_data = {}
772 this.si_docs = this.get_doc_from_localstorage();
773 if(this.name){
774 this.update_invoice()
775 }else{
776 this.name = $.now();
777 invoice_data[this.name] = this.frm.doc
778 this.si_docs.push(invoice_data)
779 this.update_localstorage();
780 this.set_primary_action();
781 }
782 },
783
784 update_invoice: function(){
785 var me = this;
786 this.si_docs = this.get_doc_from_localstorage();
787 $.each(this.si_docs, function(index, data){
788 for(key in data){
789 if(key == me.name){
790 me.si_docs[index][key] = me.frm.doc
791 me.update_localstorage();
792 }
793 }
794 })
795 },
796
797 update_localstorage: function(){
798 try{
799 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
800 }catch(e){
801 frappe.throw(__("LocalStorage is full , did not save"))
802 }
803 },
804
805 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530806 try{
807 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
808 }catch(e){
809 return []
810 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530811 },
812
813 set_interval_for_si_sync: function(){
814 var me = this;
815 setInterval(function(){
816 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530817 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530818 },
819
820 sync_sales_invoice: function(){
821 var me = this;
822 this.si_docs = this.get_submitted_invoice()
823
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530824 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530825 frappe.call({
826 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
827 args: {
828 doc_list: me.si_docs
829 },
830 callback: function(r){
831 if(r.message){
832 me.removed_items = r.message;
833 me.remove_doc_from_localstorage();
834 }
835 }
836 })
837 }
838 },
839
840 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530841 var invoices = [];
842 var index = 1;
843 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530844 if(docs){
845 invoices = $.map(docs, function(data){
846 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530847 if(data[key].docstatus == 1 && index < 50){
848 index++
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530849 return data
850 }
851 }
852 });
853 }
854
855 return invoices
856 },
857
858 remove_doc_from_localstorage: function(){
859 var me = this;
860 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530861 this.new_si_docs = []
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530862 if(this.removed_items){
863 $.each(this.si_docs, function(index, data){
864 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530865 if(!in_list(me.removed_items, key)){
866 me.new_si_docs.push(data)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530867 }
868 }
869 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530870 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530871 this.update_localstorage();
872 }
873 },
874
875 validate: function(){
876 var me = this;
877 this.customer_validate();
878 this.item_validate();
879 },
880
881 item_validate: function(){
882 if(this.frm.doc.items.length == 0){
883 frappe.throw(__("Select items to save the invoice"))
884 }
885 },
886
887 validate_serial_no: function(){
888 var me = this;
889 var item_code = serial_no = '';
890 for (key in this.item_serial_no){
891 item_code = key;
892 serial_no = me.item_serial_no[key]
893 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530894
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530895 if(item_code && serial_no){
896 $.each(this.frm.doc.items, function(index, data){
897 if(data.item_code == item_code){
898 if(in_list(data.serial_no.split('\n'), serial_no)){
899 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
900 'serial_no': serial_no
901 })))
902 }
903 }
904 })
905 }
906 },
907
908 apply_pricing_rule: function(){
909 var me = this;
910 $.each(this.frm.doc["items"], function(n, item) {
911 pricing_rule = me.get_pricing_rule(item)
912 me.validate_pricing_rule(pricing_rule)
913 if(pricing_rule.length){
914 item.margin_type = pricing_rule[0].margin_type;
915 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
916 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
917 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
918 me.apply_pricing_rule_on_item(item)
919 }
920 })
921 },
922
923 get_pricing_rule: function(item){
924 var me = this;
925 return $.grep(this.pricing_rules, function(data){
926 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
927 if(in_list(['Customer', 'Customer Group', 'Territory'], data.applicable_for)){
928 return me.validate_condition(data)
929 }else{
930 return true
931 }
932 }
933 })
934 },
935
936 validate_condition: function(data){
937 //This method check condition based on applicable for
938 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
939 if(in_list(condition[1], condition[0])){
940 return true
941 }
942 },
943
944 get_mapper_for_pricing_rule: function(data){
945 return {
946 'Customer': [data.customer, [this.doc.customer]],
947 'Customer Group': [data.customer_group, [this.doc.customer_group, 'All Customer Groups']],
948 'Territory': [data.territory, [this.doc.territory, 'All Territories']],
949 }
950 },
951
952 validate_pricing_rule: function(pricing_rule){
953 //This method validate duplicate pricing rule
954 var pricing_rule_name = '';
955 var priority = 0;
956 var pricing_rule_list = [];
957 var priority_list = []
958
959 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530960
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530961 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530962 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530963 priority_list.push(data.priority)
964 if(priority <= data.priority){
965 priority = data.priority
966 pricing_rule_list.push(data)
967 }
968 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530969
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530970 count = 0
971 $.each(priority_list, function(index, value){
972 if(value == priority){
973 count++
974 }
975 })
976
977 if(priority == 0 || count > 1){
978 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
979 'pricing_rule': pricing_rule_name
980 })))
981 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530982
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530983 return pricing_rule_list
984 }
985 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530986
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530987 validate_warehouse: function(){
988 if(!this.items[0].default_warehouse){
989 frappe.throw(__("Deafault warehouse is required for selected item"))
990 }
991 }
992})