blob: 5290caf4fd278ee34751cd91603f2a08f304b0f9 [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}
19
20
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053021erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
Rohit Waghchauree0934d12016-05-11 15:04:57 +053022 init: function(wrapper){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053023 this.page = wrapper.page;
24 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053025 this.set_indicator();
26 this.onload();
27 this.make_menu_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053028 this.si_docs = this.get_doc_from_localstorage();
29 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053030
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053031 beforeunload: function(e){
32 if(this.connection_status == false && frappe.get_route()[0] == "pos"){
33 e = e || window.event;
34
35 // For IE and Firefox prior to version 4
36 if (e) {
37 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
38 return
39 }
40
41 // For Safari
42 return __("You are in offline mode. You will not be able to reload until you have network.");
43 }
44 },
45
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053046 check_internet_connection: function(){
47 var me = this;
48 //Check Internet connection after every 30 seconds
49 setInterval(function(){
50 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053051 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053052 },
53
54 set_indicator: function(){
55 var me = this;
56 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053057 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053058 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 frappe.call({
60 method:"frappe.handler.ping",
61 callback: function(r){
62 if(r.message){
63 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053064 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053065 }
66 }
67 })
68 },
69
70 onload: function(){
71 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053072 this.get_data_from_server(function(){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053073 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053074 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 },
76
77 make_menu_list: function(){
78 var me = this;
79
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053080 this.page.add_menu_item(__("New Sales Invoice"), function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053081 me.save_previous_entry();
82 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053083 })
84
85 this.page.add_menu_item(__("View Offline Records"), function(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053086 me.show_unsync_invoice_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053087 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053088
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053089 this.page.add_menu_item(__("Sync Master Data"), function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +053090 me.get_data_from_server(function(){
Rohit Waghchaureea278b52016-07-21 23:28:04 +053091 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053092 me.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +053093 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +053094 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053095 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053096 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053097
Rohit Waghchaure82be0202016-10-04 12:46:37 +053098 this.page.add_menu_item(__("Sync Offline Invoices"), function(){
99 me.sync_sales_invoice()
100 });
101
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530102 this.page.add_menu_item(__("POS Profile"), function() {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530103 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530104 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530105 },
106
107 show_unsync_invoice_list: function(){
108 var me = this;
109 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530110 this.list_dialog = new frappe.ui.Dialog({
111 title: 'Invoice List'
112 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530113
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530114 this.list_dialog.show();
115 this.list_body = this.list_dialog.body;
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530116 if(me.pos_profile_data["allow_delete"]) {
117 this.list_dialog.set_primary_action(__("Delete"), function() {
118 frappe.confirm(__("Delete permanently?"), function () {
119 me.delete_records();
120 })
121 }).addClass("btn-danger");
122 this.toggle_primary_action();
123 }
124
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530125 if(this.si_docs.length > 0){
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530126 me.render_offline_data();
127 me.dialog_actions()
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530128 }else{
129 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
130 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530131 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530132
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530133 render_offline_data: function() {
134 var me = this;
135
136 this.removed_items = [];
137 $(this.list_body).empty();
138
139 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
140 <div class="col-xs-1"><input class="list-select-all" type="checkbox"></div>\
141 <div class="col-xs-3">Customer</div>\
142 <div class="col-xs-2 text-left">Status</div>\
143 <div class="col-xs-3 text-right">Paid Amount</div>\
144 <div class="col-xs-3 text-right">Grand Total</div>\
145 </div>')
146
147 $.each(this.si_docs, function(index, data){
148 for(key in data) {
149 $(frappe.render_template("pos_invoice_list", {
150 sr: index + 1,
151 name: key,
152 customer: data[key].customer,
153 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
154 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
155 data: me.get_doctype_status(data[key])
156 })).appendTo($(me.list_body));
157 }
158 })
159 },
160
161 dialog_actions: function() {
162 var me = this;
163
164 $(this.list_body).find('.list-column').click(function() {
165 me.name = $(this).parents().attr('invoice-name')
166 me.edit_record();
167 })
168
169 $(this.list_body).find('.list-select-all').click(function() {
170 me.removed_items = [];
171 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
172 if($(this).is(":checked")) {
173 $.each(me.si_docs, function(index, data){
174 for(key in data) {
175 me.removed_items.push(key)
176 }
177 })
178 }
179
180 me.toggle_primary_action();
181 })
182
183 $(this.list_body).find('.list-delete').click(function() {
184 me.name = $(this).parent().parent().attr('invoice-name');
185 if($(this).is(":checked")) {
186 me.removed_items.push(me.name);
187 } else {
188 me.removed_items.pop(me.name)
189 }
190
191 me.toggle_primary_action();
192 })
193 },
194
195 edit_record: function() {
196 var me = this;
197
198 doc_data = this.get_invoice_doc(this.si_docs);
199 if(doc_data){
200 this.frm.doc = doc_data[0][this.name];
201 this.set_missing_values();
202 this.refresh(false);
203 this.disable_input_field();
204 this.list_dialog.hide();
205 }
206 },
207
208 delete_records: function() {
209 var me = this;
210 this.remove_doc_from_localstorage()
211 this.update_localstorage();
212 this.render_offline_data();
213 this.dialog_actions();
214 this.toggle_primary_action();
215 },
216
217 toggle_primary_action: function() {
218 var me = this;
219 if(this.removed_items && this.removed_items.length > 0) {
220 $(this.list_dialog.wrapper).find('.btn-danger').show();
221 } else {
222 $(this.list_dialog.wrapper).find('.btn-danger').hide();
223 }
224 },
225
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530226 get_doctype_status: function(doc){
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530227 if(doc.docstatus == 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530228 return {status: "Draft", indicator: "red"}
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530229 }else if(doc.outstanding_amount == 0) {
230 return {status: "Paid", indicator: "green"}
231 }else {
232 return {status: "Submitted", indicator: "blue"}
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530233 }
234 },
235
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530236 set_missing_values: function(){
237 var me = this;
238 doc = JSON.parse(localStorage.getItem('doc'))
239 if(this.frm.doc.payments.length == 0){
240 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530241 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530242 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530243
244 if(this.frm.doc.customer){
245 this.party_field.$input.val(this.frm.doc.customer);
246 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530247
248 if(!this.frm.doc.write_off_account){
249 this.frm.doc.write_off_account = doc.write_off_account
250 }
251
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530252 if(!this.frm.doc.account_for_change_amount){
253 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530254 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530255 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530256
257 get_invoice_doc: function(si_docs){
258 var me = this;
259 this.si_docs = this.get_doc_from_localstorage();
260
261 return $.grep(this.si_docs, function(data){
262 for(key in data){
263 return key == me.name
264 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530265 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530266 },
267
268 get_data_from_server: function(callback){
269 var me = this;
270 frappe.call({
271 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
272 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530273 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530274 callback: function(r){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530275 me.init_master_data(r)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530276 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530277 me.set_interval_for_si_sync();
278 me.check_internet_connection();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 if(callback){
280 callback();
281 }
282 }
283 })
284 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530285
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530286 init_master_data: function(r){
287 var me = this;
288 this.meta = r.message.meta;
289 this.item_data = r.message.items;
290 this.customers = r.message.customers;
291 this.serial_no_data = r.message.serial_no_data;
292 this.batch_no_data = r.message.batch_no_data;
293 this.tax_data = r.message.tax_data;
294 this.price_list_data = r.message.price_list_data;
295 this.bin_data = r.message.bin_data;
296 this.pricing_rules = r.message.pricing_rules;
297 this.print_template = r.message.print_template;
298 this.pos_profile_data = r.message.pos_profile;
299 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530300 this.print_settings = locals[":Print Settings"]["Print Settings"];
301 this.letter_head = frappe.boot.letter_heads[this.pos_profile_data[letter_head]] || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530302 },
303
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530304 save_previous_entry : function(){
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530305 if(this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530306 this.create_invoice()
307 }
308 },
309
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530310 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530311 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530312 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530313 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530314 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530315 this.setup();
316 },
317
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530318 load_data: function(load_doc){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530319 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530320
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530321 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530322 this.actual_qty_dict = {};
323
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530324 if(load_doc) {
325 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
326 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530327
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530328 $.each(this.meta, function(i, data){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530329 frappe.meta.sync(data)
330 })
331
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530332 this.print_template_data = frappe.render_template("print_template", {content: this.print_template,
333 title:"POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css,
334 print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530335 },
336
337 setup: function(){
338 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
339 this.set_transaction_defaults("Customer");
340 this.make();
341 this.set_primary_action();
342 },
343
344 set_transaction_defaults: function(party) {
345 var me = this;
346 this.party = party;
347 this.price_list = (party == "Customer" ?
348 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
349 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
350 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
351 },
352
353 make: function() {
354 this.make_search();
355 this.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530356 this.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530357 this.make_discount_field()
358 },
359
360 make_search: function() {
361 var me = this;
362 this.search = frappe.ui.form.make_control({
363 df: {
364 "fieldtype": "Data",
365 "label": "Item",
366 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530367 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530368 },
369 parent: this.wrapper.find(".search-area"),
370 only_input: true,
371 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530372
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530373 this.search.make_input();
374 this.search.$input.on("keyup", function() {
375 setTimeout(function() {
376 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530377 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530378 }, 1000);
379 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530380
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 this.party_field = frappe.ui.form.make_control({
382 df: {
383 "fieldtype": "Data",
384 "options": this.party,
385 "label": this.party,
386 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530387 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530388 },
389 parent: this.wrapper.find(".party-area"),
390 only_input: true,
391 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530392
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530394 this.set_focus()
395 },
396
397 set_focus: function(){
398 if(this.default_customer){
399 this.search.$input.focus();
400 }else{
401 this.party_field.$input.focus();
402 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 },
404
405 make_customer: function() {
406 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530407
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530408 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530409 this.party_field.$input.val(this.default_customer);
410 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530411 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530412
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530414 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 source: function (request, response) {
416 me.customer_data = me.get_customers(request.term)
417 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530418 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530419 customer_group: data.customer_group, territory: data.territory}
420 }))
421 },
422 change: function(event, ui){
423 if(ui.item){
424 me.frm.doc.customer = ui.item.label;
425 me.frm.doc.customer_name = ui.item.customer_name;
426 me.frm.doc.customer_group = ui.item.customer_group;
427 me.frm.doc.territory = ui.item.territory;
428 }else{
429 me.frm.doc.customer = me.party_field.$input.val();
430 }
431 me.refresh();
432 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530433 }).on("focus", function(){
434 setTimeout(function() {
435 if(!me.party_field.$input.val()) {
436 me.party_field.$input.autocomplete( "search", " " );
437 }
438 }, 500);
439 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530440 },
441
442 get_customers: function(key){
443 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530444 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530445 var re = new RegExp('%', 'g');
446 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
447
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530448 if(key){
449 return $.grep(this.customers, function(data) {
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530450 if(reg.test(data.name.toLowerCase())
451 || reg.test(data.customer_name.toLowerCase())
452 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530453 return data
454 }
455 })
456 }else{
457 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
458 return customers.slice(0, 20)
459 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530460 },
461
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530462 make_item_list: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530463 var me = this;
464 if(!this.price_list) {
465 msgprint(__("Price List not found or disabled"));
466 return;
467 }
468
469 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530470
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530471 var $wrap = me.wrapper.find(".item-list");
472 me.wrapper.find(".item-list").empty();
473
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530474 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530475 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530476 if(index < 30){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530477 $(frappe.render_template("pos_item", {
478 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530479 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530480 item_name: obj.name===obj.item_name ? "" : obj.item_name,
481 item_image: obj.image ? "url('" + obj.image + "')" : null,
482 color: frappe.get_palette(obj.item_name),
483 abbr: frappe.get_abbr(obj.item_name)
484 })).tooltip().appendTo($wrap);
485 }
486 });
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530487 } else {
488 $("<h4>Searching record not found.</h4>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530489 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530490
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530491 if(this.items.length == 1
492 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530493 this.search.$input.val("");
494 this.add_to_cart();
495 }
496
497 // if form is local then allow this function
498 $(me.wrapper).find("div.pos-item").on("click", function() {
499 me.customer_validate();
500 if(me.frm.doc.docstatus==0) {
501 me.items = me.get_items($(this).attr("data-item-code"))
502 me.add_to_cart();
503 }
504 });
505 },
506
507 get_items: function(item_code){
508 // To search item as per the key enter
509
510 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530511 this.item_serial_no = {};
512 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530513
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530514 if(item_code){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530515 return $.grep(this.item_data, function(item){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530516 if(item.item_code == item_code ){
517 return true
518 }
519 })
520 }
521
Rohit Waghchaure9a360832016-11-24 20:00:23 +0530522 key = this.search.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g,'\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530523 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530524 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530525 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530526
527 if(key){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530528 return $.grep(this.item_data, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530529 if(search_status){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530530 if(in_list(me.batch_no_data[item.item_code], me.search.$input.val())){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530531 search_status = false;
532 return me.item_batch_no[item.item_code] = me.search.$input.val()
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530533 } else if( me.serial_no_data[item.item_code]
534 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530535 search_status = false;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530536 me.item_serial_no[item.item_code] = [me.search.$input.val(), me.serial_no_data[item.item_code][me.search.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530537 return true
538 } else if(item.barcode == me.search.$input.val()) {
539 search_status = false;
540 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530541 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
542 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530543 return true
544 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530545 }
546 })
547 }else{
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530548 return this.item_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530549 }
550 },
551
552 update_qty: function() {
553 var me = this;
554
555 $(this.wrapper).find(".pos-item-qty").on("change", function(){
556 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530557 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530558 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530559
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530560 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
561 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
562 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530563 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530564 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530565
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530566 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
567 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
568 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530569 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530570 })
571 },
572
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530573 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530574 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530575
576 $(this.wrapper).find(".pos-item-rate").on("change", function(){
577 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
578 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
579 })
580 },
581
582 update_qty_rate_against_item_code: function(item_code, field, value){
583 var me = this;
584 if(value < 0){
585 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530586 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530587
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530588 this.remove_item = []
589 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530590 if(d.serial_no){
591 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530592 }
593
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530594 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530595 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530596 d.amount = flt(d.rate) * flt(d.qty);
597 if(d.qty==0){
598 me.remove_item.push(d.idx)
599 }
600 }
601 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530602
603 if(field == 'qty'){
604 this.remove_zero_qty_item();
605 }
606
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530607 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530608 },
609
610 remove_zero_qty_item: function(){
611 var me = this;
612 idx = 0
613 this.items = []
614 idx = 0
615 $.each(this.frm.doc["items"] || [], function(i, d) {
616 if(!in_list(me.remove_item, d.idx)){
617 d.idx = idx;
618 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530619 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530620 }
621 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530622
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530623 this.frm.doc["items"] = this.items;
624 },
625
626 make_discount_field: function(){
627 var me = this;
628
629 this.wrapper.find('input.discount-percentage').on("change", function() {
630 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
631 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530632
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530633 if(me.frm.doc.apply_discount_on == 'Net Total'){
634 total = me.frm.doc.net_total
635 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530636
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530637 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
638 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
639 me.refresh();
640 });
641
642 this.wrapper.find('input.discount-amount').on("change", function() {
643 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
644 me.frm.doc.additional_discount_percentage = 0.0;
645 me.wrapper.find('input.discount-percentage').val(0);
646 me.refresh();
647 });
648 },
649
650 customer_validate: function(){
651 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530652 if(!this.frm.doc.customer){
653 frappe.throw(__("Please select customer"))
654 }
655 },
656
657 add_to_cart: function() {
658 var me = this;
659 var caught = false;
660 var no_of_items = me.wrapper.find(".pos-bill-item").length;
661
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530662 this.customer_validate();
663 this.mandatory_batch_no();
664 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530665 this.validate_warehouse();
666
667 if (no_of_items != 0) {
668 $.each(this.frm.doc["items"] || [], function(i, d) {
669 if (d.item_code == me.items[0].item_code) {
670 caught = true;
671 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530672 d.amount = flt(d.rate) * flt(d.qty);
673 if(me.item_serial_no[d.item_code]){
674 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
675 d.warehouse = me.item_serial_no[d.item_code][1]
676 }
677
678 if(me.item_batch_no.length){
679 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530680 }
681 }
682 });
683 }
684
685 // if item not found then add new item
686 if (!caught)
687 this.add_new_item_to_grid();
688
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530689 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530690 },
691
692 add_new_item_to_grid: function() {
693 var me = this;
694 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
695 this.child.item_code = this.items[0].item_code;
696 this.child.item_name = this.items[0].item_name;
697 this.child.stock_uom = this.items[0].stock_uom;
698 this.child.description = this.items[0].description;
699 this.child.qty = 1;
700 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530701 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
702 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530703 this.child.warehouse = (this.item_serial_no[this.child.item_code]
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530704 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse) );
705 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
706 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
707 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530709 this.child.batch_no = this.item_batch_no[this.child.item_code];
710 this.child.serial_no = (this.item_serial_no[this.child.item_code]
711 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530712 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530713 },
714
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530715 update_paid_amount_status: function(update_paid_amount){
716 if(this.name){
717 update_paid_amount = update_paid_amount ? false : true;
718 }
719
720 this.refresh(update_paid_amount);
721 },
722
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530723 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530724 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530725 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530726 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530727 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530728 this.set_primary_action();
729 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530730
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530731 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 this.apply_pricing_rule();
733 this.discount_amount_applied = false;
734 this._calculate_taxes_and_totals();
735 this.calculate_discount_amount();
736 this.show_items_in_item_cart();
737 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530738 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530739 this.set_totals();
740 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530741
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530742 get_company_currency: function() {
743 return erpnext.get_currency(this.frm.doc.company);
744 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530745
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530746 show_item_wise_taxes: function(){
747 return null;
748 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530749
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530750 show_items_in_item_cart: function() {
751 var me = this;
752 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530753 $.each(this.frm.doc.items|| [], function(i, d) {
754 $(frappe.render_template("pos_bill_item", {
755 item_code: d.item_code,
756 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
757 qty: d.qty,
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530758 actual_qty: me.actual_qty_dict[d.item_code] || 0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530759 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530760 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530761 amount: format_currency(d.amount, me.frm.doc.currency)
762 })).appendTo($items);
763 });
764
765 this.wrapper.find("input.pos-item-qty").on("focus", function() {
766 $(this).select();
767 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530768
769 this.wrapper.find("input.pos-item-rate").on("focus", function() {
770 $(this).select();
771 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530772 },
773
774 set_taxes: function(){
775 var me = this;
776 me.frm.doc.total_taxes_and_charges = 0.0
777
778 var taxes = this.frm.doc.taxes || [];
779 $(this.wrapper)
780 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
781 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530782
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530783 $.each(taxes, function(i, d) {
784 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
785 $(frappe.render_template("pos_tax_row", {
786 description: d.description,
787 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
788 me.frm.doc.currency)
789 })).appendTo(me.wrapper.find(".tax-table"));
790 }
791 });
792 },
793
794 set_totals: function() {
795 var me = this;
796 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
797 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
798 },
799
800 set_primary_action: function() {
801 var me = this;
802
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530803 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530804 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530805 me.validate();
806 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530807 me.create_invoice();
808 me.make_payment();
Rushabh Mehta323c06e2016-12-05 14:17:26 +0530809 }, "octicon octfa fa-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530810 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530811 this.page.set_primary_action(__("Print"), function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530812 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530813 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530814 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530815 }else {
816 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530817 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530818
819 this.page.set_secondary_action(__("New"), function() {
820 me.save_previous_entry();
821 me.create_new();
Rushabh Mehta323c06e2016-12-05 14:17:26 +0530822 }, "octicon octfa fa-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530823 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530824
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530825 print_dialog: function(){
826 var me = this;
827
828 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
829 style="margin-right: 5px;">{0}</a>\
830 <a class="btn btn-default new_doc">{1}</a>', [
831 __('Print'), __('New')
832 ]));
833
834 $('.print_doc').click(function(){
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530835 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530836 me.print_document(html)
837 })
838
839 $('.new_doc').click(function(){
840 msgprint.hide()
841 me.create_new();
842 })
843 },
844
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530845 print_document: function(html){
846 var w = window.open();
847 w.document.write(html);
848 w.document.close();
849 setTimeout(function(){
850 w.print();
851 w.close();
852 }, 1000)
853 },
854
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530855 submit_invoice: function(){
856 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530857 this.change_status();
858 if(this.frm.doc.docstatus == 1){
859 this.print_dialog()
860 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530861 },
862
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530863 change_status: function(){
864 if(this.frm.doc.docstatus == 0){
865 this.frm.doc.docstatus = 1;
866 this.update_invoice();
867 this.disable_input_field();
868 }
869 },
870
871 disable_input_field: function(){
872 var pointer_events = 'inherit'
873 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530874
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530875 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530876 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530877 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530878 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530879
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530880 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
881 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
882 this.set_primary_action();
883 },
884
885 create_invoice: function(){
886 var me = this;
887 var invoice_data = {}
888 this.si_docs = this.get_doc_from_localstorage();
889 if(this.name){
890 this.update_invoice()
891 }else{
892 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530893 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530894 this.frm.doc.posting_date = frappe.datetime.get_today();
895 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530896 invoice_data[this.name] = this.frm.doc
897 this.si_docs.push(invoice_data)
898 this.update_localstorage();
899 this.set_primary_action();
900 }
901 },
902
903 update_invoice: function(){
904 var me = this;
905 this.si_docs = this.get_doc_from_localstorage();
906 $.each(this.si_docs, function(index, data){
907 for(key in data){
908 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530909 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530910 me.update_localstorage();
911 }
912 }
913 })
914 },
915
916 update_localstorage: function(){
917 try{
918 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
919 }catch(e){
920 frappe.throw(__("LocalStorage is full , did not save"))
921 }
922 },
923
924 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530925 try{
926 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
927 }catch(e){
928 return []
929 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530930 },
931
932 set_interval_for_si_sync: function(){
933 var me = this;
934 setInterval(function(){
935 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530936 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530937 },
938
939 sync_sales_invoice: function(){
940 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530941 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530942
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530943 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530944 frappe.call({
945 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
946 args: {
947 doc_list: me.si_docs
948 },
949 callback: function(r){
950 if(r.message){
951 me.removed_items = r.message;
952 me.remove_doc_from_localstorage();
953 }
954 }
955 })
956 }
957 },
958
959 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530960 var invoices = [];
961 var index = 1;
962 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530963 if(docs){
964 invoices = $.map(docs, function(data){
965 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530966 if(data[key].docstatus == 1 && index < 50){
967 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530968 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530969 return data
970 }
971 }
972 });
973 }
974
975 return invoices
976 },
977
978 remove_doc_from_localstorage: function(){
979 var me = this;
980 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530981 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530982 if(this.removed_items){
983 $.each(this.si_docs, function(index, data){
984 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530985 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530986 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530987 }
988 }
989 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530990 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530991 this.update_localstorage();
992 }
993 },
994
995 validate: function(){
996 var me = this;
997 this.customer_validate();
998 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530999 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301000 },
1001
1002 item_validate: function(){
1003 if(this.frm.doc.items.length == 0){
1004 frappe.throw(__("Select items to save the invoice"))
1005 }
1006 },
Saurabh9a6c6662016-06-27 16:51:44 +05301007
1008 validate_mode_of_payments: function(){
1009 if (this.frm.doc.payments.length === 0){
1010 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1011 }
1012 },
1013
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301014 validate_serial_no: function(){
1015 var me = this;
1016 var item_code = serial_no = '';
1017 for (key in this.item_serial_no){
1018 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301019 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301020 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301021
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301022 if(item_code && serial_no){
1023 $.each(this.frm.doc.items, function(index, data){
1024 if(data.item_code == item_code){
1025 if(in_list(data.serial_no.split('\n'), serial_no)){
1026 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1027 'serial_no': serial_no
1028 })))
1029 }
1030 }
1031 })
1032 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301033
1034 if(this.items[0].has_serial_no && serial_no == ""){
1035 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1036 'item': this.items[0].item_code
1037 })))
1038 }
1039 },
1040
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301041 validate_serial_no_qty: function(args, item_code, field, value){
1042 var me = this;
1043 if (args.item_code == item_code && args.serial_no
1044 && field == 'qty' && cint(value) != value) {
1045 args.qty = 0.0;
1046 this.refresh();
1047 frappe.throw(__("Serial no item cannot be a fraction"))
1048 }
1049
1050 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
1051 args.qty = 0.0;
1052 args.serial_no = ''
1053 this.refresh();
1054 frappe.throw(__("Total nos of serial no is not equal to quantity."))
1055 }
1056 },
1057
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301058 mandatory_batch_no: function(){
1059 var me = this;
1060 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
1061 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1062 'item': this.items[0].item_code
1063 })))
1064 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301065 },
1066
1067 apply_pricing_rule: function(){
1068 var me = this;
1069 $.each(this.frm.doc["items"], function(n, item) {
1070 pricing_rule = me.get_pricing_rule(item)
1071 me.validate_pricing_rule(pricing_rule)
1072 if(pricing_rule.length){
1073 item.margin_type = pricing_rule[0].margin_type;
1074 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1075 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1076 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
1077 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301078 } else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) {
1079 item.margin_rate_or_amount = 0.0;
1080 item.discount_percentage = 0.0;
1081 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301082 }
1083 })
1084 },
1085
1086 get_pricing_rule: function(item){
1087 var me = this;
1088 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301089 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1090 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1091 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1092 return me.validate_condition(data)
1093 }else{
1094 return true
1095 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301096 }
1097 }
1098 })
1099 },
1100
1101 validate_condition: function(data){
1102 //This method check condition based on applicable for
1103 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1104 if(in_list(condition[1], condition[0])){
1105 return true
1106 }
1107 },
1108
1109 get_mapper_for_pricing_rule: function(data){
1110 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301111 'Customer': [data.customer, [this.frm.doc.customer]],
1112 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1113 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301114 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115 }
1116 },
1117
1118 validate_pricing_rule: function(pricing_rule){
1119 //This method validate duplicate pricing rule
1120 var pricing_rule_name = '';
1121 var priority = 0;
1122 var pricing_rule_list = [];
1123 var priority_list = []
1124
1125 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301126
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301127 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301128 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301129 priority_list.push(data.priority)
1130 if(priority <= data.priority){
1131 priority = data.priority
1132 pricing_rule_list.push(data)
1133 }
1134 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301135
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301136 count = 0
1137 $.each(priority_list, function(index, value){
1138 if(value == priority){
1139 count++
1140 }
1141 })
1142
1143 if(priority == 0 || count > 1){
1144 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1145 'pricing_rule': pricing_rule_name
1146 })))
1147 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301148
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301149 return pricing_rule_list
1150 }
1151 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301152
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301153 validate_warehouse: function(){
Nabin Hait977eff92016-11-21 18:29:16 +05301154 if(this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']){
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05301155 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301156 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301157 },
1158
1159 get_actual_qty: function(item) {
1160 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301161
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301162 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
1163 if(warehouse && this.bin_data[item.item_code]) {
1164 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301165 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301166 }
1167
1168 return this.actual_qty
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301169 }
1170})