blob: fe71f28e6187814961b8fed559426f4d4348e86f [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");
rohitwaghchaurea32e57a2018-03-08 16:21:13 +0530396 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false;
397 this.frm.doc["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530398 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530399 this.make_search();
400 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530401 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530402 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 },
404
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530405 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530406 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530407 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530408 df: {
409 "fieldtype": "Data",
410 "label": "Item",
411 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530412 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530414 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 only_input: true,
416 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530417
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530418 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530419
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530420 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530421
422 clearTimeout(me.last_search_timeout);
423 me.last_search_timeout = setTimeout(() => {
424 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
425 me.items = me.get_items();
426 me.make_item_list();
427 }
428 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530429 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530430
Faris Ansari339d9c92017-03-07 18:14:06 +0530431 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530432 sorted_item_groups = this.get_sorted_item_groups()
433 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530434 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
435 }).join("");
436
437 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
438
439 this.search_item_group.on('click', '.dropdown-menu a', function() {
440 me.selected_item_group = $(this).attr('data-value');
441 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
442
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530443 me.page_len = 20;
444 me.items = me.get_items();
445 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530446 })
447
Faris Ansari45510102017-03-09 14:43:37 +0530448 me.toggle_more_btn();
449
450 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530451 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530452 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530453 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530454 me.toggle_more_btn();
455 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530456
Faris Ansari45510102017-03-09 14:43:37 +0530457 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530458 me.update_customer()
459 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530460 },
461
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530462 get_sorted_item_groups: function() {
463 list = {}
464 $.each(this.item_groups, function(i, data) {
465 list[i] = data[0]
466 })
467
468 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
469 },
470
Faris Ansari45510102017-03-09 14:43:37 +0530471 toggle_more_btn: function() {
472 if(!this.items || this.items.length <= this.page_len) {
473 this.wrapper.find(".btn-more").hide();
474 } else {
475 this.wrapper.find(".btn-more").show();
476 }
477 },
478
479 toggle_totals_area: function(show) {
480
Faris Ansari07c9f352017-03-09 17:03:14 +0530481 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530482 show = this.is_totals_area_collapsed;
483 }
484
485 var totals_area = this.wrapper.find('.totals-area');
486 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
487 .toggle(show);
488
489 if(show) {
490 totals_area.find('.collapse-btn i')
491 .removeClass('octicon-chevron-down')
492 .addClass('octicon-chevron-up');
493 } else {
494 totals_area.find('.collapse-btn i')
495 .removeClass('octicon-chevron-up')
496 .addClass('octicon-chevron-down');
497 }
498
499 this.is_totals_area_collapsed = !show;
500 },
501
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530502 make_list_customers: function () {
503 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530504 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530505 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530506 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530507 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530508 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530509 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530510
Faris Ansari45510102017-03-09 14:43:37 +0530511 me.render_list_customers();
512 me.toggle_totals_area(false);
513
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530514 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530515 $(this).toggleClass("view_customer");
516 if($(this).hasClass("view_customer")) {
517 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530518 me.list_customers.show();
519 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530520 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530521 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530522 } else {
523 if(me.frm.doc.docstatus == 0) {
524 me.party_field.$input.attr('disabled', false);
525 }
526 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530527 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530528 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530529 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530530 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530531 }
532 });
533 this.add_customer_btn.on('click', function() {
534 me.save_previous_entry();
535 me.create_new();
536 me.refresh();
537 me.set_focus();
538 });
Faris Ansari45510102017-03-09 14:43:37 +0530539 this.pos_bill.on('click', '.collapse-btn', function() {
540 me.toggle_totals_area();
541 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530542 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200543
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530544 bind_numeric_keypad: function() {
545 var me = this;
546 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
547 me.numeric_val = '';
548 })
549
550 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
551 me.numeric_id = $(this).attr("id") || me.numeric_id;
552 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530553 if(me.numeric_id) {
554 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
555 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530556
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530557 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530558 me.numeric_val += me.val;
559 me.selected_field.val(flt(me.numeric_val))
560 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530561 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530562 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530563
564 if(me.numeric_id && $(this).hasClass('pos-operation')) {
565 me.numeric_keypad.find('button.pos-operation').removeClass('active');
566 $(this).addClass('active');
567
568 me.selected_row.find('.pos-list-row').removeClass('active');
569 me.selected_field.closest('.pos-list-row').addClass('active');
570 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530571 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200572
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530573 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530574 if(me.numeric_id) {
575 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
576 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
577 me.selected_field.val(me.numeric_val);
578 me.selected_field.trigger("change")
579 } else {
580 //Remove an item from the cart, if focus is at selected item
581 me.remove_selected_item()
582 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530583 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200584
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530585 $(this.numeric_keypad).find('.pos-pay').click(function(){
586 me.validate();
587 me.update_paid_amount_status(true);
588 me.create_invoice();
589 me.make_payment();
590 })
591 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530592
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530593 remove_selected_item: function() {
594 this.remove_item = []
595 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
596 this.remove_item.push(idx)
597 this.remove_zero_qty_item()
598 this.update_paid_amount_status(false)
599 },
600
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530601 render_list_customers: function () {
602 var me = this;
603
604 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530605 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530606 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530607 if (!this.si_docs.length) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530608 this.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530609 return;
610 }
Faris Ansari45510102017-03-09 14:43:37 +0530611
612 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530613 if(this.si_docs.length) {
614 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530615 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530616 html += frappe.render_template("pos_invoice_list", {
617 sr: i + 1,
618 name: key,
619 customer: data[key].customer,
620 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
621 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
622 data: me.get_doctype_status(data[key])
623 });
624 }
625 });
626 }
Faris Ansari45510102017-03-09 14:43:37 +0530627 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530628
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530629 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530630 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530631 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530632 me.list_customers_btn.toggleClass("view_customer");
633 me.pos_bill.show();
634 me.list_customers_btn.show();
rohitwaghchauref688af32017-11-08 11:33:39 +0530635 me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name')
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530636 me.edit_record();
637 })
638
639 //actions
640 $(this.wrapper).find('.list-select-all').click(function () {
641 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
642 me.removed_items = [];
643 if ($(this).is(":checked")) {
644 $.each(me.si_docs, function (index, data) {
645 for (key in data) {
646 me.removed_items.push(key)
647 }
648 });
649 }
650
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530651 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530652 });
653
654 $(this.wrapper).find('.list-delete').click(function () {
rohitwaghchauref688af32017-11-08 11:33:39 +0530655 me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530656 if ($(this).is(":checked")) {
rohitwaghchauref688af32017-11-08 11:33:39 +0530657 me.removed_items.push(me.frm.doc.offline_pos_name);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530658 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +0530659 me.removed_items.pop(me.frm.doc.offline_pos_name)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530660 }
661
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530662 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530663 });
664 },
665
666 bind_delete_event: function() {
667 var me = this;
668
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530669 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530670 frappe.confirm(__("Delete permanently?"), function () {
671 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530672 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530673 me.render_list_customers();
674 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530675 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530676 },
677
678 set_focus: function () {
679 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530680 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530681 this.serach_item.$input.focus();
682 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530683 this.party_field.$input.focus();
684 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530685 },
686
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530687 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530688 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530689
690 if(!this.party_field) {
691 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
692 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530693 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530694 })).insertAfter(this.page.$title_area.hide());
695 }
696
697 this.party_field = frappe.ui.form.make_control({
698 df: {
699 "fieldtype": "Data",
700 "options": this.party,
701 "label": this.party,
702 "fieldname": this.party.toLowerCase(),
703 "placeholder": __("Select or add new customer")
704 },
705 parent: this.page.wrapper.find(".party-area"),
706 only_input: true,
707 });
708
709 this.party_field.make_input();
710 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530711 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530712 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530713
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530714 this.party_field.awesomeplete =
715 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530716 minChars: 0,
717 maxItems: 99,
718 autoFirst: true,
719 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530720 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530721 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530722 return true;
723 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530724
725 input = input.toLowerCase();
726 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530727 result = item ? item.searchtext.includes(input) : '';
728 if(!result) {
729 me.prepare_customer_mapper(input);
730 } else {
731 return result;
732 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530733 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530734 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530735 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530736 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530737 if(d.customer_name) {
738 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
739 }
740
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530741 return $('<li></li>')
742 .data('item.autocomplete', d)
743 .html('<a><p>' + html + '</p></a>')
744 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530745 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530746 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530747
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530748 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530749 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530750
751 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530752 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530753 if(me.customers_mapper.length <= 1) {
754 me.prepare_customer_mapper(e.target.value);
755 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530756 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530757 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530758 .on('awesomplete-select', function (e) {
759 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530760 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530761 if (!customer) return;
762 // create customer link
763 if (customer.action) {
764 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530765 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530766 }
Faris Ansari45510102017-03-09 14:43:37 +0530767 me.toggle_list_customer(false);
768 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530769 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530770 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530771 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530772 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530773 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530774 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530775 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530776 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530777
778 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530779 me.toggle_list_customer(false)
780 me.toggle_item_cart(true)
781 } else {
782 me.toggle_list_customer(true)
783 me.toggle_item_cart(false)
784 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530785 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530786 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530787 var item = me.party_field.awesomeplete
788 .get_item(e.originalEvent.text.value);
789 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530790 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530791 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530792 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530793 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530794 },
795
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530796 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530797 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530798 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530799
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530800 if (key) {
801 key = key.toLowerCase().trim();
802 var re = new RegExp('%', 'g');
803 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
804
805 customer_data = $.grep(this.customers, function(data) {
806 contact = me.contacts[data.name];
807 if(reg.test(data.name.toLowerCase())
808 || reg.test(data.customer_name.toLowerCase())
809 || (contact && reg.test(contact["mobile_no"]))
810 || (contact && reg.test(contact["phone"]))
811 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
812 return data;
813 }
814 })
815 } else {
816 customer_data = this.customers;
817 }
818
819 this.customers_mapper = [];
820
821 customer_data.forEach(function (c, index) {
822 if(index < 30) {
823 contact = me.contacts[c.name];
824 if(contact && !c['phone']) {
825 c["phone"] = contact["phone"];
826 c["email_id"] = contact["email_id"];
827 c["mobile_no"] = contact["mobile_no"];
828 }
829
830 me.customers_mapper.push({
831 label: c.name,
832 value: c.name,
833 customer_name: c.customer_name,
834 customer_group: c.customer_group,
835 territory: c.territory,
836 phone: contact ? contact["phone"] : '',
837 mobile_no: contact ? contact["mobile_no"] : '',
838 email_id: contact ? contact["email_id"] : '',
839 searchtext: ['customer_name', 'customer_group', 'name', 'value',
840 'label', 'email_id', 'phone', 'mobile_no']
841 .map(key => c[key]).join(' ')
842 .toLowerCase()
843 });
844 } else {
845 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530846 }
847 });
848
849 this.customers_mapper.push({
850 label: "<span class='text-primary link-option'>"
851 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
852 + __("Create a new Customer")
853 + "</span>",
854 value: 'is_action',
855 action: me.add_customer
856 });
857 },
858
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530859 autocomplete_customers: function() {
860 this.party_field.awesomeplete.list = this.customers_mapper;
861 },
862
Faris Ansari45510102017-03-09 14:43:37 +0530863 toggle_edit_button: function(flag) {
864 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
865 },
866
867 toggle_list_customer: function(flag) {
868 this.list_customers.toggle(flag);
869 },
870
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530871 toggle_item_cart: function(flag) {
872 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
873 },
874
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530875 add_customer: function() {
876 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530877 this.update_customer(true);
878 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530879 },
880
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530881 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530882 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530883
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530884 this.customer_doc = new frappe.ui.Dialog({
885 'title': 'Customer',
886 fields: [
887 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530888 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530889 "fieldname": "full_name",
890 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530891 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530892 },
893 {
894 "fieldtype": "Section Break"
895 },
896 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530897 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530898 "fieldname": "email_id",
899 "fieldtype": "Data"
900 },
901 {
902 "fieldtype": "Column Break"
903 },
904 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530905 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530906 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530907 "fieldtype": "Data"
908 },
909 {
910 "fieldtype": "Section Break"
911 },
912 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530913 "label": __("Address Name"),
914 "read_only": 1,
915 "fieldname": "name",
916 "fieldtype": "Data"
917 },
918 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530919 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530920 "fieldname": "address_line1",
921 "fieldtype": "Data"
922 },
923 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530924 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530925 "fieldname": "address_line2",
926 "fieldtype": "Data"
927 },
928 {
929 "fieldtype": "Column Break"
930 },
931 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530932 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530933 "fieldname": "city",
934 "fieldtype": "Data"
935 },
936 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530937 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530938 "fieldname": "state",
939 "fieldtype": "Data"
940 },
941 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530942 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530943 "fieldname": "pincode",
944 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530945 },
946 {
947 "label": __("Customer POS Id"),
948 "fieldname": "customer_pos_id",
949 "fieldtype": "Data",
950 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530951 }
952 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530953 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530954 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530955 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530956
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530957 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530958 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530959 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530960 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530961 });
962 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530963
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530964 render_address_data: function() {
965 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530966 this.address_data = this.address[this.frm.doc.customer] || {};
967 if(!this.address_data.email_id || !this.address_data.phone) {
968 this.address_data = this.contacts[this.frm.doc.customer];
969 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530970
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530971 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530972 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
973 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530974 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530975
976 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
977 this.customer_doc.set_value("customer_pos_id", $.now())
978 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530979 },
980
981 get_address_from_localstorage: function() {
982 this.address_details = this.get_customers_details()
983 return this.address_details[this.frm.doc.customer]
984 },
985
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530986 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530987 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530988 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530989 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530990 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530991 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530992 this.update_address_and_customer_list(new_customer)
993 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530994 this.update_customer_in_localstorage()
995 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530996 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530997 },
998
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530999 update_address_and_customer_list: function(new_customer) {
1000 var me = this;
1001 if(new_customer) {
1002 this.customers_mapper.push({
1003 label: this.frm.doc.customer,
1004 value: this.frm.doc.customer,
1005 customer_group: "",
1006 territory: ""
1007 });
1008 }
1009
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301010 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301011 },
1012
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301013 get_prompt_details: function() {
1014 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301015 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301016 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1017 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301018 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301019 return JSON.stringify(this.prompt_details)
1020 },
1021
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301022 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301023 var me = this;
1024 this.frm.doc.customer = doc.label || doc.name;
1025 this.frm.doc.customer_name = doc.customer_name;
1026 this.frm.doc.customer_group = doc.customer_group;
1027 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301028 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301029 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 },
1031
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301033 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301034 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301035 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301036 return;
1037 }
1038
1039 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301040
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301041 var $wrap = me.wrapper.find(".item-list");
1042 me.wrapper.find(".item-list").empty();
1043
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301044 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301045 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301046 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301047 $(frappe.render_template("pos_item", {
1048 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301049 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301050 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301051 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301052 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301053 color: frappe.get_palette(obj.item_name),
1054 abbr: frappe.get_abbr(obj.item_name)
1055 })).tooltip().appendTo($wrap);
1056 }
1057 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001058
Faris Ansari45510102017-03-09 14:43:37 +05301059 $wrap.append(`
1060 <div class="image-view-item btn-more text-muted text-center">
1061 <div class="image-view-body">
1062 <i class="mega-octicon octicon-package"></i>
1063 <div>Load more items</div>
1064 </div>
1065 </div>
1066 `);
1067
1068 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301069 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301070 $("<p class='text-muted small' style='padding-left: 10px'>"
1071 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301072 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301073
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301074 if (this.items.length == 1
1075 && this.serach_item.$input.val()) {
1076 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301077 this.add_to_cart();
1078 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301079 },
1080
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301081 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301082 // To search item as per the key enter
1083
1084 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301085 this.item_serial_no = {};
1086 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301087
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301088 if (item_code) {
1089 return $.grep(this.item_data, function (item) {
1090 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301091 return true
1092 }
1093 })
1094 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301095
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301096 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301097
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301098 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301099 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301100 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301101 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301102
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301103 if (key) {
1104 return $.grep(this.items_list, function (item) {
1105 if (search_status) {
1106 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301107 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301108 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1109 } else if (me.serial_no_data[item.item_code]
1110 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301111 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301112 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 +05301113 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301114 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301115 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301116 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301117 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301118 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301119 return true
1120 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301121 }
1122 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301123 } else {
1124 return this.items_list;
1125 }
1126 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301127
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301128 apply_category: function() {
1129 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301130 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301131
1132 if(category == 'All Item Groups') {
1133 return this.item_data
1134 } else {
1135 return this.item_data.filter(function(element, index, array){
1136 return element.item_group == category;
1137 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301138 }
1139 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001140
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301141 bind_items_event: function() {
1142 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301143 $(this.wrapper).on('click', '.pos-bill-item', function() {
1144 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1145 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301146 me.numeric_val = "";
1147 me.numeric_id = ""
1148 me.item_code = $(this).attr("data-item-code");
1149 me.render_selected_item()
1150 me.bind_qty_event()
1151 me.update_rate()
1152 $(me.wrapper).find(".selected-item").scrollTop(1000);
1153 })
1154 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301155
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301156 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301157 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301158
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301159 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301160 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301161 var qty = $(this).val();
1162 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301163 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301164 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301165
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301166 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301167 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1168 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301169 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301170 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301171
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301172 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301173 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1174 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301175 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301176 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001177
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301178 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301179 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301180 var discount = $(this).val();
Saurabh1a4d77a2018-02-08 01:32:48 +05301181 if(discount > 100){
1182 discount = $(this).val('');
1183 frappe.show_alert({
1184 indicator: 'red',
1185 message: __('Discount amount cannot be greater than 100%')
1186 });
1187 me.update_discount(item_code, discount);
1188 }else{
1189 me.update_discount(item_code, discount);
1190 me.update_value();
1191 }
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301192 })
1193 },
1194
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301195 bind_events: function() {
1196 var me = this;
1197 // if form is local then allow this function
1198 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1199 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301200 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301201 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301202 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301203
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301204 if (me.frm.doc.docstatus == 0) {
1205 me.items = me.get_items($(this).attr("data-item-code"))
1206 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301207 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301208 }
1209 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301210
1211 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301212 },
1213
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301214 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301215 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301216 this.items = this.get_items(item_code);
1217 this.validate_serial_no()
1218 this.set_item_details(item_code, "qty", qty);
1219 },
1220
1221 update_discount: function(item_code, discount) {
1222 var me = this;
1223 this.items = this.get_items(item_code);
1224 this.set_item_details(item_code, "discount_percentage", discount);
1225 },
1226
1227 update_rate: function () {
1228 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301229 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301230 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301231 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301232 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301233 })
1234 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301235
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301236 update_value: function() {
1237 var me = this;
1238 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1239 "rate": ".pos-item-price", "amount": ".pos-amount"}
1240 this.child_doc = this.get_child_item(this.item_code);
1241
1242 if(me.child_doc.length) {
1243 $.each(fields, function(key, field) {
1244 $(me.selected_row).find(field).val(me.child_doc[0][key])
1245 })
1246 } else {
1247 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301248 }
1249 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301250
1251 clear_selected_row: function() {
1252 $(this.wrapper).find('.selected-item').empty();
1253 },
1254
1255 render_selected_item: function() {
1256 this.child_doc = this.get_child_item(this.item_code);
1257 $(this.wrapper).find('.selected-item').empty();
1258 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301259 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301260 this.child_doc[0]["allow_user_to_edit_discount"] = this.pos_profile_data["allow_user_to_edit_discount"] ? true : false;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301261 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1262 $(this.wrapper).find('.selected-item').html(this.selected_row)
1263 }
1264
1265 $(this.selected_row).find('.form-control').click(function(){
1266 $(this).select();
1267 })
1268 },
1269
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301270 get_child_item: function(item_code) {
1271 var me = this;
1272 return $.map(me.frm.doc.items, function(doc){
1273 if(doc.item_code == item_code) {
1274 return doc
1275 }
1276 })
1277 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301278
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301279 set_item_details: function (item_code, field, value) {
1280 var me = this;
1281 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301282 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301283 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301284
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301285 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301286 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301287 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301288 if (d.serial_no && field == 'qty') {
1289 me.validate_serial_no_qty(d, item_code, field, value)
1290 }
1291
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301292 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301293 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301294 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301295 me.remove_item.push(d.idx)
1296 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301297
1298 if(field=="discount_percentage" && value == 0) {
1299 d.rate = d.price_list_rate;
1300 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 }
1302 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301303
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301304 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301305 this.remove_zero_qty_item();
1306 }
1307
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301308 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301309 },
1310
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301311 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301312 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301313 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301314 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301315 $.each(this.frm.doc["items"] || [], function (i, d) {
1316 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301317 d.idx = idx;
1318 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301319 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301320 }
1321 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301322
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301323 this.frm.doc["items"] = this.items;
1324 },
1325
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301326 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301327 var me = this;
1328
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301329 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301330 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301331
1332 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1333 // Reset discount amount
1334 me.frm.doc.discount_amount = 0;
1335 }
1336
Faris Ansariab74ca72017-05-30 12:54:42 +05301337 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301338
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301339 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301340 total = me.frm.doc.net_total
1341 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301342
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301343 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 +05301344 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301345 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301346 });
1347
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301348 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301349 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1350 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301351 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301352 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301353 });
1354 },
1355
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301356 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301357 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301358 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301359 frappe.throw(__("Please select customer"))
1360 }
1361 },
1362
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301363 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301364 var me = this;
1365 var caught = false;
1366 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1367
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301368 this.customer_validate();
1369 this.mandatory_batch_no();
1370 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301371 this.validate_warehouse();
1372
1373 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301374 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301375 if (d.item_code == me.items[0].item_code) {
1376 caught = true;
1377 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301378 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301379 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301380 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1381 d.warehouse = me.item_serial_no[d.item_code][1]
1382 }
1383
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301384 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301385 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301386 }
1387 }
1388 });
1389 }
1390
1391 // if item not found then add new item
1392 if (!caught)
1393 this.add_new_item_to_grid();
1394
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301395 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301396 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301397 },
1398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301399 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301400 var me = this;
1401 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1402 this.child.item_code = this.items[0].item_code;
1403 this.child.item_name = this.items[0].item_name;
1404 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301405 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301406 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301407 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301408 this.child.qty = 1;
1409 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301410 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1411 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301412 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301413 ? 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 +05301414 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1415 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1416 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301417 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301418 this.child.batch_no = this.item_batch_no[this.child.item_code];
1419 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1420 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301421 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301422 },
1423
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301424 update_paid_amount_status: function (update_paid_amount) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301425 if (this.frm.doc.offline_pos_name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301426 update_paid_amount = update_paid_amount ? false : true;
1427 }
1428
1429 this.refresh(update_paid_amount);
1430 },
1431
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301432 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301433 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301434 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301435 this.set_primary_action();
1436 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301437
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301438 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301439 this.apply_pricing_rule();
1440 this.discount_amount_applied = false;
1441 this._calculate_taxes_and_totals();
1442 this.calculate_discount_amount();
1443 this.show_items_in_item_cart();
1444 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301445 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301446 this.set_totals();
1447 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301448
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301449 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301450 return erpnext.get_currency(this.frm.doc.company);
1451 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301452
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301453 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301454 var me = this;
1455 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301456 var $no_items_message = this.wrapper.find(".no-items-message");
1457 $no_items_message.toggle(this.frm.doc.items.length === 0);
1458
1459 var $totals_area = this.wrapper.find('.totals-area');
1460 $totals_area.toggle(this.frm.doc.items.length > 0);
1461
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301462 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301463 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301464 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301465 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301466 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301467 discount_percentage: d.discount_percentage || 0.0,
1468 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301469 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301470 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301471 amount: format_currency(d.amount, me.frm.doc.currency),
1472 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301473 })).appendTo($items);
1474 });
1475
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301476 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301477 $(this).select();
1478 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301479
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301480 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301481 $(this).select();
1482 });
1483
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301484 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301485 $(this).select();
1486 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301487 },
1488
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301489 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301490 var me = this;
1491 me.frm.doc.total_taxes_and_charges = 0.0
1492
1493 var taxes = this.frm.doc.taxes || [];
1494 $(this.wrapper)
1495 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1496 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301497
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301498 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301499 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1500 $(frappe.render_template("pos_tax_row", {
1501 description: d.description,
1502 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1503 me.frm.doc.currency)
1504 })).appendTo(me.wrapper.find(".tax-table"));
1505 }
1506 });
1507 },
1508
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301509 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301510 var me = this;
1511 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1512 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301513 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1514 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301515 },
1516
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301517 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301518 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301519 this.page.set_primary_action(__("New Cart"), function () {
1520 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301521 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301522 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301523
augustinfotech135a54c2017-11-16 15:28:42 +05301524 if (this.frm.doc.docstatus == 1 || this.pos_profile_data["allow_print_before_pay"]) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301525 this.page.set_secondary_action(__("Print"), function () {
augustinfotech135a54c2017-11-16 15:28:42 +05301526 me.create_invoice();
Faris Ansariab74ca72017-05-30 12:54:42 +05301527 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301528 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301529 })
augustinfotech135a54c2017-11-16 15:28:42 +05301530 }
1531
1532 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301533 this.page.add_menu_item(__("Email"), function () {
1534 me.email_prompt()
1535 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301536 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301537 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301538
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301539 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301540 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301541 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301542 this.save_previous_entry();
1543 this.create_new();
1544 this.refresh();
1545 this.toggle_input_field();
1546 this.render_list_customers();
1547 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301548 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301549
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301550 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301551 var me = this;
1552
mbauskar66e2a512017-06-22 16:18:12 +05301553 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301554 `<a class="btn btn-primary print_doc"
1555 style="margin-right: 5px;">${__('Print')}</a>
1556 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301557
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301558 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301559 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301560 me.print_document(html)
1561 })
1562
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301563 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301564 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301565 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301566 })
1567 },
1568
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301569 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301570 var w = window.open();
1571 w.document.write(html);
1572 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301573 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301574 w.print();
1575 w.close();
1576 }, 1000)
1577 },
1578
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301579 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301580 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301581 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301582 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301583 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301584 this.print_dialog()
1585 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301586 },
1587
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301588 update_serial_no: function() {
1589 var me = this;
1590
1591 //Remove the sold serial no from the cache
1592 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301593 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301594 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301595 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301596 if(serial_no_list) {
1597 $.each(sn, function(i, serial_no) {
1598 if(in_list(Object.keys(serial_no_list), serial_no)) {
1599 delete serial_no_list[serial_no]
1600 }
1601 })
1602 me.serial_no_data[data.item_code] = serial_no_list;
1603 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301604 }
1605 })
1606 },
1607
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301608 change_status: function () {
1609 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301610 this.frm.doc.docstatus = 1;
1611 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301612 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301613 }
1614 },
1615
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301616 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301617 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301618 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301619 $(this.wrapper).find('input').attr("disabled", disabled);
1620 $(this.wrapper).find('select').attr("disabled", disabled);
1621 $(this.wrapper).find('input').attr("disabled", disabled);
1622 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301623 $(this.wrapper).find('button').attr("disabled", disabled);
1624 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301625
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301626 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301627 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301628 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301629
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301630 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301631 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1632 this.set_primary_action();
1633 },
1634
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301635 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301636 var me = this;
rohitwaghchauref688af32017-11-08 11:33:39 +05301637 var invoice_data = {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301638 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301639 if (this.frm.doc.offline_pos_name) {
1640 this.update_invoice();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301641 } else {
rohitwaghchauref688af32017-11-08 11:33:39 +05301642 this.frm.doc.offline_pos_name = $.now();
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301643 this.frm.doc.posting_date = frappe.datetime.get_today();
1644 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001645 this.frm.doc.pos_profile = this.pos_profile_data['name'];
rohitwaghchauref688af32017-11-08 11:33:39 +05301646 invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc;
1647 this.si_docs.push(invoice_data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301648 this.update_localstorage();
1649 this.set_primary_action();
1650 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301651 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301652 },
1653
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301654 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301655 var me = this;
1656 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301657 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301658 for (var key in data) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301659 if (key == me.frm.doc.offline_pos_name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301660 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301661 me.update_localstorage();
1662 }
1663 }
rohitwaghchauref688af32017-11-08 11:33:39 +05301664 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301665 },
1666
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301667 update_localstorage: function () {
1668 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301669 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301670 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301671 frappe.throw(__("LocalStorage is full , did not save"))
1672 }
1673 },
1674
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301675 get_doc_from_localstorage: function () {
1676 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301677 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301678 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301679 return []
1680 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301681 },
1682
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301683 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301684 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301685 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301686 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301687 me.sync_sales_invoice()
rohitwaghchaurea32e57a2018-03-08 16:21:13 +05301688 }, 180000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301689 },
1690
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301691 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301693 this.si_docs = this.get_submitted_invoice() || [];
1694 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301695 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301696 if(this.customer_doc) {
1697 this.freeze = this.customer_doc.display
1698 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301699
rohitwaghchaure88491712017-10-02 12:13:36 +05301700 freeze_screen = this.freeze_screen || false;
1701
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301702 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301703 this.freeze = true;
1704
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301705 frappe.call({
1706 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88491712017-10-02 12:13:36 +05301707 freeze: freeze_screen,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301708 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301709 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301710 email_queue_list: me.email_queue_list,
1711 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301712 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301713 callback: function (r) {
1714 if (r.message) {
rohitwaghchauref688af32017-11-08 11:33:39 +05301715 me.freeze = false;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301716 me.customers = r.message.synced_customers_list;
1717 me.address = r.message.synced_address;
1718 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301719 me.removed_items = r.message.invoice;
rohitwaghchauref688af32017-11-08 11:33:39 +05301720 me.removed_email = r.message.email_queue;
1721 me.removed_customers = r.message.customers;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301722 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301723 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301724 me.remove_customer_from_localstorage();
rohitwaghchauref688af32017-11-08 11:33:39 +05301725 me.prepare_customer_mapper();
1726 me.autocomplete_customers();
1727 me.render_list_customers();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301728 }
1729 }
1730 })
1731 }
1732 },
1733
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301734 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301735 var invoices = [];
1736 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301737 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301738 if (docs) {
1739 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301740 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301741 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301742 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301743 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301744 return data
1745 }
1746 }
1747 });
1748 }
1749
1750 return invoices
1751 },
1752
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301753 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301754 var me = this;
1755 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301756 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301757 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301758 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301759 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301760 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301761 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301762 }
1763 }
1764 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301765 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301766 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301767 this.update_localstorage();
1768 }
1769 },
1770
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301771 remove_email_queue_from_localstorage: function() {
1772 var me = this;
1773 this.email_queue = this.get_email_queue()
1774 if (this.removed_email) {
1775 $.each(this.email_queue_list, function (index, data) {
1776 if (in_list(me.removed_email, index)) {
1777 delete me.email_queue[index]
1778 }
1779 })
1780 this.update_email_queue();
1781 }
1782 },
1783
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301784 remove_customer_from_localstorage: function() {
1785 var me = this;
1786 this.customer_details = this.get_customers_details()
1787 if (this.removed_customers) {
1788 $.each(this.customers_list, function (index, data) {
1789 if (in_list(me.removed_customers, index)) {
1790 delete me.customer_details[index]
1791 }
1792 })
1793 this.update_customer_in_localstorage();
1794 }
1795 },
1796
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301797 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301798 var me = this;
1799 this.customer_validate();
1800 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301801 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301802 },
1803
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301804 item_validate: function () {
1805 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301806 frappe.throw(__("Select items to save the invoice"))
1807 }
1808 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301809
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301810 validate_mode_of_payments: function () {
1811 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301812 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1813 }
1814 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301815
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301816 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301817 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301818 var item_code = ''
1819 var serial_no = '';
1820 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301821 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301822 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301823 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301824
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301825 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301826 this.refresh();
1827 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1828 'item': this.items[0].item_code
1829 })))
1830 }
1831
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301832 if (item_code && serial_no) {
1833 $.each(this.frm.doc.items, function (index, data) {
1834 if (data.item_code == item_code) {
1835 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301836 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1837 'serial_no': serial_no
1838 })))
1839 }
1840 }
1841 })
1842 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301843 },
1844
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301845 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301846 var me = this;
1847 if (args.item_code == item_code && args.serial_no
1848 && field == 'qty' && cint(value) != value) {
1849 args.qty = 0.0;
1850 this.refresh();
1851 frappe.throw(__("Serial no item cannot be a fraction"))
1852 }
1853
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301854 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301855 args.qty = 0.0;
1856 args.serial_no = ''
1857 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301858 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1859 'item': item_code
1860 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301861 }
1862 },
1863
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301864 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301865 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301866 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301867 frappe.prompt([{
1868 'fieldname': 'batch',
1869 'fieldtype': 'Select',
1870 'label': __('Batch No'),
1871 'reqd': 1,
1872 'options': this.batch_no_data[this.items[0].item_code]
1873 }],
1874 function(values){
1875 me.item_batch_no[me.items[0].item_code] = values.batch;
1876 },
1877 __('Select Batch No'))
1878 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301879 },
1880
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301881 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301882 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301883 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301884 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301885 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301886 if (pricing_rule.length) {
1887 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301888 item.margin_type = pricing_rule[0].margin_type;
1889 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1890 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1891 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301892 me.apply_pricing_rule_on_item(item)
1893 } else if (item.pricing_rule) {
1894 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301895 item.margin_rate_or_amount = 0.0;
1896 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301897 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301898 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301899 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001900
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301901 if(item.discount_percentage > 0) {
1902 me.apply_pricing_rule_on_item(item)
1903 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301904 })
1905 },
1906
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301907 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301908 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301909 return $.grep(this.pricing_rules, function (data) {
1910 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301911 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301912 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301913 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301914 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301915 return true
1916 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301917 }
1918 }
1919 })
1920 },
1921
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301922 validate_item_condition: function (data, item) {
1923 var apply_on = frappe.model.scrub(data.apply_on);
1924
1925 return (data.apply_on == 'Item Group')
1926 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1927 },
1928
1929 validate_item_group: function (pr_item_group, cart_item_group) {
1930 //pr_item_group = pricing rule's item group
1931 //cart_item_group = cart item's item group
1932 //this.item_groups has information about item group's lft and rgt
1933 //for example: {'Foods': [12, 19]}
1934
1935 pr_item_group = this.item_groups[pr_item_group]
1936 cart_item_group = this.item_groups[cart_item_group]
1937
1938 return (cart_item_group[0] >= pr_item_group[0] &&
1939 cart_item_group[1] <= pr_item_group[1])
1940 },
1941
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301942 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301943 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301944 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301945 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301946 return true
1947 }
1948 },
1949
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301950 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301951 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301952 'Customer': [data.customer, [this.frm.doc.customer]],
1953 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1954 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301955 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301956 }
1957 },
1958
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301959 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301960 //This method validate duplicate pricing rule
1961 var pricing_rule_name = '';
1962 var priority = 0;
1963 var pricing_rule_list = [];
1964 var priority_list = []
1965
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301966 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301967
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301968 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301969 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301970 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301971 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301972 priority = data.priority
1973 pricing_rule_list.push(data)
1974 }
1975 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301976
Faris Ansariab74ca72017-05-30 12:54:42 +05301977 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301978 $.each(priority_list, function (index, value) {
1979 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301980 count++
1981 }
1982 })
1983
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301984 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301985 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1986 'pricing_rule': pricing_rule_name
1987 })))
1988 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301989
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301990 return pricing_rule_list
1991 }
1992 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301993
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301994 validate_warehouse: function () {
1995 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 +05301996 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301997 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301998 },
1999
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302000 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302001 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302002
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302003 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302004 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302005 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302006 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302007 }
2008
2009 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302010 },
2011
2012 update_customer_in_localstorage: function() {
2013 var me = this;
2014 try {
2015 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2016 } catch (e) {
2017 frappe.throw(__("LocalStorage is full , did not save"))
2018 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302019 }
Saurabh1a4d77a2018-02-08 01:32:48 +05302020})