blob: 29f27fb70c038b37ac73e46daf5ddfaf508e70d8 [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
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530126 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530127 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530128 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 },
130
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530131 email_prompt: function() {
132 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530133 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530134 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
135 {fieldtype: "Section Break"},
136 {label:__("Subject"), fieldtype:"Data", reqd: 1,
137 fieldname:"subject",length:524288},
138 {fieldtype: "Section Break"},
139 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
140 fieldname:"content"},
141 {fieldtype: "Section Break"},
142 {fieldtype: "Column Break"}];
143
144 this.email_dialog = new frappe.ui.Dialog({
145 title: "Email",
146 fields: fields,
147 primary_action_label: __("Send"),
148 primary_action: function() {
149 me.send_action();
150 }
151 });
152
153 this.email_dialog.show()
154 },
155
156 send_action: function() {
157 this.email_queue = this.get_email_queue()
158 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
159 this.update_email_queue()
160 this.email_dialog.hide()
161 },
162
163 update_email_queue: function () {
164 try {
165 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
166 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530167 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530168 }
169 },
170
171 get_email_queue: function () {
172 try {
173 return JSON.parse(localStorage.getItem('email_queue')) || {};
174 } catch (e) {
175 return {}
176 }
177 },
178
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530179 get_customers_details: function () {
180 try {
181 return JSON.parse(localStorage.getItem('customer_details')) || {};
182 } catch (e) {
183 return {}
184 }
185 },
186
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530187 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530188 var me = this;
189
190 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 if (doc_data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530192 this.frm.doc = doc_data[0][this.frm.doc.offline_pos_name];
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530193 this.set_missing_values();
194 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530195 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530196 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 }
198 },
199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530202 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530203 this.remove_doc_from_localstorage()
204 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530205 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530206 },
207
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530208 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530209 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530210 this.si_docs = this.get_submitted_invoice()
rohitwaghchauref688af32017-11-08 11:33:39 +0530211 $.each(this.removed_items, function(index, pos_name){
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530212 $.each(me.si_docs, function(key, data){
rohitwaghchauref688af32017-11-08 11:33:39 +0530213 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 +0530214 frappe.throw(__("Submitted orders can not be deleted"))
215 }
216 })
217 })
218 },
219
220 toggle_delete_button: function () {
221 var me = this;
222 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530223 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530225 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530226 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530227 }
228 }
229 },
230
231 get_doctype_status: function (doc) {
232 if (doc.docstatus == 0) {
233 return { status: "Draft", indicator: "red" }
234 } else if (doc.outstanding_amount == 0) {
235 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530236 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530237 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530238 }
239 },
240
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530241 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530242 var me = this;
243 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530244 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530245 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530246 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530247 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530248
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530249 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530250
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530251 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530252 this.frm.doc.write_off_account = doc.write_off_account
253 }
254
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530255 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530256 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530257 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530258 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530259
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530260 set_customer_value_in_party_field: function() {
261 if (this.frm.doc.customer) {
262 this.party_field.$input.val(this.frm.doc.customer);
263 }
264 },
265
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530266 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530267 var me = this;
268 this.si_docs = this.get_doc_from_localstorage();
269
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530270 return $.grep(this.si_docs, function (data) {
271 for (key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530272 return key == me.frm.doc.offline_pos_name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530274 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275 },
276
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530277 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530278 var me = this;
279 frappe.call({
280 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
281 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530282 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530283 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530285 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530286 me.set_interval_for_si_sync();
287 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530288 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530289 callback();
290 }
291 }
292 })
293 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530294
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530295 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530296 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530297 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530298 this.meta = r.message.meta;
299 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530300 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530301 this.customers = r.message.customers;
302 this.serial_no_data = r.message.serial_no_data;
303 this.batch_no_data = r.message.batch_no_data;
304 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530305 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530306 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530307 this.price_list_data = r.message.price_list_data;
308 this.bin_data = r.message.bin_data;
309 this.pricing_rules = r.message.pricing_rules;
310 this.print_template = r.message.print_template;
311 this.pos_profile_data = r.message.pos_profile;
312 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530313 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530314 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 +0530315 },
316
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530317 save_previous_entry: function () {
318 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
319 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530320 }
321 },
322
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530323 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530324 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530325 this.frm = {}
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530326 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530327 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530328 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530329 },
330
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530331 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530332 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530333
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530334 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530335 this.actual_qty_dict = {};
336
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530337 if (load_doc) {
338 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
rohitwaghchauref688af32017-11-08 11:33:39 +0530339 this.frm.doc.offline_pos_name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530340 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530343 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530344 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530345 })
346
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530347 this.print_template_data = frappe.render_template("print_template", {
348 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530349 title: "POS",
350 base_url: frappe.urllib.get_base_url(),
351 print_css: frappe.boot.print_css,
352 print_settings: this.print_settings,
353 header: this.letter_head.header,
354 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530355 landscape: false,
356 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530357 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530358 },
359
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530360 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530361 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530362 this.party_field.$input.attr('disabled', false);
363 if(this.selected_row) {
364 this.selected_row.hide()
365 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530366 },
367
Rohit Waghchaure61165122017-05-02 13:04:08 +0530368 set_default_customer: function() {
369 if (this.default_customer && !this.frm.doc.customer) {
370 this.party_field.$input.val(this.default_customer);
371 this.frm.doc.customer = this.default_customer;
372 this.numeric_keypad.show();
373 this.toggle_list_customer(false)
374 this.toggle_item_cart(true)
375 }
376 },
377
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530378 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530379 var me = this;
380 this.party = party;
381 this.price_list = (party == "Customer" ?
382 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
383 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
384 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
385 },
386
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530387 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530388 this.make_item_list();
389 this.make_discount_field()
390 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200391
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530392 make_control: function() {
393 this.frm = {}
394 this.frm.doc = this.doc
395 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530396 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530397 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530398 this.make_search();
399 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530400 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530401 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530402 },
403
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530404 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530405 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530406 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 df: {
408 "fieldtype": "Data",
409 "label": "Item",
410 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530411 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530412 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530413 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530414 only_input: true,
415 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530416
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530417 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530418
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530419 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530420
421 clearTimeout(me.last_search_timeout);
422 me.last_search_timeout = setTimeout(() => {
423 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
424 me.items = me.get_items();
425 me.make_item_list();
426 }
427 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530428 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530429
Faris Ansari339d9c92017-03-07 18:14:06 +0530430 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530431 sorted_item_groups = this.get_sorted_item_groups()
432 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530433 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
434 }).join("");
435
436 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
437
438 this.search_item_group.on('click', '.dropdown-menu a', function() {
439 me.selected_item_group = $(this).attr('data-value');
440 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
441
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530442 me.page_len = 20;
443 me.items = me.get_items();
444 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530445 })
446
Faris Ansari45510102017-03-09 14:43:37 +0530447 me.toggle_more_btn();
448
449 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530450 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530451 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530452 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530453 me.toggle_more_btn();
454 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530455
Faris Ansari45510102017-03-09 14:43:37 +0530456 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530457 me.update_customer()
458 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530459 },
460
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530461 get_sorted_item_groups: function() {
462 list = {}
463 $.each(this.item_groups, function(i, data) {
464 list[i] = data[0]
465 })
466
467 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
468 },
469
Faris Ansari45510102017-03-09 14:43:37 +0530470 toggle_more_btn: function() {
471 if(!this.items || this.items.length <= this.page_len) {
472 this.wrapper.find(".btn-more").hide();
473 } else {
474 this.wrapper.find(".btn-more").show();
475 }
476 },
477
478 toggle_totals_area: function(show) {
479
Faris Ansari07c9f352017-03-09 17:03:14 +0530480 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530481 show = this.is_totals_area_collapsed;
482 }
483
484 var totals_area = this.wrapper.find('.totals-area');
485 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
486 .toggle(show);
487
488 if(show) {
489 totals_area.find('.collapse-btn i')
490 .removeClass('octicon-chevron-down')
491 .addClass('octicon-chevron-up');
492 } else {
493 totals_area.find('.collapse-btn i')
494 .removeClass('octicon-chevron-up')
495 .addClass('octicon-chevron-down');
496 }
497
498 this.is_totals_area_collapsed = !show;
499 },
500
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530501 make_list_customers: function () {
502 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530503 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530504 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530505 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530506 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530507 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530508 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530509
Faris Ansari45510102017-03-09 14:43:37 +0530510 me.render_list_customers();
511 me.toggle_totals_area(false);
512
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530513 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530514 $(this).toggleClass("view_customer");
515 if($(this).hasClass("view_customer")) {
516 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530517 me.list_customers.show();
518 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530519 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530520 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530521 } else {
522 if(me.frm.doc.docstatus == 0) {
523 me.party_field.$input.attr('disabled', false);
524 }
525 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530526 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530527 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530528 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530529 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530530 }
531 });
532 this.add_customer_btn.on('click', function() {
533 me.save_previous_entry();
534 me.create_new();
535 me.refresh();
536 me.set_focus();
537 });
Faris Ansari45510102017-03-09 14:43:37 +0530538 this.pos_bill.on('click', '.collapse-btn', function() {
539 me.toggle_totals_area();
540 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530541 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200542
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530543 bind_numeric_keypad: function() {
544 var me = this;
545 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
546 me.numeric_val = '';
547 })
548
549 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
550 me.numeric_id = $(this).attr("id") || me.numeric_id;
551 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530552 if(me.numeric_id) {
553 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
554 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530555
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530556 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530557 me.numeric_val += me.val;
558 me.selected_field.val(flt(me.numeric_val))
559 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530560 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530561 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530562
563 if(me.numeric_id && $(this).hasClass('pos-operation')) {
564 me.numeric_keypad.find('button.pos-operation').removeClass('active');
565 $(this).addClass('active');
566
567 me.selected_row.find('.pos-list-row').removeClass('active');
568 me.selected_field.closest('.pos-list-row').addClass('active');
569 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530570 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200571
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530572 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530573 if(me.numeric_id) {
574 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
575 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
576 me.selected_field.val(me.numeric_val);
577 me.selected_field.trigger("change")
578 } else {
579 //Remove an item from the cart, if focus is at selected item
580 me.remove_selected_item()
581 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530582 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200583
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530584 $(this.numeric_keypad).find('.pos-pay').click(function(){
585 me.validate();
586 me.update_paid_amount_status(true);
587 me.create_invoice();
588 me.make_payment();
589 })
590 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530591
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530592 remove_selected_item: function() {
593 this.remove_item = []
594 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
595 this.remove_item.push(idx)
596 this.remove_zero_qty_item()
597 this.update_paid_amount_status(false)
598 },
599
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530600 render_list_customers: function () {
601 var me = this;
602
603 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530604 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530605 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530606 if (!this.si_docs.length) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530607 this.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530608 return;
609 }
Faris Ansari45510102017-03-09 14:43:37 +0530610
611 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530612 if(this.si_docs.length) {
613 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530614 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530615 html += frappe.render_template("pos_invoice_list", {
616 sr: i + 1,
617 name: key,
618 customer: data[key].customer,
619 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
620 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
621 data: me.get_doctype_status(data[key])
622 });
623 }
624 });
625 }
Faris Ansari45510102017-03-09 14:43:37 +0530626 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530627
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530628 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530629 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530630 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530631 me.list_customers_btn.toggleClass("view_customer");
632 me.pos_bill.show();
633 me.list_customers_btn.show();
rohitwaghchauref688af32017-11-08 11:33:39 +0530634 me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name')
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530635 me.edit_record();
636 })
637
638 //actions
639 $(this.wrapper).find('.list-select-all').click(function () {
640 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
641 me.removed_items = [];
642 if ($(this).is(":checked")) {
643 $.each(me.si_docs, function (index, data) {
644 for (key in data) {
645 me.removed_items.push(key)
646 }
647 });
648 }
649
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530650 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530651 });
652
653 $(this.wrapper).find('.list-delete').click(function () {
rohitwaghchauref688af32017-11-08 11:33:39 +0530654 me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530655 if ($(this).is(":checked")) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530656 me.removed_items.push(me.frm.doc.offline_pos_name);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530657 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +0530658 me.removed_items.pop(me.frm.doc.offline_pos_name)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530659 }
660
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530661 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530662 });
663 },
664
665 bind_delete_event: function() {
666 var me = this;
667
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530668 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530669 frappe.confirm(__("Delete permanently?"), function () {
670 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530671 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530672 me.render_list_customers();
673 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530674 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530675 },
676
677 set_focus: function () {
678 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530679 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530680 this.serach_item.$input.focus();
681 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530682 this.party_field.$input.focus();
683 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530684 },
685
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530686 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530687 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530688
689 if(!this.party_field) {
690 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
691 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530692 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530693 })).insertAfter(this.page.$title_area.hide());
694 }
695
696 this.party_field = frappe.ui.form.make_control({
697 df: {
698 "fieldtype": "Data",
699 "options": this.party,
700 "label": this.party,
701 "fieldname": this.party.toLowerCase(),
702 "placeholder": __("Select or add new customer")
703 },
704 parent: this.page.wrapper.find(".party-area"),
705 only_input: true,
706 });
707
708 this.party_field.make_input();
709 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530710 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530711 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530712
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530713 this.party_field.awesomeplete =
714 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530715 minChars: 0,
716 maxItems: 99,
717 autoFirst: true,
718 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530719 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530720 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530721 return true;
722 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530723
724 input = input.toLowerCase();
725 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530726 result = item ? item.searchtext.includes(input) : '';
727 if(!result) {
728 me.prepare_customer_mapper(input);
729 } else {
730 return result;
731 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530732 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530733 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530734 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530735 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530736 if(d.customer_name) {
737 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
738 }
739
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530740 return $('<li></li>')
741 .data('item.autocomplete', d)
742 .html('<a><p>' + html + '</p></a>')
743 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530744 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530745 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530746
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530747 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530748 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530749
750 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530751 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530752 if(me.customers_mapper.length <= 1) {
753 me.prepare_customer_mapper(e.target.value);
754 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530755 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530756 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530757 .on('awesomplete-select', function (e) {
758 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530759 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530760 if (!customer) return;
761 // create customer link
762 if (customer.action) {
763 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530764 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530765 }
Faris Ansari45510102017-03-09 14:43:37 +0530766 me.toggle_list_customer(false);
767 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530768 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530769 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530770 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530771 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530772 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530773 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530774 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530775 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530776
777 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530778 me.toggle_list_customer(false)
779 me.toggle_item_cart(true)
780 } else {
781 me.toggle_list_customer(true)
782 me.toggle_item_cart(false)
783 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530784 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530785 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530786 var item = me.party_field.awesomeplete
787 .get_item(e.originalEvent.text.value);
788 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530789 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530790 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530791 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530792 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530793 },
794
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530795 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530796 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530797 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530798
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530799 if (key) {
800 key = key.toLowerCase().trim();
801 var re = new RegExp('%', 'g');
802 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
803
804 customer_data = $.grep(this.customers, function(data) {
805 contact = me.contacts[data.name];
806 if(reg.test(data.name.toLowerCase())
807 || reg.test(data.customer_name.toLowerCase())
808 || (contact && reg.test(contact["mobile_no"]))
809 || (contact && reg.test(contact["phone"]))
810 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
811 return data;
812 }
813 })
814 } else {
815 customer_data = this.customers;
816 }
817
818 this.customers_mapper = [];
819
820 customer_data.forEach(function (c, index) {
821 if(index < 30) {
822 contact = me.contacts[c.name];
823 if(contact && !c['phone']) {
824 c["phone"] = contact["phone"];
825 c["email_id"] = contact["email_id"];
826 c["mobile_no"] = contact["mobile_no"];
827 }
828
829 me.customers_mapper.push({
830 label: c.name,
831 value: c.name,
832 customer_name: c.customer_name,
833 customer_group: c.customer_group,
834 territory: c.territory,
835 phone: contact ? contact["phone"] : '',
836 mobile_no: contact ? contact["mobile_no"] : '',
837 email_id: contact ? contact["email_id"] : '',
838 searchtext: ['customer_name', 'customer_group', 'name', 'value',
839 'label', 'email_id', 'phone', 'mobile_no']
840 .map(key => c[key]).join(' ')
841 .toLowerCase()
842 });
843 } else {
844 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530845 }
846 });
847
848 this.customers_mapper.push({
849 label: "<span class='text-primary link-option'>"
850 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
851 + __("Create a new Customer")
852 + "</span>",
853 value: 'is_action',
854 action: me.add_customer
855 });
856 },
857
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530858 autocomplete_customers: function() {
859 this.party_field.awesomeplete.list = this.customers_mapper;
860 },
861
Faris Ansari45510102017-03-09 14:43:37 +0530862 toggle_edit_button: function(flag) {
863 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
864 },
865
866 toggle_list_customer: function(flag) {
867 this.list_customers.toggle(flag);
868 },
869
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530870 toggle_item_cart: function(flag) {
871 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
872 },
873
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530874 add_customer: function() {
875 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530876 this.update_customer(true);
877 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530878 },
879
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530880 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530881 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530882
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530883 this.customer_doc = new frappe.ui.Dialog({
884 'title': 'Customer',
885 fields: [
886 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530887 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530888 "fieldname": "full_name",
889 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530890 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530891 },
892 {
893 "fieldtype": "Section Break"
894 },
895 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530896 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530897 "fieldname": "email_id",
898 "fieldtype": "Data"
899 },
900 {
901 "fieldtype": "Column Break"
902 },
903 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530904 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530905 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530906 "fieldtype": "Data"
907 },
908 {
909 "fieldtype": "Section Break"
910 },
911 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530912 "label": __("Address Name"),
913 "read_only": 1,
914 "fieldname": "name",
915 "fieldtype": "Data"
916 },
917 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530918 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530919 "fieldname": "address_line1",
920 "fieldtype": "Data"
921 },
922 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530923 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530924 "fieldname": "address_line2",
925 "fieldtype": "Data"
926 },
927 {
928 "fieldtype": "Column Break"
929 },
930 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530931 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530932 "fieldname": "city",
933 "fieldtype": "Data"
934 },
935 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530936 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530937 "fieldname": "state",
938 "fieldtype": "Data"
939 },
940 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530941 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530942 "fieldname": "pincode",
943 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530944 },
945 {
946 "label": __("Customer POS Id"),
947 "fieldname": "customer_pos_id",
948 "fieldtype": "Data",
949 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530950 }
951 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530952 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530953 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530954 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530955
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530956 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530957 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530958 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530959 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530960 });
961 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530962
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530963 render_address_data: function() {
964 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530965 this.address_data = this.address[this.frm.doc.customer] || {};
966 if(!this.address_data.email_id || !this.address_data.phone) {
967 this.address_data = this.contacts[this.frm.doc.customer];
968 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530969
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530970 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530971 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
972 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530973 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530974
975 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
976 this.customer_doc.set_value("customer_pos_id", $.now())
977 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530978 },
979
980 get_address_from_localstorage: function() {
981 this.address_details = this.get_customers_details()
982 return this.address_details[this.frm.doc.customer]
983 },
984
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530985 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530986 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530987 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530988 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530989 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530990 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530991 this.update_address_and_customer_list(new_customer)
992 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530993 this.update_customer_in_localstorage()
994 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530995 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530996 },
997
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530998 update_address_and_customer_list: function(new_customer) {
999 var me = this;
1000 if(new_customer) {
1001 this.customers_mapper.push({
1002 label: this.frm.doc.customer,
1003 value: this.frm.doc.customer,
1004 customer_group: "",
1005 territory: ""
1006 });
1007 }
1008
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301009 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301010 },
1011
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301012 get_prompt_details: function() {
1013 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301014 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301015 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1016 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301017 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301018 return JSON.stringify(this.prompt_details)
1019 },
1020
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301021 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301022 var me = this;
1023 this.frm.doc.customer = doc.label || doc.name;
1024 this.frm.doc.customer_name = doc.customer_name;
1025 this.frm.doc.customer_group = doc.customer_group;
1026 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301027 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301028 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301029 },
1030
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301031 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301032 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301033 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301034 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301035 return;
1036 }
1037
1038 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301039
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301040 var $wrap = me.wrapper.find(".item-list");
1041 me.wrapper.find(".item-list").empty();
1042
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301043 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301044 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301045 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301046 $(frappe.render_template("pos_item", {
1047 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301048 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301049 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301050 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301051 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301052 color: frappe.get_palette(obj.item_name),
1053 abbr: frappe.get_abbr(obj.item_name)
1054 })).tooltip().appendTo($wrap);
1055 }
1056 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001057
Faris Ansari45510102017-03-09 14:43:37 +05301058 $wrap.append(`
1059 <div class="image-view-item btn-more text-muted text-center">
1060 <div class="image-view-body">
1061 <i class="mega-octicon octicon-package"></i>
1062 <div>Load more items</div>
1063 </div>
1064 </div>
1065 `);
1066
1067 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301068 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301069 $("<p class='text-muted small' style='padding-left: 10px'>"
1070 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301071 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301072
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301073 if (this.items.length == 1
1074 && this.serach_item.$input.val()) {
1075 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301076 this.add_to_cart();
1077 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301078 },
1079
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301080 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301081 // To search item as per the key enter
1082
1083 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301084 this.item_serial_no = {};
1085 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301086
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301087 if (item_code) {
1088 return $.grep(this.item_data, function (item) {
1089 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301090 return true
1091 }
1092 })
1093 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301094
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301095 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301096
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301097 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301098 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301099 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301100 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301101
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301102 if (key) {
1103 return $.grep(this.items_list, function (item) {
1104 if (search_status) {
1105 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301106 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301107 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1108 } else if (me.serial_no_data[item.item_code]
1109 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301110 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301111 me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]]
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301112 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301113 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301114 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301115 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301116 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301117 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301118 return true
1119 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301120 }
1121 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301122 } else {
1123 return this.items_list;
1124 }
1125 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301126
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301127 apply_category: function() {
1128 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301129 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301130
1131 if(category == 'All Item Groups') {
1132 return this.item_data
1133 } else {
1134 return this.item_data.filter(function(element, index, array){
1135 return element.item_group == category;
1136 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301137 }
1138 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001139
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301140 bind_items_event: function() {
1141 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301142 $(this.wrapper).on('click', '.pos-bill-item', function() {
1143 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1144 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301145 me.numeric_val = "";
1146 me.numeric_id = ""
1147 me.item_code = $(this).attr("data-item-code");
1148 me.render_selected_item()
1149 me.bind_qty_event()
1150 me.update_rate()
1151 $(me.wrapper).find(".selected-item").scrollTop(1000);
1152 })
1153 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301154
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301155 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301156 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301157
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301158 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301159 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301160 var qty = $(this).val();
1161 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301162 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301163 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301164
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301165 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301166 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1167 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301168 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301169 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301170
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301171 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301172 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1173 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301174 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301175 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001176
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301177 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301178 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301179 var discount = $(this).val();
Saurabh1a4d77a2018-02-08 01:32:48 +05301180 if(discount > 100){
1181 discount = $(this).val('');
1182 frappe.show_alert({
1183 indicator: 'red',
1184 message: __('Discount amount cannot be greater than 100%')
1185 });
1186 me.update_discount(item_code, discount);
1187 }else{
1188 me.update_discount(item_code, discount);
1189 me.update_value();
1190 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301191 })
1192 },
1193
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301194 bind_events: function() {
1195 var me = this;
1196 // if form is local then allow this function
1197 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1198 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301199 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301200 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301201 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301202
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301203 if (me.frm.doc.docstatus == 0) {
1204 me.items = me.get_items($(this).attr("data-item-code"))
1205 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301206 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301207 }
1208 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301209
1210 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301211 },
1212
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301213 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301214 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301215 this.items = this.get_items(item_code);
1216 this.validate_serial_no()
1217 this.set_item_details(item_code, "qty", qty);
1218 },
1219
1220 update_discount: function(item_code, discount) {
1221 var me = this;
1222 this.items = this.get_items(item_code);
1223 this.set_item_details(item_code, "discount_percentage", discount);
1224 },
1225
1226 update_rate: function () {
1227 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301228 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301229 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301230 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301231 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301232 })
1233 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301234
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301235 update_value: function() {
1236 var me = this;
1237 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1238 "rate": ".pos-item-price", "amount": ".pos-amount"}
1239 this.child_doc = this.get_child_item(this.item_code);
1240
1241 if(me.child_doc.length) {
1242 $.each(fields, function(key, field) {
1243 $(me.selected_row).find(field).val(me.child_doc[0][key])
1244 })
1245 } else {
1246 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301247 }
1248 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301249
1250 clear_selected_row: function() {
1251 $(this.wrapper).find('.selected-item').empty();
1252 },
1253
1254 render_selected_item: function() {
1255 this.child_doc = this.get_child_item(this.item_code);
1256 $(this.wrapper).find('.selected-item').empty();
1257 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301258 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301259 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1260 $(this.wrapper).find('.selected-item').html(this.selected_row)
1261 }
1262
1263 $(this.selected_row).find('.form-control').click(function(){
1264 $(this).select();
1265 })
1266 },
1267
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301268 get_child_item: function(item_code) {
1269 var me = this;
1270 return $.map(me.frm.doc.items, function(doc){
1271 if(doc.item_code == item_code) {
1272 return doc
1273 }
1274 })
1275 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301276
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301277 set_item_details: function (item_code, field, value) {
1278 var me = this;
1279 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301280 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301281 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301282
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301283 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301284 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301285 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301286 if (d.serial_no && field == 'qty') {
1287 me.validate_serial_no_qty(d, item_code, field, value)
1288 }
1289
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301290 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301291 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301292 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301293 me.remove_item.push(d.idx)
1294 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301295
1296 if(field=="discount_percentage" && value == 0) {
1297 d.rate = d.price_list_rate;
1298 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301299 }
1300 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301301
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301302 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301303 this.remove_zero_qty_item();
1304 }
1305
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301306 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301307 },
1308
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301309 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301310 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301311 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301312 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301313 $.each(this.frm.doc["items"] || [], function (i, d) {
1314 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301315 d.idx = idx;
1316 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301317 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301318 }
1319 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301320
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301321 this.frm.doc["items"] = this.items;
1322 },
1323
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301324 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301325 var me = this;
1326
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301327 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301328 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301329
1330 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1331 // Reset discount amount
1332 me.frm.doc.discount_amount = 0;
1333 }
1334
Faris Ansariab74ca72017-05-30 12:54:42 +05301335 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301336
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301337 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301338 total = me.frm.doc.net_total
1339 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301340
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301341 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 +05301342 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301343 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301344 });
1345
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301346 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1348 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301349 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301350 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301351 });
1352 },
1353
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301354 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301355 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301356 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301357 frappe.throw(__("Please select customer"))
1358 }
1359 },
1360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301361 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301362 var me = this;
1363 var caught = false;
1364 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1365
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301366 this.customer_validate();
1367 this.mandatory_batch_no();
1368 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301369 this.validate_warehouse();
1370
1371 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301372 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301373 if (d.item_code == me.items[0].item_code) {
1374 caught = true;
1375 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301376 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301377 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301378 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1379 d.warehouse = me.item_serial_no[d.item_code][1]
1380 }
1381
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301382 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301383 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301384 }
1385 }
1386 });
1387 }
1388
1389 // if item not found then add new item
1390 if (!caught)
1391 this.add_new_item_to_grid();
1392
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301393 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301394 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301395 },
1396
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301397 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301398 var me = this;
1399 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1400 this.child.item_code = this.items[0].item_code;
1401 this.child.item_name = this.items[0].item_name;
1402 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301403 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301404 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301405 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301406 this.child.qty = 1;
1407 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301408 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1409 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301410 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301411 ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301412 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1413 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1414 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301415 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301416 this.child.batch_no = this.item_batch_no[this.child.item_code];
1417 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1418 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301419 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301420 },
1421
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301422 update_paid_amount_status: function (update_paid_amount) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301423 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301424 update_paid_amount = update_paid_amount ? false : true;
1425 }
1426
1427 this.refresh(update_paid_amount);
1428 },
1429
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301430 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301431 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301432 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301433 this.set_primary_action();
1434 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301435
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301436 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301437 this.apply_pricing_rule();
1438 this.discount_amount_applied = false;
1439 this._calculate_taxes_and_totals();
1440 this.calculate_discount_amount();
1441 this.show_items_in_item_cart();
1442 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301443 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301444 this.set_totals();
1445 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301446
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301447 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 return erpnext.get_currency(this.frm.doc.company);
1449 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301450
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301451 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301452 var me = this;
1453 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301454 var $no_items_message = this.wrapper.find(".no-items-message");
1455 $no_items_message.toggle(this.frm.doc.items.length === 0);
1456
1457 var $totals_area = this.wrapper.find('.totals-area');
1458 $totals_area.toggle(this.frm.doc.items.length > 0);
1459
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301460 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301461 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301462 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301463 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301464 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301465 discount_percentage: d.discount_percentage || 0.0,
1466 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301467 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301468 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301469 amount: format_currency(d.amount, me.frm.doc.currency),
1470 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301471 })).appendTo($items);
1472 });
1473
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301474 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 $(this).select();
1476 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301477
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301478 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301479 $(this).select();
1480 });
1481
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301482 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301483 $(this).select();
1484 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301485 },
1486
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301487 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301488 var me = this;
1489 me.frm.doc.total_taxes_and_charges = 0.0
1490
1491 var taxes = this.frm.doc.taxes || [];
1492 $(this.wrapper)
1493 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1494 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301495
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301496 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301497 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1498 $(frappe.render_template("pos_tax_row", {
1499 description: d.description,
1500 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1501 me.frm.doc.currency)
1502 })).appendTo(me.wrapper.find(".tax-table"));
1503 }
1504 });
1505 },
1506
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301507 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301508 var me = this;
1509 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1510 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301511 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1512 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301513 },
1514
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301515 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301516 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301517 this.page.set_primary_action(__("New Cart"), function () {
1518 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301519 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301520 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301521
augustinfotech135a54c2017-11-16 15:28:42 +05301522 if (this.frm.doc.docstatus == 1 || this.pos_profile_data["allow_print_before_pay"]) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301523 this.page.set_secondary_action(__("Print"), function () {
augustinfotech135a54c2017-11-16 15:28:42 +05301524 me.create_invoice();
Faris Ansariab74ca72017-05-30 12:54:42 +05301525 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301526 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301527 })
augustinfotech135a54c2017-11-16 15:28:42 +05301528 }
1529
1530 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301531 this.page.add_menu_item(__("Email"), function () {
1532 me.email_prompt()
1533 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301534 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301535 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301536
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301537 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301538 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301539 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301540 this.save_previous_entry();
1541 this.create_new();
1542 this.refresh();
1543 this.toggle_input_field();
1544 this.render_list_customers();
1545 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301546 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301547
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301548 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301549 var me = this;
1550
mbauskar66e2a512017-06-22 16:18:12 +05301551 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301552 `<a class="btn btn-primary print_doc"
1553 style="margin-right: 5px;">${__('Print')}</a>
1554 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301555
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301556 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301557 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301558 me.print_document(html)
1559 })
1560
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301561 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301562 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301563 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301564 })
1565 },
1566
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301567 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301568 var w = window.open();
1569 w.document.write(html);
1570 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301571 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301572 w.print();
1573 w.close();
1574 }, 1000)
1575 },
1576
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301577 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301578 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301579 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301580 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301581 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301582 this.print_dialog()
1583 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301584 },
1585
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301586 update_serial_no: function() {
1587 var me = this;
1588
1589 //Remove the sold serial no from the cache
1590 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301591 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301592 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301593 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301594 if(serial_no_list) {
1595 $.each(sn, function(i, serial_no) {
1596 if(in_list(Object.keys(serial_no_list), serial_no)) {
1597 delete serial_no_list[serial_no]
1598 }
1599 })
1600 me.serial_no_data[data.item_code] = serial_no_list;
1601 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301602 }
1603 })
1604 },
1605
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301606 change_status: function () {
1607 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301608 this.frm.doc.docstatus = 1;
1609 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301610 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301611 }
1612 },
1613
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301614 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301615 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301616 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301617 $(this.wrapper).find('input').attr("disabled", disabled);
1618 $(this.wrapper).find('select').attr("disabled", disabled);
1619 $(this.wrapper).find('input').attr("disabled", disabled);
1620 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301621 $(this.wrapper).find('button').attr("disabled", disabled);
1622 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301623
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301624 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301625 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301626 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301627
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301628 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301629 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1630 this.set_primary_action();
1631 },
1632
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301633 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301634 var me = this;
rohitwaghchauref688af32017-11-08 11:33:39 +05301635 var invoice_data = {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301636 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301637 if (this.frm.doc.offline_pos_name) {
1638 this.update_invoice();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301639 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +05301640 this.frm.doc.offline_pos_name = $.now();
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301641 this.frm.doc.posting_date = frappe.datetime.get_today();
1642 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001643 this.frm.doc.pos_profile = this.pos_profile_data['name'];
rohitwaghchauref688af32017-11-08 11:33:39 +05301644 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1645 this.si_docs.push(invoice_data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301646 this.update_localstorage();
1647 this.set_primary_action();
1648 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301649 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301650 },
1651
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301652 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301653 var me = this;
1654 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301655 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301656 for (var key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301657 if (key == me.frm.doc.offline_pos_name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301658 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301659 me.update_localstorage();
1660 }
1661 }
rohitwaghchauref688af32017-11-08 11:33:39 +05301662 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301663 },
1664
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301665 update_localstorage: function () {
1666 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301667 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301668 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301669 frappe.throw(__("LocalStorage is full , did not save"))
1670 }
1671 },
1672
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301673 get_doc_from_localstorage: function () {
1674 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301675 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301676 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301677 return []
1678 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301679 },
1680
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301681 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301682 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301683 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301684 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301685 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301686 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301687 },
1688
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301689 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301690 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301691 this.si_docs = this.get_submitted_invoice() || [];
1692 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301693 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301694 if(this.customer_doc) {
1695 this.freeze = this.customer_doc.display
1696 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301697
rohitwaghchaure88491712017-10-02 12:13:36 +05301698 freeze_screen = this.freeze_screen || false;
1699
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301700 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301701 this.freeze = true;
1702
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301703 frappe.call({
1704 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88491712017-10-02 12:13:36 +05301705 freeze: freeze_screen,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301706 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301707 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301708 email_queue_list: me.email_queue_list,
1709 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301710 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301711 callback: function (r) {
1712 if (r.message) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301713 me.freeze = false;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301714 me.customers = r.message.synced_customers_list;
1715 me.address = r.message.synced_address;
1716 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301717 me.removed_items = r.message.invoice;
rohitwaghchauref688af32017-11-08 11:33:39 +05301718 me.removed_email = r.message.email_queue;
1719 me.removed_customers = r.message.customers;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301720 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301721 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301722 me.remove_customer_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301723 me.prepare_customer_mapper();
1724 me.autocomplete_customers();
1725 me.render_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301726 }
1727 }
1728 })
1729 }
1730 },
1731
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301732 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301733 var invoices = [];
1734 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301735 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301736 if (docs) {
1737 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301738 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301739 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301740 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301741 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301742 return data
1743 }
1744 }
1745 });
1746 }
1747
1748 return invoices
1749 },
1750
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301751 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301752 var me = this;
1753 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301754 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301755 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301756 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301757 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301758 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301759 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301760 }
1761 }
1762 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301763 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301764 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301765 this.update_localstorage();
1766 }
1767 },
1768
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301769 remove_email_queue_from_localstorage: function() {
1770 var me = this;
1771 this.email_queue = this.get_email_queue()
1772 if (this.removed_email) {
1773 $.each(this.email_queue_list, function (index, data) {
1774 if (in_list(me.removed_email, index)) {
1775 delete me.email_queue[index]
1776 }
1777 })
1778 this.update_email_queue();
1779 }
1780 },
1781
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301782 remove_customer_from_localstorage: function() {
1783 var me = this;
1784 this.customer_details = this.get_customers_details()
1785 if (this.removed_customers) {
1786 $.each(this.customers_list, function (index, data) {
1787 if (in_list(me.removed_customers, index)) {
1788 delete me.customer_details[index]
1789 }
1790 })
1791 this.update_customer_in_localstorage();
1792 }
1793 },
1794
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301795 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301796 var me = this;
1797 this.customer_validate();
1798 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301799 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301800 },
1801
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301802 item_validate: function () {
1803 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301804 frappe.throw(__("Select items to save the invoice"))
1805 }
1806 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301807
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301808 validate_mode_of_payments: function () {
1809 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301810 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1811 }
1812 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301813
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301814 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301815 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301816 var item_code = ''
1817 var serial_no = '';
1818 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301819 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301820 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301821 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301822
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301823 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301824 this.refresh();
1825 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1826 'item': this.items[0].item_code
1827 })))
1828 }
1829
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301830 if (item_code && serial_no) {
1831 $.each(this.frm.doc.items, function (index, data) {
1832 if (data.item_code == item_code) {
1833 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301834 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1835 'serial_no': serial_no
1836 })))
1837 }
1838 }
1839 })
1840 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301841 },
1842
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301843 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301844 var me = this;
1845 if (args.item_code == item_code && args.serial_no
1846 && field == 'qty' && cint(value) != value) {
1847 args.qty = 0.0;
1848 this.refresh();
1849 frappe.throw(__("Serial no item cannot be a fraction"))
1850 }
1851
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301852 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301853 args.qty = 0.0;
1854 args.serial_no = ''
1855 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301856 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1857 'item': item_code
1858 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301859 }
1860 },
1861
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301862 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301863 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301864 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301865 frappe.prompt([{
1866 'fieldname': 'batch',
1867 'fieldtype': 'Select',
1868 'label': __('Batch No'),
1869 'reqd': 1,
1870 'options': this.batch_no_data[this.items[0].item_code]
1871 }],
1872 function(values){
1873 me.item_batch_no[me.items[0].item_code] = values.batch;
1874 },
1875 __('Select Batch No'))
1876 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301877 },
1878
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301879 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301880 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301881 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301882 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301883 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301884 if (pricing_rule.length) {
1885 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301886 item.margin_type = pricing_rule[0].margin_type;
1887 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1888 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1889 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301890 me.apply_pricing_rule_on_item(item)
1891 } else if (item.pricing_rule) {
1892 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301893 item.margin_rate_or_amount = 0.0;
1894 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301895 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301896 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301897 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001898
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301899 if(item.discount_percentage > 0) {
1900 me.apply_pricing_rule_on_item(item)
1901 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301902 })
1903 },
1904
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301905 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301906 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301907 return $.grep(this.pricing_rules, function (data) {
1908 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301909 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301910 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301911 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301912 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301913 return true
1914 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301915 }
1916 }
1917 })
1918 },
1919
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301920 validate_item_condition: function (data, item) {
1921 var apply_on = frappe.model.scrub(data.apply_on);
1922
1923 return (data.apply_on == 'Item Group')
1924 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1925 },
1926
1927 validate_item_group: function (pr_item_group, cart_item_group) {
1928 //pr_item_group = pricing rule's item group
1929 //cart_item_group = cart item's item group
1930 //this.item_groups has information about item group's lft and rgt
1931 //for example: {'Foods': [12, 19]}
1932
1933 pr_item_group = this.item_groups[pr_item_group]
1934 cart_item_group = this.item_groups[cart_item_group]
1935
1936 return (cart_item_group[0] >= pr_item_group[0] &&
1937 cart_item_group[1] <= pr_item_group[1])
1938 },
1939
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301940 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301941 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301942 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301943 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301944 return true
1945 }
1946 },
1947
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301948 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301949 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301950 'Customer': [data.customer, [this.frm.doc.customer]],
1951 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1952 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301953 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301954 }
1955 },
1956
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301957 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301958 //This method validate duplicate pricing rule
1959 var pricing_rule_name = '';
1960 var priority = 0;
1961 var pricing_rule_list = [];
1962 var priority_list = []
1963
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301964 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301965
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301966 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301967 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301968 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301969 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301970 priority = data.priority
1971 pricing_rule_list.push(data)
1972 }
1973 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301974
Faris Ansariab74ca72017-05-30 12:54:42 +05301975 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301976 $.each(priority_list, function (index, value) {
1977 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301978 count++
1979 }
1980 })
1981
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301982 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301983 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1984 'pricing_rule': pricing_rule_name
1985 })))
1986 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301987
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301988 return pricing_rule_list
1989 }
1990 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301991
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301992 validate_warehouse: function () {
1993 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 +05301994 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301995 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301996 },
1997
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301998 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301999 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302000
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302001 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302002 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302003 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302004 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302005 }
2006
2007 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302008 },
2009
2010 update_customer_in_localstorage: function() {
2011 var me = this;
2012 try {
2013 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2014 } catch (e) {
2015 frappe.throw(__("LocalStorage is full , did not save"))
2016 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302017 }
Saurabh1a4d77a2018-02-08 01:32:48 +05302018})