blob: 9aafdaeb74ef967fcc841db489b25f9f284d97e2 [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();
110
111 this.list_dialog = new frappe.ui.Dialog({
112 title: 'Invoice List'
113 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530114
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530115 this.list_dialog.show();
116 this.list_body = this.list_dialog.body;
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530117 if(this.si_docs.length > 0){
118 $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530119 <div class="col-xs-1">Sr</div>\
120 <div class="col-xs-3">Customer</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530121 <div class="col-xs-2 text-left">Status</div>\
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530122 <div class="col-xs-3 text-right">Paid Amount</div>\
123 <div class="col-xs-3 text-right">Grand Total</div>\
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530124 </div>')
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530125
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530126 $.each(this.si_docs, function(index, data){
127 for(key in data) {
128 $(frappe.render_template("pos_invoice_list", {
129 sr: index + 1,
130 name: key,
131 customer: data[key].customer,
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530132 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530133 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
134 data: me.get_doctype_status(data[key])
135 })).appendTo($(me.list_body));
136 }
137 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530138
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530139 $(this.list_body).find('.list-row').click(function() {
140 me.name = $(this).attr('invoice-name')
141 doc_data = me.get_invoice_doc(me.si_docs)
142 if(doc_data){
143 me.frm.doc = doc_data[0][me.name];
144 me.set_missing_values();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530145 me.refresh(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530146 me.disable_input_field();
147 me.list_dialog.hide();
148 }
149 })
150 }else{
151 $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")}))
152 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530153 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530154
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530155 get_doctype_status: function(doc){
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530156 if(doc.docstatus == 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530157 return {status: "Draft", indicator: "red"}
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530158 }else if(doc.outstanding_amount == 0) {
159 return {status: "Paid", indicator: "green"}
160 }else {
161 return {status: "Submitted", indicator: "blue"}
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530162 }
163 },
164
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530165 set_missing_values: function(){
166 var me = this;
167 doc = JSON.parse(localStorage.getItem('doc'))
168 if(this.frm.doc.payments.length == 0){
169 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530170 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530171 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530172
173 if(this.frm.doc.customer){
174 this.party_field.$input.val(this.frm.doc.customer);
175 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530176
177 if(!this.frm.doc.write_off_account){
178 this.frm.doc.write_off_account = doc.write_off_account
179 }
180
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530181 if(!this.frm.doc.account_for_change_amount){
182 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530183 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530184 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530185
186 get_invoice_doc: function(si_docs){
187 var me = this;
188 this.si_docs = this.get_doc_from_localstorage();
189
190 return $.grep(this.si_docs, function(data){
191 for(key in data){
192 return key == me.name
193 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530194 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530195 },
196
197 get_data_from_server: function(callback){
198 var me = this;
199 frappe.call({
200 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
201 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530202 freeze_message: __("Master data syncing, it might take some time"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530203 callback: function(r){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530204 me.init_master_data(r)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530205 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530206 me.set_interval_for_si_sync();
207 me.check_internet_connection();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530208 if(callback){
209 callback();
210 }
211 }
212 })
213 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530214
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530215 init_master_data: function(r){
216 var me = this;
217 this.meta = r.message.meta;
218 this.item_data = r.message.items;
219 this.customers = r.message.customers;
220 this.serial_no_data = r.message.serial_no_data;
221 this.batch_no_data = r.message.batch_no_data;
222 this.tax_data = r.message.tax_data;
223 this.price_list_data = r.message.price_list_data;
224 this.bin_data = r.message.bin_data;
225 this.pricing_rules = r.message.pricing_rules;
226 this.print_template = r.message.print_template;
227 this.pos_profile_data = r.message.pos_profile;
228 this.default_customer = r.message.default_customer || null;
229 },
230
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530231 save_previous_entry : function(){
232 if(this.frm.doc.items.length > 0){
233 this.create_invoice()
234 }
235 },
236
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530237 create_new: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530238 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530239 this.frm = {}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530240 this.name = '';
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530241 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530242 this.setup();
243 },
244
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530245 load_data: function(load_doc){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530246 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530247
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530248 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530249 this.actual_qty_dict = {};
250
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530251 if(load_doc) {
252 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
253 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530254
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530255 $.each(this.meta, function(i, data){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530256 frappe.meta.sync(data)
257 })
258
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530259 this.print_template = frappe.render_template("print_template",
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530260 {content: this.print_template, title:"POS",
Rohit Waghchaure3c4cdd22016-09-19 00:17:32 +0530261 base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530262 },
263
264 setup: function(){
265 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
266 this.set_transaction_defaults("Customer");
267 this.make();
268 this.set_primary_action();
269 },
270
271 set_transaction_defaults: function(party) {
272 var me = this;
273 this.party = party;
274 this.price_list = (party == "Customer" ?
275 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
276 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
277 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
278 },
279
280 make: function() {
281 this.make_search();
282 this.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530283 this.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 this.make_discount_field()
285 },
286
287 make_search: function() {
288 var me = this;
289 this.search = frappe.ui.form.make_control({
290 df: {
291 "fieldtype": "Data",
292 "label": "Item",
293 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530294 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530295 },
296 parent: this.wrapper.find(".search-area"),
297 only_input: true,
298 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530299
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530300 this.search.make_input();
301 this.search.$input.on("keyup", function() {
302 setTimeout(function() {
303 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530304 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530305 }, 1000);
306 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530307
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530308 this.party_field = frappe.ui.form.make_control({
309 df: {
310 "fieldtype": "Data",
311 "options": this.party,
312 "label": this.party,
313 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530314 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530315 },
316 parent: this.wrapper.find(".party-area"),
317 only_input: true,
318 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530319
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530320 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530321 this.set_focus()
322 },
323
324 set_focus: function(){
325 if(this.default_customer){
326 this.search.$input.focus();
327 }else{
328 this.party_field.$input.focus();
329 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530330 },
331
332 make_customer: function() {
333 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530334
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530335 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530336 this.party_field.$input.val(this.default_customer);
337 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530339
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530341 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530342 source: function (request, response) {
343 me.customer_data = me.get_customers(request.term)
344 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530345 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530346 customer_group: data.customer_group, territory: data.territory}
347 }))
348 },
349 change: function(event, ui){
350 if(ui.item){
351 me.frm.doc.customer = ui.item.label;
352 me.frm.doc.customer_name = ui.item.customer_name;
353 me.frm.doc.customer_group = ui.item.customer_group;
354 me.frm.doc.territory = ui.item.territory;
355 }else{
356 me.frm.doc.customer = me.party_field.$input.val();
357 }
358 me.refresh();
359 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530360 }).on("focus", function(){
361 setTimeout(function() {
362 if(!me.party_field.$input.val()) {
363 me.party_field.$input.autocomplete( "search", " " );
364 }
365 }, 500);
366 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530367 },
368
369 get_customers: function(key){
370 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530371 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530372 var re = new RegExp('%', 'g');
373 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
374
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530375 if(key){
376 return $.grep(this.customers, function(data) {
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530377 if(reg.test(data.name.toLowerCase())
378 || reg.test(data.customer_name.toLowerCase())
379 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530380 return data
381 }
382 })
383 }else{
384 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
385 return customers.slice(0, 20)
386 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530387 },
388
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530389 make_item_list: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530390 var me = this;
391 if(!this.price_list) {
392 msgprint(__("Price List not found or disabled"));
393 return;
394 }
395
396 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530397
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530398 var $wrap = me.wrapper.find(".item-list");
399 me.wrapper.find(".item-list").empty();
400
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530401 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530402 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530403 if(index < 30){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530404 $(frappe.render_template("pos_item", {
405 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530406 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 item_name: obj.name===obj.item_name ? "" : obj.item_name,
408 item_image: obj.image ? "url('" + obj.image + "')" : null,
409 color: frappe.get_palette(obj.item_name),
410 abbr: frappe.get_abbr(obj.item_name)
411 })).tooltip().appendTo($wrap);
412 }
413 });
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530414 } else {
415 $("<h4>Searching record not found.</h4>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530416 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530417
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530418 if(this.items.length == 1
419 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530420 this.search.$input.val("");
421 this.add_to_cart();
422 }
423
424 // if form is local then allow this function
425 $(me.wrapper).find("div.pos-item").on("click", function() {
426 me.customer_validate();
427 if(me.frm.doc.docstatus==0) {
428 me.items = me.get_items($(this).attr("data-item-code"))
429 me.add_to_cart();
430 }
431 });
432 },
433
434 get_items: function(item_code){
435 // To search item as per the key enter
436
437 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530438 this.item_serial_no = {};
439 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530440
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530441 if(item_code){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530442 return $.grep(this.item_data, function(item){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530443 if(item.item_code == item_code ){
444 return true
445 }
446 })
447 }
448
Rohit Waghchaure9a360832016-11-24 20:00:23 +0530449 key = this.search.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g,'\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530450 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530451 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530452 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530453
454 if(key){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530455 return $.grep(this.item_data, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530456 if(search_status){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530457 if(in_list(me.batch_no_data[item.item_code], me.search.$input.val())){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530458 search_status = false;
459 return me.item_batch_no[item.item_code] = me.search.$input.val()
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530460 } else if( me.serial_no_data[item.item_code]
461 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530462 search_status = false;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530463 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 +0530464 return true
465 } else if(item.barcode == me.search.$input.val()) {
466 search_status = false;
467 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530468 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
469 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530470 return true
471 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530472 }
473 })
474 }else{
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530475 return this.item_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530476 }
477 },
478
479 update_qty: function() {
480 var me = this;
481
482 $(this.wrapper).find(".pos-item-qty").on("change", function(){
483 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530484 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530485 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530486
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530487 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
488 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
489 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530490 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530492
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530493 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
494 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
495 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530496 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530497 })
498 },
499
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530500 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530501 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530502
503 $(this.wrapper).find(".pos-item-rate").on("change", function(){
504 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
505 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
506 })
507 },
508
509 update_qty_rate_against_item_code: function(item_code, field, value){
510 var me = this;
511 if(value < 0){
512 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530513 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530514
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530515 this.remove_item = []
516 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530517 if(d.serial_no){
518 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530519 }
520
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530521 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530522 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530523 d.amount = flt(d.rate) * flt(d.qty);
524 if(d.qty==0){
525 me.remove_item.push(d.idx)
526 }
527 }
528 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530529
530 if(field == 'qty'){
531 this.remove_zero_qty_item();
532 }
533
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530534 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530535 },
536
537 remove_zero_qty_item: function(){
538 var me = this;
539 idx = 0
540 this.items = []
541 idx = 0
542 $.each(this.frm.doc["items"] || [], function(i, d) {
543 if(!in_list(me.remove_item, d.idx)){
544 d.idx = idx;
545 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530546 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530547 }
548 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530549
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530550 this.frm.doc["items"] = this.items;
551 },
552
553 make_discount_field: function(){
554 var me = this;
555
556 this.wrapper.find('input.discount-percentage').on("change", function() {
557 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
558 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530559
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530560 if(me.frm.doc.apply_discount_on == 'Net Total'){
561 total = me.frm.doc.net_total
562 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530563
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530564 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
565 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
566 me.refresh();
567 });
568
569 this.wrapper.find('input.discount-amount').on("change", function() {
570 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
571 me.frm.doc.additional_discount_percentage = 0.0;
572 me.wrapper.find('input.discount-percentage').val(0);
573 me.refresh();
574 });
575 },
576
577 customer_validate: function(){
578 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530579 if(!this.frm.doc.customer){
580 frappe.throw(__("Please select customer"))
581 }
582 },
583
584 add_to_cart: function() {
585 var me = this;
586 var caught = false;
587 var no_of_items = me.wrapper.find(".pos-bill-item").length;
588
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530589 this.customer_validate();
590 this.mandatory_batch_no();
591 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530592 this.validate_warehouse();
593
594 if (no_of_items != 0) {
595 $.each(this.frm.doc["items"] || [], function(i, d) {
596 if (d.item_code == me.items[0].item_code) {
597 caught = true;
598 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530599 d.amount = flt(d.rate) * flt(d.qty);
600 if(me.item_serial_no[d.item_code]){
601 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
602 d.warehouse = me.item_serial_no[d.item_code][1]
603 }
604
605 if(me.item_batch_no.length){
606 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530607 }
608 }
609 });
610 }
611
612 // if item not found then add new item
613 if (!caught)
614 this.add_new_item_to_grid();
615
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530616 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530617 },
618
619 add_new_item_to_grid: function() {
620 var me = this;
621 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
622 this.child.item_code = this.items[0].item_code;
623 this.child.item_name = this.items[0].item_name;
624 this.child.stock_uom = this.items[0].stock_uom;
625 this.child.description = this.items[0].description;
626 this.child.qty = 1;
627 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530628 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
629 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530630 this.child.warehouse = (this.item_serial_no[this.child.item_code]
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530631 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse) );
632 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
633 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
634 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530635 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530636 this.child.batch_no = this.item_batch_no[this.child.item_code];
637 this.child.serial_no = (this.item_serial_no[this.child.item_code]
638 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530639 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530640 },
641
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530642 update_paid_amount_status: function(update_paid_amount){
643 if(this.name){
644 update_paid_amount = update_paid_amount ? false : true;
645 }
646
647 this.refresh(update_paid_amount);
648 },
649
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530650 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530651 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530652 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530653 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530654 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 this.set_primary_action();
656 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530657
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530658 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530659 this.apply_pricing_rule();
660 this.discount_amount_applied = false;
661 this._calculate_taxes_and_totals();
662 this.calculate_discount_amount();
663 this.show_items_in_item_cart();
664 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530665 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530666 this.set_totals();
667 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530668
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530669 get_company_currency: function() {
670 return erpnext.get_currency(this.frm.doc.company);
671 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530672
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530673 show_item_wise_taxes: function(){
674 return null;
675 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530676
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530677 show_items_in_item_cart: function() {
678 var me = this;
679 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530680 $.each(this.frm.doc.items|| [], function(i, d) {
681 $(frappe.render_template("pos_bill_item", {
682 item_code: d.item_code,
683 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
684 qty: d.qty,
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530685 actual_qty: me.actual_qty_dict[d.item_code] || 0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530686 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530687 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530688 amount: format_currency(d.amount, me.frm.doc.currency)
689 })).appendTo($items);
690 });
691
692 this.wrapper.find("input.pos-item-qty").on("focus", function() {
693 $(this).select();
694 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530695
696 this.wrapper.find("input.pos-item-rate").on("focus", function() {
697 $(this).select();
698 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530699 },
700
701 set_taxes: function(){
702 var me = this;
703 me.frm.doc.total_taxes_and_charges = 0.0
704
705 var taxes = this.frm.doc.taxes || [];
706 $(this.wrapper)
707 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
708 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530709
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530710 $.each(taxes, function(i, d) {
711 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
712 $(frappe.render_template("pos_tax_row", {
713 description: d.description,
714 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
715 me.frm.doc.currency)
716 })).appendTo(me.wrapper.find(".tax-table"));
717 }
718 });
719 },
720
721 set_totals: function() {
722 var me = this;
723 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
724 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
725 },
726
727 set_primary_action: function() {
728 var me = this;
729
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530730 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530731 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530732 me.validate();
733 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530734 me.create_invoice();
735 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530736 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530737 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530738 this.page.set_primary_action(__("Print"), function() {
739 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530740 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530741 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530742 }else {
743 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530744 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530745
746 this.page.set_secondary_action(__("New"), function() {
747 me.save_previous_entry();
748 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530749 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530750 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530751
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530752 print_dialog: function(){
753 var me = this;
754
755 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
756 style="margin-right: 5px;">{0}</a>\
757 <a class="btn btn-default new_doc">{1}</a>', [
758 __('Print'), __('New')
759 ]));
760
761 $('.print_doc').click(function(){
762 html = frappe.render(me.print_template, me.frm.doc)
763 me.print_document(html)
764 })
765
766 $('.new_doc').click(function(){
767 msgprint.hide()
768 me.create_new();
769 })
770 },
771
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530772 print_document: function(html){
773 var w = window.open();
774 w.document.write(html);
775 w.document.close();
776 setTimeout(function(){
777 w.print();
778 w.close();
779 }, 1000)
780 },
781
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530782 submit_invoice: function(){
783 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530784 this.change_status();
785 if(this.frm.doc.docstatus == 1){
786 this.print_dialog()
787 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530788 },
789
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530790 change_status: function(){
791 if(this.frm.doc.docstatus == 0){
792 this.frm.doc.docstatus = 1;
793 this.update_invoice();
794 this.disable_input_field();
795 }
796 },
797
798 disable_input_field: function(){
799 var pointer_events = 'inherit'
800 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530801
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530802 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530803 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530804 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530805 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530806
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530807 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
808 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
809 this.set_primary_action();
810 },
811
812 create_invoice: function(){
813 var me = this;
814 var invoice_data = {}
815 this.si_docs = this.get_doc_from_localstorage();
816 if(this.name){
817 this.update_invoice()
818 }else{
819 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530820 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530821 this.frm.doc.posting_date = frappe.datetime.get_today();
822 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530823 invoice_data[this.name] = this.frm.doc
824 this.si_docs.push(invoice_data)
825 this.update_localstorage();
826 this.set_primary_action();
827 }
828 },
829
830 update_invoice: function(){
831 var me = this;
832 this.si_docs = this.get_doc_from_localstorage();
833 $.each(this.si_docs, function(index, data){
834 for(key in data){
835 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530836 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530837 me.update_localstorage();
838 }
839 }
840 })
841 },
842
843 update_localstorage: function(){
844 try{
845 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
846 }catch(e){
847 frappe.throw(__("LocalStorage is full , did not save"))
848 }
849 },
850
851 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530852 try{
853 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
854 }catch(e){
855 return []
856 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530857 },
858
859 set_interval_for_si_sync: function(){
860 var me = this;
861 setInterval(function(){
862 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530863 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530864 },
865
866 sync_sales_invoice: function(){
867 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530868 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530869
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530870 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530871 frappe.call({
872 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
873 args: {
874 doc_list: me.si_docs
875 },
876 callback: function(r){
877 if(r.message){
878 me.removed_items = r.message;
879 me.remove_doc_from_localstorage();
880 }
881 }
882 })
883 }
884 },
885
886 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530887 var invoices = [];
888 var index = 1;
889 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530890 if(docs){
891 invoices = $.map(docs, function(data){
892 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530893 if(data[key].docstatus == 1 && index < 50){
894 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530895 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530896 return data
897 }
898 }
899 });
900 }
901
902 return invoices
903 },
904
905 remove_doc_from_localstorage: function(){
906 var me = this;
907 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530908 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530909 if(this.removed_items){
910 $.each(this.si_docs, function(index, data){
911 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530912 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530913 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530914 }
915 }
916 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530917 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530918 this.update_localstorage();
919 }
920 },
921
922 validate: function(){
923 var me = this;
924 this.customer_validate();
925 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530926 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530927 },
928
929 item_validate: function(){
930 if(this.frm.doc.items.length == 0){
931 frappe.throw(__("Select items to save the invoice"))
932 }
933 },
Saurabh9a6c6662016-06-27 16:51:44 +0530934
935 validate_mode_of_payments: function(){
936 if (this.frm.doc.payments.length === 0){
937 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
938 }
939 },
940
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530941 validate_serial_no: function(){
942 var me = this;
943 var item_code = serial_no = '';
944 for (key in this.item_serial_no){
945 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530946 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530947 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530948
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530949 if(item_code && serial_no){
950 $.each(this.frm.doc.items, function(index, data){
951 if(data.item_code == item_code){
952 if(in_list(data.serial_no.split('\n'), serial_no)){
953 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
954 'serial_no': serial_no
955 })))
956 }
957 }
958 })
959 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530960
961 if(this.items[0].has_serial_no && serial_no == ""){
962 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
963 'item': this.items[0].item_code
964 })))
965 }
966 },
967
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530968 validate_serial_no_qty: function(args, item_code, field, value){
969 var me = this;
970 if (args.item_code == item_code && args.serial_no
971 && field == 'qty' && cint(value) != value) {
972 args.qty = 0.0;
973 this.refresh();
974 frappe.throw(__("Serial no item cannot be a fraction"))
975 }
976
977 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
978 args.qty = 0.0;
979 args.serial_no = ''
980 this.refresh();
981 frappe.throw(__("Total nos of serial no is not equal to quantity."))
982 }
983 },
984
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530985 mandatory_batch_no: function(){
986 var me = this;
987 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
988 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
989 'item': this.items[0].item_code
990 })))
991 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530992 },
993
994 apply_pricing_rule: function(){
995 var me = this;
996 $.each(this.frm.doc["items"], function(n, item) {
997 pricing_rule = me.get_pricing_rule(item)
998 me.validate_pricing_rule(pricing_rule)
999 if(pricing_rule.length){
1000 item.margin_type = pricing_rule[0].margin_type;
1001 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1002 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1003 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
1004 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301005 } else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) {
1006 item.margin_rate_or_amount = 0.0;
1007 item.discount_percentage = 0.0;
1008 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301009 }
1010 })
1011 },
1012
1013 get_pricing_rule: function(item){
1014 var me = this;
1015 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301016 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1017 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1018 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1019 return me.validate_condition(data)
1020 }else{
1021 return true
1022 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301023 }
1024 }
1025 })
1026 },
1027
1028 validate_condition: function(data){
1029 //This method check condition based on applicable for
1030 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1031 if(in_list(condition[1], condition[0])){
1032 return true
1033 }
1034 },
1035
1036 get_mapper_for_pricing_rule: function(data){
1037 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301038 'Customer': [data.customer, [this.frm.doc.customer]],
1039 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1040 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301041 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301042 }
1043 },
1044
1045 validate_pricing_rule: function(pricing_rule){
1046 //This method validate duplicate pricing rule
1047 var pricing_rule_name = '';
1048 var priority = 0;
1049 var pricing_rule_list = [];
1050 var priority_list = []
1051
1052 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301053
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301054 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301055 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301056 priority_list.push(data.priority)
1057 if(priority <= data.priority){
1058 priority = data.priority
1059 pricing_rule_list.push(data)
1060 }
1061 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301062
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301063 count = 0
1064 $.each(priority_list, function(index, value){
1065 if(value == priority){
1066 count++
1067 }
1068 })
1069
1070 if(priority == 0 || count > 1){
1071 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1072 'pricing_rule': pricing_rule_name
1073 })))
1074 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301075
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301076 return pricing_rule_list
1077 }
1078 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301079
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301080 validate_warehouse: function(){
Nabin Hait977eff92016-11-21 18:29:16 +05301081 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 +05301082 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301083 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301084 },
1085
1086 get_actual_qty: function(item) {
1087 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301088
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301089 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
1090 if(warehouse && this.bin_data[item.item_code]) {
1091 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301092 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301093 }
1094
1095 return this.actual_qty
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301096 }
1097})