blob: 26bf3ea54c361dc4d2bf6ae9ac79be1e97de3c4c [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 Waghchaureea278b52016-07-21 23:28:04 +0530249 if(load_doc) {
250 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
251 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530252
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530253 $.each(this.meta, function(i, data){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530254 frappe.meta.sync(data)
255 })
256
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530257 this.print_template = frappe.render_template("print_template",
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530258 {content: this.print_template, title:"POS",
Rohit Waghchaure3c4cdd22016-09-19 00:17:32 +0530259 base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css})
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530260 },
261
262 setup: function(){
263 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
264 this.set_transaction_defaults("Customer");
265 this.make();
266 this.set_primary_action();
267 },
268
269 set_transaction_defaults: function(party) {
270 var me = this;
271 this.party = party;
272 this.price_list = (party == "Customer" ?
273 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
274 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
275 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
276 },
277
278 make: function() {
279 this.make_search();
280 this.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530281 this.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530282 this.make_discount_field()
283 },
284
285 make_search: function() {
286 var me = this;
287 this.search = frappe.ui.form.make_control({
288 df: {
289 "fieldtype": "Data",
290 "label": "Item",
291 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530292 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530293 },
294 parent: this.wrapper.find(".search-area"),
295 only_input: true,
296 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530297
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 this.search.make_input();
299 this.search.$input.on("keyup", function() {
300 setTimeout(function() {
301 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530302 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 }, 1000);
304 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530305
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530306 this.party_field = frappe.ui.form.make_control({
307 df: {
308 "fieldtype": "Data",
309 "options": this.party,
310 "label": this.party,
311 "fieldname": this.party.toLowerCase(),
Rushabh Mehtae0686b32016-06-10 12:34:41 +0530312 "placeholder": __("Select or add new customer")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530313 },
314 parent: this.wrapper.find(".party-area"),
315 only_input: true,
316 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530317
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530318 this.party_field.make_input();
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530319 this.set_focus()
320 },
321
322 set_focus: function(){
323 if(this.default_customer){
324 this.search.$input.focus();
325 }else{
326 this.party_field.$input.focus();
327 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530328 },
329
330 make_customer: function() {
331 var me = this;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530332
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530333 if(this.default_customer && !this.frm.doc.customer){
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530334 this.party_field.$input.val(this.default_customer);
335 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530336 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530337
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 this.party_field.$input.autocomplete({
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530339 autoFocus: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 source: function (request, response) {
341 me.customer_data = me.get_customers(request.term)
342 response($.map(me.customer_data, function(data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530343 return {label: data.name, value: data.name,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 customer_group: data.customer_group, territory: data.territory}
345 }))
346 },
347 change: function(event, ui){
348 if(ui.item){
349 me.frm.doc.customer = ui.item.label;
350 me.frm.doc.customer_name = ui.item.customer_name;
351 me.frm.doc.customer_group = ui.item.customer_group;
352 me.frm.doc.territory = ui.item.territory;
353 }else{
354 me.frm.doc.customer = me.party_field.$input.val();
355 }
356 me.refresh();
357 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530358 }).on("focus", function(){
359 setTimeout(function() {
360 if(!me.party_field.$input.val()) {
361 me.party_field.$input.autocomplete( "search", " " );
362 }
363 }, 500);
364 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530365 },
366
367 get_customers: function(key){
368 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530369 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530370 var re = new RegExp('%', 'g');
371 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
372
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530373 if(key){
374 return $.grep(this.customers, function(data) {
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530375 if(reg.test(data.name.toLowerCase())
376 || reg.test(data.customer_name.toLowerCase())
377 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530378 return data
379 }
380 })
381 }else{
382 customers = this.customers.sort(function(a,b){ return a.idx < b.idx })
383 return customers.slice(0, 20)
384 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530385 },
386
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530387 make_item_list: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530388 var me = this;
389 if(!this.price_list) {
390 msgprint(__("Price List not found or disabled"));
391 return;
392 }
393
394 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530395
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530396 var $wrap = me.wrapper.find(".item-list");
397 me.wrapper.find(".item-list").empty();
398
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530399 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530400 $.each(this.items, function(index, obj) {
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530401 if(index < 30){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530402 $(frappe.render_template("pos_item", {
403 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530404 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530405 item_name: obj.name===obj.item_name ? "" : obj.item_name,
406 item_image: obj.image ? "url('" + obj.image + "')" : null,
407 color: frappe.get_palette(obj.item_name),
408 abbr: frappe.get_abbr(obj.item_name)
409 })).tooltip().appendTo($wrap);
410 }
411 });
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530412 } else {
413 $("<h4>Searching record not found.</h4>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530414 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530415
Rohit Waghchaure28c02fe2016-06-10 00:48:03 +0530416 if(this.items.length == 1
417 && this.search.$input.val()) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530418 this.search.$input.val("");
419 this.add_to_cart();
420 }
421
422 // if form is local then allow this function
423 $(me.wrapper).find("div.pos-item").on("click", function() {
424 me.customer_validate();
425 if(me.frm.doc.docstatus==0) {
426 me.items = me.get_items($(this).attr("data-item-code"))
427 me.add_to_cart();
428 }
429 });
430 },
431
432 get_items: function(item_code){
433 // To search item as per the key enter
434
435 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530436 this.item_serial_no = {};
437 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530438
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530439 if(item_code){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530440 return $.grep(this.item_data, function(item){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530441 if(item.item_code == item_code ){
442 return true
443 }
444 })
445 }
446
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530447 key = this.search.$input.val().toLowerCase();
448 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530449 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530450 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530451
452 if(key){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530453 return $.grep(this.item_data, function(item){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530454 if(search_status){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530455 if(in_list(me.batch_no_data[item.item_code], me.search.$input.val())){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530456 search_status = false;
457 return me.item_batch_no[item.item_code] = me.search.$input.val()
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530458 } else if( me.serial_no_data[item.item_code]
459 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530460 search_status = false;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530461 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 +0530462 return true
463 } else if(item.barcode == me.search.$input.val()) {
464 search_status = false;
465 return item.barcode == me.search.$input.val();
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530466 } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
467 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530468 return true
469 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530470 }
471 })
472 }else{
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530473 return this.item_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530474 }
475 },
476
477 update_qty: function() {
478 var me = this;
479
480 $(this.wrapper).find(".pos-item-qty").on("change", function(){
481 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530482 me.update_qty_rate_against_item_code(item_code, "qty", $(this).val());
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530483 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530484
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530485 $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){
486 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
487 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530488 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530489 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530490
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530491 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){
492 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
493 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530494 me.update_qty_rate_against_item_code(item_code, "qty", qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530495 })
496 },
497
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530498 update_rate: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530499 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530500
501 $(this.wrapper).find(".pos-item-rate").on("change", function(){
502 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
503 me.update_qty_rate_against_item_code(item_code, "rate", $(this).val());
504 })
505 },
506
507 update_qty_rate_against_item_code: function(item_code, field, value){
508 var me = this;
509 if(value < 0){
510 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530511 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530512
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530513 this.remove_item = []
514 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530515 if(d.serial_no){
516 me.validate_serial_no_qty(d, item_code, field, value)
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530517 }
518
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530519 if (d.item_code == item_code) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530520 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530521 d.amount = flt(d.rate) * flt(d.qty);
522 if(d.qty==0){
523 me.remove_item.push(d.idx)
524 }
525 }
526 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530527
528 if(field == 'qty'){
529 this.remove_zero_qty_item();
530 }
531
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530532 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530533 },
534
535 remove_zero_qty_item: function(){
536 var me = this;
537 idx = 0
538 this.items = []
539 idx = 0
540 $.each(this.frm.doc["items"] || [], function(i, d) {
541 if(!in_list(me.remove_item, d.idx)){
542 d.idx = idx;
543 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530544 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530545 }
546 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530547
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530548 this.frm.doc["items"] = this.items;
549 },
550
551 make_discount_field: function(){
552 var me = this;
553
554 this.wrapper.find('input.discount-percentage').on("change", function() {
555 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
556 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530557
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530558 if(me.frm.doc.apply_discount_on == 'Net Total'){
559 total = me.frm.doc.net_total
560 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530561
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530562 me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
563 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
564 me.refresh();
565 });
566
567 this.wrapper.find('input.discount-amount').on("change", function() {
568 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
569 me.frm.doc.additional_discount_percentage = 0.0;
570 me.wrapper.find('input.discount-percentage').val(0);
571 me.refresh();
572 });
573 },
574
575 customer_validate: function(){
576 var me = this;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530577 if(!this.frm.doc.customer){
578 frappe.throw(__("Please select customer"))
579 }
580 },
581
582 add_to_cart: function() {
583 var me = this;
584 var caught = false;
585 var no_of_items = me.wrapper.find(".pos-bill-item").length;
586
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530587 this.customer_validate();
588 this.mandatory_batch_no();
589 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530590 this.validate_warehouse();
591
592 if (no_of_items != 0) {
593 $.each(this.frm.doc["items"] || [], function(i, d) {
594 if (d.item_code == me.items[0].item_code) {
595 caught = true;
596 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530597 d.amount = flt(d.rate) * flt(d.qty);
598 if(me.item_serial_no[d.item_code]){
599 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
600 d.warehouse = me.item_serial_no[d.item_code][1]
601 }
602
603 if(me.item_batch_no.length){
604 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530605 }
606 }
607 });
608 }
609
610 // if item not found then add new item
611 if (!caught)
612 this.add_new_item_to_grid();
613
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530614 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530615 },
616
617 add_new_item_to_grid: function() {
618 var me = this;
619 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
620 this.child.item_code = this.items[0].item_code;
621 this.child.item_name = this.items[0].item_name;
622 this.child.stock_uom = this.items[0].stock_uom;
623 this.child.description = this.items[0].description;
624 this.child.qty = 1;
625 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530626 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
627 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530628 this.child.warehouse = (this.item_serial_no[this.child.item_code]
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530629 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse) );
630 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
631 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
632 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530633 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530634 this.child.batch_no = this.item_batch_no[this.child.item_code];
635 this.child.serial_no = (this.item_serial_no[this.child.item_code]
636 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530637 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530638 },
639
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530640 update_paid_amount_status: function(update_paid_amount){
641 if(this.name){
642 update_paid_amount = update_paid_amount ? false : true;
643 }
644
645 this.refresh(update_paid_amount);
646 },
647
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530648 refresh: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530649 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530650 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530651 this.update_qty();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530652 this.update_rate();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530653 this.set_primary_action();
654 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530655
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530656 refresh_fields: function(update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530657 this.apply_pricing_rule();
658 this.discount_amount_applied = false;
659 this._calculate_taxes_and_totals();
660 this.calculate_discount_amount();
661 this.show_items_in_item_cart();
662 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530663 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530664 this.set_totals();
665 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530666
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530667 get_company_currency: function() {
668 return erpnext.get_currency(this.frm.doc.company);
669 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530670
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530671 show_item_wise_taxes: function(){
672 return null;
673 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530674
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530675 show_items_in_item_cart: function() {
676 var me = this;
677 var $items = this.wrapper.find(".items").empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530678 $.each(this.frm.doc.items|| [], function(i, d) {
679 $(frappe.render_template("pos_bill_item", {
680 item_code: d.item_code,
681 item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
682 qty: d.qty,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530683 actual_qty: me.actual_qty,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530684 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530685 rate: format_number(d.rate, me.frm.doc.currency),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530686 amount: format_currency(d.amount, me.frm.doc.currency)
687 })).appendTo($items);
688 });
689
690 this.wrapper.find("input.pos-item-qty").on("focus", function() {
691 $(this).select();
692 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530693
694 this.wrapper.find("input.pos-item-rate").on("focus", function() {
695 $(this).select();
696 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530697 },
698
699 set_taxes: function(){
700 var me = this;
701 me.frm.doc.total_taxes_and_charges = 0.0
702
703 var taxes = this.frm.doc.taxes || [];
704 $(this.wrapper)
705 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
706 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530707
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 $.each(taxes, function(i, d) {
709 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
710 $(frappe.render_template("pos_tax_row", {
711 description: d.description,
712 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
713 me.frm.doc.currency)
714 })).appendTo(me.wrapper.find(".tax-table"));
715 }
716 });
717 },
718
719 set_totals: function() {
720 var me = this;
721 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
722 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
723 },
724
725 set_primary_action: function() {
726 var me = this;
727
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530728 if (this.frm.doc.docstatus==0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530729 this.page.set_primary_action(__("Pay"), function() {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530730 me.validate();
731 me.update_paid_amount_status(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 me.create_invoice();
733 me.make_payment();
Faris Ansari8908d192016-07-20 13:59:19 +0530734 }, "octicon octicon-credit-card");
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530735 }else if(this.frm.doc.docstatus == 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530736 this.page.set_primary_action(__("Print"), function() {
737 html = frappe.render(me.print_template, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530738 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530739 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530740 }else {
741 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530742 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530743
744 this.page.set_secondary_action(__("New"), function() {
745 me.save_previous_entry();
746 me.create_new();
Faris Ansari8908d192016-07-20 13:59:19 +0530747 }, "octicon octicon-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530748 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530749
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530750 print_dialog: function(){
751 var me = this;
752
753 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
754 style="margin-right: 5px;">{0}</a>\
755 <a class="btn btn-default new_doc">{1}</a>', [
756 __('Print'), __('New')
757 ]));
758
759 $('.print_doc').click(function(){
760 html = frappe.render(me.print_template, me.frm.doc)
761 me.print_document(html)
762 })
763
764 $('.new_doc').click(function(){
765 msgprint.hide()
766 me.create_new();
767 })
768 },
769
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530770 print_document: function(html){
771 var w = window.open();
772 w.document.write(html);
773 w.document.close();
774 setTimeout(function(){
775 w.print();
776 w.close();
777 }, 1000)
778 },
779
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530780 submit_invoice: function(){
781 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530782 this.change_status();
783 if(this.frm.doc.docstatus == 1){
784 this.print_dialog()
785 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530786 },
787
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530788 change_status: function(){
789 if(this.frm.doc.docstatus == 0){
790 this.frm.doc.docstatus = 1;
791 this.update_invoice();
792 this.disable_input_field();
793 }
794 },
795
796 disable_input_field: function(){
797 var pointer_events = 'inherit'
798 $(this.wrapper).find('input').attr("disabled", false);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530799
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530800 if(this.frm.doc.docstatus == 1){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530801 pointer_events = 'none';
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530802 $(this.wrapper).find('input').attr("disabled", true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530803 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530804
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530805 $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events);
806 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
807 this.set_primary_action();
808 },
809
810 create_invoice: function(){
811 var me = this;
812 var invoice_data = {}
813 this.si_docs = this.get_doc_from_localstorage();
814 if(this.name){
815 this.update_invoice()
816 }else{
817 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +0530818 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +0530819 this.frm.doc.posting_date = frappe.datetime.get_today();
820 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530821 invoice_data[this.name] = this.frm.doc
822 this.si_docs.push(invoice_data)
823 this.update_localstorage();
824 this.set_primary_action();
825 }
826 },
827
828 update_invoice: function(){
829 var me = this;
830 this.si_docs = this.get_doc_from_localstorage();
831 $.each(this.si_docs, function(index, data){
832 for(key in data){
833 if(key == me.name){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530834 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530835 me.update_localstorage();
836 }
837 }
838 })
839 },
840
841 update_localstorage: function(){
842 try{
843 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
844 }catch(e){
845 frappe.throw(__("LocalStorage is full , did not save"))
846 }
847 },
848
849 get_doc_from_localstorage: function(){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530850 try{
851 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
852 }catch(e){
853 return []
854 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530855 },
856
857 set_interval_for_si_sync: function(){
858 var me = this;
859 setInterval(function(){
860 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530861 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530862 },
863
864 sync_sales_invoice: function(){
865 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530866 this.si_docs = this.get_submitted_invoice();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530867
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530868 if(this.si_docs.length){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530869 frappe.call({
870 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
871 args: {
872 doc_list: me.si_docs
873 },
874 callback: function(r){
875 if(r.message){
876 me.removed_items = r.message;
877 me.remove_doc_from_localstorage();
878 }
879 }
880 })
881 }
882 },
883
884 get_submitted_invoice: function(){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530885 var invoices = [];
886 var index = 1;
887 docs = this.get_doc_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530888 if(docs){
889 invoices = $.map(docs, function(data){
890 for(key in data){
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530891 if(data[key].docstatus == 1 && index < 50){
892 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530893 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530894 return data
895 }
896 }
897 });
898 }
899
900 return invoices
901 },
902
903 remove_doc_from_localstorage: function(){
904 var me = this;
905 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530906 this.new_si_docs = [];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530907 if(this.removed_items){
908 $.each(this.si_docs, function(index, data){
909 for(key in data){
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530910 if(!in_list(me.removed_items, key)){
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530911 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530912 }
913 }
914 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530915 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530916 this.update_localstorage();
917 }
918 },
919
920 validate: function(){
921 var me = this;
922 this.customer_validate();
923 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530924 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530925 },
926
927 item_validate: function(){
928 if(this.frm.doc.items.length == 0){
929 frappe.throw(__("Select items to save the invoice"))
930 }
931 },
Saurabh9a6c6662016-06-27 16:51:44 +0530932
933 validate_mode_of_payments: function(){
934 if (this.frm.doc.payments.length === 0){
935 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
936 }
937 },
938
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530939 validate_serial_no: function(){
940 var me = this;
941 var item_code = serial_no = '';
942 for (key in this.item_serial_no){
943 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530944 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530945 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530946
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530947 if(item_code && serial_no){
948 $.each(this.frm.doc.items, function(index, data){
949 if(data.item_code == item_code){
950 if(in_list(data.serial_no.split('\n'), serial_no)){
951 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
952 'serial_no': serial_no
953 })))
954 }
955 }
956 })
957 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530958
959 if(this.items[0].has_serial_no && serial_no == ""){
960 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
961 'item': this.items[0].item_code
962 })))
963 }
964 },
965
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530966 validate_serial_no_qty: function(args, item_code, field, value){
967 var me = this;
968 if (args.item_code == item_code && args.serial_no
969 && field == 'qty' && cint(value) != value) {
970 args.qty = 0.0;
971 this.refresh();
972 frappe.throw(__("Serial no item cannot be a fraction"))
973 }
974
975 if(args.serial_no && args.serial_no.split('\n').length != cint(value)){
976 args.qty = 0.0;
977 args.serial_no = ''
978 this.refresh();
979 frappe.throw(__("Total nos of serial no is not equal to quantity."))
980 }
981 },
982
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530983 mandatory_batch_no: function(){
984 var me = this;
985 if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){
986 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
987 'item': this.items[0].item_code
988 })))
989 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530990 },
991
992 apply_pricing_rule: function(){
993 var me = this;
994 $.each(this.frm.doc["items"], function(n, item) {
995 pricing_rule = me.get_pricing_rule(item)
996 me.validate_pricing_rule(pricing_rule)
997 if(pricing_rule.length){
998 item.margin_type = pricing_rule[0].margin_type;
999 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1000 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1001 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
1002 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301003 } else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) {
1004 item.margin_rate_or_amount = 0.0;
1005 item.discount_percentage = 0.0;
1006 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301007 }
1008 })
1009 },
1010
1011 get_pricing_rule: function(item){
1012 var me = this;
1013 return $.grep(this.pricing_rules, function(data){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301014 if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){
1015 if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1016 if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){
1017 return me.validate_condition(data)
1018 }else{
1019 return true
1020 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301021 }
1022 }
1023 })
1024 },
1025
1026 validate_condition: function(data){
1027 //This method check condition based on applicable for
1028 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
1029 if(in_list(condition[1], condition[0])){
1030 return true
1031 }
1032 },
1033
1034 get_mapper_for_pricing_rule: function(data){
1035 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301036 'Customer': [data.customer, [this.frm.doc.customer]],
1037 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1038 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301039 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301040 }
1041 },
1042
1043 validate_pricing_rule: function(pricing_rule){
1044 //This method validate duplicate pricing rule
1045 var pricing_rule_name = '';
1046 var priority = 0;
1047 var pricing_rule_list = [];
1048 var priority_list = []
1049
1050 if(pricing_rule.length > 1){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301051
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301052 $.each(pricing_rule, function(index, data){
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301053 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301054 priority_list.push(data.priority)
1055 if(priority <= data.priority){
1056 priority = data.priority
1057 pricing_rule_list.push(data)
1058 }
1059 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301060
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301061 count = 0
1062 $.each(priority_list, function(index, value){
1063 if(value == priority){
1064 count++
1065 }
1066 })
1067
1068 if(priority == 0 || count > 1){
1069 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1070 'pricing_rule': pricing_rule_name
1071 })))
1072 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301073
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301074 return pricing_rule_list
1075 }
1076 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301077
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301078 validate_warehouse: function(){
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301079 if(!this.items[0].default_warehouse && !this.pos_profile_data['warehouse']){
Neil Trini Lasrado5ddd7ac2016-10-24 15:25:33 +05301080 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301081 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301082 },
1083
1084 get_actual_qty: function(item) {
1085 this.actual_qty = 0.0;
1086 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
1087 if(warehouse && this.bin_data[item.item_code]) {
1088 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
1089 }
1090
1091 return this.actual_qty
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301092 }
1093})