blob: 8dc00f3f218c3e24663391d899bafbb6fae532e3 [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
Faris Ansaric70bbac2017-08-29 15:27:17 +053011 frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => {
Rohit Waghchaure1a779222017-09-11 11:49:25 +053012 if (r && r.use_pos_in_offline_mode && cint(r.use_pos_in_offline_mode)) {
Faris Ansaric70bbac2017-08-29 15:27:17 +053013 // offline
14 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
15 cur_pos = wrapper.pos;
16 } else {
17 // online
rohitwaghchaurea4c40e02018-02-05 12:24:06 +053018 frappe.flags.is_online = true
Faris Ansaric70bbac2017-08-29 15:27:17 +053019 frappe.set_route('point-of-sale');
20 }
21 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053022}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053023
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053024frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053025 window.onbeforeunload = function () {
26 return wrapper.pos.beforeunload()
27 }
rohitwaghchaurea4c40e02018-02-05 12:24:06 +053028
29 if (frappe.flags.is_online) {
30 frappe.set_route('point-of-sale');
31 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053032}
33
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053034erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053035 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053036 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053037 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053038 this.page = wrapper.page;
39 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.set_indicator();
41 this.onload();
42 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053043 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053044 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053045 this.si_docs = this.get_doc_from_localstorage();
46 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053047
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053048 beforeunload: function (e) {
49 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053050 e = e || window.event;
51
52 // For IE and Firefox prior to version 4
53 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053054 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 +053055 return
56 }
57
58 // For Safari
59 return __("You are in offline mode. You will not be able to reload until you have network.");
60 }
61 },
62
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053063 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 var me = this;
65 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053068 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053069 },
70
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053071 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053072 var me = this;
73 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053074 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053075 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053076 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053077 method: "frappe.handler.ping",
78 callback: function (r) {
79 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053080 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053081 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053082 }
83 }
84 })
85 },
86
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053087 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053089 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053090 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053091 me.create_new();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +053092 me.make();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053093 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053094 },
95
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053096 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053097 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053098 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053099
100 // for mobile
101 this.page.add_menu_item(__("Pay"), function () {
102 me.validate();
103 me.update_paid_amount_status(true);
104 me.create_invoice();
105 me.make_payment();
106 }).addClass('visible-xs');
107
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530108 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530109 me.save_previous_entry();
110 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530111 })
112
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530113 this.page.add_menu_item(__("Sync Master Data"), function () {
114 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530115 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530116 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530117 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530118 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530119 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530121 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
rohitwaghchaure88491712017-10-02 12:13:36 +0530122 me.freeze_screen = true;
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530123 me.sync_sales_invoice()
124 });
125
Bassam Ramadan574cd462018-09-06 15:32:25 +0200126 this.page.add_menu_item(__("Cashier Closing"), function () {
127 frappe.set_route('List', 'Cashier Closing');
deepeshgarg007c2ffcc52019-01-27 16:09:07 +0530128 });
Bassam Ramadan574cd462018-09-06 15:32:25 +0200129
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530130 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530131 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530132 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530133 },
134
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530135 email_prompt: function() {
136 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530137 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Shreya Shah44e719d2018-04-20 10:28:52 +0530138 {fieldtype: "Section Break", collapsible: 1, label: "CC & Email Template"},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530139 {fieldtype: "Section Break"},
140 {label:__("Subject"), fieldtype:"Data", reqd: 1,
141 fieldname:"subject",length:524288},
142 {fieldtype: "Section Break"},
143 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
144 fieldname:"content"},
145 {fieldtype: "Section Break"},
146 {fieldtype: "Column Break"}];
147
148 this.email_dialog = new frappe.ui.Dialog({
149 title: "Email",
150 fields: fields,
151 primary_action_label: __("Send"),
152 primary_action: function() {
153 me.send_action();
154 }
155 });
156
157 this.email_dialog.show()
158 },
159
160 send_action: function() {
161 this.email_queue = this.get_email_queue()
162 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
163 this.update_email_queue()
164 this.email_dialog.hide()
165 },
166
167 update_email_queue: function () {
168 try {
169 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
170 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530171 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530172 }
173 },
174
175 get_email_queue: function () {
176 try {
177 return JSON.parse(localStorage.getItem('email_queue')) || {};
178 } catch (e) {
179 return {}
180 }
181 },
182
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530183 get_customers_details: function () {
184 try {
185 return JSON.parse(localStorage.getItem('customer_details')) || {};
186 } catch (e) {
187 return {}
188 }
189 },
190
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530192 var me = this;
193
194 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530195 if (doc_data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530196 this.frm.doc = doc_data[0][this.frm.doc.offline_pos_name];
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 this.set_missing_values();
198 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530199 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 }
202 },
203
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530205 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530206 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530207 this.remove_doc_from_localstorage()
208 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530209 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 },
211
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530212 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530213 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530214 this.si_docs = this.get_submitted_invoice()
rohitwaghchauref688af32017-11-08 11:33:39 +0530215 $.each(this.removed_items, function(index, pos_name){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530216 $.each(me.si_docs, function(key, data){
rohitwaghchauref688af32017-11-08 11:33:39 +0530217 if(me.si_docs[key][pos_name] && me.si_docs[key][pos_name].offline_pos_name == pos_name ){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530218 frappe.throw(__("Submitted orders can not be deleted"))
219 }
220 })
221 })
222 },
223
224 toggle_delete_button: function () {
225 var me = this;
226 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530227 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530228 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530229 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530230 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530231 }
232 }
233 },
234
235 get_doctype_status: function (doc) {
236 if (doc.docstatus == 0) {
237 return { status: "Draft", indicator: "red" }
238 } else if (doc.outstanding_amount == 0) {
239 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530240 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530241 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530242 }
243 },
244
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530245 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530246 var me = this;
247 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530248 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530249 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530250 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530251 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530252
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530253 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530254
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530255 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530256 this.frm.doc.write_off_account = doc.write_off_account
257 }
258
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530259 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530260 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530261 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530262 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530263
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530264 set_customer_value_in_party_field: function() {
265 if (this.frm.doc.customer) {
266 this.party_field.$input.val(this.frm.doc.customer);
267 }
268 },
269
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530270 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530271 var me = this;
272 this.si_docs = this.get_doc_from_localstorage();
273
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530274 return $.grep(this.si_docs, function (data) {
275 for (key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530276 return key == me.frm.doc.offline_pos_name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530277 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530278 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 },
280
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530281 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530282 var me = this;
283 frappe.call({
284 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
285 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530286 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530287 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530288 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530289 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530290 me.set_interval_for_si_sync();
291 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530292 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530293 callback();
294 }
Rushabh Mehta69fa8082018-07-17 18:22:51 +0530295 },
296 error: () => {
297 setTimeout(() => frappe.set_route('List', 'POS Profile'), 2000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 }
299 })
300 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530301
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530302 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530303 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530304 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530305 this.meta = r.message.meta;
306 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530307 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530308 this.customers = r.message.customers;
309 this.serial_no_data = r.message.serial_no_data;
310 this.batch_no_data = r.message.batch_no_data;
Giovanni2144e022017-12-10 17:27:09 +0100311 this.barcode_data = r.message.barcode_data;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530312 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530313 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530314 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530315 this.price_list_data = r.message.price_list_data;
deepeshgarg007c2ffcc52019-01-27 16:09:07 +0530316 this.customer_wise_price_list = r.message.customer_wise_price_list
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530317 this.bin_data = r.message.bin_data;
318 this.pricing_rules = r.message.pricing_rules;
319 this.print_template = r.message.print_template;
320 this.pos_profile_data = r.message.pos_profile;
321 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530322 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530323 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 +0530324 },
325
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530326 save_previous_entry: function () {
327 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
328 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530329 }
330 },
331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530332 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530333 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530334 this.frm = {}
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530335 this.load_data(true);
Rohit Waghchaure1df62092019-03-26 19:12:25 +0530336 this.frm.doc.offline_pos_name = '';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530337 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530338 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530339 },
340
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530341 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530342 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530343
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530344 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530345 this.actual_qty_dict = {};
346
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530347 if (load_doc) {
348 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530349 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530350
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530351 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530352 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530353 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530354 })
355
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530356 this.print_template_data = frappe.render_template("print_template", {
357 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530358 title: "POS",
359 base_url: frappe.urllib.get_base_url(),
360 print_css: frappe.boot.print_css,
361 print_settings: this.print_settings,
362 header: this.letter_head.header,
363 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530364 landscape: false,
365 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530366 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530367 },
368
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530369 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530370 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530371 this.party_field.$input.attr('disabled', false);
372 if(this.selected_row) {
373 this.selected_row.hide()
374 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530375 },
376
Rohit Waghchaure61165122017-05-02 13:04:08 +0530377 set_default_customer: function() {
378 if (this.default_customer && !this.frm.doc.customer) {
379 this.party_field.$input.val(this.default_customer);
380 this.frm.doc.customer = this.default_customer;
381 this.numeric_keypad.show();
382 this.toggle_list_customer(false)
383 this.toggle_item_cart(true)
384 }
385 },
386
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530387 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530388 var me = this;
389 this.party = party;
390 this.price_list = (party == "Customer" ?
391 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
392 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
393 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
394 },
395
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530396 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530397 this.make_item_list();
398 this.make_discount_field()
399 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200400
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530401 make_control: function() {
402 this.frm = {}
403 this.frm.doc = this.doc
404 this.set_transaction_defaults("Customer");
rohitwaghchaurea32e57a2018-03-08 16:21:13 +0530405 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false;
406 this.frm.doc["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530407 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530408 this.make_search();
409 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530410 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530411 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530412 },
413
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530414 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 var me = this;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530416 this.search_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 df: {
418 "fieldtype": "Data",
Mohammed R Abukhamseen8662a4f2018-07-01 14:14:09 +0300419 "label": __("Item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530420 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530421 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530422 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530423 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530424 only_input: true,
425 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530426
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530427 this.search_item.make_input();
Giovanni2144e022017-12-10 17:27:09 +0100428
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530429 this.search_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530430
431 clearTimeout(me.last_search_timeout);
432 me.last_search_timeout = setTimeout(() => {
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530433 if((me.search_item.$input.val() != "") || (event.which == 13)) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530434 me.items = me.get_items();
435 me.make_item_list();
Giovanni2144e022017-12-10 17:27:09 +0100436 }
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530437 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530438 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530439
Faris Ansari339d9c92017-03-07 18:14:06 +0530440 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530441 sorted_item_groups = this.get_sorted_item_groups()
442 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530443 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
444 }).join("");
445
446 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
447
448 this.search_item_group.on('click', '.dropdown-menu a', function() {
449 me.selected_item_group = $(this).attr('data-value');
450 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
451
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530452 me.page_len = 20;
453 me.items = me.get_items();
454 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530455 })
456
Faris Ansari45510102017-03-09 14:43:37 +0530457 me.toggle_more_btn();
458
459 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530460 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530461 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530462 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530463 me.toggle_more_btn();
464 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530465
Faris Ansari45510102017-03-09 14:43:37 +0530466 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530467 me.update_customer()
468 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530469 },
470
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530471 get_sorted_item_groups: function() {
472 list = {}
473 $.each(this.item_groups, function(i, data) {
474 list[i] = data[0]
475 })
476
477 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
478 },
479
Faris Ansari45510102017-03-09 14:43:37 +0530480 toggle_more_btn: function() {
481 if(!this.items || this.items.length <= this.page_len) {
482 this.wrapper.find(".btn-more").hide();
483 } else {
484 this.wrapper.find(".btn-more").show();
485 }
486 },
487
488 toggle_totals_area: function(show) {
489
Faris Ansari07c9f352017-03-09 17:03:14 +0530490 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530491 show = this.is_totals_area_collapsed;
492 }
493
494 var totals_area = this.wrapper.find('.totals-area');
495 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
496 .toggle(show);
497
498 if(show) {
499 totals_area.find('.collapse-btn i')
500 .removeClass('octicon-chevron-down')
501 .addClass('octicon-chevron-up');
502 } else {
503 totals_area.find('.collapse-btn i')
504 .removeClass('octicon-chevron-up')
505 .addClass('octicon-chevron-down');
506 }
507
508 this.is_totals_area_collapsed = !show;
509 },
510
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530511 make_list_customers: function () {
512 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530513 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530514 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530515 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530516 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530517 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530518 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530519
Faris Ansari45510102017-03-09 14:43:37 +0530520 me.render_list_customers();
521 me.toggle_totals_area(false);
522
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530523 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530524 $(this).toggleClass("view_customer");
525 if($(this).hasClass("view_customer")) {
526 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530527 me.list_customers.show();
528 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530529 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530530 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530531 } else {
532 if(me.frm.doc.docstatus == 0) {
533 me.party_field.$input.attr('disabled', false);
534 }
535 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530536 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530537 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530538 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530539 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530540 }
541 });
542 this.add_customer_btn.on('click', function() {
543 me.save_previous_entry();
544 me.create_new();
545 me.refresh();
546 me.set_focus();
547 });
Faris Ansari45510102017-03-09 14:43:37 +0530548 this.pos_bill.on('click', '.collapse-btn', function() {
549 me.toggle_totals_area();
550 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530551 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200552
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530553 bind_numeric_keypad: function() {
554 var me = this;
555 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
556 me.numeric_val = '';
557 })
558
559 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
560 me.numeric_id = $(this).attr("id") || me.numeric_id;
561 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530562 if(me.numeric_id) {
563 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
564 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530565
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530566 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530567 me.numeric_val += me.val;
568 me.selected_field.val(flt(me.numeric_val))
569 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530570 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530571 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530572
573 if(me.numeric_id && $(this).hasClass('pos-operation')) {
574 me.numeric_keypad.find('button.pos-operation').removeClass('active');
575 $(this).addClass('active');
576
577 me.selected_row.find('.pos-list-row').removeClass('active');
578 me.selected_field.closest('.pos-list-row').addClass('active');
579 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530580 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200581
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530582 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530583 if(me.numeric_id) {
584 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
585 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
586 me.selected_field.val(me.numeric_val);
587 me.selected_field.trigger("change")
588 } else {
589 //Remove an item from the cart, if focus is at selected item
590 me.remove_selected_item()
591 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530592 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200593
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530594 $(this.numeric_keypad).find('.pos-pay').click(function(){
595 me.validate();
596 me.update_paid_amount_status(true);
597 me.create_invoice();
598 me.make_payment();
599 })
600 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530601
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530602 remove_selected_item: function() {
603 this.remove_item = []
604 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
605 this.remove_item.push(idx)
Rohit Waghchaure07f75a32019-01-29 12:01:54 +0530606 this.remove_zero_qty_items_from_cart()
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530607 this.update_paid_amount_status(false)
608 },
609
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530610 render_list_customers: function () {
611 var me = this;
612
613 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530614 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530615 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530616 if (!this.si_docs.length) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530617 this.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530618 return;
619 }
Faris Ansari45510102017-03-09 14:43:37 +0530620
621 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530622 if(this.si_docs.length) {
623 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530624 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530625 html += frappe.render_template("pos_invoice_list", {
626 sr: i + 1,
627 name: key,
628 customer: data[key].customer,
629 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
630 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
631 data: me.get_doctype_status(data[key])
632 });
633 }
634 });
635 }
Faris Ansari45510102017-03-09 14:43:37 +0530636 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530637
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530638 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530639 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530640 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530641 me.list_customers_btn.toggleClass("view_customer");
642 me.pos_bill.show();
643 me.list_customers_btn.show();
Rohit Waghchaure1df62092019-03-26 19:12:25 +0530644 me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530645 me.edit_record();
646 })
647
648 //actions
649 $(this.wrapper).find('.list-select-all').click(function () {
650 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
651 me.removed_items = [];
652 if ($(this).is(":checked")) {
653 $.each(me.si_docs, function (index, data) {
654 for (key in data) {
655 me.removed_items.push(key)
656 }
657 });
658 }
659
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530660 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530661 });
662
663 $(this.wrapper).find('.list-delete').click(function () {
rohitwaghchauref688af32017-11-08 11:33:39 +0530664 me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530665 if ($(this).is(":checked")) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530666 me.removed_items.push(me.frm.doc.offline_pos_name);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530667 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +0530668 me.removed_items.pop(me.frm.doc.offline_pos_name)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530669 }
670
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530671 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530672 });
673 },
674
675 bind_delete_event: function() {
676 var me = this;
677
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530678 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530679 frappe.confirm(__("Delete permanently?"), function () {
680 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530681 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530682 me.render_list_customers();
683 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530684 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530685 },
686
687 set_focus: function () {
688 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530689 this.set_customer_value_in_party_field();
Rohit Waghchaureeee830e2018-05-13 16:17:18 +0530690 this.search_item.$input.focus();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530691 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530692 this.party_field.$input.focus();
693 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530694 },
695
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530696 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530697 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530698
699 if(!this.party_field) {
700 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
701 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530702 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530703 })).insertAfter(this.page.$title_area.hide());
704 }
705
706 this.party_field = frappe.ui.form.make_control({
707 df: {
708 "fieldtype": "Data",
709 "options": this.party,
710 "label": this.party,
711 "fieldname": this.party.toLowerCase(),
712 "placeholder": __("Select or add new customer")
713 },
714 parent: this.page.wrapper.find(".party-area"),
715 only_input: true,
716 });
717
718 this.party_field.make_input();
719 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530720 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530721 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530722
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530723 this.party_field.awesomeplete =
724 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530725 minChars: 0,
726 maxItems: 99,
727 autoFirst: true,
728 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530729 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530730 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530731 return true;
732 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530733
734 input = input.toLowerCase();
735 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530736 result = item ? item.searchtext.includes(input) : '';
737 if(!result) {
738 me.prepare_customer_mapper(input);
739 } else {
740 return result;
741 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530742 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530743 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530744 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530745 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530746 if(d.customer_name) {
747 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
748 }
749
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530750 return $('<li></li>')
751 .data('item.autocomplete', d)
752 .html('<a><p>' + html + '</p></a>')
753 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530754 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530755 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530756
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530757 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530758 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530759
760 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530761 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530762 if(me.customers_mapper.length <= 1) {
763 me.prepare_customer_mapper(e.target.value);
764 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530765 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530766 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530767 .on('awesomplete-select', function (e) {
768 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530769 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530770 if (!customer) return;
771 // create customer link
772 if (customer.action) {
773 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530774 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530775 }
Faris Ansari45510102017-03-09 14:43:37 +0530776 me.toggle_list_customer(false);
777 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530778 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530779 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530780 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530781 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530782 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530783 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530784 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530785 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530786
787 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530788 me.toggle_list_customer(false)
789 me.toggle_item_cart(true)
790 } else {
791 me.toggle_list_customer(true)
792 me.toggle_item_cart(false)
793 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530794 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530795 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530796 var item = me.party_field.awesomeplete
797 .get_item(e.originalEvent.text.value);
798 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530799 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530800 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530801 }
deepeshgarg007c2ffcc52019-01-27 16:09:07 +0530802 me.make_item_list(item.customer_name);
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530803 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530804 },
805
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530806 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530807 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530808 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530809
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530810 if (key) {
811 key = key.toLowerCase().trim();
812 var re = new RegExp('%', 'g');
813 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
814
815 customer_data = $.grep(this.customers, function(data) {
816 contact = me.contacts[data.name];
817 if(reg.test(data.name.toLowerCase())
818 || reg.test(data.customer_name.toLowerCase())
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530819 || (contact && reg.test(contact["phone"]))
Himanshu25ab1e42019-09-30 10:08:15 +0530820 || (contact && reg.test(contact["mobile_no"]))
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530821 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
822 return data;
823 }
824 })
825 } else {
826 customer_data = this.customers;
827 }
828
829 this.customers_mapper = [];
830
831 customer_data.forEach(function (c, index) {
832 if(index < 30) {
833 contact = me.contacts[c.name];
834 if(contact && !c['phone']) {
835 c["phone"] = contact["phone"];
836 c["email_id"] = contact["email_id"];
Himanshu25ab1e42019-09-30 10:08:15 +0530837 c["mobile_no"] = contact["mobile_no"];
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530838 }
839
840 me.customers_mapper.push({
841 label: c.name,
842 value: c.name,
843 customer_name: c.customer_name,
844 customer_group: c.customer_group,
845 territory: c.territory,
846 phone: contact ? contact["phone"] : '',
Himanshu25ab1e42019-09-30 10:08:15 +0530847 mobile_no: contact ? contact["mobile_no"] : '',
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530848 email_id: contact ? contact["email_id"] : '',
849 searchtext: ['customer_name', 'customer_group', 'name', 'value',
Himanshu25ab1e42019-09-30 10:08:15 +0530850 'label', 'email_id', 'phone', 'mobile_no']
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530851 .map(key => c[key]).join(' ')
852 .toLowerCase()
853 });
854 } else {
855 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530856 }
857 });
858
859 this.customers_mapper.push({
860 label: "<span class='text-primary link-option'>"
861 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
862 + __("Create a new Customer")
863 + "</span>",
864 value: 'is_action',
865 action: me.add_customer
866 });
867 },
868
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530869 autocomplete_customers: function() {
870 this.party_field.awesomeplete.list = this.customers_mapper;
871 },
872
Faris Ansari45510102017-03-09 14:43:37 +0530873 toggle_edit_button: function(flag) {
874 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
875 },
876
877 toggle_list_customer: function(flag) {
878 this.list_customers.toggle(flag);
879 },
880
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530881 toggle_item_cart: function(flag) {
882 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
883 },
884
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530885 add_customer: function() {
886 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530887 this.update_customer(true);
888 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530889 },
890
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530891 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530892 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530893
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530894 this.customer_doc = new frappe.ui.Dialog({
895 'title': 'Customer',
896 fields: [
897 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530898 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530899 "fieldname": "full_name",
900 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530901 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530902 },
903 {
904 "fieldtype": "Section Break"
905 },
906 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530907 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530908 "fieldname": "email_id",
909 "fieldtype": "Data"
910 },
911 {
912 "fieldtype": "Column Break"
913 },
914 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530915 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530916 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530917 "fieldtype": "Data"
918 },
919 {
920 "fieldtype": "Section Break"
921 },
922 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530923 "label": __("Address Name"),
924 "read_only": 1,
925 "fieldname": "name",
926 "fieldtype": "Data"
927 },
928 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530929 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530930 "fieldname": "address_line1",
931 "fieldtype": "Data"
932 },
933 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530934 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530935 "fieldname": "address_line2",
936 "fieldtype": "Data"
937 },
938 {
939 "fieldtype": "Column Break"
940 },
941 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530942 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530943 "fieldname": "city",
944 "fieldtype": "Data"
945 },
946 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530947 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530948 "fieldname": "state",
949 "fieldtype": "Data"
950 },
951 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530952 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530953 "fieldname": "pincode",
954 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530955 },
956 {
957 "label": __("Customer POS Id"),
958 "fieldname": "customer_pos_id",
959 "fieldtype": "Data",
960 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530961 }
962 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530963 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530964 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530965 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530966
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530967 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530968 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530969 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530970 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530971 });
972 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530973
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530974 render_address_data: function() {
975 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530976 this.address_data = this.address[this.frm.doc.customer] || {};
977 if(!this.address_data.email_id || !this.address_data.phone) {
978 this.address_data = this.contacts[this.frm.doc.customer];
979 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530980
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530981 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530982 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
983 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530984 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530985
986 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
Rohit Waghchaure1df62092019-03-26 19:12:25 +0530987 this.customer_doc.set_value("customer_pos_id", frappe.datetime.now_datetime())
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530988 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530989 },
990
991 get_address_from_localstorage: function() {
992 this.address_details = this.get_customers_details()
993 return this.address_details[this.frm.doc.customer]
994 },
995
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530996 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530997 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530998 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530999 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301000 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301001 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301002 this.update_address_and_customer_list(new_customer)
1003 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301004 this.update_customer_in_localstorage()
1005 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301006 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301007 },
1008
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301009 update_address_and_customer_list: function(new_customer) {
1010 var me = this;
1011 if(new_customer) {
1012 this.customers_mapper.push({
1013 label: this.frm.doc.customer,
1014 value: this.frm.doc.customer,
1015 customer_group: "",
1016 territory: ""
1017 });
1018 }
1019
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301020 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301021 },
1022
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301023 get_prompt_details: function() {
1024 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301025 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301026 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1027 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301028 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301029 return JSON.stringify(this.prompt_details)
1030 },
1031
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301033 var me = this;
1034 this.frm.doc.customer = doc.label || doc.name;
1035 this.frm.doc.customer_name = doc.customer_name;
1036 this.frm.doc.customer_group = doc.customer_group;
1037 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301038 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301039 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301040 },
1041
deepeshgarg007f0cfe982019-01-28 17:58:44 +05301042 make_item_list: function (customer) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301043 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301044 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301045 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301046 return;
1047 }
1048
1049 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301050
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301051 var $wrap = me.wrapper.find(".item-list");
1052 me.wrapper.find(".item-list").empty();
1053
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301054 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301055 $.each(this.items, function(index, obj) {
deepeshgarg007c2ffcc52019-01-27 16:09:07 +05301056 let customer_price_list = me.customer_wise_price_list[customer];
1057 let item_price
1058 if (customer && customer_price_list && customer_price_list[obj.name]) {
1059 item_price = format_currency(customer_price_list[obj.name], me.frm.doc.currency);
1060 } else {
1061 item_price = format_currency(me.price_list_data[obj.name], me.frm.doc.currency);
1062 }
Faris Ansari1f261a82017-03-06 18:01:58 +05301063 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301064 $(frappe.render_template("pos_item", {
1065 item_code: obj.name,
deepeshgarg007c2ffcc52019-01-27 16:09:07 +05301066 item_price: item_price,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301067 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301068 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301069 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Valmik Janglab52d5b22018-05-17 00:02:03 -07001070 item_uom: obj.stock_uom,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301071 color: frappe.get_palette(obj.item_name),
1072 abbr: frappe.get_abbr(obj.item_name)
1073 })).tooltip().appendTo($wrap);
1074 }
1075 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001076
Faris Ansari45510102017-03-09 14:43:37 +05301077 $wrap.append(`
1078 <div class="image-view-item btn-more text-muted text-center">
1079 <div class="image-view-body">
1080 <i class="mega-octicon octicon-package"></i>
1081 <div>Load more items</div>
1082 </div>
1083 </div>
1084 `);
1085
1086 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301087 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301088 $("<p class='text-muted small' style='padding-left: 10px'>"
1089 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301090 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301091
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301092 if (this.items.length == 1
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301093 && this.search_item.$input.val()) {
1094 this.search_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301095 this.add_to_cart();
1096 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301097 },
1098
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301099 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301100 // To search item as per the key enter
1101
1102 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301103 this.item_serial_no = {};
1104 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301105
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301106 if (item_code) {
1107 return $.grep(this.item_data, function (item) {
1108 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301109 return true
1110 }
1111 })
1112 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301113
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301114 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301116 key = this.search_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301117 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301118 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301119 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301121 if (key) {
1122 return $.grep(this.items_list, function (item) {
1123 if (search_status) {
Rohit Waghchaure93bad512019-09-03 23:29:41 +05301124 if (me.batch_no_data[item.item_code] &&
1125 in_list(me.batch_no_data[item.item_code], me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301126 search_status = false;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301127 return me.item_batch_no[item.item_code] = me.search_item.$input.val()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301128 } else if (me.serial_no_data[item.item_code]
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301129 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301130 search_status = false;
Rohit Waghchaureeee830e2018-05-13 16:17:18 +05301131 me.item_serial_no[item.item_code] = [me.search_item.$input.val(), me.serial_no_data[item.item_code][me.search_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301132 return true
Rohit Waghchaure93bad512019-09-03 23:29:41 +05301133 } else if (me.barcode_data[item.item_code] &&
1134 in_list(me.barcode_data[item.item_code], me.search_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301135 search_status = false;
Giovanni2144e022017-12-10 17:27:09 +01001136 return true;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301137 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301138 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301139 return true
1140 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301141 }
1142 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301143 } else {
1144 return this.items_list;
1145 }
1146 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301147
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301148 apply_category: function() {
Himanshu79d44002019-01-19 14:01:38 +05301149 var me = this;
1150 category = this.selected_item_group || "All Item Groups";
1151 if(category == 'All Item Groups') {
1152 return this.item_data
1153 } else {
1154 return this.item_data.filter(function(element, index, array){
1155 return element.item_group == category;
1156 });
1157 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301158 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001159
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301160 bind_items_event: function() {
1161 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301162 $(this.wrapper).on('click', '.pos-bill-item', function() {
1163 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1164 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301165 me.numeric_val = "";
1166 me.numeric_id = ""
1167 me.item_code = $(this).attr("data-item-code");
1168 me.render_selected_item()
1169 me.bind_qty_event()
1170 me.update_rate()
1171 $(me.wrapper).find(".selected-item").scrollTop(1000);
1172 })
1173 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301174
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301175 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301176 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301177
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301178 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301179 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301180 var qty = $(this).val();
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301181 me.update_qty(item_code, qty);
1182 me.update_value();
1183 })
1184
1185 $(this.wrapper).on("focusout", ".pos-item-qty", function () {
1186 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
1187 var qty = $(this).val();
1188 me.update_qty(item_code, qty, true);
1189 me.update_value();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301190 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301191
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301192 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301193 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1194 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301195 me.update_qty(item_code, qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301196 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301197
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301198 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301199 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1200 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301201 me.update_qty(item_code, qty);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301202 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001203
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301204 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301205 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301206 var discount = $(this).val();
Saurabh1a4d77a2018-02-08 01:32:48 +05301207 if(discount > 100){
1208 discount = $(this).val('');
1209 frappe.show_alert({
1210 indicator: 'red',
1211 message: __('Discount amount cannot be greater than 100%')
1212 });
1213 me.update_discount(item_code, discount);
Rushabh Mehta69fa8082018-07-17 18:22:51 +05301214 }else{
Saurabh1a4d77a2018-02-08 01:32:48 +05301215 me.update_discount(item_code, discount);
1216 me.update_value();
1217 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301218 })
1219 },
1220
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301221 bind_events: function() {
1222 var me = this;
1223 // if form is local then allow this function
1224 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1225 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301226 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301227 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301228 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301229
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301230 if (me.frm.doc.docstatus == 0) {
1231 me.items = me.get_items($(this).attr("data-item-code"))
1232 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301233 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301234 }
1235 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301236
1237 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301238 },
1239
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301240 update_qty: function (item_code, qty, remove_zero_qty_items) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301241 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301242 this.items = this.get_items(item_code);
1243 this.validate_serial_no()
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301244 this.set_item_details(item_code, "qty", qty, remove_zero_qty_items);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301245 },
1246
1247 update_discount: function(item_code, discount) {
1248 var me = this;
1249 this.items = this.get_items(item_code);
1250 this.set_item_details(item_code, "discount_percentage", discount);
1251 },
1252
1253 update_rate: function () {
1254 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301255 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301256 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301257 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301258 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301259 })
1260 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301261
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301262 update_value: function() {
1263 var me = this;
1264 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1265 "rate": ".pos-item-price", "amount": ".pos-amount"}
1266 this.child_doc = this.get_child_item(this.item_code);
1267
1268 if(me.child_doc.length) {
1269 $.each(fields, function(key, field) {
1270 $(me.selected_row).find(field).val(me.child_doc[0][key])
1271 })
1272 } else {
1273 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301274 }
1275 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301276
1277 clear_selected_row: function() {
1278 $(this.wrapper).find('.selected-item').empty();
1279 },
1280
1281 render_selected_item: function() {
1282 this.child_doc = this.get_child_item(this.item_code);
1283 $(this.wrapper).find('.selected-item').empty();
1284 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301285 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301286 this.child_doc[0]["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301287 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1288 $(this.wrapper).find('.selected-item').html(this.selected_row)
1289 }
1290
1291 $(this.selected_row).find('.form-control').click(function(){
1292 $(this).select();
1293 })
1294 },
1295
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301296 get_child_item: function(item_code) {
1297 var me = this;
1298 return $.map(me.frm.doc.items, function(doc){
1299 if(doc.item_code == item_code) {
1300 return doc
1301 }
1302 })
1303 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301304
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301305 set_item_details: function (item_code, field, value, remove_zero_qty_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301306 var me = this;
1307 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301308 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301309 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301310
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301311 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301312 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301313 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301314 if (d.serial_no && field == 'qty') {
1315 me.validate_serial_no_qty(d, item_code, field, value)
1316 }
1317
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301318 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301319 d.amount = flt(d.rate) * flt(d.qty);
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301320 if (d.qty == 0 && remove_zero_qty_items) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301321 me.remove_item.push(d.idx)
1322 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301323
1324 if(field=="discount_percentage" && value == 0) {
1325 d.rate = d.price_list_rate;
1326 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301327 }
1328 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301329
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301330 if (field == 'qty') {
1331 this.remove_zero_qty_items_from_cart();
1332 }
1333
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301334 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301335 },
1336
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301337 remove_zero_qty_items_from_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301338 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301339 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301340 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301341 $.each(this.frm.doc["items"] || [], function (i, d) {
1342 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301343 d.idx = idx;
1344 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301345 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301346 }
1347 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301348
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301349 this.frm.doc["items"] = this.items;
1350 },
1351
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301352 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301353 var me = this;
1354
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301355 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301356 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301357
1358 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1359 // Reset discount amount
1360 me.frm.doc.discount_amount = 0;
1361 }
1362
Faris Ansariab74ca72017-05-30 12:54:42 +05301363 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301364
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301365 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301366 total = me.frm.doc.net_total
1367 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301368
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301369 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 +05301370 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301371 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301372 });
1373
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301374 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301375 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1376 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301377 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301378 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301379 });
1380 },
1381
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301382 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301383 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301384 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301385 frappe.throw(__("Please select customer"))
1386 }
1387 },
1388
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301389 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301390 var me = this;
1391 var caught = false;
1392 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1393
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301394 this.customer_validate();
1395 this.mandatory_batch_no();
1396 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301397 this.validate_warehouse();
1398
1399 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301400 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301401 if (d.item_code == me.items[0].item_code) {
1402 caught = true;
1403 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301404 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301405 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301406 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1407 d.warehouse = me.item_serial_no[d.item_code][1]
1408 }
1409
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301410 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301411 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301412 }
1413 }
1414 });
1415 }
1416
1417 // if item not found then add new item
1418 if (!caught)
1419 this.add_new_item_to_grid();
1420
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301421 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301422 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301423 },
1424
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301425 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301426 var me = this;
1427 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1428 this.child.item_code = this.items[0].item_code;
1429 this.child.item_name = this.items[0].item_name;
1430 this.child.stock_uom = this.items[0].stock_uom;
Britlog9e9e37d2018-11-13 07:04:27 +01001431 this.child.uom = this.items[0].sales_uom || this.items[0].stock_uom;
1432 this.child.conversion_factor = this.items[0].conversion_factor || 1;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301433 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301434 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301435 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301436 this.child.qty = 1;
1437 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301438 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1439 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301440 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301441 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse));
deepeshgarg007c2ffcc52019-01-27 16:09:07 +05301442
1443 customer = this.frm.doc.customer;
deepeshgarg007f0cfe982019-01-28 17:58:44 +05301444 let rate;
deepeshgarg007c2ffcc52019-01-27 16:09:07 +05301445
1446 customer_price_list = this.customer_wise_price_list[customer]
1447 if (customer_price_list && customer_price_list[this.child.item_code]){
1448 rate = flt(this.customer_wise_price_list[customer][this.child.item_code] * this.child.conversion_factor, 9) / flt(this.frm.doc.conversion_rate, 9);
1449 }
1450 else{
1451 rate = flt(this.price_list_data[this.child.item_code] * this.child.conversion_factor, 9) / flt(this.frm.doc.conversion_rate, 9);
1452 }
1453
1454 this.child.price_list_rate = rate;
1455 this.child.rate = rate;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301456 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301457 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301458 this.child.batch_no = this.item_batch_no[this.child.item_code];
1459 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1460 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301461 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301462 },
1463
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301464 update_paid_amount_status: function (update_paid_amount) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301465 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301466 update_paid_amount = update_paid_amount ? false : true;
1467 }
1468
1469 this.refresh(update_paid_amount);
1470 },
1471
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301472 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301473 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301474 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 this.set_primary_action();
1476 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301477
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301478 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 this.apply_pricing_rule();
1480 this.discount_amount_applied = false;
1481 this._calculate_taxes_and_totals();
1482 this.calculate_discount_amount();
1483 this.show_items_in_item_cart();
1484 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301485 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301486 this.set_totals();
pawan53a4d2c2018-03-21 09:19:36 +05301487 this.update_total_qty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301488 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301489
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301490 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301491 return erpnext.get_currency(this.frm.doc.company);
1492 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301493
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301494 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301495 var me = this;
1496 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301497 var $no_items_message = this.wrapper.find(".no-items-message");
1498 $no_items_message.toggle(this.frm.doc.items.length === 0);
1499
1500 var $totals_area = this.wrapper.find('.totals-area');
1501 $totals_area.toggle(this.frm.doc.items.length > 0);
1502
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301503 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301504 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301505 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301506 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301507 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301508 discount_percentage: d.discount_percentage || 0.0,
1509 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301510 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301511 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301512 amount: format_currency(d.amount, me.frm.doc.currency),
1513 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301514 })).appendTo($items);
1515 });
1516
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301517 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301518 $(this).select();
1519 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301520
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301521 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301522 $(this).select();
1523 });
1524
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301525 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301526 $(this).select();
1527 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301528 },
1529
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301530 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301531 var me = this;
1532 me.frm.doc.total_taxes_and_charges = 0.0
1533
1534 var taxes = this.frm.doc.taxes || [];
1535 $(this.wrapper)
1536 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1537 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301538
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301539 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301540 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1541 $(frappe.render_template("pos_tax_row", {
1542 description: d.description,
1543 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1544 me.frm.doc.currency)
1545 })).appendTo(me.wrapper.find(".tax-table"));
1546 }
1547 });
1548 },
1549
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301550 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301551 var me = this;
1552 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1553 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301554 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1555 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301556 },
1557
pawan53a4d2c2018-03-21 09:19:36 +05301558 update_total_qty: function() {
1559 var me = this;
1560 var qty_total = 0;
1561 $.each(this.frm.doc["items"] || [], function (i, d) {
1562 if (d.item_code) {
1563 qty_total += d.qty;
1564 }
1565 });
1566 this.frm.doc.qty_total = qty_total;
1567 this.wrapper.find('.qty-total').text(this.frm.doc.qty_total);
1568 },
1569
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301570 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301571 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301572 this.page.set_primary_action(__("New Cart"), function () {
1573 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301574 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301575 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301576
augustinfotech135a54c2017-11-16 15:28:42 +05301577 if (this.frm.doc.docstatus == 1 || this.pos_profile_data["allow_print_before_pay"]) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301578 this.page.set_secondary_action(__("Print"), function () {
augustinfotech135a54c2017-11-16 15:28:42 +05301579 me.create_invoice();
Faris Ansariab74ca72017-05-30 12:54:42 +05301580 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301581 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301582 })
augustinfotech135a54c2017-11-16 15:28:42 +05301583 }
Giovanni2144e022017-12-10 17:27:09 +01001584
1585 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301586 this.page.add_menu_item(__("Email"), function () {
1587 me.email_prompt()
1588 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301589 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301590 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301591
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301592 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301593 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301594 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301595 this.save_previous_entry();
1596 this.create_new();
1597 this.refresh();
1598 this.toggle_input_field();
1599 this.render_list_customers();
1600 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301601 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301602
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301603 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301604 var me = this;
1605
mbauskar66e2a512017-06-22 16:18:12 +05301606 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301607 `<a class="btn btn-primary print_doc"
1608 style="margin-right: 5px;">${__('Print')}</a>
1609 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301610
Rohit Waghchaure152a0c82018-10-29 16:50:18 +05301611 this.msgprint.msg_area.find('.print_doc').on('click', function() {
1612 var html = frappe.render(me.print_template_data, me.frm.doc);
1613 me.print_document(html);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301614 })
1615
Rohit Waghchaure152a0c82018-10-29 16:50:18 +05301616 this.msgprint.msg_area.find('.new_doc').on('click', function() {
1617 me.msgprint.hide();
1618 me.make_new_cart();
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301619 })
Rohit Waghchaure152a0c82018-10-29 16:50:18 +05301620
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301621 },
1622
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301623 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301624 var w = window.open();
1625 w.document.write(html);
1626 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301627 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301628 w.print();
1629 w.close();
Rohit Waghchaurea9435cc2019-09-16 11:14:33 +05301630 }, 1000);
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301631 },
1632
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301633 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301634 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301635 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301636 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301637 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301638 this.print_dialog()
1639 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301640 },
1641
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301642 update_serial_no: function() {
1643 var me = this;
1644
1645 //Remove the sold serial no from the cache
1646 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301647 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301648 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301649 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301650 if(serial_no_list) {
1651 $.each(sn, function(i, serial_no) {
1652 if(in_list(Object.keys(serial_no_list), serial_no)) {
1653 delete serial_no_list[serial_no]
1654 }
1655 })
1656 me.serial_no_data[data.item_code] = serial_no_list;
1657 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301658 }
1659 })
1660 },
1661
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301662 change_status: function () {
1663 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301664 this.frm.doc.docstatus = 1;
1665 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301666 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301667 }
1668 },
1669
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301670 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301671 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301672 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301673 $(this.wrapper).find('input').attr("disabled", disabled);
1674 $(this.wrapper).find('select').attr("disabled", disabled);
1675 $(this.wrapper).find('input').attr("disabled", disabled);
1676 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301677 $(this.wrapper).find('button').attr("disabled", disabled);
1678 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301679
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301680 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301681 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301682 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301683
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301684 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301685 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1686 this.set_primary_action();
Rohit Waghchaurea9435cc2019-09-16 11:14:33 +05301687
1688 $(this.wrapper).find('#pos-item-disc').prop('disabled',
1689 this.pos_profile_data.allow_user_to_edit_discount ? false : true);
1690
1691 $(this.wrapper).find('#pos-item-price').prop('disabled',
1692 this.pos_profile_data.allow_user_to_edit_rate ? false : true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301693 },
1694
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301695 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301696 var me = this;
Rohit Waghchaure1df62092019-03-26 19:12:25 +05301697 var existing_pos_list = [];
rohitwaghchauref688af32017-11-08 11:33:39 +05301698 var invoice_data = {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301699 this.si_docs = this.get_doc_from_localstorage();
pawan53a4d2c2018-03-21 09:19:36 +05301700
Rohit Waghchaure1df62092019-03-26 19:12:25 +05301701 if(this.si_docs) {
1702 this.si_docs.forEach((row) => {
rohitwaghchaure4a323462019-09-13 18:36:57 +05301703 existing_pos_list.push(Object.keys(row)[0]);
Rohit Waghchaure1df62092019-03-26 19:12:25 +05301704 });
1705 }
1706
1707 if (this.frm.doc.offline_pos_name
rohitwaghchaure4a323462019-09-13 18:36:57 +05301708 && in_list(existing_pos_list, cstr(this.frm.doc.offline_pos_name))) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301709 this.update_invoice()
Rohit Waghchaure1df62092019-03-26 19:12:25 +05301710 } else if(!this.frm.doc.offline_pos_name) {
1711 this.frm.doc.offline_pos_name = frappe.datetime.now_datetime();
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301712 this.frm.doc.posting_date = frappe.datetime.get_today();
1713 this.frm.doc.posting_time = frappe.datetime.now_time();
pawan53a4d2c2018-03-21 09:19:36 +05301714 this.frm.doc.pos_total_qty = this.frm.doc.qty_total;
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001715 this.frm.doc.pos_profile = this.pos_profile_data['name'];
rohitwaghchauref688af32017-11-08 11:33:39 +05301716 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1717 this.si_docs.push(invoice_data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301718 this.update_localstorage();
1719 this.set_primary_action();
1720 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301721 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301722 },
1723
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301724 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301725 var me = this;
1726 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301727 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301728 for (var key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301729 if (key == me.frm.doc.offline_pos_name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301730 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301731 me.update_localstorage();
1732 }
1733 }
rohitwaghchauref688af32017-11-08 11:33:39 +05301734 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301735 },
1736
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301737 update_localstorage: function () {
1738 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301739 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301740 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301741 frappe.throw(__("LocalStorage is full , did not save"))
1742 }
1743 },
1744
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301745 get_doc_from_localstorage: function () {
1746 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301747 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301748 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301749 return []
1750 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301751 },
1752
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301753 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301754 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301755 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301756 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301757 me.sync_sales_invoice()
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301758 }, 180000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301759 },
1760
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301761 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301762 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301763 this.si_docs = this.get_submitted_invoice() || [];
1764 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301765 this.customers_list = this.get_customers_details() || {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301766
rohitwaghchaure88ee7d82019-08-23 11:13:58 +05301767 if (this.si_docs.length || this.email_queue_list || this.customers_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301768 frappe.call({
1769 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88ee7d82019-08-23 11:13:58 +05301770 freeze: true,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301771 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301772 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301773 email_queue_list: me.email_queue_list,
1774 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301775 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301776 callback: function (r) {
1777 if (r.message) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301778 me.freeze = false;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301779 me.customers = r.message.synced_customers_list;
1780 me.address = r.message.synced_address;
1781 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301782 me.removed_items = r.message.invoice;
rohitwaghchauref688af32017-11-08 11:33:39 +05301783 me.removed_email = r.message.email_queue;
1784 me.removed_customers = r.message.customers;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301785 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301786 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301787 me.remove_customer_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301788 me.prepare_customer_mapper();
1789 me.autocomplete_customers();
1790 me.render_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301791 }
1792 }
1793 })
1794 }
1795 },
1796
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301797 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301798 var invoices = [];
1799 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301800 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301801 if (docs) {
1802 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301803 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301804 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301805 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301806 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301807 return data
1808 }
1809 }
1810 });
1811 }
1812
1813 return invoices
1814 },
1815
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301816 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301817 var me = this;
1818 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301819 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301820 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301821 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301822 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301823 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301824 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301825 }
1826 }
1827 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301828 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301829 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301830 this.update_localstorage();
1831 }
1832 },
1833
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301834 remove_email_queue_from_localstorage: function() {
1835 var me = this;
1836 this.email_queue = this.get_email_queue()
1837 if (this.removed_email) {
1838 $.each(this.email_queue_list, function (index, data) {
1839 if (in_list(me.removed_email, index)) {
1840 delete me.email_queue[index]
1841 }
1842 })
1843 this.update_email_queue();
1844 }
1845 },
1846
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301847 remove_customer_from_localstorage: function() {
1848 var me = this;
1849 this.customer_details = this.get_customers_details()
1850 if (this.removed_customers) {
1851 $.each(this.customers_list, function (index, data) {
1852 if (in_list(me.removed_customers, index)) {
1853 delete me.customer_details[index]
1854 }
1855 })
1856 this.update_customer_in_localstorage();
1857 }
1858 },
1859
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301860 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301861 var me = this;
1862 this.customer_validate();
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301863 this.validate_zero_qty_items();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301864 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301865 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301866 },
1867
Rohit Waghchaure07f75a32019-01-29 12:01:54 +05301868 validate_zero_qty_items: function() {
1869 this.remove_item = [];
1870
1871 this.frm.doc.items.forEach(d => {
1872 if (d.qty == 0) {
1873 this.remove_item.push(d.idx);
1874 }
1875 });
1876
1877 if(this.remove_item) {
1878 this.remove_zero_qty_items_from_cart();
1879 }
1880 },
1881
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301882 item_validate: function () {
1883 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301884 frappe.throw(__("Select items to save the invoice"))
1885 }
1886 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301887
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301888 validate_mode_of_payments: function () {
1889 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301890 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1891 }
1892 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301893
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301894 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301895 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301896 var item_code = ''
1897 var serial_no = '';
1898 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301899 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301900 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301901 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301902
Rohit Waghchaurea9435cc2019-09-16 11:14:33 +05301903 if (this.items && this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301904 this.refresh();
1905 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1906 'item': this.items[0].item_code
1907 })))
1908 }
1909
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301910 if (item_code && serial_no) {
1911 $.each(this.frm.doc.items, function (index, data) {
1912 if (data.item_code == item_code) {
1913 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301914 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1915 'serial_no': serial_no
1916 })))
1917 }
1918 }
1919 })
1920 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301921 },
1922
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301923 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301924 var me = this;
1925 if (args.item_code == item_code && args.serial_no
1926 && field == 'qty' && cint(value) != value) {
1927 args.qty = 0.0;
1928 this.refresh();
1929 frappe.throw(__("Serial no item cannot be a fraction"))
1930 }
1931
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301932 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301933 args.qty = 0.0;
1934 args.serial_no = ''
1935 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301936 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1937 'item': item_code
1938 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301939 }
1940 },
1941
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301942 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301943 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301944 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301945 frappe.prompt([{
1946 'fieldname': 'batch',
1947 'fieldtype': 'Select',
1948 'label': __('Batch No'),
1949 'reqd': 1,
1950 'options': this.batch_no_data[this.items[0].item_code]
1951 }],
1952 function(values){
1953 me.item_batch_no[me.items[0].item_code] = values.batch;
Nabin Hait34c551d2019-07-03 10:34:31 +05301954 const item = me.frm.doc.items.find(
1955 ({ item_code }) => item_code === me.items[0].item_code
1956 );
1957 if (item) {
1958 item.batch_no = values.batch;
1959 }
Faris Ansariab74ca72017-05-30 12:54:42 +05301960 },
1961 __('Select Batch No'))
1962 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301963 },
1964
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301965 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301966 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301967 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301968 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301969 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301970 if (pricing_rule.length) {
1971 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301972 item.margin_type = pricing_rule[0].margin_type;
1973 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1974 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1975 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301976 me.apply_pricing_rule_on_item(item)
1977 } else if (item.pricing_rule) {
1978 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301979 item.margin_rate_or_amount = 0.0;
1980 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301981 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301982 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301983 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001984
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301985 if(item.discount_percentage > 0) {
1986 me.apply_pricing_rule_on_item(item)
1987 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301988 })
1989 },
1990
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301991 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301992 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301993 return $.grep(this.pricing_rules, function (data) {
1994 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301995 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301996 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301997 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301998 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301999 return true
2000 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302001 }
2002 }
2003 })
2004 },
2005
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05302006 validate_item_condition: function (data, item) {
2007 var apply_on = frappe.model.scrub(data.apply_on);
2008
2009 return (data.apply_on == 'Item Group')
2010 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
2011 },
2012
2013 validate_item_group: function (pr_item_group, cart_item_group) {
2014 //pr_item_group = pricing rule's item group
2015 //cart_item_group = cart item's item group
2016 //this.item_groups has information about item group's lft and rgt
2017 //for example: {'Foods': [12, 19]}
2018
2019 pr_item_group = this.item_groups[pr_item_group]
2020 cart_item_group = this.item_groups[cart_item_group]
2021
2022 return (cart_item_group[0] >= pr_item_group[0] &&
2023 cart_item_group[1] <= pr_item_group[1])
2024 },
2025
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302026 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302027 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05302028 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302029 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302030 return true
2031 }
2032 },
2033
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302034 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302035 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05302036 'Customer': [data.customer, [this.frm.doc.customer]],
2037 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
2038 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05302039 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302040 }
2041 },
2042
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302043 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302044 //This method validate duplicate pricing rule
2045 var pricing_rule_name = '';
2046 var priority = 0;
2047 var pricing_rule_list = [];
2048 var priority_list = []
2049
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302050 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302051
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302052 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302053 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302054 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302055 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302056 priority = data.priority
2057 pricing_rule_list.push(data)
2058 }
2059 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302060
Faris Ansariab74ca72017-05-30 12:54:42 +05302061 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302062 $.each(priority_list, function (index, value) {
2063 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302064 count++
2065 }
2066 })
2067
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302068 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302069 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
2070 'pricing_rule': pricing_rule_name
2071 })))
2072 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302073
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302074 return pricing_rule_list
2075 }
2076 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05302077
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302078 validate_warehouse: function () {
2079 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 +05302080 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302081 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302082 },
2083
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302084 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302085 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302086
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302087 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302088 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302089 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302090 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302091 }
2092
2093 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302094 },
2095
2096 update_customer_in_localstorage: function() {
2097 var me = this;
2098 try {
2099 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2100 } catch (e) {
2101 frappe.throw(__("LocalStorage is full , did not save"))
2102 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302103 }
Saurabh1a4d77a2018-02-08 01:32:48 +05302104})