blob: 4b3da285f3e16817c110cb179f5812475827d2a6 [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) {
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 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053030
Rohit Waghchauree0934d12016-05-11 15:04:57 +053031 on_refresh_page: function() {
32 var me = this;
33 if(this.load){
34 this.load = false;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053035 }else if(this.connection_status){
36 this.onload();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053037 }else{
38 this.create_new();
39 }
40 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053041
42 check_internet_connection: function(){
43 var me = this;
44 //Check Internet connection after every 30 seconds
45 setInterval(function(){
46 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053047 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053048 },
49
50 set_indicator: function(){
51 var me = this;
52 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053053 this.connection_status = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053054 this.page.set_indicator("Offline", "grey")
55 frappe.call({
56 method:"frappe.handler.ping",
57 callback: function(r){
58 if(r.message){
59 me.connection_status = true;
60 me.page.set_indicator("Online", "green")
61 }
62 }
63 })
64 },
65
66 onload: function(){
67 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053068 this.get_data_from_server(function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053069 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053070 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053071
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053072 this.check_internet_connection();
73 },
74
75 make_menu_list: function(){
76 var me = this;
77
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053078 this.page.add_menu_item(__("New Sales Invoice"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053079 me.save_previous_entry()
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053080 me.create_new()
81 })
82
83 this.page.add_menu_item(__("View Offline Records"), function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053084 me.show_unsync_invoice_list()
85 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053087 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053088 me.get_data_from_server(function(){
89 me.load_data()
90 me.make_customer()
91 me.make_item_list()
92 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053093 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053094
95 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053096 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053097 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053098 },
99
100 show_unsync_invoice_list: function(){
101 var me = this;
102 this.si_docs = this.get_doc_from_localstorage();
103
104 this.list_dialog = new frappe.ui.Dialog({
105 title: 'Invoice List'
106 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530107
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530108 this.list_dialog.show();
109 this.list_body = this.list_dialog.body;
110 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530111 <div class="col-xs-3">Sr</div>\
112 <div class="col-xs-3">Customer</div>\
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530113 <div class="col-xs-4 text-center">Grand Total</div>\
114 <div class="col-xs-2 text-left">Status</div>\
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530115 </div>')
116
117 $.each(this.si_docs, function(index, data){
118 for(key in data) {
119 $(frappe.render_template("pos_invoice_list", {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530120 sr: index + 1,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530121 name: key,
122 customer: data[key].customer,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530123 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530124 data: me.get_doctype_status(data[key])
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530125 })).appendTo($(me.list_body));
126 }
127 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530128
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 $(this.list_body).find('.list-row').click(function() {
130 me.name = $(this).attr('invoice-name')
131 doc_data = me.get_invoice_doc(me.si_docs)
132 if(doc_data){
133 me.frm.doc = doc_data[0][me.name];
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530134 me.set_missing_values();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530135 me.refresh();
136 me.disable_input_field();
137 me.list_dialog.hide();
138 }
139 })
140 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530141
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530142 get_doctype_status: function(doc){
143 if(doc.outstanding_amount == 0){
144 return {status: "Paid", indicator: "green"}
145 }else if(doc.docstatus == 0){
146 return {status: "Draft", indicator: "red"}
147 }else if(doc.paid_amount >= 0){
148 return {status: "Unpaid", indicator: "orange"}
149 }
150 },
151
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530152 set_missing_values: function(){
153 var me = this;
154 doc = JSON.parse(localStorage.getItem('doc'))
155 if(this.frm.doc.payments.length == 0){
156 this.frm.doc.payments = doc.payments;
157 }
158 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530159
160 get_invoice_doc: function(si_docs){
161 var me = this;
162 this.si_docs = this.get_doc_from_localstorage();
163
164 return $.grep(this.si_docs, function(data){
165 for(key in data){
166 return key == me.name
167 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530168 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530169 },
170
171 get_data_from_server: function(callback){
172 var me = this;
173 frappe.call({
174 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
175 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530176 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530177 callback: function(r){
178 window.items = r.message.items;
179 window.customers = r.message.customers;
180 window.pricing_rules = r.message.pricing_rules;
181 window.meta = r.message.meta;
182 window.print_template = r.message.print_template;
183 localStorage.setItem('doc', JSON.stringify(r.message.doc));
184 if(callback){
185 callback();
186 }
187 }
188 })
189 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530190
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530191 save_previous_entry : function(){
192 if(this.frm.doc.items.length > 0){
193 this.create_invoice()
194 }
195 },
196
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530197 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530198 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530199 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530200 this.name = '';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530201 this.load_data();
202 this.setup();
203 },
204
205 load_data: function(){
206 this.items = window.items;
207 this.customers = window.customers;
208 this.pricing_rules = window.pricing_rules;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530209 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530210
211 $.each(window.meta, function(i, data){
212 frappe.meta.sync(data)
213 })
214
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530215 this.print_template = frappe.render_template("print_template",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530216 {content: window.print_template, title:"POS"})
217 },
218
219 setup: function(){
220 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
221 this.set_transaction_defaults("Customer");
222 this.make();
223 this.set_primary_action();
224 },
225
226 set_transaction_defaults: function(party) {
227 var me = this;
228 this.party = party;
229 this.price_list = (party == "Customer" ?
230 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
231 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
232 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
233 },
234
235 make: function() {
236 this.make_search();
237 this.make_customer();
238 this.make_item_list();
239 this.make_discount_field()
240 },
241
242 make_search: function() {
243 var me = this;
244 this.search = frappe.ui.form.make_control({
245 df: {
246 "fieldtype": "Data",
247 "label": "Item",
248 "fieldname": "pos_item",
249 "placeholder": "Search Item"
250 },
251 parent: this.wrapper.find(".search-area"),
252 only_input: true,
253 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530254
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530255 this.search.make_input();
256 this.search.$input.on("keyup", function() {
257 setTimeout(function() {
258 me.items = me.get_items();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530259 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530260 }, 1000);
261 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530262
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530263 this.party_field = frappe.ui.form.make_control({
264 df: {
265 "fieldtype": "Data",
266 "options": this.party,
267 "label": this.party,
268 "fieldname": this.party.toLowerCase(),
269 "placeholder": this.party
270 },
271 parent: this.wrapper.find(".party-area"),
272 only_input: true,
273 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530274
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275 this.party_field.make_input();
276 },
277
278 make_customer: function() {
279 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530280
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530281 if(this.customers.length == 1){
282 this.party_field.$input.val(this.customers[0].name);
283 this.frm.doc.customer = this.customers[0].name;
284 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530285
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530287 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530288 source: function (request, response) {
289 me.customer_data = me.get_customers(request.term)
290 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530291 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530292 customer_group: data.customer_group, territory: data.territory}
293 }))
294 },
295 change: function(event, ui){
296 if(ui.item){
297 me.frm.doc.customer = ui.item.label;
298 me.frm.doc.customer_name = ui.item.customer_name;
299 me.frm.doc.customer_group = ui.item.customer_group;
300 me.frm.doc.territory = ui.item.territory;
301 }else{
302 me.frm.doc.customer = me.party_field.$input.val();
303 }
304 me.refresh();
305 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530306 }).on("focus", function(){
307 setTimeout(function() {
308 if(!me.party_field.$input.val()) {
309 me.party_field.$input.autocomplete( "search", " " );
310 }
311 }, 500);
312 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530313 },
314
315 get_customers: function(key){
316 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530317 key = key.toLowerCase().trim()
318 if(key){
319 return $.grep(this.customers, function(data) {
320 if(data.name.toLowerCase().match(key)
321 || data.customer_name.toLowerCase().match(key)
322 || (data.customer_group && data.customer_group.toLowerCase().match(key))){
323 return data
324 }
325 })
326 }else{
327 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
328 return customers.slice(0, 20)
329 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530330 },
331
332 make_item_list: function() {
333 var me = this;
334 if(!this.price_list) {
335 msgprint(__("Price List not found or disabled"));
336 return;
337 }
338
339 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530340
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530341 var $wrap = me.wrapper.find(".item-list");
342 me.wrapper.find(".item-list").empty();
343
344 if (this.items) {
345 $.each(this.items, function(index, obj) {
346 if(index < 16){
347 $(frappe.render_template("pos_item", {
348 item_code: obj.name,
349 item_price: format_currency(obj.price_list_rate, obj.currency),
350 item_name: obj.name===obj.item_name ? "" : obj.item_name,
351 item_image: obj.image ? "url('" + obj.image + "')" : null,
352 color: frappe.get_palette(obj.item_name),
353 abbr: frappe.get_abbr(obj.item_name)
354 })).tooltip().appendTo($wrap);
355 }
356 });
357 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530358
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530359 if(this.items.length == 1){
360 this.search.$input.val("");
361 this.add_to_cart();
362 }
363
364 // if form is local then allow this function
365 $(me.wrapper).find("div.pos-item").on("click", function() {
366 me.customer_validate();
367 if(me.frm.doc.docstatus==0) {
368 me.items = me.get_items($(this).attr("data-item-code"))
369 me.add_to_cart();
370 }
371 });
372 },
373
374 get_items: function(item_code){
375 // To search item as per the key enter
376
377 var me = this;
378 this.item_serial_no = {}
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530379
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530380 if(item_code){
381 return $.grep(window.items, function(item){
382 if(item.item_code == item_code ){
383 return true
384 }
385 })
386 }
387
388 key = this.search.$input.val().toLowerCase();
389
390 if(key){
391 return $.grep(window.items, function(item){
392 if( (item.item_code.toLowerCase().match(key)) ||
393 (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key)) ){
394 return true
395 }else if(item.barcode){
396 return item.barcode == me.search.$input.val()
397 } else if (in_list(item.serial_nos, me.search.$input.val())){
398 me.item_serial_no[item.item_code] = me.search.$input.val()
399 return true
400 }
401 })
402 }else{
403 return window.items;
404 }
405 },
406
407 update_qty: function() {
408 var me = this;
409
410 $(this.wrapper).find(".pos-item-qty").on("change", function(){
411 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530412 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530414
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
416 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
417 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530418 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530419 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530420
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530421 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
422 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
423 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530424 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530425 })
426 },
427
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530428 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530429 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530430
431 $(this.wrapper).find(".pos-item-rate").on("change", function(){
432 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
433 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
434 })
435 },
436
437 update_qty_rate_against_item_code: function(item_code, field, value){
438 var me = this;
439 if(value < 0){
440 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530441 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530442
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530443 this.remove_item = []
444 $.each(this.frm.doc["items"] || [], function(i, d) {
445 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530446 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530447 d.amount = flt(d.rate) * flt(d.qty);
448 if(d.qty==0){
449 me.remove_item.push(d.idx)
450 }
451 }
452 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530453
454 if(field == 'qty'){
455 this.remove_zero_qty_item();
456 }
457
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530458 this.refresh();
459 },
460
461 remove_zero_qty_item: function(){
462 var me = this;
463 idx = 0
464 this.items = []
465 idx = 0
466 $.each(this.frm.doc["items"] || [], function(i, d) {
467 if(!in_list(me.remove_item, d.idx)){
468 d.idx = idx;
469 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530470 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 }
472 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530473
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530474 this.frm.doc["items"] = this.items;
475 },
476
477 make_discount_field: function(){
478 var me = this;
479
480 this.wrapper.find('input.discount-percentage').on("change", function() {
481 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
482 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530483
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530484 if(me.frm.doc.apply_discount_on == 'Net Total'){
485 total = me.frm.doc.net_total
486 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530487
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530488 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
489 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
490 me.refresh();
491 });
492
493 this.wrapper.find('input.discount-amount').on("change", function() {
494 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
495 me.frm.doc.additional_discount_percentage = 0.0;
496 me.wrapper.find('input.discount-percentage').val(0);
497 me.refresh();
498 });
499 },
500
501 customer_validate: function(){
502 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530503
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530504 if(!this.frm.doc.customer){
505 frappe.throw(__("Please select customer"))
506 }
507 },
508
509 add_to_cart: function() {
510 var me = this;
511 var caught = false;
512 var no_of_items = me.wrapper.find(".pos-bill-item").length;
513
514 this.validate_serial_no()
515 this.validate_warehouse();
516
517 if (no_of_items != 0) {
518 $.each(this.frm.doc["items"] || [], function(i, d) {
519 if (d.item_code == me.items[0].item_code) {
520 caught = true;
521 d.qty += 1;
522 d.amount = flt(d.rate) * flt(d.qty)
523 if(me.item_serial_no.length){
524 d.serial_no += '\n' + me.item_serial_no[d.item_code]
525 }
526 }
527 });
528 }
529
530 // if item not found then add new item
531 if (!caught)
532 this.add_new_item_to_grid();
533
534 this.refresh();
535 },
536
537 add_new_item_to_grid: function() {
538 var me = this;
539 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
540 this.child.item_code = this.items[0].item_code;
541 this.child.item_name = this.items[0].item_name;
542 this.child.stock_uom = this.items[0].stock_uom;
543 this.child.description = this.items[0].description;
544 this.child.qty = 1;
545 this.child.item_group = this.items[0].item_group;
546 this.child.cost_center = this.items[0].cost_center;
547 this.child.income_account = this.items[0].income_account;
548 this.child.warehouse = this.items[0].default_warehouse;
549 this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
550 this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9);
551 this.child.actual_qty = this.items[0].actual_qty;
552 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530553 this.child.serial_no = this.item_serial_no[this.child.item_code];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530554 },
555
556 refresh: function() {
557 var me = this;
558 this.refresh_fields();
559 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530560 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530561 this.set_primary_action();
562 },
563 refresh_fields: function() {
564 this.apply_pricing_rule();
565 this.discount_amount_applied = false;
566 this._calculate_taxes_and_totals();
567 this.calculate_discount_amount();
568 this.show_items_in_item_cart();
569 this.set_taxes();
570 this.calculate_outstanding_amount();
571 this.set_totals();
572 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530573
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530574 get_company_currency: function() {
575 return erpnext.get_currency(this.frm.doc.company);
576 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530577
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530578 show_item_wise_taxes: function(){
579 return null;
580 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530581
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530582 show_items_in_item_cart: function() {
583 var me = this;
584 var $items = this.wrapper.find(".items").empty();
585 me.frm.doc.net_total = 0.0
586 $.each(this.frm.doc.items|| [], function(i, d) {
587 $(frappe.render_template("pos_bill_item", {
588 item_code: d.item_code,
589 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
590 qty: d.qty,
591 actual_qty: d.actual_qty,
592 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530593 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530594 amount: format_currency(d.amount, me.frm.doc.currency)
595 })).appendTo($items);
596 });
597
598 this.wrapper.find("input.pos-item-qty").on("focus", function() {
599 $(this).select();
600 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530601
602 this.wrapper.find("input.pos-item-rate").on("focus", function() {
603 $(this).select();
604 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530605 },
606
607 set_taxes: function(){
608 var me = this;
609 me.frm.doc.total_taxes_and_charges = 0.0
610
611 var taxes = this.frm.doc.taxes || [];
612 $(this.wrapper)
613 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
614 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530615
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530616 $.each(taxes, function(i, d) {
617 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
618 $(frappe.render_template("pos_tax_row", {
619 description: d.description,
620 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
621 me.frm.doc.currency)
622 })).appendTo(me.wrapper.find(".tax-table"));
623 }
624 });
625 },
626
627 set_totals: function() {
628 var me = this;
629 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
630 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
631 },
632
633 set_primary_action: function() {
634 var me = this;
635
636 if (this.frm.doc.docstatus==0 && this.frm.doc.outstanding_amount > 0) {
637 this.page.set_primary_action(__("Pay"), function() {
638 me.validate()
639 me.create_invoice();
640 me.make_payment();
641 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530642 }else if(this.frm.doc.docstatus == 0 && this.name){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530643 this.page.set_primary_action(__("Submit"), function() {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530644 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530645 })
646 }else if(this.frm.doc.docstatus == 1){
647 this.page.set_primary_action(__("Print"), function() {
648 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530649 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530650 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530651 }else {
652 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530653 }
654 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530655
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530656 print_document: function(html){
657 var w = window.open();
658 w.document.write(html);
659 w.document.close();
660 setTimeout(function(){
661 w.print();
662 w.close();
663 }, 1000)
664 },
665
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530666 write_off_amount: function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530667 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530668 var value = 0.0;
669
670 if(this.frm.doc.outstanding_amount > 0){
671 dialog = new frappe.ui.Dialog({
672 title: 'Write Off Amount',
673 fields: [
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530674 {fieldtype: "Check", fieldname: "write_off_amount", label: __("Write of Outstanding Amount")},
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530675 ]
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530676 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530677
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530678 dialog.show();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530679
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530680 dialog.fields_dict.write_off_amount.$input.change(function(){
681 write_off_amount = dialog.get_values().write_off_amount
682 me.frm.doc.write_off_outstanding_amount_automatically = write_off_amount;
683 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;
684 me.frm.doc.write_off_amount = flt(me.frm.doc.base_write_off_amount * me.frm.doc.conversion_rate, precision("write_off_amount"))
685 me.calculate_outstanding_amount();
686 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530687
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530688 dialog.set_primary_action(__("Submit"), function(){
689 dialog.hide()
690 me.submit_invoice()
691 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530692 }else{
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530693 this.submit_invoice()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530694 }
695 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530696
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530697 submit_invoice: function(){
698 var me = this;
699 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
700 me.change_status();
701 })
702 },
703
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530704 change_status: function(){
705 if(this.frm.doc.docstatus == 0){
706 this.frm.doc.docstatus = 1;
707 this.update_invoice();
708 this.disable_input_field();
709 }
710 },
711
712 disable_input_field: function(){
713 var pointer_events = 'inherit'
714 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530715
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530716 if(this.frm.doc.docstatus == 1){
717 pointer_events = 'none'
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530718 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530719 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530720
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530721 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
722 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
723 this.set_primary_action();
724 },
725
726 create_invoice: function(){
727 var me = this;
728 var invoice_data = {}
729 this.si_docs = this.get_doc_from_localstorage();
730 if(this.name){
731 this.update_invoice()
732 }else{
733 this.name = $.now();
734 invoice_data[this.name] = this.frm.doc
735 this.si_docs.push(invoice_data)
736 this.update_localstorage();
737 this.set_primary_action();
738 }
739 },
740
741 update_invoice: function(){
742 var me = this;
743 this.si_docs = this.get_doc_from_localstorage();
744 $.each(this.si_docs, function(index, data){
745 for(key in data){
746 if(key == me.name){
747 me.si_docs[index][key] = me.frm.doc
748 me.update_localstorage();
749 }
750 }
751 })
752 },
753
754 update_localstorage: function(){
755 try{
756 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
757 }catch(e){
758 frappe.throw(__("LocalStorage is full , did not save"))
759 }
760 },
761
762 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530763 try{
764 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
765 }catch(e){
766 return []
767 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530768 },
769
770 set_interval_for_si_sync: function(){
771 var me = this;
772 setInterval(function(){
773 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530774 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530775 },
776
777 sync_sales_invoice: function(){
778 var me = this;
779 this.si_docs = this.get_submitted_invoice()
780
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530781 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530782 frappe.call({
783 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
784 args: {
785 doc_list: me.si_docs
786 },
787 callback: function(r){
788 if(r.message){
789 me.removed_items = r.message;
790 me.remove_doc_from_localstorage();
791 }
792 }
793 })
794 }
795 },
796
797 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530798 var invoices = [];
799 var index = 1;
800 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530801 if(docs){
802 invoices = $.map(docs, function(data){
803 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530804 if(data[key].docstatus == 1 && index < 50){
805 index++
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530806 return data
807 }
808 }
809 });
810 }
811
812 return invoices
813 },
814
815 remove_doc_from_localstorage: function(){
816 var me = this;
817 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530818 this.new_si_docs = []
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530819 if(this.removed_items){
820 $.each(this.si_docs, function(index, data){
821 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530822 if(!in_list(me.removed_items, key)){
823 me.new_si_docs.push(data)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530824 }
825 }
826 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530827 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530828 this.update_localstorage();
829 }
830 },
831
832 validate: function(){
833 var me = this;
834 this.customer_validate();
835 this.item_validate();
836 },
837
838 item_validate: function(){
839 if(this.frm.doc.items.length == 0){
840 frappe.throw(__("Select items to save the invoice"))
841 }
842 },
843
844 validate_serial_no: function(){
845 var me = this;
846 var item_code = serial_no = '';
847 for (key in this.item_serial_no){
848 item_code = key;
849 serial_no = me.item_serial_no[key]
850 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530851
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530852 if(item_code && serial_no){
853 $.each(this.frm.doc.items, function(index, data){
854 if(data.item_code == item_code){
855 if(in_list(data.serial_no.split('\n'), serial_no)){
856 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
857 'serial_no': serial_no
858 })))
859 }
860 }
861 })
862 }
863 },
864
865 apply_pricing_rule: function(){
866 var me = this;
867 $.each(this.frm.doc["items"], function(n, item) {
868 pricing_rule = me.get_pricing_rule(item)
869 me.validate_pricing_rule(pricing_rule)
870 if(pricing_rule.length){
871 item.margin_type = pricing_rule[0].margin_type;
872 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
873 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
874 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
875 me.apply_pricing_rule_on_item(item)
876 }
877 })
878 },
879
880 get_pricing_rule: function(item){
881 var me = this;
882 return $.grep(this.pricing_rules, function(data){
883 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
884 if(in_list(['Customer', 'Customer Group', 'Territory'], data.applicable_for)){
885 return me.validate_condition(data)
886 }else{
887 return true
888 }
889 }
890 })
891 },
892
893 validate_condition: function(data){
894 //This method check condition based on applicable for
895 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
896 if(in_list(condition[1], condition[0])){
897 return true
898 }
899 },
900
901 get_mapper_for_pricing_rule: function(data){
902 return {
903 'Customer': [data.customer, [this.doc.customer]],
904 'Customer Group': [data.customer_group, [this.doc.customer_group, 'All Customer Groups']],
905 'Territory': [data.territory, [this.doc.territory, 'All Territories']],
906 }
907 },
908
909 validate_pricing_rule: function(pricing_rule){
910 //This method validate duplicate pricing rule
911 var pricing_rule_name = '';
912 var priority = 0;
913 var pricing_rule_list = [];
914 var priority_list = []
915
916 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530917
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530918 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530919 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530920 priority_list.push(data.priority)
921 if(priority <= data.priority){
922 priority = data.priority
923 pricing_rule_list.push(data)
924 }
925 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530926
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530927 count = 0
928 $.each(priority_list, function(index, value){
929 if(value == priority){
930 count++
931 }
932 })
933
934 if(priority == 0 || count > 1){
935 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
936 'pricing_rule': pricing_rule_name
937 })))
938 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530939
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530940 return pricing_rule_list
941 }
942 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530943
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530944 validate_warehouse: function(){
945 if(!this.items[0].default_warehouse){
946 frappe.throw(__("Deafault warehouse is required for selected item"))
947 }
948 }
949})