blob: 0416b193d7af34784ff185ba36f2b65063bc1766 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
11 wrapper.pos = new erpnext.pos.PointOfSale(wrapper)
Rushabh Mehta72e17192014-08-08 15:30:49 +053012}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053013
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053014frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053015 window.onbeforeunload = function () {
16 return wrapper.pos.beforeunload()
17 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053018}
19
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053020erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053021 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053022 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053023 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053024 this.page = wrapper.page;
25 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053026 this.set_indicator();
27 this.onload();
28 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053029 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053030 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053031 this.si_docs = this.get_doc_from_localstorage();
32 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053033
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053034 beforeunload: function (e) {
35 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053036 e = e || window.event;
37
38 // For IE and Firefox prior to version 4
39 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053040 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053041 return
42 }
43
44 // For Safari
45 return __("You are in offline mode. You will not be able to reload until you have network.");
46 }
47 },
48
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053050 var me = this;
51 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053052 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053053 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053054 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053055 },
56
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053057 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053058 var me = this;
59 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053060 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053061 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053063 method: "frappe.handler.ping",
64 callback: function (r) {
65 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053067 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053068 }
69 }
70 })
71 },
72
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053073 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053074 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053075 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053076 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053077 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053078 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 },
80
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053081 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053082 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053083 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053084
85 // for mobile
86 this.page.add_menu_item(__("Pay"), function () {
87 me.validate();
88 me.update_paid_amount_status(true);
89 me.create_invoice();
90 me.make_payment();
91 }).addClass('visible-xs');
92
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053093 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053094 me.save_previous_entry();
95 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053096 })
97
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053098 this.page.add_menu_item(__("Sync Master Data"), function () {
99 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530100 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530101 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530102 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530103 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530105
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530106 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530107 me.sync_sales_invoice()
108 });
109
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530110 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530111 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530112 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530113 },
114
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530115 email_prompt: function() {
116 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530117 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530118 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
119 {fieldtype: "Section Break"},
120 {label:__("Subject"), fieldtype:"Data", reqd: 1,
121 fieldname:"subject",length:524288},
122 {fieldtype: "Section Break"},
123 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
124 fieldname:"content"},
125 {fieldtype: "Section Break"},
126 {fieldtype: "Column Break"}];
127
128 this.email_dialog = new frappe.ui.Dialog({
129 title: "Email",
130 fields: fields,
131 primary_action_label: __("Send"),
132 primary_action: function() {
133 me.send_action();
134 }
135 });
136
137 this.email_dialog.show()
138 },
139
140 send_action: function() {
141 this.email_queue = this.get_email_queue()
142 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
143 this.update_email_queue()
144 this.email_dialog.hide()
145 },
146
147 update_email_queue: function () {
148 try {
149 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
150 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530151 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530152 }
153 },
154
155 get_email_queue: function () {
156 try {
157 return JSON.parse(localStorage.getItem('email_queue')) || {};
158 } catch (e) {
159 return {}
160 }
161 },
162
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530163 get_customers_details: function () {
164 try {
165 return JSON.parse(localStorage.getItem('customer_details')) || {};
166 } catch (e) {
167 return {}
168 }
169 },
170
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530171 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530172 var me = this;
173
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530174 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530175 me.removed_items = [];
176 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530177 if ($(this).is(":checked")) {
178 $.each(me.si_docs, function (index, data) {
179 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530180 me.removed_items.push(key)
181 }
182 })
183 }
184
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530185 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530186 })
187
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530188 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530189 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530190 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530191 me.removed_items.push(me.name);
192 } else {
193 me.removed_items.pop(me.name)
194 }
195
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530196 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 })
198 },
199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 var me = this;
202
203 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530205 this.frm.doc = doc_data[0][this.name];
206 this.set_missing_values();
207 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530208 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530209 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 }
211 },
212
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530213 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530215 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530216 this.remove_doc_from_localstorage()
217 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530218 // this.dialog_actions();
219 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530220 },
221
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530222 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530223 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 this.si_docs = this.get_submitted_invoice()
225 $.each(this.removed_items, function(index, name){
226 $.each(me.si_docs, function(key, data){
227 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
228 frappe.throw(__("Submitted orders can not be deleted"))
229 }
230 })
231 })
232 },
233
234 toggle_delete_button: function () {
235 var me = this;
236 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530237 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530238 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530239 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530240 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530241 }
242 }
243 },
244
245 get_doctype_status: function (doc) {
246 if (doc.docstatus == 0) {
247 return { status: "Draft", indicator: "red" }
248 } else if (doc.outstanding_amount == 0) {
249 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530250 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530251 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530252 }
253 },
254
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530255 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530256 var me = this;
257 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530258 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530259 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530260 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530261 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530262
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530263 this.set_customer_value_in_party_field();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530264
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530265 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530266 this.frm.doc.write_off_account = doc.write_off_account
267 }
268
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530269 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530270 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530271 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530272 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530274 set_customer_value_in_party_field: function() {
275 if (this.frm.doc.customer) {
276 this.party_field.$input.val(this.frm.doc.customer);
277 }
278 },
279
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530280 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530281 var me = this;
282 this.si_docs = this.get_doc_from_localstorage();
283
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530284 return $.grep(this.si_docs, function (data) {
285 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 return key == me.name
287 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530288 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530289 },
290
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530291 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530292 var me = this;
293 frappe.call({
294 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
295 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530296 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530297 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530298 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530299 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530300 me.set_interval_for_si_sync();
301 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530302 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 callback();
304 }
305 }
306 })
307 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530308
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530309 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530310 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530311 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530312 this.meta = r.message.meta;
313 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530314 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530315 this.customers = r.message.customers;
316 this.serial_no_data = r.message.serial_no_data;
317 this.batch_no_data = r.message.batch_no_data;
318 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530319 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530320 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530321 this.price_list_data = r.message.price_list_data;
322 this.bin_data = r.message.bin_data;
323 this.pricing_rules = r.message.pricing_rules;
324 this.print_template = r.message.print_template;
325 this.pos_profile_data = r.message.pos_profile;
326 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530327 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530328 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 +0530329 },
330
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530331 save_previous_entry: function () {
332 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
333 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530334 }
335 },
336
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530337 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530338 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530339 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530340 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530341 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530342 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530343 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 },
345
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530346 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530347 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530348
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530349 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530350 this.actual_qty_dict = {};
351
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530352 if (load_doc) {
353 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530354 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530355
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530356 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530357 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530358 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530359 })
360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530361 this.print_template_data = frappe.render_template("print_template", {
362 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530363 title: "POS",
364 base_url: frappe.urllib.get_base_url(),
365 print_css: frappe.boot.print_css,
366 print_settings: this.print_settings,
367 header: this.letter_head.header,
368 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530369 landscape: false,
370 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530371 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530372 },
373
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530374 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530375 this.make();
376 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530377 this.party_field.$input.attr('disabled', false);
378 if(this.selected_row) {
379 this.selected_row.hide()
380 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530381 },
382
Rohit Waghchaure61165122017-05-02 13:04:08 +0530383 set_default_customer: function() {
384 if (this.default_customer && !this.frm.doc.customer) {
385 this.party_field.$input.val(this.default_customer);
386 this.frm.doc.customer = this.default_customer;
387 this.numeric_keypad.show();
388 this.toggle_list_customer(false)
389 this.toggle_item_cart(true)
390 }
391 },
392
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530393 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530394 var me = this;
395 this.party = party;
396 this.price_list = (party == "Customer" ?
397 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
398 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
399 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
400 },
401
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530402 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530403 this.make_item_list();
404 this.make_discount_field()
405 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200406
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530407 make_control: function() {
408 this.frm = {}
409 this.frm.doc = this.doc
410 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530411 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 +0530412 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530413 this.make_search();
414 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530415 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530416 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 },
418
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530419 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530420 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530421 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530422 df: {
423 "fieldtype": "Data",
424 "label": "Item",
425 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530426 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530427 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530428 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530429 only_input: true,
430 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530431
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530432 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530433
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530434 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530435
436 clearTimeout(me.last_search_timeout);
437 me.last_search_timeout = setTimeout(() => {
438 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
439 me.items = me.get_items();
440 me.make_item_list();
441 }
442 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530443 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530444
Faris Ansari339d9c92017-03-07 18:14:06 +0530445 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530446 sorted_item_groups = this.get_sorted_item_groups()
447 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530448 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
449 }).join("");
450
451 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
452
453 this.search_item_group.on('click', '.dropdown-menu a', function() {
454 me.selected_item_group = $(this).attr('data-value');
455 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
456
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530457 me.page_len = 20;
458 me.items = me.get_items();
459 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530460 })
461
Faris Ansari45510102017-03-09 14:43:37 +0530462 me.toggle_more_btn();
463
464 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530465 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530466 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530467 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530468 me.toggle_more_btn();
469 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530470
Faris Ansari45510102017-03-09 14:43:37 +0530471 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530472 me.update_customer()
473 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530474 },
475
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530476 get_sorted_item_groups: function() {
477 list = {}
478 $.each(this.item_groups, function(i, data) {
479 list[i] = data[0]
480 })
481
482 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
483 },
484
Faris Ansari45510102017-03-09 14:43:37 +0530485 toggle_more_btn: function() {
486 if(!this.items || this.items.length <= this.page_len) {
487 this.wrapper.find(".btn-more").hide();
488 } else {
489 this.wrapper.find(".btn-more").show();
490 }
491 },
492
493 toggle_totals_area: function(show) {
494
Faris Ansari07c9f352017-03-09 17:03:14 +0530495 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530496 show = this.is_totals_area_collapsed;
497 }
498
499 var totals_area = this.wrapper.find('.totals-area');
500 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
501 .toggle(show);
502
503 if(show) {
504 totals_area.find('.collapse-btn i')
505 .removeClass('octicon-chevron-down')
506 .addClass('octicon-chevron-up');
507 } else {
508 totals_area.find('.collapse-btn i')
509 .removeClass('octicon-chevron-up')
510 .addClass('octicon-chevron-down');
511 }
512
513 this.is_totals_area_collapsed = !show;
514 },
515
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530516 make_list_customers: function () {
517 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530518 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530519 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530520 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530521 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530522 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530523 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530524
Faris Ansari45510102017-03-09 14:43:37 +0530525 me.render_list_customers();
526 me.toggle_totals_area(false);
527
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530528 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530529 $(this).toggleClass("view_customer");
530 if($(this).hasClass("view_customer")) {
531 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530532 me.list_customers.show();
533 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530534 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530535 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530536 } else {
537 if(me.frm.doc.docstatus == 0) {
538 me.party_field.$input.attr('disabled', false);
539 }
540 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530541 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530542 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530543 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530544 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530545 }
546 });
547 this.add_customer_btn.on('click', function() {
548 me.save_previous_entry();
549 me.create_new();
550 me.refresh();
551 me.set_focus();
552 });
Faris Ansari45510102017-03-09 14:43:37 +0530553 this.pos_bill.on('click', '.collapse-btn', function() {
554 me.toggle_totals_area();
555 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530556 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200557
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530558 bind_numeric_keypad: function() {
559 var me = this;
560 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
561 me.numeric_val = '';
562 })
563
564 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
565 me.numeric_id = $(this).attr("id") || me.numeric_id;
566 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530567 if(me.numeric_id) {
568 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
569 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530570
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530571 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530572 me.numeric_val += me.val;
573 me.selected_field.val(flt(me.numeric_val))
574 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530575 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530576 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530577
578 if(me.numeric_id && $(this).hasClass('pos-operation')) {
579 me.numeric_keypad.find('button.pos-operation').removeClass('active');
580 $(this).addClass('active');
581
582 me.selected_row.find('.pos-list-row').removeClass('active');
583 me.selected_field.closest('.pos-list-row').addClass('active');
584 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530585 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200586
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530587 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530588 if(me.numeric_id) {
589 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
590 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
591 me.selected_field.val(me.numeric_val);
592 me.selected_field.trigger("change")
593 } else {
594 //Remove an item from the cart, if focus is at selected item
595 me.remove_selected_item()
596 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530597 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200598
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530599 $(this.numeric_keypad).find('.pos-pay').click(function(){
600 me.validate();
601 me.update_paid_amount_status(true);
602 me.create_invoice();
603 me.make_payment();
604 })
605 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530606
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530607 remove_selected_item: function() {
608 this.remove_item = []
609 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
610 this.remove_item.push(idx)
611 this.remove_zero_qty_item()
612 this.update_paid_amount_status(false)
613 },
614
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530615 render_list_customers: function () {
616 var me = this;
617
618 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530619 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530620 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530621 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530622 return;
623 }
Faris Ansari45510102017-03-09 14:43:37 +0530624
625 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530626 if(this.si_docs.length) {
627 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530628 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530629 html += frappe.render_template("pos_invoice_list", {
630 sr: i + 1,
631 name: key,
632 customer: data[key].customer,
633 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
634 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
635 data: me.get_doctype_status(data[key])
636 });
637 }
638 });
639 }
Faris Ansari45510102017-03-09 14:43:37 +0530640 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530641
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530642 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530643 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530644 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530645 me.list_customers_btn.toggleClass("view_customer");
646 me.pos_bill.show();
647 me.list_customers_btn.show();
648 me.name = $(this).parents().attr('invoice-name')
649 me.edit_record();
650 })
651
652 //actions
653 $(this.wrapper).find('.list-select-all').click(function () {
654 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
655 me.removed_items = [];
656 if ($(this).is(":checked")) {
657 $.each(me.si_docs, function (index, data) {
658 for (key in data) {
659 me.removed_items.push(key)
660 }
661 });
662 }
663
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530664 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530665 });
666
667 $(this.wrapper).find('.list-delete').click(function () {
668 me.name = $(this).parent().parent().attr('invoice-name');
669 if ($(this).is(":checked")) {
670 me.removed_items.push(me.name);
671 } else {
672 me.removed_items.pop(me.name)
673 }
674
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530675 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530676 });
677 },
678
679 bind_delete_event: function() {
680 var me = this;
681
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530682 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530683 frappe.confirm(__("Delete permanently?"), function () {
684 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530685 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530686 me.render_list_customers();
687 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530688 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530689 },
690
691 set_focus: function () {
692 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530693 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530694 this.serach_item.$input.focus();
695 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530696 this.party_field.$input.focus();
697 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530698 },
699
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530700 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530701 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530702
703 if(!this.party_field) {
704 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
705 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530706 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530707 })).insertAfter(this.page.$title_area.hide());
708 }
709
710 this.party_field = frappe.ui.form.make_control({
711 df: {
712 "fieldtype": "Data",
713 "options": this.party,
714 "label": this.party,
715 "fieldname": this.party.toLowerCase(),
716 "placeholder": __("Select or add new customer")
717 },
718 parent: this.page.wrapper.find(".party-area"),
719 only_input: true,
720 });
721
722 this.party_field.make_input();
723 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530724 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530725 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530726
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530727 this.party_field.awesomeplete =
728 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530729 minChars: 0,
730 maxItems: 99,
731 autoFirst: true,
732 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530733 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530734 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530735 return true;
736 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530737
738 input = input.toLowerCase();
739 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530740 result = item ? item.searchtext.includes(input) : '';
741 if(!result) {
742 me.prepare_customer_mapper(input);
743 } else {
744 return result;
745 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530746 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530747 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530748 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530749 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530750 if(d.customer_name) {
751 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
752 }
753
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530754 return $('<li></li>')
755 .data('item.autocomplete', d)
756 .html('<a><p>' + html + '</p></a>')
757 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530758 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530759 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530760
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530761 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530762 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530763
764 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530765 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530766 if(me.customers_mapper.length <= 1) {
767 me.prepare_customer_mapper(e.target.value);
768 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530769 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530770 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530771 .on('awesomplete-select', function (e) {
772 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530773 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530774 if (!customer) return;
775 // create customer link
776 if (customer.action) {
777 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530778 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530779 }
Faris Ansari45510102017-03-09 14:43:37 +0530780 me.toggle_list_customer(false);
781 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530782 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530783 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530784 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530785 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530786 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530787 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530788 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530789 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530790
791 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530792 me.toggle_list_customer(false)
793 me.toggle_item_cart(true)
794 } else {
795 me.toggle_list_customer(true)
796 me.toggle_item_cart(false)
797 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530798 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530799 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530800 var item = me.party_field.awesomeplete
801 .get_item(e.originalEvent.text.value);
802 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530803 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530804 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530805 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530806 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530807 },
808
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530809 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530810 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530811 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530812
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530813 if (key) {
814 key = key.toLowerCase().trim();
815 var re = new RegExp('%', 'g');
816 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
817
818 customer_data = $.grep(this.customers, function(data) {
819 contact = me.contacts[data.name];
820 if(reg.test(data.name.toLowerCase())
821 || reg.test(data.customer_name.toLowerCase())
822 || (contact && reg.test(contact["mobile_no"]))
823 || (contact && reg.test(contact["phone"]))
824 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
825 return data;
826 }
827 })
828 } else {
829 customer_data = this.customers;
830 }
831
832 this.customers_mapper = [];
833
834 customer_data.forEach(function (c, index) {
835 if(index < 30) {
836 contact = me.contacts[c.name];
837 if(contact && !c['phone']) {
838 c["phone"] = contact["phone"];
839 c["email_id"] = contact["email_id"];
840 c["mobile_no"] = contact["mobile_no"];
841 }
842
843 me.customers_mapper.push({
844 label: c.name,
845 value: c.name,
846 customer_name: c.customer_name,
847 customer_group: c.customer_group,
848 territory: c.territory,
849 phone: contact ? contact["phone"] : '',
850 mobile_no: contact ? contact["mobile_no"] : '',
851 email_id: contact ? contact["email_id"] : '',
852 searchtext: ['customer_name', 'customer_group', 'name', 'value',
853 'label', 'email_id', 'phone', 'mobile_no']
854 .map(key => c[key]).join(' ')
855 .toLowerCase()
856 });
857 } else {
858 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530859 }
860 });
861
862 this.customers_mapper.push({
863 label: "<span class='text-primary link-option'>"
864 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
865 + __("Create a new Customer")
866 + "</span>",
867 value: 'is_action',
868 action: me.add_customer
869 });
870 },
871
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530872 autocomplete_customers: function() {
873 this.party_field.awesomeplete.list = this.customers_mapper;
874 },
875
Faris Ansari45510102017-03-09 14:43:37 +0530876 toggle_edit_button: function(flag) {
877 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
878 },
879
880 toggle_list_customer: function(flag) {
881 this.list_customers.toggle(flag);
882 },
883
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530884 toggle_item_cart: function(flag) {
885 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
886 },
887
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530888 add_customer: function() {
889 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530890 this.update_customer(true);
891 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530892 },
893
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530894 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530895 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530896
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530897 this.customer_doc = new frappe.ui.Dialog({
898 'title': 'Customer',
899 fields: [
900 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530901 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530902 "fieldname": "full_name",
903 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530904 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530905 },
906 {
907 "fieldtype": "Section Break"
908 },
909 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530910 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530911 "fieldname": "email_id",
912 "fieldtype": "Data"
913 },
914 {
915 "fieldtype": "Column Break"
916 },
917 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530918 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530919 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530920 "fieldtype": "Data"
921 },
922 {
923 "fieldtype": "Section Break"
924 },
925 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530926 "label": __("Address Name"),
927 "read_only": 1,
928 "fieldname": "name",
929 "fieldtype": "Data"
930 },
931 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530932 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530933 "fieldname": "address_line1",
934 "fieldtype": "Data"
935 },
936 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530937 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530938 "fieldname": "address_line2",
939 "fieldtype": "Data"
940 },
941 {
942 "fieldtype": "Column Break"
943 },
944 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530945 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530946 "fieldname": "city",
947 "fieldtype": "Data"
948 },
949 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530950 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530951 "fieldname": "state",
952 "fieldtype": "Data"
953 },
954 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530955 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530956 "fieldname": "pincode",
957 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530958 },
959 {
960 "label": __("Customer POS Id"),
961 "fieldname": "customer_pos_id",
962 "fieldtype": "Data",
963 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530964 }
965 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530966 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530967 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530968 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530969
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530970 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530971 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530972 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530973 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530974 });
975 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530976
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530977 render_address_data: function() {
978 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530979 this.address_data = this.address[this.frm.doc.customer] || {};
980 if(!this.address_data.email_id || !this.address_data.phone) {
981 this.address_data = this.contacts[this.frm.doc.customer];
982 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530983
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530984 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530985 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
986 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530987 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530988
989 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
990 this.customer_doc.set_value("customer_pos_id", $.now())
991 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530992 },
993
994 get_address_from_localstorage: function() {
995 this.address_details = this.get_customers_details()
996 return this.address_details[this.frm.doc.customer]
997 },
998
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530999 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301000 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301001 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301002 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301003 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301004 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301005 this.update_address_and_customer_list(new_customer)
1006 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301007 this.update_customer_in_localstorage()
1008 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301009 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301010 },
1011
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301012 update_address_and_customer_list: function(new_customer) {
1013 var me = this;
1014 if(new_customer) {
1015 this.customers_mapper.push({
1016 label: this.frm.doc.customer,
1017 value: this.frm.doc.customer,
1018 customer_group: "",
1019 territory: ""
1020 });
1021 }
1022
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301023 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301024 },
1025
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301026 get_prompt_details: function() {
1027 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301028 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301029 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1030 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301031 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301032 return JSON.stringify(this.prompt_details)
1033 },
1034
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301035 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301036 var me = this;
1037 this.frm.doc.customer = doc.label || doc.name;
1038 this.frm.doc.customer_name = doc.customer_name;
1039 this.frm.doc.customer_group = doc.customer_group;
1040 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301041 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301042 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301043 },
1044
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301045 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301046 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301047 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301048 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301049 return;
1050 }
1051
1052 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301053
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301054 var $wrap = me.wrapper.find(".item-list");
1055 me.wrapper.find(".item-list").empty();
1056
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301057 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301058 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301059 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301060 $(frappe.render_template("pos_item", {
1061 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301062 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301063 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301064 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301065 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301066 color: frappe.get_palette(obj.item_name),
1067 abbr: frappe.get_abbr(obj.item_name)
1068 })).tooltip().appendTo($wrap);
1069 }
1070 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001071
Faris Ansari45510102017-03-09 14:43:37 +05301072 $wrap.append(`
1073 <div class="image-view-item btn-more text-muted text-center">
1074 <div class="image-view-body">
1075 <i class="mega-octicon octicon-package"></i>
1076 <div>Load more items</div>
1077 </div>
1078 </div>
1079 `);
1080
1081 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301082 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301083 $("<p class='text-muted small' style='padding-left: 10px'>"
1084 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301085 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301086
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301087 if (this.items.length == 1
1088 && this.serach_item.$input.val()) {
1089 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301090 this.add_to_cart();
1091 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301092 },
1093
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301094 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301095 // To search item as per the key enter
1096
1097 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301098 this.item_serial_no = {};
1099 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301100
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301101 if (item_code) {
1102 return $.grep(this.item_data, function (item) {
1103 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301104 return true
1105 }
1106 })
1107 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301108
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301109 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301110
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301111 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301112 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301113 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301114 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301116 if (key) {
1117 return $.grep(this.items_list, function (item) {
1118 if (search_status) {
1119 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301120 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301121 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1122 } else if (me.serial_no_data[item.item_code]
1123 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301124 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301125 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 +05301126 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301127 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301128 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301129 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301130 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301131 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301132 return true
1133 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301134 }
1135 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301136 } else {
1137 return this.items_list;
1138 }
1139 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301140
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301141 apply_category: function() {
1142 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301143 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301144
1145 if(category == 'All Item Groups') {
1146 return this.item_data
1147 } else {
1148 return this.item_data.filter(function(element, index, array){
1149 return element.item_group == category;
1150 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301151 }
1152 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001153
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301154 bind_items_event: function() {
1155 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301156 $(this.wrapper).on('click', '.pos-bill-item', function() {
1157 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1158 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301159 me.numeric_val = "";
1160 me.numeric_id = ""
1161 me.item_code = $(this).attr("data-item-code");
1162 me.render_selected_item()
1163 me.bind_qty_event()
1164 me.update_rate()
1165 $(me.wrapper).find(".selected-item").scrollTop(1000);
1166 })
1167 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301168
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301169 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301170 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301171
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301172 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301173 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301174 var qty = $(this).val();
1175 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301176 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301177 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301178
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301179 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301180 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1181 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301182 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301183 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301184
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301185 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301186 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1187 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301188 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301189 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001190
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301191 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301192 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301193 var discount = $(this).val();
1194 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301195 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301196 })
1197 },
1198
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301199 bind_events: function() {
1200 var me = this;
1201 // if form is local then allow this function
1202 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1203 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301204 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301205 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301206 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301207
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301208 if (me.frm.doc.docstatus == 0) {
1209 me.items = me.get_items($(this).attr("data-item-code"))
1210 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301211 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301212 }
1213 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301214
1215 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301216 },
1217
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301218 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301219 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301220 this.items = this.get_items(item_code);
1221 this.validate_serial_no()
1222 this.set_item_details(item_code, "qty", qty);
1223 },
1224
1225 update_discount: function(item_code, discount) {
1226 var me = this;
1227 this.items = this.get_items(item_code);
1228 this.set_item_details(item_code, "discount_percentage", discount);
1229 },
1230
1231 update_rate: function () {
1232 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301233 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301234 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301235 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301236 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301237 })
1238 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301239
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301240 update_value: function() {
1241 var me = this;
1242 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1243 "rate": ".pos-item-price", "amount": ".pos-amount"}
1244 this.child_doc = this.get_child_item(this.item_code);
1245
1246 if(me.child_doc.length) {
1247 $.each(fields, function(key, field) {
1248 $(me.selected_row).find(field).val(me.child_doc[0][key])
1249 })
1250 } else {
1251 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301252 }
1253 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301254
1255 clear_selected_row: function() {
1256 $(this.wrapper).find('.selected-item').empty();
1257 },
1258
1259 render_selected_item: function() {
1260 this.child_doc = this.get_child_item(this.item_code);
1261 $(this.wrapper).find('.selected-item').empty();
1262 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301263 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 +05301264 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1265 $(this.wrapper).find('.selected-item').html(this.selected_row)
1266 }
1267
1268 $(this.selected_row).find('.form-control').click(function(){
1269 $(this).select();
1270 })
1271 },
1272
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301273 get_child_item: function(item_code) {
1274 var me = this;
1275 return $.map(me.frm.doc.items, function(doc){
1276 if(doc.item_code == item_code) {
1277 return doc
1278 }
1279 })
1280 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301281
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301282 set_item_details: function (item_code, field, value) {
1283 var me = this;
1284 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301285 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301286 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301287
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301288 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301289 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301290 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301291 if (d.serial_no && field == 'qty') {
1292 me.validate_serial_no_qty(d, item_code, field, value)
1293 }
1294
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301295 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301296 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301297 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301298 me.remove_item.push(d.idx)
1299 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301300
1301 if(field=="discount_percentage" && value == 0) {
1302 d.rate = d.price_list_rate;
1303 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301304 }
1305 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301306
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301307 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301308 this.remove_zero_qty_item();
1309 }
1310
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301311 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301312 },
1313
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301314 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301315 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301316 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301317 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301318 $.each(this.frm.doc["items"] || [], function (i, d) {
1319 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301320 d.idx = idx;
1321 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301322 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301323 }
1324 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301325
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301326 this.frm.doc["items"] = this.items;
1327 },
1328
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301329 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301330 var me = this;
1331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301332 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301333 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
Faris Ansariab74ca72017-05-30 12:54:42 +05301334 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301335
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301336 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301337 total = me.frm.doc.net_total
1338 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301339
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 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 +05301341 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1342 me.refresh();
1343 });
1344
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301345 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301346 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1347 me.frm.doc.additional_discount_percentage = 0.0;
1348 me.wrapper.find('input.discount-percentage').val(0);
1349 me.refresh();
1350 });
1351 },
1352
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301353 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301354 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301355 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301356 frappe.throw(__("Please select customer"))
1357 }
1358 },
1359
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301360 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301361 var me = this;
1362 var caught = false;
1363 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1364
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301365 this.customer_validate();
1366 this.mandatory_batch_no();
1367 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301368 this.validate_warehouse();
1369
1370 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301371 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301372 if (d.item_code == me.items[0].item_code) {
1373 caught = true;
1374 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301375 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301376 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301377 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1378 d.warehouse = me.item_serial_no[d.item_code][1]
1379 }
1380
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301381 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301382 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301383 }
1384 }
1385 });
1386 }
1387
1388 // if item not found then add new item
1389 if (!caught)
1390 this.add_new_item_to_grid();
1391
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301392 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301393 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301394 },
1395
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301396 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301397 var me = this;
1398 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1399 this.child.item_code = this.items[0].item_code;
1400 this.child.item_name = this.items[0].item_name;
1401 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301402 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301403 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301404 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301405 this.child.qty = 1;
1406 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301407 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1408 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301409 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301410 ? 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 +05301411 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1412 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1413 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301414 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301415 this.child.batch_no = this.item_batch_no[this.child.item_code];
1416 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1417 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301418 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301419 },
1420
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301421 update_paid_amount_status: function (update_paid_amount) {
1422 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301423 update_paid_amount = update_paid_amount ? false : true;
1424 }
1425
1426 this.refresh(update_paid_amount);
1427 },
1428
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301429 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301430 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301431 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301432 this.set_primary_action();
1433 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301434
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301435 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301436 this.apply_pricing_rule();
1437 this.discount_amount_applied = false;
1438 this._calculate_taxes_and_totals();
1439 this.calculate_discount_amount();
1440 this.show_items_in_item_cart();
1441 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301442 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301443 this.set_totals();
1444 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301445
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301446 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301447 return erpnext.get_currency(this.frm.doc.company);
1448 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301449
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301450 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301451 var me = this;
1452 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301453 var $no_items_message = this.wrapper.find(".no-items-message");
1454 $no_items_message.toggle(this.frm.doc.items.length === 0);
1455
1456 var $totals_area = this.wrapper.find('.totals-area');
1457 $totals_area.toggle(this.frm.doc.items.length > 0);
1458
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301459 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301460 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301461 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301462 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301463 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301464 discount_percentage: d.discount_percentage || 0.0,
1465 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301466 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301467 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301468 amount: format_currency(d.amount, me.frm.doc.currency),
1469 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301470 })).appendTo($items);
1471 });
1472
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301473 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301474 $(this).select();
1475 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301476
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301477 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301478 $(this).select();
1479 });
1480
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301481 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301482 $(this).select();
1483 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301484 },
1485
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301486 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301487 var me = this;
1488 me.frm.doc.total_taxes_and_charges = 0.0
1489
1490 var taxes = this.frm.doc.taxes || [];
1491 $(this.wrapper)
1492 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1493 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301494
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301495 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301496 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1497 $(frappe.render_template("pos_tax_row", {
1498 description: d.description,
1499 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1500 me.frm.doc.currency)
1501 })).appendTo(me.wrapper.find(".tax-table"));
1502 }
1503 });
1504 },
1505
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301506 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301507 var me = this;
1508 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1509 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1510 },
1511
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301512 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301513 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301514 this.page.set_primary_action(__("New Cart"), function () {
1515 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301516 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301517 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301518
1519 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301520 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301521 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301522 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301523 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301524 this.page.add_menu_item(__("Email"), function () {
1525 me.email_prompt()
1526 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301527 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301528 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301529
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301530 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301531 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301532 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301533 this.save_previous_entry();
1534 this.create_new();
1535 this.refresh();
1536 this.toggle_input_field();
1537 this.render_list_customers();
1538 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301539 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301540
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301541 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301542 var me = this;
1543
mbauskar66e2a512017-06-22 16:18:12 +05301544 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301545 `<a class="btn btn-primary print_doc"
1546 style="margin-right: 5px;">${__('Print')}</a>
1547 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301548
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301549 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301550 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301551 me.print_document(html)
1552 })
1553
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301554 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301555 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301556 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301557 })
1558 },
1559
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301560 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301561 var w = window.open();
1562 w.document.write(html);
1563 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301564 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301565 w.print();
1566 w.close();
1567 }, 1000)
1568 },
1569
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301570 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301571 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301572 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301573 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301574 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301575 this.print_dialog()
1576 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301577 },
1578
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301579 update_serial_no: function() {
1580 var me = this;
1581
1582 //Remove the sold serial no from the cache
1583 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301584 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301585 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301586 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301587 if(serial_no_list) {
1588 $.each(sn, function(i, serial_no) {
1589 if(in_list(Object.keys(serial_no_list), serial_no)) {
1590 delete serial_no_list[serial_no]
1591 }
1592 })
1593 me.serial_no_data[data.item_code] = serial_no_list;
1594 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301595 }
1596 })
1597 },
1598
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301599 change_status: function () {
1600 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301601 this.frm.doc.docstatus = 1;
1602 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301603 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301604 }
1605 },
1606
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301607 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301608 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301609 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301610 $(this.wrapper).find('input').attr("disabled", disabled);
1611 $(this.wrapper).find('select').attr("disabled", disabled);
1612 $(this.wrapper).find('input').attr("disabled", disabled);
1613 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301614 $(this.wrapper).find('button').attr("disabled", disabled);
1615 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301616
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301617 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301618 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301619 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301620
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301621 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301622 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1623 this.set_primary_action();
1624 },
1625
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301626 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301627 var me = this;
1628 var invoice_data = {}
1629 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301630 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301631 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301632 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301633 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301634 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301635 this.frm.doc.posting_date = frappe.datetime.get_today();
1636 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001637 this.frm.doc.pos_profile = this.pos_profile_data['name'];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301638 invoice_data[this.name] = this.frm.doc
1639 this.si_docs.push(invoice_data)
1640 this.update_localstorage();
1641 this.set_primary_action();
1642 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301643 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301644 },
1645
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301646 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301647 var me = this;
1648 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301649 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301650 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301651 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301652 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301653 me.update_localstorage();
1654 }
1655 }
1656 })
1657 },
1658
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301659 update_localstorage: function () {
1660 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301661 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301662 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301663 frappe.throw(__("LocalStorage is full , did not save"))
1664 }
1665 },
1666
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301667 get_doc_from_localstorage: function () {
1668 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301669 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301670 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301671 return []
1672 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301673 },
1674
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301675 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301676 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301677 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301678 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301679 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301680 },
1681
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301682 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301683 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301684 this.si_docs = this.get_submitted_invoice() || [];
1685 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301686 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301687 if(this.customer_doc) {
1688 this.freeze = this.customer_doc.display
1689 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301690
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301691 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 frappe.call({
1693 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1694 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301695 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301696 email_queue_list: me.email_queue_list,
1697 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301698 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301699 callback: function (r) {
1700 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301701 me.customers = r.message.synced_customers_list;
1702 me.address = r.message.synced_address;
1703 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301704 me.removed_items = r.message.invoice;
1705 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301706 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301707 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301708 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301709 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301710 me.prepare_customer_mapper()
1711 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301712 }
1713 }
1714 })
1715 }
1716 },
1717
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301718 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301719 var invoices = [];
1720 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301721 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301722 if (docs) {
1723 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301724 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301725 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301726 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301727 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301728 return data
1729 }
1730 }
1731 });
1732 }
1733
1734 return invoices
1735 },
1736
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301737 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301738 var me = this;
1739 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301740 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301741 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301742 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301743 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301744 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301745 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301746 }
1747 }
1748 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301749 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301750 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301751 this.update_localstorage();
1752 }
1753 },
1754
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301755 remove_email_queue_from_localstorage: function() {
1756 var me = this;
1757 this.email_queue = this.get_email_queue()
1758 if (this.removed_email) {
1759 $.each(this.email_queue_list, function (index, data) {
1760 if (in_list(me.removed_email, index)) {
1761 delete me.email_queue[index]
1762 }
1763 })
1764 this.update_email_queue();
1765 }
1766 },
1767
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301768 remove_customer_from_localstorage: function() {
1769 var me = this;
1770 this.customer_details = this.get_customers_details()
1771 if (this.removed_customers) {
1772 $.each(this.customers_list, function (index, data) {
1773 if (in_list(me.removed_customers, index)) {
1774 delete me.customer_details[index]
1775 }
1776 })
1777 this.update_customer_in_localstorage();
1778 }
1779 },
1780
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301781 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301782 var me = this;
1783 this.customer_validate();
1784 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301785 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301786 },
1787
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301788 item_validate: function () {
1789 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301790 frappe.throw(__("Select items to save the invoice"))
1791 }
1792 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301793
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301794 validate_mode_of_payments: function () {
1795 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301796 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1797 }
1798 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301799
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301800 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301801 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301802 var item_code = ''
1803 var serial_no = '';
1804 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301805 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301806 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301807 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301808
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301809 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301810 this.refresh();
1811 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1812 'item': this.items[0].item_code
1813 })))
1814 }
1815
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301816 if (item_code && serial_no) {
1817 $.each(this.frm.doc.items, function (index, data) {
1818 if (data.item_code == item_code) {
1819 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301820 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1821 'serial_no': serial_no
1822 })))
1823 }
1824 }
1825 })
1826 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301827 },
1828
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301829 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301830 var me = this;
1831 if (args.item_code == item_code && args.serial_no
1832 && field == 'qty' && cint(value) != value) {
1833 args.qty = 0.0;
1834 this.refresh();
1835 frappe.throw(__("Serial no item cannot be a fraction"))
1836 }
1837
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301838 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301839 args.qty = 0.0;
1840 args.serial_no = ''
1841 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301842 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1843 'item': item_code
1844 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301845 }
1846 },
1847
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301848 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301849 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301850 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301851 frappe.prompt([{
1852 'fieldname': 'batch',
1853 'fieldtype': 'Select',
1854 'label': __('Batch No'),
1855 'reqd': 1,
1856 'options': this.batch_no_data[this.items[0].item_code]
1857 }],
1858 function(values){
1859 me.item_batch_no[me.items[0].item_code] = values.batch;
1860 },
1861 __('Select Batch No'))
1862 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301863 },
1864
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301865 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301866 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301867 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301868 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301869 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301870 if (pricing_rule.length) {
1871 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301872 item.margin_type = pricing_rule[0].margin_type;
1873 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1874 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1875 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301876 me.apply_pricing_rule_on_item(item)
1877 } else if (item.pricing_rule) {
1878 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301879 item.margin_rate_or_amount = 0.0;
1880 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301881 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301882 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301883 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001884
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301885 if(item.discount_percentage > 0) {
1886 me.apply_pricing_rule_on_item(item)
1887 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301888 })
1889 },
1890
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301891 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301892 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301893 return $.grep(this.pricing_rules, function (data) {
1894 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301895 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301896 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301897 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301898 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301899 return true
1900 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301901 }
1902 }
1903 })
1904 },
1905
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301906 validate_item_condition: function (data, item) {
1907 var apply_on = frappe.model.scrub(data.apply_on);
1908
1909 return (data.apply_on == 'Item Group')
1910 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1911 },
1912
1913 validate_item_group: function (pr_item_group, cart_item_group) {
1914 //pr_item_group = pricing rule's item group
1915 //cart_item_group = cart item's item group
1916 //this.item_groups has information about item group's lft and rgt
1917 //for example: {'Foods': [12, 19]}
1918
1919 pr_item_group = this.item_groups[pr_item_group]
1920 cart_item_group = this.item_groups[cart_item_group]
1921
1922 return (cart_item_group[0] >= pr_item_group[0] &&
1923 cart_item_group[1] <= pr_item_group[1])
1924 },
1925
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301926 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301927 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301928 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301929 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301930 return true
1931 }
1932 },
1933
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301934 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301935 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301936 'Customer': [data.customer, [this.frm.doc.customer]],
1937 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1938 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301939 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301940 }
1941 },
1942
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301943 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301944 //This method validate duplicate pricing rule
1945 var pricing_rule_name = '';
1946 var priority = 0;
1947 var pricing_rule_list = [];
1948 var priority_list = []
1949
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301950 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301951
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301952 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301953 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301954 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301955 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301956 priority = data.priority
1957 pricing_rule_list.push(data)
1958 }
1959 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301960
Faris Ansariab74ca72017-05-30 12:54:42 +05301961 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301962 $.each(priority_list, function (index, value) {
1963 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301964 count++
1965 }
1966 })
1967
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301968 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301969 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1970 'pricing_rule': pricing_rule_name
1971 })))
1972 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301973
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301974 return pricing_rule_list
1975 }
1976 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301977
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301978 validate_warehouse: function () {
1979 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 +05301980 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301981 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301982 },
1983
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301984 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301985 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301986
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301987 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301988 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301989 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301990 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301991 }
1992
1993 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301994 },
1995
1996 update_customer_in_localstorage: function() {
1997 var me = this;
1998 try {
1999 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2000 } catch (e) {
2001 frappe.throw(__("LocalStorage is full , did not save"))
2002 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302003 }
Faris Ansari185762a2017-04-13 12:36:08 +05302004})