blob: be0b6f7b7274148214c8e18e1266c06201668471 [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
18 frappe.set_route('point-of-sale');
19 }
20 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053021}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053023frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053024 window.onbeforeunload = function () {
25 return wrapper.pos.beforeunload()
26 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053027}
28
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053029erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053030 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053031 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053032 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053033 this.page = wrapper.page;
34 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053035 this.set_indicator();
36 this.onload();
37 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053038 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053039 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.si_docs = this.get_doc_from_localstorage();
41 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053042
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053043 beforeunload: function (e) {
44 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 e = e || window.event;
46
47 // For IE and Firefox prior to version 4
48 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 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 +053050 return
51 }
52
53 // For Safari
54 return __("You are in offline mode. You will not be able to reload until you have network.");
55 }
56 },
57
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053058 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 var me = this;
60 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053061 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053063 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 },
65
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 var me = this;
68 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053069 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053070 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053071 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 method: "frappe.handler.ping",
73 callback: function (r) {
74 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053076 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 }
78 }
79 })
80 },
81
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053082 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053083 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053084 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053085 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053087 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 },
89
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053090 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053091 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053092 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053093
94 // for mobile
95 this.page.add_menu_item(__("Pay"), function () {
96 me.validate();
97 me.update_paid_amount_status(true);
98 me.create_invoice();
99 me.make_payment();
100 }).addClass('visible-xs');
101
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530102 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530103 me.save_previous_entry();
104 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530105 })
106
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530107 this.page.add_menu_item(__("Sync Master Data"), function () {
108 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530109 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530110 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530111 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530112 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530113 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530114
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530115 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530116 me.sync_sales_invoice()
117 });
118
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530119 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530120 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530121 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530122 },
123
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530124 email_prompt: function() {
125 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530126 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530127 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
128 {fieldtype: "Section Break"},
129 {label:__("Subject"), fieldtype:"Data", reqd: 1,
130 fieldname:"subject",length:524288},
131 {fieldtype: "Section Break"},
132 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
133 fieldname:"content"},
134 {fieldtype: "Section Break"},
135 {fieldtype: "Column Break"}];
136
137 this.email_dialog = new frappe.ui.Dialog({
138 title: "Email",
139 fields: fields,
140 primary_action_label: __("Send"),
141 primary_action: function() {
142 me.send_action();
143 }
144 });
145
146 this.email_dialog.show()
147 },
148
149 send_action: function() {
150 this.email_queue = this.get_email_queue()
151 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
152 this.update_email_queue()
153 this.email_dialog.hide()
154 },
155
156 update_email_queue: function () {
157 try {
158 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
159 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530160 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530161 }
162 },
163
164 get_email_queue: function () {
165 try {
166 return JSON.parse(localStorage.getItem('email_queue')) || {};
167 } catch (e) {
168 return {}
169 }
170 },
171
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530172 get_customers_details: function () {
173 try {
174 return JSON.parse(localStorage.getItem('customer_details')) || {};
175 } catch (e) {
176 return {}
177 }
178 },
179
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530180 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530181 var me = this;
182
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530183 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530184 me.removed_items = [];
185 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530186 if ($(this).is(":checked")) {
187 $.each(me.si_docs, function (index, data) {
188 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530189 me.removed_items.push(key)
190 }
191 })
192 }
193
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530194 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530195 })
196
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530197 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530198 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530199 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530200 me.removed_items.push(me.name);
201 } else {
202 me.removed_items.pop(me.name)
203 }
204
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530205 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530206 })
207 },
208
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530209 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 var me = this;
211
212 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530213 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 this.frm.doc = doc_data[0][this.name];
215 this.set_missing_values();
216 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530217 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530218 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530219 }
220 },
221
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530222 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530223 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530225 this.remove_doc_from_localstorage()
226 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530227 // this.dialog_actions();
228 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530229 },
230
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530231 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530232 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530233 this.si_docs = this.get_submitted_invoice()
234 $.each(this.removed_items, function(index, name){
235 $.each(me.si_docs, function(key, data){
236 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
237 frappe.throw(__("Submitted orders can not be deleted"))
238 }
239 })
240 })
241 },
242
243 toggle_delete_button: function () {
244 var me = this;
245 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530246 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530247 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530248 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530249 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530250 }
251 }
252 },
253
254 get_doctype_status: function (doc) {
255 if (doc.docstatus == 0) {
256 return { status: "Draft", indicator: "red" }
257 } else if (doc.outstanding_amount == 0) {
258 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530259 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530260 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530261 }
262 },
263
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530264 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530265 var me = this;
266 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530267 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530268 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530269 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530270 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530271
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530272 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530273
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530274 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530275 this.frm.doc.write_off_account = doc.write_off_account
276 }
277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530278 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530279 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530280 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530281 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530282
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530283 set_customer_value_in_party_field: function() {
284 if (this.frm.doc.customer) {
285 this.party_field.$input.val(this.frm.doc.customer);
286 }
287 },
288
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530289 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530290 var me = this;
291 this.si_docs = this.get_doc_from_localstorage();
292
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530293 return $.grep(this.si_docs, function (data) {
294 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530295 return key == me.name
296 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530297 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 },
299
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530300 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530301 var me = this;
302 frappe.call({
303 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
304 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530305 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530306 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530307 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530308 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530309 me.set_interval_for_si_sync();
310 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530311 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530312 callback();
313 }
314 }
315 })
316 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530317
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530318 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530319 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530320 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530321 this.meta = r.message.meta;
322 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530323 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530324 this.customers = r.message.customers;
325 this.serial_no_data = r.message.serial_no_data;
326 this.batch_no_data = r.message.batch_no_data;
327 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530328 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530329 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530330 this.price_list_data = r.message.price_list_data;
331 this.bin_data = r.message.bin_data;
332 this.pricing_rules = r.message.pricing_rules;
333 this.print_template = r.message.print_template;
334 this.pos_profile_data = r.message.pos_profile;
335 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530336 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530337 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 +0530338 },
339
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530340 save_previous_entry: function () {
341 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
342 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530343 }
344 },
345
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530346 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530347 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530348 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530349 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530350 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530351 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530352 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 },
354
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530355 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530356 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530357
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530358 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530359 this.actual_qty_dict = {};
360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530361 if (load_doc) {
362 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530363 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530365 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530366 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530367 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530368 })
369
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530370 this.print_template_data = frappe.render_template("print_template", {
371 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530372 title: "POS",
373 base_url: frappe.urllib.get_base_url(),
374 print_css: frappe.boot.print_css,
375 print_settings: this.print_settings,
376 header: this.letter_head.header,
377 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530378 landscape: false,
379 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530380 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 },
382
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530383 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530384 this.make();
385 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530386 this.party_field.$input.attr('disabled', false);
387 if(this.selected_row) {
388 this.selected_row.hide()
389 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530390 },
391
Rohit Waghchaure61165122017-05-02 13:04:08 +0530392 set_default_customer: function() {
393 if (this.default_customer && !this.frm.doc.customer) {
394 this.party_field.$input.val(this.default_customer);
395 this.frm.doc.customer = this.default_customer;
396 this.numeric_keypad.show();
397 this.toggle_list_customer(false)
398 this.toggle_item_cart(true)
399 }
400 },
401
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530402 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 var me = this;
404 this.party = party;
405 this.price_list = (party == "Customer" ?
406 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
407 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
408 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
409 },
410
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530411 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530412 this.make_item_list();
413 this.make_discount_field()
414 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200415
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530416 make_control: function() {
417 this.frm = {}
418 this.frm.doc = this.doc
419 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530420 this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530421 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530422 this.make_search();
423 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530424 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530425 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530426 },
427
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530428 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530429 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530430 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530431 df: {
432 "fieldtype": "Data",
433 "label": "Item",
434 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530435 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530436 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530437 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530438 only_input: true,
439 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530440
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530441 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530442
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530443 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530444
445 clearTimeout(me.last_search_timeout);
446 me.last_search_timeout = setTimeout(() => {
447 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
448 me.items = me.get_items();
449 me.make_item_list();
450 }
451 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530452 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530453
Faris Ansari339d9c92017-03-07 18:14:06 +0530454 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530455 sorted_item_groups = this.get_sorted_item_groups()
456 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530457 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
458 }).join("");
459
460 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
461
462 this.search_item_group.on('click', '.dropdown-menu a', function() {
463 me.selected_item_group = $(this).attr('data-value');
464 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
465
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530466 me.page_len = 20;
467 me.items = me.get_items();
468 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530469 })
470
Faris Ansari45510102017-03-09 14:43:37 +0530471 me.toggle_more_btn();
472
473 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530474 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530475 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530476 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530477 me.toggle_more_btn();
478 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530479
Faris Ansari45510102017-03-09 14:43:37 +0530480 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530481 me.update_customer()
482 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530483 },
484
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530485 get_sorted_item_groups: function() {
486 list = {}
487 $.each(this.item_groups, function(i, data) {
488 list[i] = data[0]
489 })
490
491 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
492 },
493
Faris Ansari45510102017-03-09 14:43:37 +0530494 toggle_more_btn: function() {
495 if(!this.items || this.items.length <= this.page_len) {
496 this.wrapper.find(".btn-more").hide();
497 } else {
498 this.wrapper.find(".btn-more").show();
499 }
500 },
501
502 toggle_totals_area: function(show) {
503
Faris Ansari07c9f352017-03-09 17:03:14 +0530504 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530505 show = this.is_totals_area_collapsed;
506 }
507
508 var totals_area = this.wrapper.find('.totals-area');
509 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
510 .toggle(show);
511
512 if(show) {
513 totals_area.find('.collapse-btn i')
514 .removeClass('octicon-chevron-down')
515 .addClass('octicon-chevron-up');
516 } else {
517 totals_area.find('.collapse-btn i')
518 .removeClass('octicon-chevron-up')
519 .addClass('octicon-chevron-down');
520 }
521
522 this.is_totals_area_collapsed = !show;
523 },
524
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530525 make_list_customers: function () {
526 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530527 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530528 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530529 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530530 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530531 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530532 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530533
Faris Ansari45510102017-03-09 14:43:37 +0530534 me.render_list_customers();
535 me.toggle_totals_area(false);
536
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530537 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530538 $(this).toggleClass("view_customer");
539 if($(this).hasClass("view_customer")) {
540 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530541 me.list_customers.show();
542 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530543 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530544 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530545 } else {
546 if(me.frm.doc.docstatus == 0) {
547 me.party_field.$input.attr('disabled', false);
548 }
549 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530550 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530551 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530552 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530553 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530554 }
555 });
556 this.add_customer_btn.on('click', function() {
557 me.save_previous_entry();
558 me.create_new();
559 me.refresh();
560 me.set_focus();
561 });
Faris Ansari45510102017-03-09 14:43:37 +0530562 this.pos_bill.on('click', '.collapse-btn', function() {
563 me.toggle_totals_area();
564 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530565 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200566
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530567 bind_numeric_keypad: function() {
568 var me = this;
569 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
570 me.numeric_val = '';
571 })
572
573 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
574 me.numeric_id = $(this).attr("id") || me.numeric_id;
575 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530576 if(me.numeric_id) {
577 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
578 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530579
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530580 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530581 me.numeric_val += me.val;
582 me.selected_field.val(flt(me.numeric_val))
583 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530584 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530585 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530586
587 if(me.numeric_id && $(this).hasClass('pos-operation')) {
588 me.numeric_keypad.find('button.pos-operation').removeClass('active');
589 $(this).addClass('active');
590
591 me.selected_row.find('.pos-list-row').removeClass('active');
592 me.selected_field.closest('.pos-list-row').addClass('active');
593 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530594 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200595
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530596 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530597 if(me.numeric_id) {
598 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
599 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
600 me.selected_field.val(me.numeric_val);
601 me.selected_field.trigger("change")
602 } else {
603 //Remove an item from the cart, if focus is at selected item
604 me.remove_selected_item()
605 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530606 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200607
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530608 $(this.numeric_keypad).find('.pos-pay').click(function(){
609 me.validate();
610 me.update_paid_amount_status(true);
611 me.create_invoice();
612 me.make_payment();
613 })
614 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530615
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530616 remove_selected_item: function() {
617 this.remove_item = []
618 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
619 this.remove_item.push(idx)
620 this.remove_zero_qty_item()
621 this.update_paid_amount_status(false)
622 },
623
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530624 render_list_customers: function () {
625 var me = this;
626
627 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530628 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530629 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530630 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530631 return;
632 }
Faris Ansari45510102017-03-09 14:43:37 +0530633
634 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530635 if(this.si_docs.length) {
636 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530637 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530638 html += frappe.render_template("pos_invoice_list", {
639 sr: i + 1,
640 name: key,
641 customer: data[key].customer,
642 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
643 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
644 data: me.get_doctype_status(data[key])
645 });
646 }
647 });
648 }
Faris Ansari45510102017-03-09 14:43:37 +0530649 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530650
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530651 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530652 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530653 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530654 me.list_customers_btn.toggleClass("view_customer");
655 me.pos_bill.show();
656 me.list_customers_btn.show();
657 me.name = $(this).parents().attr('invoice-name')
658 me.edit_record();
659 })
660
661 //actions
662 $(this.wrapper).find('.list-select-all').click(function () {
663 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
664 me.removed_items = [];
665 if ($(this).is(":checked")) {
666 $.each(me.si_docs, function (index, data) {
667 for (key in data) {
668 me.removed_items.push(key)
669 }
670 });
671 }
672
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530673 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530674 });
675
676 $(this.wrapper).find('.list-delete').click(function () {
677 me.name = $(this).parent().parent().attr('invoice-name');
678 if ($(this).is(":checked")) {
679 me.removed_items.push(me.name);
680 } else {
681 me.removed_items.pop(me.name)
682 }
683
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530684 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530685 });
686 },
687
688 bind_delete_event: function() {
689 var me = this;
690
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530691 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530692 frappe.confirm(__("Delete permanently?"), function () {
693 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530694 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530695 me.render_list_customers();
696 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530697 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530698 },
699
700 set_focus: function () {
701 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530702 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530703 this.serach_item.$input.focus();
704 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530705 this.party_field.$input.focus();
706 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530707 },
708
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530709 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530710 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530711
712 if(!this.party_field) {
713 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
714 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530715 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530716 })).insertAfter(this.page.$title_area.hide());
717 }
718
719 this.party_field = frappe.ui.form.make_control({
720 df: {
721 "fieldtype": "Data",
722 "options": this.party,
723 "label": this.party,
724 "fieldname": this.party.toLowerCase(),
725 "placeholder": __("Select or add new customer")
726 },
727 parent: this.page.wrapper.find(".party-area"),
728 only_input: true,
729 });
730
731 this.party_field.make_input();
732 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530733 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530734 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530735
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530736 this.party_field.awesomeplete =
737 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530738 minChars: 0,
739 maxItems: 99,
740 autoFirst: true,
741 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530742 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530743 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530744 return true;
745 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530746
747 input = input.toLowerCase();
748 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530749 result = item ? item.searchtext.includes(input) : '';
750 if(!result) {
751 me.prepare_customer_mapper(input);
752 } else {
753 return result;
754 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530755 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530756 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530757 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530758 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530759 if(d.customer_name) {
760 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
761 }
762
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530763 return $('<li></li>')
764 .data('item.autocomplete', d)
765 .html('<a><p>' + html + '</p></a>')
766 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530767 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530768 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530769
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530770 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530771 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530772
773 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530774 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530775 if(me.customers_mapper.length <= 1) {
776 me.prepare_customer_mapper(e.target.value);
777 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530778 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530779 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530780 .on('awesomplete-select', function (e) {
781 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530782 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530783 if (!customer) return;
784 // create customer link
785 if (customer.action) {
786 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530787 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530788 }
Faris Ansari45510102017-03-09 14:43:37 +0530789 me.toggle_list_customer(false);
790 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530791 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530792 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530793 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530794 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530795 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530796 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530797 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530798 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530799
800 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530801 me.toggle_list_customer(false)
802 me.toggle_item_cart(true)
803 } else {
804 me.toggle_list_customer(true)
805 me.toggle_item_cart(false)
806 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530807 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530808 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530809 var item = me.party_field.awesomeplete
810 .get_item(e.originalEvent.text.value);
811 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530812 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530813 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530814 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530815 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530816 },
817
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530818 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530819 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530820 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530821
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530822 if (key) {
823 key = key.toLowerCase().trim();
824 var re = new RegExp('%', 'g');
825 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
826
827 customer_data = $.grep(this.customers, function(data) {
828 contact = me.contacts[data.name];
829 if(reg.test(data.name.toLowerCase())
830 || reg.test(data.customer_name.toLowerCase())
831 || (contact && reg.test(contact["mobile_no"]))
832 || (contact && reg.test(contact["phone"]))
833 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
834 return data;
835 }
836 })
837 } else {
838 customer_data = this.customers;
839 }
840
841 this.customers_mapper = [];
842
843 customer_data.forEach(function (c, index) {
844 if(index < 30) {
845 contact = me.contacts[c.name];
846 if(contact && !c['phone']) {
847 c["phone"] = contact["phone"];
848 c["email_id"] = contact["email_id"];
849 c["mobile_no"] = contact["mobile_no"];
850 }
851
852 me.customers_mapper.push({
853 label: c.name,
854 value: c.name,
855 customer_name: c.customer_name,
856 customer_group: c.customer_group,
857 territory: c.territory,
858 phone: contact ? contact["phone"] : '',
859 mobile_no: contact ? contact["mobile_no"] : '',
860 email_id: contact ? contact["email_id"] : '',
861 searchtext: ['customer_name', 'customer_group', 'name', 'value',
862 'label', 'email_id', 'phone', 'mobile_no']
863 .map(key => c[key]).join(' ')
864 .toLowerCase()
865 });
866 } else {
867 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530868 }
869 });
870
871 this.customers_mapper.push({
872 label: "<span class='text-primary link-option'>"
873 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
874 + __("Create a new Customer")
875 + "</span>",
876 value: 'is_action',
877 action: me.add_customer
878 });
879 },
880
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530881 autocomplete_customers: function() {
882 this.party_field.awesomeplete.list = this.customers_mapper;
883 },
884
Faris Ansari45510102017-03-09 14:43:37 +0530885 toggle_edit_button: function(flag) {
886 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
887 },
888
889 toggle_list_customer: function(flag) {
890 this.list_customers.toggle(flag);
891 },
892
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530893 toggle_item_cart: function(flag) {
894 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
895 },
896
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530897 add_customer: function() {
898 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530899 this.update_customer(true);
900 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530901 },
902
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530903 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530904 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530905
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530906 this.customer_doc = new frappe.ui.Dialog({
907 'title': 'Customer',
908 fields: [
909 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530910 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530911 "fieldname": "full_name",
912 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530913 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530914 },
915 {
916 "fieldtype": "Section Break"
917 },
918 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530919 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530920 "fieldname": "email_id",
921 "fieldtype": "Data"
922 },
923 {
924 "fieldtype": "Column Break"
925 },
926 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530927 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530928 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530929 "fieldtype": "Data"
930 },
931 {
932 "fieldtype": "Section Break"
933 },
934 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530935 "label": __("Address Name"),
936 "read_only": 1,
937 "fieldname": "name",
938 "fieldtype": "Data"
939 },
940 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530941 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530942 "fieldname": "address_line1",
943 "fieldtype": "Data"
944 },
945 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530946 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530947 "fieldname": "address_line2",
948 "fieldtype": "Data"
949 },
950 {
951 "fieldtype": "Column Break"
952 },
953 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530954 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530955 "fieldname": "city",
956 "fieldtype": "Data"
957 },
958 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530959 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530960 "fieldname": "state",
961 "fieldtype": "Data"
962 },
963 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530964 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530965 "fieldname": "pincode",
966 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530967 },
968 {
969 "label": __("Customer POS Id"),
970 "fieldname": "customer_pos_id",
971 "fieldtype": "Data",
972 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530973 }
974 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530975 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530976 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530977 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530978
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530979 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530980 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530981 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530982 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530983 });
984 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530985
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530986 render_address_data: function() {
987 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530988 this.address_data = this.address[this.frm.doc.customer] || {};
989 if(!this.address_data.email_id || !this.address_data.phone) {
990 this.address_data = this.contacts[this.frm.doc.customer];
991 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530992
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530993 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530994 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
995 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530996 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530997
998 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
999 this.customer_doc.set_value("customer_pos_id", $.now())
1000 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301001 },
1002
1003 get_address_from_localstorage: function() {
1004 this.address_details = this.get_customers_details()
1005 return this.address_details[this.frm.doc.customer]
1006 },
1007
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301008 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301009 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301010 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301011 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301012 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301013 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301014 this.update_address_and_customer_list(new_customer)
1015 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301016 this.update_customer_in_localstorage()
1017 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301018 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301019 },
1020
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301021 update_address_and_customer_list: function(new_customer) {
1022 var me = this;
1023 if(new_customer) {
1024 this.customers_mapper.push({
1025 label: this.frm.doc.customer,
1026 value: this.frm.doc.customer,
1027 customer_group: "",
1028 territory: ""
1029 });
1030 }
1031
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301032 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301033 },
1034
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301035 get_prompt_details: function() {
1036 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301037 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301038 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1039 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301040 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301041 return JSON.stringify(this.prompt_details)
1042 },
1043
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301044 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301045 var me = this;
1046 this.frm.doc.customer = doc.label || doc.name;
1047 this.frm.doc.customer_name = doc.customer_name;
1048 this.frm.doc.customer_group = doc.customer_group;
1049 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301050 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301051 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301052 },
1053
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301054 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301055 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301056 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301057 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301058 return;
1059 }
1060
1061 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301062
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301063 var $wrap = me.wrapper.find(".item-list");
1064 me.wrapper.find(".item-list").empty();
1065
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301066 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301067 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301068 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301069 $(frappe.render_template("pos_item", {
1070 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301071 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301072 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301073 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301074 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301075 color: frappe.get_palette(obj.item_name),
1076 abbr: frappe.get_abbr(obj.item_name)
1077 })).tooltip().appendTo($wrap);
1078 }
1079 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001080
Faris Ansari45510102017-03-09 14:43:37 +05301081 $wrap.append(`
1082 <div class="image-view-item btn-more text-muted text-center">
1083 <div class="image-view-body">
1084 <i class="mega-octicon octicon-package"></i>
1085 <div>Load more items</div>
1086 </div>
1087 </div>
1088 `);
1089
1090 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301091 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301092 $("<p class='text-muted small' style='padding-left: 10px'>"
1093 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301094 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301095
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301096 if (this.items.length == 1
1097 && this.serach_item.$input.val()) {
1098 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301099 this.add_to_cart();
1100 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301101 },
1102
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301103 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301104 // To search item as per the key enter
1105
1106 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301107 this.item_serial_no = {};
1108 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301109
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301110 if (item_code) {
1111 return $.grep(this.item_data, function (item) {
1112 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301113 return true
1114 }
1115 })
1116 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301117
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301118 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301119
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301120 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301121 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301122 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301123 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301124
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301125 if (key) {
1126 return $.grep(this.items_list, function (item) {
1127 if (search_status) {
1128 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301129 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301130 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1131 } else if (me.serial_no_data[item.item_code]
1132 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301133 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301134 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 +05301135 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301136 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301137 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301138 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301139 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301140 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301141 return true
1142 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301143 }
1144 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301145 } else {
1146 return this.items_list;
1147 }
1148 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301149
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301150 apply_category: function() {
1151 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301152 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301153
1154 if(category == 'All Item Groups') {
1155 return this.item_data
1156 } else {
1157 return this.item_data.filter(function(element, index, array){
1158 return element.item_group == category;
1159 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301160 }
1161 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001162
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301163 bind_items_event: function() {
1164 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301165 $(this.wrapper).on('click', '.pos-bill-item', function() {
1166 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1167 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301168 me.numeric_val = "";
1169 me.numeric_id = ""
1170 me.item_code = $(this).attr("data-item-code");
1171 me.render_selected_item()
1172 me.bind_qty_event()
1173 me.update_rate()
1174 $(me.wrapper).find(".selected-item").scrollTop(1000);
1175 })
1176 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301177
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301178 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301179 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301180
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301181 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301182 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301183 var qty = $(this).val();
1184 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301185 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301186 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301187
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301188 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301189 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1190 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301191 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301192 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301193
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301194 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301195 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1196 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301197 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301198 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001199
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301200 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301201 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301202 var discount = $(this).val();
1203 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301204 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301205 })
1206 },
1207
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301208 bind_events: function() {
1209 var me = this;
1210 // if form is local then allow this function
1211 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1212 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301213 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301214 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301215 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301216
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301217 if (me.frm.doc.docstatus == 0) {
1218 me.items = me.get_items($(this).attr("data-item-code"))
1219 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301220 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301221 }
1222 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301223
1224 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301225 },
1226
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301227 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301228 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301229 this.items = this.get_items(item_code);
1230 this.validate_serial_no()
1231 this.set_item_details(item_code, "qty", qty);
1232 },
1233
1234 update_discount: function(item_code, discount) {
1235 var me = this;
1236 this.items = this.get_items(item_code);
1237 this.set_item_details(item_code, "discount_percentage", discount);
1238 },
1239
1240 update_rate: function () {
1241 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301242 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301243 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301244 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301245 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301246 })
1247 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301248
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301249 update_value: function() {
1250 var me = this;
1251 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1252 "rate": ".pos-item-price", "amount": ".pos-amount"}
1253 this.child_doc = this.get_child_item(this.item_code);
1254
1255 if(me.child_doc.length) {
1256 $.each(fields, function(key, field) {
1257 $(me.selected_row).find(field).val(me.child_doc[0][key])
1258 })
1259 } else {
1260 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301261 }
1262 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301263
1264 clear_selected_row: function() {
1265 $(this.wrapper).find('.selected-item').empty();
1266 },
1267
1268 render_selected_item: function() {
1269 this.child_doc = this.get_child_item(this.item_code);
1270 $(this.wrapper).find('.selected-item').empty();
1271 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301272 this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301273 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1274 $(this.wrapper).find('.selected-item').html(this.selected_row)
1275 }
1276
1277 $(this.selected_row).find('.form-control').click(function(){
1278 $(this).select();
1279 })
1280 },
1281
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301282 get_child_item: function(item_code) {
1283 var me = this;
1284 return $.map(me.frm.doc.items, function(doc){
1285 if(doc.item_code == item_code) {
1286 return doc
1287 }
1288 })
1289 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301290
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301291 set_item_details: function (item_code, field, value) {
1292 var me = this;
1293 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301294 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301295 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301296
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301297 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301298 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301299 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301300 if (d.serial_no && field == 'qty') {
1301 me.validate_serial_no_qty(d, item_code, field, value)
1302 }
1303
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301304 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301305 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301306 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301307 me.remove_item.push(d.idx)
1308 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301309
1310 if(field=="discount_percentage" && value == 0) {
1311 d.rate = d.price_list_rate;
1312 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301313 }
1314 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301315
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301316 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301317 this.remove_zero_qty_item();
1318 }
1319
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301320 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301321 },
1322
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301323 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301324 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301325 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301326 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301327 $.each(this.frm.doc["items"] || [], function (i, d) {
1328 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301329 d.idx = idx;
1330 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301331 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301332 }
1333 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301334
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301335 this.frm.doc["items"] = this.items;
1336 },
1337
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301338 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301339 var me = this;
1340
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301341 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301342 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
Faris Ansariab74ca72017-05-30 12:54:42 +05301343 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301344
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301345 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301346 total = me.frm.doc.net_total
1347 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301348
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301349 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 +05301350 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1351 me.refresh();
1352 });
1353
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301354 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301355 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1356 me.frm.doc.additional_discount_percentage = 0.0;
1357 me.wrapper.find('input.discount-percentage').val(0);
1358 me.refresh();
1359 });
1360 },
1361
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301362 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301363 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301364 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301365 frappe.throw(__("Please select customer"))
1366 }
1367 },
1368
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301369 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301370 var me = this;
1371 var caught = false;
1372 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1373
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301374 this.customer_validate();
1375 this.mandatory_batch_no();
1376 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301377 this.validate_warehouse();
1378
1379 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301380 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301381 if (d.item_code == me.items[0].item_code) {
1382 caught = true;
1383 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301384 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301385 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301386 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1387 d.warehouse = me.item_serial_no[d.item_code][1]
1388 }
1389
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301390 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301391 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301392 }
1393 }
1394 });
1395 }
1396
1397 // if item not found then add new item
1398 if (!caught)
1399 this.add_new_item_to_grid();
1400
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301401 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301402 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301403 },
1404
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301405 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301406 var me = this;
1407 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1408 this.child.item_code = this.items[0].item_code;
1409 this.child.item_name = this.items[0].item_name;
1410 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301411 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301412 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301413 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301414 this.child.qty = 1;
1415 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301416 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1417 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301418 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301419 ? 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 +05301420 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1421 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1422 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301423 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301424 this.child.batch_no = this.item_batch_no[this.child.item_code];
1425 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1426 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301427 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301428 },
1429
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301430 update_paid_amount_status: function (update_paid_amount) {
1431 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301432 update_paid_amount = update_paid_amount ? false : true;
1433 }
1434
1435 this.refresh(update_paid_amount);
1436 },
1437
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301438 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301439 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301440 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301441 this.set_primary_action();
1442 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301443
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301444 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301445 this.apply_pricing_rule();
1446 this.discount_amount_applied = false;
1447 this._calculate_taxes_and_totals();
1448 this.calculate_discount_amount();
1449 this.show_items_in_item_cart();
1450 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301451 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301452 this.set_totals();
1453 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301454
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301455 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301456 return erpnext.get_currency(this.frm.doc.company);
1457 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301458
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301459 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301460 var me = this;
1461 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301462 var $no_items_message = this.wrapper.find(".no-items-message");
1463 $no_items_message.toggle(this.frm.doc.items.length === 0);
1464
1465 var $totals_area = this.wrapper.find('.totals-area');
1466 $totals_area.toggle(this.frm.doc.items.length > 0);
1467
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301468 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301469 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301470 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301471 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301472 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301473 discount_percentage: d.discount_percentage || 0.0,
1474 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301476 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301477 amount: format_currency(d.amount, me.frm.doc.currency),
1478 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 })).appendTo($items);
1480 });
1481
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301482 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301483 $(this).select();
1484 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301485
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301486 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301487 $(this).select();
1488 });
1489
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301490 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301491 $(this).select();
1492 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301493 },
1494
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301495 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301496 var me = this;
1497 me.frm.doc.total_taxes_and_charges = 0.0
1498
1499 var taxes = this.frm.doc.taxes || [];
1500 $(this.wrapper)
1501 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1502 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301503
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301504 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301505 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1506 $(frappe.render_template("pos_tax_row", {
1507 description: d.description,
1508 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1509 me.frm.doc.currency)
1510 })).appendTo(me.wrapper.find(".tax-table"));
1511 }
1512 });
1513 },
1514
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301515 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301516 var me = this;
1517 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1518 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1519 },
1520
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301521 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301522 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301523 this.page.set_primary_action(__("New Cart"), function () {
1524 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301525 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301526 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301527
1528 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301529 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301530 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301531 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301532 })
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;
1637 var invoice_data = {}
1638 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301639 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301640 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301641 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301642 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301643 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301644 this.frm.doc.posting_date = frappe.datetime.get_today();
1645 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001646 this.frm.doc.pos_profile = this.pos_profile_data['name'];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301647 invoice_data[this.name] = this.frm.doc
1648 this.si_docs.push(invoice_data)
1649 this.update_localstorage();
1650 this.set_primary_action();
1651 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301652 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301653 },
1654
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301655 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301656 var me = this;
1657 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301658 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301659 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301660 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301661 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301662 me.update_localstorage();
1663 }
1664 }
1665 })
1666 },
1667
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301668 update_localstorage: function () {
1669 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301670 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301671 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301672 frappe.throw(__("LocalStorage is full , did not save"))
1673 }
1674 },
1675
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301676 get_doc_from_localstorage: function () {
1677 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301678 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301679 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301680 return []
1681 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301682 },
1683
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301684 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301685 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301686 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301687 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301688 }, 60000)
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
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301700 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301701 frappe.call({
1702 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1703 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301704 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301705 email_queue_list: me.email_queue_list,
1706 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301707 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301708 callback: function (r) {
1709 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301710 me.customers = r.message.synced_customers_list;
1711 me.address = r.message.synced_address;
1712 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301713 me.removed_items = r.message.invoice;
1714 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301715 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301716 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301717 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301718 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301719 me.prepare_customer_mapper()
1720 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301721 }
1722 }
1723 })
1724 }
1725 },
1726
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301727 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301728 var invoices = [];
1729 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301730 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301731 if (docs) {
1732 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301733 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301734 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301735 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301736 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301737 return data
1738 }
1739 }
1740 });
1741 }
1742
1743 return invoices
1744 },
1745
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301746 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301747 var me = this;
1748 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301749 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301750 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301751 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301752 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301753 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301754 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301755 }
1756 }
1757 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301758 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301759 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301760 this.update_localstorage();
1761 }
1762 },
1763
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301764 remove_email_queue_from_localstorage: function() {
1765 var me = this;
1766 this.email_queue = this.get_email_queue()
1767 if (this.removed_email) {
1768 $.each(this.email_queue_list, function (index, data) {
1769 if (in_list(me.removed_email, index)) {
1770 delete me.email_queue[index]
1771 }
1772 })
1773 this.update_email_queue();
1774 }
1775 },
1776
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301777 remove_customer_from_localstorage: function() {
1778 var me = this;
1779 this.customer_details = this.get_customers_details()
1780 if (this.removed_customers) {
1781 $.each(this.customers_list, function (index, data) {
1782 if (in_list(me.removed_customers, index)) {
1783 delete me.customer_details[index]
1784 }
1785 })
1786 this.update_customer_in_localstorage();
1787 }
1788 },
1789
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301790 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301791 var me = this;
1792 this.customer_validate();
1793 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301794 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301795 },
1796
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301797 item_validate: function () {
1798 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301799 frappe.throw(__("Select items to save the invoice"))
1800 }
1801 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301802
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301803 validate_mode_of_payments: function () {
1804 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301805 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1806 }
1807 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301808
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301809 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301810 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301811 var item_code = ''
1812 var serial_no = '';
1813 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301814 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301815 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301816 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301817
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301818 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301819 this.refresh();
1820 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1821 'item': this.items[0].item_code
1822 })))
1823 }
1824
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301825 if (item_code && serial_no) {
1826 $.each(this.frm.doc.items, function (index, data) {
1827 if (data.item_code == item_code) {
1828 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301829 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1830 'serial_no': serial_no
1831 })))
1832 }
1833 }
1834 })
1835 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301836 },
1837
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301838 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301839 var me = this;
1840 if (args.item_code == item_code && args.serial_no
1841 && field == 'qty' && cint(value) != value) {
1842 args.qty = 0.0;
1843 this.refresh();
1844 frappe.throw(__("Serial no item cannot be a fraction"))
1845 }
1846
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301847 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301848 args.qty = 0.0;
1849 args.serial_no = ''
1850 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301851 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1852 'item': item_code
1853 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301854 }
1855 },
1856
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301857 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301858 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301859 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301860 frappe.prompt([{
1861 'fieldname': 'batch',
1862 'fieldtype': 'Select',
1863 'label': __('Batch No'),
1864 'reqd': 1,
1865 'options': this.batch_no_data[this.items[0].item_code]
1866 }],
1867 function(values){
1868 me.item_batch_no[me.items[0].item_code] = values.batch;
1869 },
1870 __('Select Batch No'))
1871 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301872 },
1873
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301874 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301875 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301876 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301877 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301878 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301879 if (pricing_rule.length) {
1880 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301881 item.margin_type = pricing_rule[0].margin_type;
1882 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1883 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1884 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301885 me.apply_pricing_rule_on_item(item)
1886 } else if (item.pricing_rule) {
1887 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301888 item.margin_rate_or_amount = 0.0;
1889 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301890 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301891 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301892 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001893
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301894 if(item.discount_percentage > 0) {
1895 me.apply_pricing_rule_on_item(item)
1896 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301897 })
1898 },
1899
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301900 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301901 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301902 return $.grep(this.pricing_rules, function (data) {
1903 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301904 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301905 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301906 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301907 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301908 return true
1909 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301910 }
1911 }
1912 })
1913 },
1914
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301915 validate_item_condition: function (data, item) {
1916 var apply_on = frappe.model.scrub(data.apply_on);
1917
1918 return (data.apply_on == 'Item Group')
1919 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1920 },
1921
1922 validate_item_group: function (pr_item_group, cart_item_group) {
1923 //pr_item_group = pricing rule's item group
1924 //cart_item_group = cart item's item group
1925 //this.item_groups has information about item group's lft and rgt
1926 //for example: {'Foods': [12, 19]}
1927
1928 pr_item_group = this.item_groups[pr_item_group]
1929 cart_item_group = this.item_groups[cart_item_group]
1930
1931 return (cart_item_group[0] >= pr_item_group[0] &&
1932 cart_item_group[1] <= pr_item_group[1])
1933 },
1934
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301935 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301936 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301937 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301938 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301939 return true
1940 }
1941 },
1942
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301943 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301944 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301945 'Customer': [data.customer, [this.frm.doc.customer]],
1946 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1947 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301948 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301949 }
1950 },
1951
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301952 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301953 //This method validate duplicate pricing rule
1954 var pricing_rule_name = '';
1955 var priority = 0;
1956 var pricing_rule_list = [];
1957 var priority_list = []
1958
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301959 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301960
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301961 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301962 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301963 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301964 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301965 priority = data.priority
1966 pricing_rule_list.push(data)
1967 }
1968 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301969
Faris Ansariab74ca72017-05-30 12:54:42 +05301970 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301971 $.each(priority_list, function (index, value) {
1972 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301973 count++
1974 }
1975 })
1976
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301977 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301978 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1979 'pricing_rule': pricing_rule_name
1980 })))
1981 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301982
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301983 return pricing_rule_list
1984 }
1985 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301986
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301987 validate_warehouse: function () {
1988 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 +05301989 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301990 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301991 },
1992
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301993 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301994 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301995
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301996 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301997 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301998 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301999 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302000 }
2001
2002 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302003 },
2004
2005 update_customer_in_localstorage: function() {
2006 var me = this;
2007 try {
2008 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2009 } catch (e) {
2010 frappe.throw(__("LocalStorage is full , did not save"))
2011 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302012 }
Faris Ansari185762a2017-04-13 12:36:08 +05302013})