blob: 1b670d113d9db1ae847006877588626a3ed2e018 [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
rohitwaghchaurec13dbd42017-02-01 18:07:22 +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
rohitwaghchaurec13dbd42017-02-01 18:07:22 +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
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053020erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053021 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053022 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053023 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053024 this.page = wrapper.page;
25 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053026 this.set_indicator();
27 this.onload();
28 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053029 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053030 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053031 this.si_docs = this.get_doc_from_localstorage();
32 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053033
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053034 beforeunload: function (e) {
35 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053036 e = e || window.event;
37
38 // For IE and Firefox prior to version 4
39 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053040 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053041 return
42 }
43
44 // For Safari
45 return __("You are in offline mode. You will not be able to reload until you have network.");
46 }
47 },
48
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053050 var me = this;
51 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053052 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053053 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053054 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053055 },
56
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053057 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053058 var me = this;
59 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053060 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053061 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053063 method: "frappe.handler.ping",
64 callback: function (r) {
65 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053067 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053068 }
69 }
70 })
71 },
72
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053073 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053074 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053075 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053076 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053077 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053078 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 },
80
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053081 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053082 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053083 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053084
85 // for mobile
86 this.page.add_menu_item(__("Pay"), function () {
87 me.validate();
88 me.update_paid_amount_status(true);
89 me.create_invoice();
90 me.make_payment();
91 }).addClass('visible-xs');
92
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053093 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053094 me.save_previous_entry();
95 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053096 })
97
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053098 this.page.add_menu_item(__("Sync Master Data"), function () {
99 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530100 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530101 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530102 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530103 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530105
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530106 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530107 me.sync_sales_invoice()
108 });
109
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530110 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530111 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530112 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530113 },
114
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530115 email_prompt: function() {
116 var me = this;
117 fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
118 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
119 {fieldtype: "Section Break"},
120 {label:__("Subject"), fieldtype:"Data", reqd: 1,
121 fieldname:"subject",length:524288},
122 {fieldtype: "Section Break"},
123 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
124 fieldname:"content"},
125 {fieldtype: "Section Break"},
126 {fieldtype: "Column Break"}];
127
128 this.email_dialog = new frappe.ui.Dialog({
129 title: "Email",
130 fields: fields,
131 primary_action_label: __("Send"),
132 primary_action: function() {
133 me.send_action();
134 }
135 });
136
137 this.email_dialog.show()
138 },
139
140 send_action: function() {
141 this.email_queue = this.get_email_queue()
142 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
143 this.update_email_queue()
144 this.email_dialog.hide()
145 },
146
147 update_email_queue: function () {
148 try {
149 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
150 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530151 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530152 }
153 },
154
155 get_email_queue: function () {
156 try {
157 return JSON.parse(localStorage.getItem('email_queue')) || {};
158 } catch (e) {
159 return {}
160 }
161 },
162
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530163 get_customers_details: function () {
164 try {
165 return JSON.parse(localStorage.getItem('customer_details')) || {};
166 } catch (e) {
167 return {}
168 }
169 },
170
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530171 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530172 var me = this;
173
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530174 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530175 me.removed_items = [];
176 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530177 if ($(this).is(":checked")) {
178 $.each(me.si_docs, function (index, data) {
179 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530180 me.removed_items.push(key)
181 }
182 })
183 }
184
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530185 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530186 })
187
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530188 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530189 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530190 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530191 me.removed_items.push(me.name);
192 } else {
193 me.removed_items.pop(me.name)
194 }
195
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530196 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 })
198 },
199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 var me = this;
202
203 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530205 this.frm.doc = doc_data[0][this.name];
206 this.set_missing_values();
207 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530208 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530209 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 }
211 },
212
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530213 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530215 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530216 this.remove_doc_from_localstorage()
217 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530218 // this.dialog_actions();
219 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530220 },
221
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530222 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530223 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 this.si_docs = this.get_submitted_invoice()
225 $.each(this.removed_items, function(index, name){
226 $.each(me.si_docs, function(key, data){
227 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
228 frappe.throw(__("Submitted orders can not be deleted"))
229 }
230 })
231 })
232 },
233
234 toggle_delete_button: function () {
235 var me = this;
236 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530237 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530238 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530239 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530240 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530241 }
242 }
243 },
244
245 get_doctype_status: function (doc) {
246 if (doc.docstatus == 0) {
247 return { status: "Draft", indicator: "red" }
248 } else if (doc.outstanding_amount == 0) {
249 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530250 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530251 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530252 }
253 },
254
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530255 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530256 var me = this;
257 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530258 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530259 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530260 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530261 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530262
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530263 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530264 this.party_field.$input.val(this.frm.doc.customer);
265 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530266
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530267 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530268 this.frm.doc.write_off_account = doc.write_off_account
269 }
270
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530271 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530272 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530273 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530274 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530276 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530277 var me = this;
278 this.si_docs = this.get_doc_from_localstorage();
279
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530280 return $.grep(this.si_docs, function (data) {
281 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530282 return key == me.name
283 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530284 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530285 },
286
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530287 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530288 var me = this;
289 frappe.call({
290 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
291 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530292 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530293 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530295 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530296 me.set_interval_for_si_sync();
297 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530298 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530299 callback();
300 }
301 }
302 })
303 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530304
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530305 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530306 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530307 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530308 this.meta = r.message.meta;
309 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530310 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530311 this.customers = r.message.customers;
312 this.serial_no_data = r.message.serial_no_data;
313 this.batch_no_data = r.message.batch_no_data;
314 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530315 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530316 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530317 this.price_list_data = r.message.price_list_data;
318 this.bin_data = r.message.bin_data;
319 this.pricing_rules = r.message.pricing_rules;
320 this.print_template = r.message.print_template;
321 this.pos_profile_data = r.message.pos_profile;
322 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530323 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530324 this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530325 },
326
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530327 save_previous_entry: function () {
328 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
329 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530330 }
331 },
332
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530333 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530334 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530335 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530336 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530337 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530339 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530340 },
341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530343 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530344
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530345 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530346 this.actual_qty_dict = {};
347
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530348 if (load_doc) {
349 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530350 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530351
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530352 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530354 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530355 })
356
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530357 this.print_template_data = frappe.render_template("print_template", {
358 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530359 title: "POS",
360 base_url: frappe.urllib.get_base_url(),
361 print_css: frappe.boot.print_css,
362 print_settings: this.print_settings,
363 header: this.letter_head.header,
364 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530365 landscape: false,
366 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530367 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530368 },
369
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530370 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530371 this.make();
372 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530373 this.party_field.$input.attr('disabled', false);
374 if(this.selected_row) {
375 this.selected_row.hide()
376 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530377 },
378
Rohit Waghchaure61165122017-05-02 13:04:08 +0530379 set_default_customer: function() {
380 if (this.default_customer && !this.frm.doc.customer) {
381 this.party_field.$input.val(this.default_customer);
382 this.frm.doc.customer = this.default_customer;
383 this.numeric_keypad.show();
384 this.toggle_list_customer(false)
385 this.toggle_item_cart(true)
386 }
387 },
388
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530389 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530390 var me = this;
391 this.party = party;
392 this.price_list = (party == "Customer" ?
393 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
394 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
395 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
396 },
397
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530398 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530399 this.make_item_list();
400 this.make_discount_field()
401 },
402
403 make_control: function() {
404 this.frm = {}
405 this.frm.doc = this.doc
406 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530407 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530408 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530409 this.make_search();
410 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530411 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530412 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 },
414
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530415 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530416 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530417 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530418 df: {
419 "fieldtype": "Data",
420 "label": "Item",
421 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530422 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530423 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530424 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530425 only_input: true,
426 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530427
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530428 this.serach_item.make_input();
429 this.serach_item.$input.on("keyup", function () {
430 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530431 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530432 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530433 }, 1000);
434 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530435
Faris Ansari339d9c92017-03-07 18:14:06 +0530436 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530437 sorted_item_groups = this.get_sorted_item_groups()
438 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530439 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
440 }).join("");
441
442 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
443
444 this.search_item_group.on('click', '.dropdown-menu a', function() {
445 me.selected_item_group = $(this).attr('data-value');
446 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
447
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530448 me.page_len = 20;
449 me.items = me.get_items();
450 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530451 })
452
Faris Ansari45510102017-03-09 14:43:37 +0530453 me.toggle_more_btn();
454
455 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530456 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530457 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530458 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530459 me.toggle_more_btn();
460 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530461
Faris Ansari45510102017-03-09 14:43:37 +0530462 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530463 me.update_customer()
464 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530465 },
466
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530467 get_sorted_item_groups: function() {
468 list = {}
469 $.each(this.item_groups, function(i, data) {
470 list[i] = data[0]
471 })
472
473 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
474 },
475
Faris Ansari45510102017-03-09 14:43:37 +0530476 toggle_more_btn: function() {
477 if(!this.items || this.items.length <= this.page_len) {
478 this.wrapper.find(".btn-more").hide();
479 } else {
480 this.wrapper.find(".btn-more").show();
481 }
482 },
483
484 toggle_totals_area: function(show) {
485
Faris Ansari07c9f352017-03-09 17:03:14 +0530486 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530487 show = this.is_totals_area_collapsed;
488 }
489
490 var totals_area = this.wrapper.find('.totals-area');
491 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
492 .toggle(show);
493
494 if(show) {
495 totals_area.find('.collapse-btn i')
496 .removeClass('octicon-chevron-down')
497 .addClass('octicon-chevron-up');
498 } else {
499 totals_area.find('.collapse-btn i')
500 .removeClass('octicon-chevron-up')
501 .addClass('octicon-chevron-down');
502 }
503
504 this.is_totals_area_collapsed = !show;
505 },
506
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530507 make_list_customers: function () {
508 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530509 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530510 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530511 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530512 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530513 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530514 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530515
Faris Ansari45510102017-03-09 14:43:37 +0530516 me.render_list_customers();
517 me.toggle_totals_area(false);
518
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530519 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530520 $(this).toggleClass("view_customer");
521 if($(this).hasClass("view_customer")) {
522 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530523 me.list_customers.show();
524 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530525 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530526 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530527 } else {
528 if(me.frm.doc.docstatus == 0) {
529 me.party_field.$input.attr('disabled', false);
530 }
531 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530532 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530533 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530534 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530535 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530536 }
537 });
538 this.add_customer_btn.on('click', function() {
539 me.save_previous_entry();
540 me.create_new();
541 me.refresh();
542 me.set_focus();
543 });
Faris Ansari45510102017-03-09 14:43:37 +0530544 this.pos_bill.on('click', '.collapse-btn', function() {
545 me.toggle_totals_area();
546 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530547 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530548
549 bind_numeric_keypad: function() {
550 var me = this;
551 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
552 me.numeric_val = '';
553 })
554
555 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
556 me.numeric_id = $(this).attr("id") || me.numeric_id;
557 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530558 if(me.numeric_id) {
559 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
560 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530561
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530562 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530563 me.numeric_val += me.val;
564 me.selected_field.val(flt(me.numeric_val))
565 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530566 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530567 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530568
569 if(me.numeric_id && $(this).hasClass('pos-operation')) {
570 me.numeric_keypad.find('button.pos-operation').removeClass('active');
571 $(this).addClass('active');
572
573 me.selected_row.find('.pos-list-row').removeClass('active');
574 me.selected_field.closest('.pos-list-row').addClass('active');
575 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530576 })
577
578 $(this.numeric_keypad).find('.numeric-del').click(function(){
579 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
580 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
581 me.selected_field.val(me.numeric_val);
582 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530583 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530584 })
585
586 $(this.numeric_keypad).find('.pos-pay').click(function(){
587 me.validate();
588 me.update_paid_amount_status(true);
589 me.create_invoice();
590 me.make_payment();
591 })
592 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530593
594 render_list_customers: function () {
595 var me = this;
596
597 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530598 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530599 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530600 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530601 return;
602 }
Faris Ansari45510102017-03-09 14:43:37 +0530603
604 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530605 if(this.si_docs.length) {
606 this.si_docs.forEach(function (data, i) {
607 for (key in data) {
608 html += frappe.render_template("pos_invoice_list", {
609 sr: i + 1,
610 name: key,
611 customer: data[key].customer,
612 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
613 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
614 data: me.get_doctype_status(data[key])
615 });
616 }
617 });
618 }
Faris Ansari45510102017-03-09 14:43:37 +0530619 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530620
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530621 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530622 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530623 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530624 me.list_customers_btn.toggleClass("view_customer");
625 me.pos_bill.show();
626 me.list_customers_btn.show();
627 me.name = $(this).parents().attr('invoice-name')
628 me.edit_record();
629 })
630
631 //actions
632 $(this.wrapper).find('.list-select-all').click(function () {
633 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
634 me.removed_items = [];
635 if ($(this).is(":checked")) {
636 $.each(me.si_docs, function (index, data) {
637 for (key in data) {
638 me.removed_items.push(key)
639 }
640 });
641 }
642
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530643 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530644 });
645
646 $(this.wrapper).find('.list-delete').click(function () {
647 me.name = $(this).parent().parent().attr('invoice-name');
648 if ($(this).is(":checked")) {
649 me.removed_items.push(me.name);
650 } else {
651 me.removed_items.pop(me.name)
652 }
653
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530654 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530655 });
656 },
657
658 bind_delete_event: function() {
659 var me = this;
660
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530661 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530662 frappe.confirm(__("Delete permanently?"), function () {
663 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530664 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530665 me.render_list_customers();
666 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530667 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530668 },
669
670 set_focus: function () {
671 if (this.default_customer || this.frm.doc.customer) {
672 this.serach_item.$input.focus();
673 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530674 this.party_field.$input.focus();
675 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530676 },
677
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530678 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530679 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530680
681 if(!this.party_field) {
682 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
683 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530684 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530685 })).insertAfter(this.page.$title_area.hide());
686 }
687
688 this.party_field = frappe.ui.form.make_control({
689 df: {
690 "fieldtype": "Data",
691 "options": this.party,
692 "label": this.party,
693 "fieldname": this.party.toLowerCase(),
694 "placeholder": __("Select or add new customer")
695 },
696 parent: this.page.wrapper.find(".party-area"),
697 only_input: true,
698 });
699
700 this.party_field.make_input();
701 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530702 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530703 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530704
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530705 this.party_field.awesomeplete =
706 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530707 minChars: 0,
708 maxItems: 99,
709 autoFirst: true,
710 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530711 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530712 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530713 return true;
714 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530715
716 input = input.toLowerCase();
717 item = this.get_item(item.value);
718 var searchtext =
719 Object.keys(item)
720 .filter(key => ['customer_name', 'customer_group', 'value', 'label', 'email_id', 'phone'].includes(key))
721 .map(key => item[key])
722 .join(" ")
723 .toLowerCase();
724
725 return searchtext.includes(input)
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530726 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530727 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530728 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530729 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530730 if(d.customer_name) {
731 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
732 }
733
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530734 return $('<li></li>')
735 .data('item.autocomplete', d)
736 .html('<a><p>' + html + '</p></a>')
737 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530738 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530739 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530740
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530741 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530742 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530743
744 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530745 .on('input', function (e) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530746 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530747 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530748 .on('awesomplete-select', function (e) {
749 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530750 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530751 if (!customer) return;
752 // create customer link
753 if (customer.action) {
754 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530755 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530756 }
Faris Ansari45510102017-03-09 14:43:37 +0530757 me.toggle_list_customer(false);
758 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530759 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530760 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530761 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530762 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530763 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530764 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530765 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530766 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530767
768 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530769 me.toggle_list_customer(false)
770 me.toggle_item_cart(true)
771 } else {
772 me.toggle_list_customer(true)
773 me.toggle_item_cart(false)
774 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530775 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530776 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530777 var item = me.party_field.awesomeplete
778 .get_item(e.originalEvent.text.value);
779 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530780 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530781 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530782 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530783 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530784 },
785
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530786 prepare_customer_mapper: function() {
787 var me = this;
788
789 this.customers_mapper = this.customers.map(function (c) {
790 contact = me.contacts[c.name];
791 return {
792 label: c.name,
793 value: c.name,
794 customer_name: c.customer_name,
795 customer_group: c.customer_group,
796 territory: c.territory,
797 phone: contact ? contact["phone"] : '',
798 email_id: contact ? contact["email_id"] : ''
799 }
800 });
801
802 this.customers_mapper.push({
803 label: "<span class='text-primary link-option'>"
804 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
805 + __("Create a new Customer")
806 + "</span>",
807 value: 'is_action',
808 action: me.add_customer
809 });
810 },
811
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530812 autocomplete_customers: function() {
813 this.party_field.awesomeplete.list = this.customers_mapper;
814 },
815
Faris Ansari45510102017-03-09 14:43:37 +0530816 toggle_edit_button: function(flag) {
817 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
818 },
819
820 toggle_list_customer: function(flag) {
821 this.list_customers.toggle(flag);
822 },
823
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530824 toggle_item_cart: function(flag) {
825 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
826 },
827
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530828 add_customer: function() {
829 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530830 this.update_customer(true);
831 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530832 },
833
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530834 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530835 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530836
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530837 this.customer_doc = new frappe.ui.Dialog({
838 'title': 'Customer',
839 fields: [
840 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530841 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530842 "fieldname": "full_name",
843 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530844 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530845 },
846 {
847 "fieldtype": "Section Break"
848 },
849 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530850 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530851 "fieldname": "email_id",
852 "fieldtype": "Data"
853 },
854 {
855 "fieldtype": "Column Break"
856 },
857 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530858 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530859 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530860 "fieldtype": "Data"
861 },
862 {
863 "fieldtype": "Section Break"
864 },
865 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530866 "label": __("Address Name"),
867 "read_only": 1,
868 "fieldname": "name",
869 "fieldtype": "Data"
870 },
871 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530872 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530873 "fieldname": "address_line1",
874 "fieldtype": "Data"
875 },
876 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530877 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530878 "fieldname": "address_line2",
879 "fieldtype": "Data"
880 },
881 {
882 "fieldtype": "Column Break"
883 },
884 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530885 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530886 "fieldname": "city",
887 "fieldtype": "Data"
888 },
889 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530890 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530891 "fieldname": "state",
892 "fieldtype": "Data"
893 },
894 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530895 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530896 "fieldname": "pincode",
897 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530898 },
899 {
900 "label": __("Customer POS Id"),
901 "fieldname": "customer_pos_id",
902 "fieldtype": "Data",
903 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530904 }
905 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530906 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530907 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530908 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530909
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530910 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530911 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530912 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530913 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530914 });
915 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530916
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530917 render_address_data: function() {
918 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530919 this.address_data = this.address[this.frm.doc.customer] || {};
920 if(!this.address_data.email_id || !this.address_data.phone) {
921 this.address_data = this.contacts[this.frm.doc.customer];
922 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530923
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530924 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530925 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
926 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530927 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530928
929 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
930 this.customer_doc.set_value("customer_pos_id", $.now())
931 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530932 },
933
934 get_address_from_localstorage: function() {
935 this.address_details = this.get_customers_details()
936 return this.address_details[this.frm.doc.customer]
937 },
938
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530939 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530940 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530941 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530942 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530943 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530944 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530945 this.update_address_and_customer_list(new_customer)
946 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530947 this.update_customer_in_localstorage()
948 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530949 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530950 },
951
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530952 update_address_and_customer_list: function(new_customer) {
953 var me = this;
954 if(new_customer) {
955 this.customers_mapper.push({
956 label: this.frm.doc.customer,
957 value: this.frm.doc.customer,
958 customer_group: "",
959 territory: ""
960 });
961 }
962
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530963 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530964 },
965
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530966 get_prompt_details: function() {
967 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530968 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530969 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530970 return JSON.stringify(this.prompt_details)
971 },
972
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530973 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530974 var me = this;
975 this.frm.doc.customer = doc.label || doc.name;
976 this.frm.doc.customer_name = doc.customer_name;
977 this.frm.doc.customer_group = doc.customer_group;
978 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530979 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530980 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530981 },
982
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530983 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530984 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530985 if (!this.price_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530986 msgprint(__("Price List not found or disabled"));
987 return;
988 }
989
990 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530991
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530992 var $wrap = me.wrapper.find(".item-list");
993 me.wrapper.find(".item-list").empty();
994
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530995 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530996 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530997 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530998 $(frappe.render_template("pos_item", {
999 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301000 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301001 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301002 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301003 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301004 color: frappe.get_palette(obj.item_name),
1005 abbr: frappe.get_abbr(obj.item_name)
1006 })).tooltip().appendTo($wrap);
1007 }
1008 });
Faris Ansari45510102017-03-09 14:43:37 +05301009
1010 $wrap.append(`
1011 <div class="image-view-item btn-more text-muted text-center">
1012 <div class="image-view-body">
1013 <i class="mega-octicon octicon-package"></i>
1014 <div>Load more items</div>
1015 </div>
1016 </div>
1017 `);
1018
1019 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301020 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301021 $("<p class='text-muted small' style='padding-left: 10px'>"
1022 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301023 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301024
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301025 if (this.items.length == 1
1026 && this.serach_item.$input.val()) {
1027 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301028 this.add_to_cart();
1029 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 },
1031
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301033 // To search item as per the key enter
1034
1035 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301036 this.item_serial_no = {};
1037 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301038
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301039 if (item_code) {
1040 return $.grep(this.item_data, function (item) {
1041 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301042 return true
1043 }
1044 })
1045 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301046
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301047 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301048
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301049 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301050 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301051 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301052 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301053
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301054 if (key) {
1055 return $.grep(this.items_list, function (item) {
1056 if (search_status) {
1057 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301058 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301059 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1060 } else if (me.serial_no_data[item.item_code]
1061 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301062 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301063 me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301064 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301065 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301066 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301067 return item.barcode == me.serach_item.$input.val();
1068 } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
1069 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301070 return true
1071 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301072 }
1073 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301074 } else {
1075 return this.items_list;
1076 }
1077 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301078
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301079 apply_category: function() {
1080 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301081 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301082
1083 if(category == 'All Item Groups') {
1084 return this.item_data
1085 } else {
1086 return this.item_data.filter(function(element, index, array){
1087 return element.item_group == category;
1088 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301089 }
1090 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301091
1092 bind_items_event: function() {
1093 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301094 $(this.wrapper).on('click', '.pos-bill-item', function() {
1095 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1096 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301097 me.numeric_val = "";
1098 me.numeric_id = ""
1099 me.item_code = $(this).attr("data-item-code");
1100 me.render_selected_item()
1101 me.bind_qty_event()
1102 me.update_rate()
1103 $(me.wrapper).find(".selected-item").scrollTop(1000);
1104 })
1105 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301106
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301107 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301108 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301109
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301110 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301111 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301112 var qty = $(this).val();
1113 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301114 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301116
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301117 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301118 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1119 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301120 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301121 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301122
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301123 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301124 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1125 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301126 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301127 })
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301128
1129 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301130 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301131 var discount = $(this).val();
1132 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301133 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301134 })
1135 },
1136
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301137 bind_events: function() {
1138 var me = this;
1139 // if form is local then allow this function
1140 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1141 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301142 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301143 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301144 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301145
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301146 if (me.frm.doc.docstatus == 0) {
1147 me.items = me.get_items($(this).attr("data-item-code"))
1148 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301149 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301150 }
1151 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301152
1153 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301154 },
1155
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301156 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301157 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301158 this.items = this.get_items(item_code);
1159 this.validate_serial_no()
1160 this.set_item_details(item_code, "qty", qty);
1161 },
1162
1163 update_discount: function(item_code, discount) {
1164 var me = this;
1165 this.items = this.get_items(item_code);
1166 this.set_item_details(item_code, "discount_percentage", discount);
1167 },
1168
1169 update_rate: function () {
1170 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301171 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301172 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301173 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301174 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301175 })
1176 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301177
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301178 update_value: function() {
1179 var me = this;
1180 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1181 "rate": ".pos-item-price", "amount": ".pos-amount"}
1182 this.child_doc = this.get_child_item(this.item_code);
1183
1184 if(me.child_doc.length) {
1185 $.each(fields, function(key, field) {
1186 $(me.selected_row).find(field).val(me.child_doc[0][key])
1187 })
1188 } else {
1189 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301190 }
1191 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301192
1193 clear_selected_row: function() {
1194 $(this.wrapper).find('.selected-item').empty();
1195 },
1196
1197 render_selected_item: function() {
1198 this.child_doc = this.get_child_item(this.item_code);
1199 $(this.wrapper).find('.selected-item').empty();
1200 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301201 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301202 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1203 $(this.wrapper).find('.selected-item').html(this.selected_row)
1204 }
1205
1206 $(this.selected_row).find('.form-control').click(function(){
1207 $(this).select();
1208 })
1209 },
1210
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301211 get_child_item: function(item_code) {
1212 var me = this;
1213 return $.map(me.frm.doc.items, function(doc){
1214 if(doc.item_code == item_code) {
1215 return doc
1216 }
1217 })
1218 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301219
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301220 set_item_details: function (item_code, field, value) {
1221 var me = this;
1222 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301223 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301224 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301225
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301226 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301227 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301228 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301229 if (d.serial_no && field == 'qty') {
1230 me.validate_serial_no_qty(d, item_code, field, value)
1231 }
1232
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301233 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301234 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301235 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301236 me.remove_item.push(d.idx)
1237 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301238
1239 if(field=="discount_percentage" && value == 0) {
1240 d.rate = d.price_list_rate;
1241 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301242 }
1243 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301244
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301245 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301246 this.remove_zero_qty_item();
1247 }
1248
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301249 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301250 },
1251
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301252 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301253 var me = this;
1254 idx = 0
1255 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301256 $.each(this.frm.doc["items"] || [], function (i, d) {
1257 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301258 d.idx = idx;
1259 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301260 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301261 }
1262 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301263
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301264 this.frm.doc["items"] = this.items;
1265 },
1266
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301267 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301268 var me = this;
1269
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301270 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301271 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
1272 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301273
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301274 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301275 total = me.frm.doc.net_total
1276 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301278 me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301279 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1280 me.refresh();
1281 });
1282
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301283 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301284 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1285 me.frm.doc.additional_discount_percentage = 0.0;
1286 me.wrapper.find('input.discount-percentage').val(0);
1287 me.refresh();
1288 });
1289 },
1290
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301291 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301292 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301293 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301294 frappe.throw(__("Please select customer"))
1295 }
1296 },
1297
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301298 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301299 var me = this;
1300 var caught = false;
1301 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1302
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301303 this.customer_validate();
1304 this.mandatory_batch_no();
1305 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301306 this.validate_warehouse();
1307
1308 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301309 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301310 if (d.item_code == me.items[0].item_code) {
1311 caught = true;
1312 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301313 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301314 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301315 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1316 d.warehouse = me.item_serial_no[d.item_code][1]
1317 }
1318
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301319 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301320 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301321 }
1322 }
1323 });
1324 }
1325
1326 // if item not found then add new item
1327 if (!caught)
1328 this.add_new_item_to_grid();
1329
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301330 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301331 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301332 },
1333
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301334 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301335 var me = this;
1336 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1337 this.child.item_code = this.items[0].item_code;
1338 this.child.item_name = this.items[0].item_name;
1339 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301340 this.child.brand = this.items[0].brand;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301341 this.child.description = this.items[0].description;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301342 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301343 this.child.qty = 1;
1344 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301345 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1346 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301347 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301348 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301349 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1350 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1351 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301352 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301353 this.child.batch_no = this.item_batch_no[this.child.item_code];
1354 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1355 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301356 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301357 },
1358
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301359 update_paid_amount_status: function (update_paid_amount) {
1360 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301361 update_paid_amount = update_paid_amount ? false : true;
1362 }
1363
1364 this.refresh(update_paid_amount);
1365 },
1366
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301367 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301368 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301369 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301370 this.set_primary_action();
1371 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301372
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301373 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301374 this.apply_pricing_rule();
1375 this.discount_amount_applied = false;
1376 this._calculate_taxes_and_totals();
1377 this.calculate_discount_amount();
1378 this.show_items_in_item_cart();
1379 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301380 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301381 this.set_totals();
1382 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301383
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301384 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301385 return erpnext.get_currency(this.frm.doc.company);
1386 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301387
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301388 show_item_wise_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301389 return null;
1390 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301391
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301392 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301393 var me = this;
1394 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301395 var $no_items_message = this.wrapper.find(".no-items-message");
1396 $no_items_message.toggle(this.frm.doc.items.length === 0);
1397
1398 var $totals_area = this.wrapper.find('.totals-area');
1399 $totals_area.toggle(this.frm.doc.items.length > 0);
1400
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301401 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301402 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301403 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301404 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301405 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301406 discount_percentage: d.discount_percentage || 0.0,
1407 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301408 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301409 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301410 amount: format_currency(d.amount, me.frm.doc.currency),
1411 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301412 })).appendTo($items);
1413 });
1414
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301415 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301416 $(this).select();
1417 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301418
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301419 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301420 $(this).select();
1421 });
1422
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301423 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301424 $(this).select();
1425 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301426 },
1427
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301428 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301429 var me = this;
1430 me.frm.doc.total_taxes_and_charges = 0.0
1431
1432 var taxes = this.frm.doc.taxes || [];
1433 $(this.wrapper)
1434 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1435 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301437 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301438 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1439 $(frappe.render_template("pos_tax_row", {
1440 description: d.description,
1441 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1442 me.frm.doc.currency)
1443 })).appendTo(me.wrapper.find(".tax-table"));
1444 }
1445 });
1446 },
1447
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301448 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301449 var me = this;
1450 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1451 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1452 },
1453
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301454 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301455 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301456 this.page.set_primary_action(__("New Cart"), function () {
1457 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301458 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301459 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301460
1461 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301462 this.page.set_secondary_action(__("Print"), function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301463 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301464 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301465 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301466 this.page.add_menu_item(__("Email"), function () {
1467 me.email_prompt()
1468 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301469 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301470 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301471
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301472 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301473 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301474 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301475 this.save_previous_entry();
1476 this.create_new();
1477 this.refresh();
1478 this.toggle_input_field();
1479 this.render_list_customers();
1480 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301481 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301482
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301483 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301484 var me = this;
1485
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301486 this.msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301487 style="margin-right: 5px;">{0}</a>\
1488 <a class="btn btn-default new_doc">{1}</a>', [
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301489 __('Print'), __('New')
1490 ]));
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301491
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301492 $('.print_doc').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301493 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301494 me.print_document(html)
1495 })
1496
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301497 $('.new_doc').click(function () {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301498 me.msgprint.hide()
1499 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301500 })
1501 },
1502
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301503 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301504 var w = window.open();
1505 w.document.write(html);
1506 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301507 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301508 w.print();
1509 w.close();
1510 }, 1000)
1511 },
1512
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301513 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301514 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301515 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301516 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301517 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301518 this.print_dialog()
1519 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301520 },
1521
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301522 update_serial_no: function() {
1523 var me = this;
1524
1525 //Remove the sold serial no from the cache
1526 $.each(this.frm.doc.items, function(index, data) {
1527 sn = data.serial_no.split('\n')
1528 if(sn.length) {
1529 serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301530 if(serial_no_list) {
1531 $.each(sn, function(i, serial_no) {
1532 if(in_list(Object.keys(serial_no_list), serial_no)) {
1533 delete serial_no_list[serial_no]
1534 }
1535 })
1536 me.serial_no_data[data.item_code] = serial_no_list;
1537 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301538 }
1539 })
1540 },
1541
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301542 change_status: function () {
1543 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301544 this.frm.doc.docstatus = 1;
1545 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301546 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301547 }
1548 },
1549
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301550 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301551 var pointer_events = 'inherit'
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301552 disabled = this.frm.doc.docstatus == 1 ? true: false;
1553 $(this.wrapper).find('input').attr("disabled", disabled);
1554 $(this.wrapper).find('select').attr("disabled", disabled);
1555 $(this.wrapper).find('input').attr("disabled", disabled);
1556 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301557 $(this.wrapper).find('button').attr("disabled", disabled);
1558 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301559
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301560 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301561 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301562 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301563
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301564 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301565 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1566 this.set_primary_action();
1567 },
1568
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301569 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301570 var me = this;
1571 var invoice_data = {}
1572 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301573 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301574 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301575 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301576 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301577 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301578 this.frm.doc.posting_date = frappe.datetime.get_today();
1579 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301580 invoice_data[this.name] = this.frm.doc
1581 this.si_docs.push(invoice_data)
1582 this.update_localstorage();
1583 this.set_primary_action();
1584 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301585 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301586 },
1587
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301588 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301589 var me = this;
1590 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301591 $.each(this.si_docs, function (index, data) {
1592 for (key in data) {
1593 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301594 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301595 me.update_localstorage();
1596 }
1597 }
1598 })
1599 },
1600
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301601 update_localstorage: function () {
1602 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301603 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301604 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301605 frappe.throw(__("LocalStorage is full , did not save"))
1606 }
1607 },
1608
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301609 get_doc_from_localstorage: function () {
1610 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301611 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301612 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301613 return []
1614 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301615 },
1616
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301617 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301618 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301619 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301620 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301621 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301622 },
1623
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301624 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301625 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301626 this.si_docs = this.get_submitted_invoice() || [];
1627 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301628 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301629 if(this.customer_doc) {
1630 this.freeze = this.customer_doc.display
1631 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301632
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301633 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301634 frappe.call({
1635 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1636 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301637 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301638 email_queue_list: me.email_queue_list,
1639 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301640 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301641 callback: function (r) {
1642 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301643 me.customers = r.message.synced_customers_list;
1644 me.address = r.message.synced_address;
1645 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301646 me.removed_items = r.message.invoice;
1647 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301648 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301649 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301650 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301651 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301652 me.prepare_customer_mapper()
1653 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301654 }
1655 }
1656 })
1657 }
1658 },
1659
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301660 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301661 var invoices = [];
1662 var index = 1;
1663 docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301664 if (docs) {
1665 invoices = $.map(docs, function (data) {
1666 for (key in data) {
1667 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301668 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301669 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301670 return data
1671 }
1672 }
1673 });
1674 }
1675
1676 return invoices
1677 },
1678
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301679 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301680 var me = this;
1681 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301682 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301683 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301684 $.each(this.si_docs, function (index, data) {
1685 for (key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301686 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301687 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301688 }
1689 }
1690 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301691 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301692 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301693 this.update_localstorage();
1694 }
1695 },
1696
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301697 remove_email_queue_from_localstorage: function() {
1698 var me = this;
1699 this.email_queue = this.get_email_queue()
1700 if (this.removed_email) {
1701 $.each(this.email_queue_list, function (index, data) {
1702 if (in_list(me.removed_email, index)) {
1703 delete me.email_queue[index]
1704 }
1705 })
1706 this.update_email_queue();
1707 }
1708 },
1709
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301710 remove_customer_from_localstorage: function() {
1711 var me = this;
1712 this.customer_details = this.get_customers_details()
1713 if (this.removed_customers) {
1714 $.each(this.customers_list, function (index, data) {
1715 if (in_list(me.removed_customers, index)) {
1716 delete me.customer_details[index]
1717 }
1718 })
1719 this.update_customer_in_localstorage();
1720 }
1721 },
1722
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301723 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301724 var me = this;
1725 this.customer_validate();
1726 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301727 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301728 },
1729
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301730 item_validate: function () {
1731 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301732 frappe.throw(__("Select items to save the invoice"))
1733 }
1734 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301735
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301736 validate_mode_of_payments: function () {
1737 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301738 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1739 }
1740 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301741
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301742 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301743 var me = this;
1744 var item_code = serial_no = '';
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301745 for (key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301746 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301747 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301748 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301749
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301750 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301751 this.refresh();
1752 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1753 'item': this.items[0].item_code
1754 })))
1755 }
1756
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301757 if (item_code && serial_no) {
1758 $.each(this.frm.doc.items, function (index, data) {
1759 if (data.item_code == item_code) {
1760 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301761 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1762 'serial_no': serial_no
1763 })))
1764 }
1765 }
1766 })
1767 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301768 },
1769
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301770 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301771 var me = this;
1772 if (args.item_code == item_code && args.serial_no
1773 && field == 'qty' && cint(value) != value) {
1774 args.qty = 0.0;
1775 this.refresh();
1776 frappe.throw(__("Serial no item cannot be a fraction"))
1777 }
1778
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301779 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301780 args.qty = 0.0;
1781 args.serial_no = ''
1782 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301783 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1784 'item': item_code
1785 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301786 }
1787 },
1788
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301789 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301790 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301791 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301792 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1793 'item': this.items[0].item_code
1794 })))
1795 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301796 },
1797
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301798 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301799 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301800 $.each(this.frm.doc["items"], function (n, item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301801 pricing_rule = me.get_pricing_rule(item)
1802 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301803 if (pricing_rule.length) {
1804 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301805 item.margin_type = pricing_rule[0].margin_type;
1806 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1807 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1808 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301809 me.apply_pricing_rule_on_item(item)
1810 } else if (item.pricing_rule) {
1811 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301812 item.margin_rate_or_amount = 0.0;
1813 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301814 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301815 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301816 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301817
1818 if(item.discount_percentage > 0) {
1819 me.apply_pricing_rule_on_item(item)
1820 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301821 })
1822 },
1823
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301824 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301825 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301826 return $.grep(this.pricing_rules, function (data) {
1827 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301828 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301829 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301830 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301831 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301832 return true
1833 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301834 }
1835 }
1836 })
1837 },
1838
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301839 validate_item_condition: function (data, item) {
1840 var apply_on = frappe.model.scrub(data.apply_on);
1841
1842 return (data.apply_on == 'Item Group')
1843 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1844 },
1845
1846 validate_item_group: function (pr_item_group, cart_item_group) {
1847 //pr_item_group = pricing rule's item group
1848 //cart_item_group = cart item's item group
1849 //this.item_groups has information about item group's lft and rgt
1850 //for example: {'Foods': [12, 19]}
1851
1852 pr_item_group = this.item_groups[pr_item_group]
1853 cart_item_group = this.item_groups[cart_item_group]
1854
1855 return (cart_item_group[0] >= pr_item_group[0] &&
1856 cart_item_group[1] <= pr_item_group[1])
1857 },
1858
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301859 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301860 //This method check condition based on applicable for
1861 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301862 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301863 return true
1864 }
1865 },
1866
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301867 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301868 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301869 'Customer': [data.customer, [this.frm.doc.customer]],
1870 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1871 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301872 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301873 }
1874 },
1875
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301876 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301877 //This method validate duplicate pricing rule
1878 var pricing_rule_name = '';
1879 var priority = 0;
1880 var pricing_rule_list = [];
1881 var priority_list = []
1882
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301883 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301884
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301885 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301886 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301887 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301888 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301889 priority = data.priority
1890 pricing_rule_list.push(data)
1891 }
1892 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301893
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301894 count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301895 $.each(priority_list, function (index, value) {
1896 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301897 count++
1898 }
1899 })
1900
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301901 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301902 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1903 'pricing_rule': pricing_rule_name
1904 })))
1905 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301906
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301907 return pricing_rule_list
1908 }
1909 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301910
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301911 validate_warehouse: function () {
1912 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 +05301913 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301914 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301915 },
1916
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301917 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301918 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301919
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301920 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301921 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301922 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301923 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301924 }
1925
1926 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301927 },
1928
1929 update_customer_in_localstorage: function() {
1930 var me = this;
1931 try {
1932 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1933 } catch (e) {
1934 frappe.throw(__("LocalStorage is full , did not save"))
1935 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301936 }
Faris Ansari185762a2017-04-13 12:36:08 +05301937})