blob: 2f425248a16442eb894c6678a43cb649d992d20e [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
Faris Ansaric70bbac2017-08-29 15:27:17 +053011 frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => {
12 if (r && r.is_online && !cint(r.is_online)) {
13 // offline
14 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
15 cur_pos = wrapper.pos;
16 } else {
17 // online
18 frappe.set_route('point-of-sale');
19 }
20 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053021}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053023frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053024 window.onbeforeunload = function () {
25 return wrapper.pos.beforeunload()
26 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053027}
28
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053029erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053030 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053031 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053032 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053033 this.page = wrapper.page;
34 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053035 this.set_indicator();
36 this.onload();
37 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053038 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053039 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.si_docs = this.get_doc_from_localstorage();
41 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053042
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053043 beforeunload: function (e) {
44 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 e = e || window.event;
46
47 // For IE and Firefox prior to version 4
48 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053050 return
51 }
52
53 // For Safari
54 return __("You are in offline mode. You will not be able to reload until you have network.");
55 }
56 },
57
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053058 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 var me = this;
60 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053061 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053063 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 },
65
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 var me = this;
68 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053069 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053070 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053071 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 method: "frappe.handler.ping",
73 callback: function (r) {
74 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053076 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 }
78 }
79 })
80 },
81
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053082 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053083 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053084 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053085 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053087 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 },
89
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053090 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053091 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053092 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053093
94 // for mobile
95 this.page.add_menu_item(__("Pay"), function () {
96 me.validate();
97 me.update_paid_amount_status(true);
98 me.create_invoice();
99 me.make_payment();
100 }).addClass('visible-xs');
101
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530102 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530103 me.save_previous_entry();
104 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530105 })
106
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530107 this.page.add_menu_item(__("Sync Master Data"), function () {
108 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530109 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530110 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530111 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530112 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530113 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530114
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530115 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530116 me.sync_sales_invoice()
117 });
118
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530119 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530120 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530121 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530122 },
123
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530124 email_prompt: function() {
125 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530126 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530127 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
128 {fieldtype: "Section Break"},
129 {label:__("Subject"), fieldtype:"Data", reqd: 1,
130 fieldname:"subject",length:524288},
131 {fieldtype: "Section Break"},
132 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
133 fieldname:"content"},
134 {fieldtype: "Section Break"},
135 {fieldtype: "Column Break"}];
136
137 this.email_dialog = new frappe.ui.Dialog({
138 title: "Email",
139 fields: fields,
140 primary_action_label: __("Send"),
141 primary_action: function() {
142 me.send_action();
143 }
144 });
145
146 this.email_dialog.show()
147 },
148
149 send_action: function() {
150 this.email_queue = this.get_email_queue()
151 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
152 this.update_email_queue()
153 this.email_dialog.hide()
154 },
155
156 update_email_queue: function () {
157 try {
158 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
159 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530160 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530161 }
162 },
163
164 get_email_queue: function () {
165 try {
166 return JSON.parse(localStorage.getItem('email_queue')) || {};
167 } catch (e) {
168 return {}
169 }
170 },
171
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530172 get_customers_details: function () {
173 try {
174 return JSON.parse(localStorage.getItem('customer_details')) || {};
175 } catch (e) {
176 return {}
177 }
178 },
179
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530180 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530181 var me = this;
182
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530183 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530184 me.removed_items = [];
185 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530186 if ($(this).is(":checked")) {
187 $.each(me.si_docs, function (index, data) {
188 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530189 me.removed_items.push(key)
190 }
191 })
192 }
193
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530194 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530195 })
196
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530197 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530198 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530199 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530200 me.removed_items.push(me.name);
201 } else {
202 me.removed_items.pop(me.name)
203 }
204
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530205 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530206 })
207 },
208
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530209 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 var me = this;
211
212 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530213 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 this.frm.doc = doc_data[0][this.name];
215 this.set_missing_values();
216 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530217 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530218 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530219 }
220 },
221
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530222 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530223 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530224 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530225 this.remove_doc_from_localstorage()
226 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530227 // this.dialog_actions();
228 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530229 },
230
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530231 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530232 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530233 this.si_docs = this.get_submitted_invoice()
234 $.each(this.removed_items, function(index, name){
235 $.each(me.si_docs, function(key, data){
236 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
237 frappe.throw(__("Submitted orders can not be deleted"))
238 }
239 })
240 })
241 },
242
243 toggle_delete_button: function () {
244 var me = this;
245 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530246 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530247 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530248 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530249 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530250 }
251 }
252 },
253
254 get_doctype_status: function (doc) {
255 if (doc.docstatus == 0) {
256 return { status: "Draft", indicator: "red" }
257 } else if (doc.outstanding_amount == 0) {
258 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530259 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530260 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530261 }
262 },
263
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530264 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530265 var me = this;
266 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530267 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530268 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530269 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530270 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530271
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530272 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530273 this.party_field.$input.val(this.frm.doc.customer);
274 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530275
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530276 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530277 this.frm.doc.write_off_account = doc.write_off_account
278 }
279
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530280 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530281 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530282 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530283 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530284
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530285 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 var me = this;
287 this.si_docs = this.get_doc_from_localstorage();
288
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530289 return $.grep(this.si_docs, function (data) {
290 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530291 return key == me.name
292 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530293 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530294 },
295
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530296 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530297 var me = this;
298 frappe.call({
299 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
300 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530301 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530302 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530304 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530305 me.set_interval_for_si_sync();
306 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530307 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530308 callback();
309 }
310 }
311 })
312 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530313
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530314 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530315 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530316 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530317 this.meta = r.message.meta;
318 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530319 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530320 this.customers = r.message.customers;
321 this.serial_no_data = r.message.serial_no_data;
322 this.batch_no_data = r.message.batch_no_data;
323 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530324 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530325 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530326 this.price_list_data = r.message.price_list_data;
327 this.bin_data = r.message.bin_data;
328 this.pricing_rules = r.message.pricing_rules;
329 this.print_template = r.message.print_template;
330 this.pos_profile_data = r.message.pos_profile;
331 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530332 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530333 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 +0530334 },
335
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530336 save_previous_entry: function () {
337 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
338 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530339 }
340 },
341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530343 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530344 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530345 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530346 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530347 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530348 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530349 },
350
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530351 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530352 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530353
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530354 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530355 this.actual_qty_dict = {};
356
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530357 if (load_doc) {
358 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530359 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530361 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530362 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530363 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364 })
365
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530366 this.print_template_data = frappe.render_template("print_template", {
367 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530368 title: "POS",
369 base_url: frappe.urllib.get_base_url(),
370 print_css: frappe.boot.print_css,
371 print_settings: this.print_settings,
372 header: this.letter_head.header,
373 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530374 landscape: false,
375 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530376 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530377 },
378
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530379 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530380 this.make();
381 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530382 this.party_field.$input.attr('disabled', false);
383 if(this.selected_row) {
384 this.selected_row.hide()
385 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530386 },
387
Rohit Waghchaure61165122017-05-02 13:04:08 +0530388 set_default_customer: function() {
389 if (this.default_customer && !this.frm.doc.customer) {
390 this.party_field.$input.val(this.default_customer);
391 this.frm.doc.customer = this.default_customer;
392 this.numeric_keypad.show();
393 this.toggle_list_customer(false)
394 this.toggle_item_cart(true)
395 }
396 },
397
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530398 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530399 var me = this;
400 this.party = party;
401 this.price_list = (party == "Customer" ?
402 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
403 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
404 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
405 },
406
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530407 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530408 this.make_item_list();
409 this.make_discount_field()
410 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200411
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530412 make_control: function() {
413 this.frm = {}
414 this.frm.doc = this.doc
415 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530416 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 +0530417 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530418 this.make_search();
419 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530420 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530421 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530422 },
423
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530424 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530425 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530426 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530427 df: {
428 "fieldtype": "Data",
429 "label": "Item",
430 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530431 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530432 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530433 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530434 only_input: true,
435 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530437 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530438
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530439 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530440
441 clearTimeout(me.last_search_timeout);
442 me.last_search_timeout = setTimeout(() => {
443 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
444 me.items = me.get_items();
445 me.make_item_list();
446 }
447 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530448 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530449
Faris Ansari339d9c92017-03-07 18:14:06 +0530450 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530451 sorted_item_groups = this.get_sorted_item_groups()
452 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530453 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
454 }).join("");
455
456 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
457
458 this.search_item_group.on('click', '.dropdown-menu a', function() {
459 me.selected_item_group = $(this).attr('data-value');
460 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
461
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530462 me.page_len = 20;
463 me.items = me.get_items();
464 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530465 })
466
Faris Ansari45510102017-03-09 14:43:37 +0530467 me.toggle_more_btn();
468
469 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530470 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530471 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530472 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530473 me.toggle_more_btn();
474 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530475
Faris Ansari45510102017-03-09 14:43:37 +0530476 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530477 me.update_customer()
478 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530479 },
480
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530481 get_sorted_item_groups: function() {
482 list = {}
483 $.each(this.item_groups, function(i, data) {
484 list[i] = data[0]
485 })
486
487 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
488 },
489
Faris Ansari45510102017-03-09 14:43:37 +0530490 toggle_more_btn: function() {
491 if(!this.items || this.items.length <= this.page_len) {
492 this.wrapper.find(".btn-more").hide();
493 } else {
494 this.wrapper.find(".btn-more").show();
495 }
496 },
497
498 toggle_totals_area: function(show) {
499
Faris Ansari07c9f352017-03-09 17:03:14 +0530500 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530501 show = this.is_totals_area_collapsed;
502 }
503
504 var totals_area = this.wrapper.find('.totals-area');
505 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
506 .toggle(show);
507
508 if(show) {
509 totals_area.find('.collapse-btn i')
510 .removeClass('octicon-chevron-down')
511 .addClass('octicon-chevron-up');
512 } else {
513 totals_area.find('.collapse-btn i')
514 .removeClass('octicon-chevron-up')
515 .addClass('octicon-chevron-down');
516 }
517
518 this.is_totals_area_collapsed = !show;
519 },
520
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530521 make_list_customers: function () {
522 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530523 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530524 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530525 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530526 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530527 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530528 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530529
Faris Ansari45510102017-03-09 14:43:37 +0530530 me.render_list_customers();
531 me.toggle_totals_area(false);
532
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530533 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530534 $(this).toggleClass("view_customer");
535 if($(this).hasClass("view_customer")) {
536 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530537 me.list_customers.show();
538 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530539 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530540 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530541 } else {
542 if(me.frm.doc.docstatus == 0) {
543 me.party_field.$input.attr('disabled', false);
544 }
545 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530546 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530547 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530548 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530549 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530550 }
551 });
552 this.add_customer_btn.on('click', function() {
553 me.save_previous_entry();
554 me.create_new();
555 me.refresh();
556 me.set_focus();
557 });
Faris Ansari45510102017-03-09 14:43:37 +0530558 this.pos_bill.on('click', '.collapse-btn', function() {
559 me.toggle_totals_area();
560 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530561 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200562
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530563 bind_numeric_keypad: function() {
564 var me = this;
565 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
566 me.numeric_val = '';
567 })
568
569 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
570 me.numeric_id = $(this).attr("id") || me.numeric_id;
571 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530572 if(me.numeric_id) {
573 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
574 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530575
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530576 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530577 me.numeric_val += me.val;
578 me.selected_field.val(flt(me.numeric_val))
579 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530580 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530581 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530582
583 if(me.numeric_id && $(this).hasClass('pos-operation')) {
584 me.numeric_keypad.find('button.pos-operation').removeClass('active');
585 $(this).addClass('active');
586
587 me.selected_row.find('.pos-list-row').removeClass('active');
588 me.selected_field.closest('.pos-list-row').addClass('active');
589 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530590 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200591
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530592 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530593 if(me.numeric_id) {
594 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
595 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
596 me.selected_field.val(me.numeric_val);
597 me.selected_field.trigger("change")
598 } else {
599 //Remove an item from the cart, if focus is at selected item
600 me.remove_selected_item()
601 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530602 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200603
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530604 $(this.numeric_keypad).find('.pos-pay').click(function(){
605 me.validate();
606 me.update_paid_amount_status(true);
607 me.create_invoice();
608 me.make_payment();
609 })
610 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530611
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530612 remove_selected_item: function() {
613 this.remove_item = []
614 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
615 this.remove_item.push(idx)
616 this.remove_zero_qty_item()
617 this.update_paid_amount_status(false)
618 },
619
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530620 render_list_customers: function () {
621 var me = this;
622
623 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530624 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530625 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530626 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530627 return;
628 }
Faris Ansari45510102017-03-09 14:43:37 +0530629
630 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530631 if(this.si_docs.length) {
632 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530633 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530634 html += frappe.render_template("pos_invoice_list", {
635 sr: i + 1,
636 name: key,
637 customer: data[key].customer,
638 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
639 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
640 data: me.get_doctype_status(data[key])
641 });
642 }
643 });
644 }
Faris Ansari45510102017-03-09 14:43:37 +0530645 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530646
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530647 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530648 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530649 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530650 me.list_customers_btn.toggleClass("view_customer");
651 me.pos_bill.show();
652 me.list_customers_btn.show();
653 me.name = $(this).parents().attr('invoice-name')
654 me.edit_record();
655 })
656
657 //actions
658 $(this.wrapper).find('.list-select-all').click(function () {
659 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
660 me.removed_items = [];
661 if ($(this).is(":checked")) {
662 $.each(me.si_docs, function (index, data) {
663 for (key in data) {
664 me.removed_items.push(key)
665 }
666 });
667 }
668
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530669 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530670 });
671
672 $(this.wrapper).find('.list-delete').click(function () {
673 me.name = $(this).parent().parent().attr('invoice-name');
674 if ($(this).is(":checked")) {
675 me.removed_items.push(me.name);
676 } else {
677 me.removed_items.pop(me.name)
678 }
679
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530680 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530681 });
682 },
683
684 bind_delete_event: function() {
685 var me = this;
686
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530687 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530688 frappe.confirm(__("Delete permanently?"), function () {
689 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530690 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530691 me.render_list_customers();
692 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530693 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530694 },
695
696 set_focus: function () {
697 if (this.default_customer || this.frm.doc.customer) {
698 this.serach_item.$input.focus();
699 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530700 this.party_field.$input.focus();
701 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530702 },
703
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530704 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530705 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530706
707 if(!this.party_field) {
708 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
709 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530710 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530711 })).insertAfter(this.page.$title_area.hide());
712 }
713
714 this.party_field = frappe.ui.form.make_control({
715 df: {
716 "fieldtype": "Data",
717 "options": this.party,
718 "label": this.party,
719 "fieldname": this.party.toLowerCase(),
720 "placeholder": __("Select or add new customer")
721 },
722 parent: this.page.wrapper.find(".party-area"),
723 only_input: true,
724 });
725
726 this.party_field.make_input();
727 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530728 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530729 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530730
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530731 this.party_field.awesomeplete =
732 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530733 minChars: 0,
734 maxItems: 99,
735 autoFirst: true,
736 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530737 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530738 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530739 return true;
740 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530741
742 input = input.toLowerCase();
743 item = this.get_item(item.value);
rohitwaghchaure56e31d02017-08-31 14:19:58 +0530744 return item.searchtext.includes(input)
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530745 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530746 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530747 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530748 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530749 if(d.customer_name) {
750 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
751 }
752
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530753 return $('<li></li>')
754 .data('item.autocomplete', d)
755 .html('<a><p>' + html + '</p></a>')
756 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530757 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530758 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530759
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530760 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530761 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530762
763 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530764 .on('input', function (e) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530765 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530766 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530767 .on('awesomplete-select', function (e) {
768 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530769 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530770 if (!customer) return;
771 // create customer link
772 if (customer.action) {
773 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530774 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530775 }
Faris Ansari45510102017-03-09 14:43:37 +0530776 me.toggle_list_customer(false);
777 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530778 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530779 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530780 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530781 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530782 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530783 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530784 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530785 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530786
787 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530788 me.toggle_list_customer(false)
789 me.toggle_item_cart(true)
790 } else {
791 me.toggle_list_customer(true)
792 me.toggle_item_cart(false)
793 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530794 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530795 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530796 var item = me.party_field.awesomeplete
797 .get_item(e.originalEvent.text.value);
798 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530799 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530800 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530801 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530802 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530803 },
804
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530805 prepare_customer_mapper: function() {
806 var me = this;
807
808 this.customers_mapper = this.customers.map(function (c) {
809 contact = me.contacts[c.name];
810 return {
811 label: c.name,
812 value: c.name,
813 customer_name: c.customer_name,
814 customer_group: c.customer_group,
815 territory: c.territory,
816 phone: contact ? contact["phone"] : '',
Rohit Waghchaurec8490622017-06-10 12:21:37 +0530817 mobile_no: contact ? contact["mobile_no"] : '',
rohitwaghchaure56e31d02017-08-31 14:19:58 +0530818 email_id: contact ? contact["email_id"] : '',
819 searchtext: ['customer_name', 'customer_group', 'value',
820 'label', 'email_id', 'phone', 'mobile_no']
821 .map(key => c[key]).join(' ')
822 .toLowerCase()
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530823 }
824 });
825
826 this.customers_mapper.push({
827 label: "<span class='text-primary link-option'>"
828 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
829 + __("Create a new Customer")
830 + "</span>",
831 value: 'is_action',
832 action: me.add_customer
833 });
834 },
835
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530836 autocomplete_customers: function() {
837 this.party_field.awesomeplete.list = this.customers_mapper;
838 },
839
Faris Ansari45510102017-03-09 14:43:37 +0530840 toggle_edit_button: function(flag) {
841 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
842 },
843
844 toggle_list_customer: function(flag) {
845 this.list_customers.toggle(flag);
846 },
847
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530848 toggle_item_cart: function(flag) {
849 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
850 },
851
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530852 add_customer: function() {
853 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530854 this.update_customer(true);
855 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530856 },
857
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530858 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530859 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530860
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530861 this.customer_doc = new frappe.ui.Dialog({
862 'title': 'Customer',
863 fields: [
864 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530865 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530866 "fieldname": "full_name",
867 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530868 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530869 },
870 {
871 "fieldtype": "Section Break"
872 },
873 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530874 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530875 "fieldname": "email_id",
876 "fieldtype": "Data"
877 },
878 {
879 "fieldtype": "Column Break"
880 },
881 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530882 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530883 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530884 "fieldtype": "Data"
885 },
886 {
887 "fieldtype": "Section Break"
888 },
889 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530890 "label": __("Address Name"),
891 "read_only": 1,
892 "fieldname": "name",
893 "fieldtype": "Data"
894 },
895 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530896 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530897 "fieldname": "address_line1",
898 "fieldtype": "Data"
899 },
900 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530901 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530902 "fieldname": "address_line2",
903 "fieldtype": "Data"
904 },
905 {
906 "fieldtype": "Column Break"
907 },
908 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530909 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530910 "fieldname": "city",
911 "fieldtype": "Data"
912 },
913 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530914 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530915 "fieldname": "state",
916 "fieldtype": "Data"
917 },
918 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530919 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530920 "fieldname": "pincode",
921 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530922 },
923 {
924 "label": __("Customer POS Id"),
925 "fieldname": "customer_pos_id",
926 "fieldtype": "Data",
927 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530928 }
929 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530930 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530931 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530932 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530933
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530934 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530935 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530936 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530937 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530938 });
939 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530940
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530941 render_address_data: function() {
942 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530943 this.address_data = this.address[this.frm.doc.customer] || {};
944 if(!this.address_data.email_id || !this.address_data.phone) {
945 this.address_data = this.contacts[this.frm.doc.customer];
946 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530947
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530948 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530949 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
950 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530951 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530952
953 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
954 this.customer_doc.set_value("customer_pos_id", $.now())
955 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530956 },
957
958 get_address_from_localstorage: function() {
959 this.address_details = this.get_customers_details()
960 return this.address_details[this.frm.doc.customer]
961 },
962
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530963 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530964 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530965 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530966 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530967 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530968 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530969 this.update_address_and_customer_list(new_customer)
970 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530971 this.update_customer_in_localstorage()
972 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530973 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530974 },
975
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530976 update_address_and_customer_list: function(new_customer) {
977 var me = this;
978 if(new_customer) {
979 this.customers_mapper.push({
980 label: this.frm.doc.customer,
981 value: this.frm.doc.customer,
982 customer_group: "",
983 territory: ""
984 });
985 }
986
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530987 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530988 },
989
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530990 get_prompt_details: function() {
991 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530992 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +0530993 this.prompt_details['territory'] = this.pos_profile_data["territory"];
994 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530995 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530996 return JSON.stringify(this.prompt_details)
997 },
998
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530999 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301000 var me = this;
1001 this.frm.doc.customer = doc.label || doc.name;
1002 this.frm.doc.customer_name = doc.customer_name;
1003 this.frm.doc.customer_group = doc.customer_group;
1004 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301005 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301006 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301007 },
1008
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301009 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301010 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301011 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301012 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301013 return;
1014 }
1015
1016 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301017
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301018 var $wrap = me.wrapper.find(".item-list");
1019 me.wrapper.find(".item-list").empty();
1020
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301021 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301022 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301023 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301024 $(frappe.render_template("pos_item", {
1025 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301026 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301027 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301028 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301029 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301030 color: frappe.get_palette(obj.item_name),
1031 abbr: frappe.get_abbr(obj.item_name)
1032 })).tooltip().appendTo($wrap);
1033 }
1034 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001035
Faris Ansari45510102017-03-09 14:43:37 +05301036 $wrap.append(`
1037 <div class="image-view-item btn-more text-muted text-center">
1038 <div class="image-view-body">
1039 <i class="mega-octicon octicon-package"></i>
1040 <div>Load more items</div>
1041 </div>
1042 </div>
1043 `);
1044
1045 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301046 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301047 $("<p class='text-muted small' style='padding-left: 10px'>"
1048 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301049 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301050
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301051 if (this.items.length == 1
1052 && this.serach_item.$input.val()) {
1053 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301054 this.add_to_cart();
1055 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301056 },
1057
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301058 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301059 // To search item as per the key enter
1060
1061 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301062 this.item_serial_no = {};
1063 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301064
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301065 if (item_code) {
1066 return $.grep(this.item_data, function (item) {
1067 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301068 return true
1069 }
1070 })
1071 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301072
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301073 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301074
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301075 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301076 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301077 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301078 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301079
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301080 if (key) {
1081 return $.grep(this.items_list, function (item) {
1082 if (search_status) {
1083 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301084 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301085 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1086 } else if (me.serial_no_data[item.item_code]
1087 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301088 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301089 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 +05301090 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301091 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301092 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301093 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301094 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301095 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301096 return true
1097 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301098 }
1099 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301100 } else {
1101 return this.items_list;
1102 }
1103 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301104
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301105 apply_category: function() {
1106 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301107 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301108
1109 if(category == 'All Item Groups') {
1110 return this.item_data
1111 } else {
1112 return this.item_data.filter(function(element, index, array){
1113 return element.item_group == category;
1114 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301115 }
1116 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001117
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301118 bind_items_event: function() {
1119 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301120 $(this.wrapper).on('click', '.pos-bill-item', function() {
1121 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1122 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301123 me.numeric_val = "";
1124 me.numeric_id = ""
1125 me.item_code = $(this).attr("data-item-code");
1126 me.render_selected_item()
1127 me.bind_qty_event()
1128 me.update_rate()
1129 $(me.wrapper).find(".selected-item").scrollTop(1000);
1130 })
1131 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301132
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301133 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301134 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301135
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301136 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301137 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301138 var qty = $(this).val();
1139 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301140 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301141 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301142
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301143 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301144 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1145 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301146 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301147 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301148
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301149 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301150 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1151 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301152 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301153 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001154
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301155 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301156 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301157 var discount = $(this).val();
1158 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301159 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301160 })
1161 },
1162
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301163 bind_events: function() {
1164 var me = this;
1165 // if form is local then allow this function
1166 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1167 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301168 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301169 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301170 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301171
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301172 if (me.frm.doc.docstatus == 0) {
1173 me.items = me.get_items($(this).attr("data-item-code"))
1174 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301175 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301176 }
1177 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301178
1179 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301180 },
1181
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301182 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301183 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301184 this.items = this.get_items(item_code);
1185 this.validate_serial_no()
1186 this.set_item_details(item_code, "qty", qty);
1187 },
1188
1189 update_discount: function(item_code, discount) {
1190 var me = this;
1191 this.items = this.get_items(item_code);
1192 this.set_item_details(item_code, "discount_percentage", discount);
1193 },
1194
1195 update_rate: function () {
1196 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301197 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301198 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301199 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301200 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301201 })
1202 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301203
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301204 update_value: function() {
1205 var me = this;
1206 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1207 "rate": ".pos-item-price", "amount": ".pos-amount"}
1208 this.child_doc = this.get_child_item(this.item_code);
1209
1210 if(me.child_doc.length) {
1211 $.each(fields, function(key, field) {
1212 $(me.selected_row).find(field).val(me.child_doc[0][key])
1213 })
1214 } else {
1215 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301216 }
1217 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301218
1219 clear_selected_row: function() {
1220 $(this.wrapper).find('.selected-item').empty();
1221 },
1222
1223 render_selected_item: function() {
1224 this.child_doc = this.get_child_item(this.item_code);
1225 $(this.wrapper).find('.selected-item').empty();
1226 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301227 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 +05301228 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1229 $(this.wrapper).find('.selected-item').html(this.selected_row)
1230 }
1231
1232 $(this.selected_row).find('.form-control').click(function(){
1233 $(this).select();
1234 })
1235 },
1236
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301237 get_child_item: function(item_code) {
1238 var me = this;
1239 return $.map(me.frm.doc.items, function(doc){
1240 if(doc.item_code == item_code) {
1241 return doc
1242 }
1243 })
1244 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301245
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301246 set_item_details: function (item_code, field, value) {
1247 var me = this;
1248 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301249 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301250 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301251
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301252 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301253 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301254 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301255 if (d.serial_no && field == 'qty') {
1256 me.validate_serial_no_qty(d, item_code, field, value)
1257 }
1258
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301259 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301260 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301261 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301262 me.remove_item.push(d.idx)
1263 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301264
1265 if(field=="discount_percentage" && value == 0) {
1266 d.rate = d.price_list_rate;
1267 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301268 }
1269 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301270
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301271 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301272 this.remove_zero_qty_item();
1273 }
1274
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301275 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301276 },
1277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301278 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301279 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301280 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301281 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301282 $.each(this.frm.doc["items"] || [], function (i, d) {
1283 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301284 d.idx = idx;
1285 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301286 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301287 }
1288 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301289
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301290 this.frm.doc["items"] = this.items;
1291 },
1292
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301293 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301294 var me = this;
1295
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301296 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301297 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
Faris Ansariab74ca72017-05-30 12:54:42 +05301298 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301299
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301300 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 total = me.frm.doc.net_total
1302 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301303
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301304 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 +05301305 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1306 me.refresh();
1307 });
1308
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301309 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301310 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1311 me.frm.doc.additional_discount_percentage = 0.0;
1312 me.wrapper.find('input.discount-percentage').val(0);
1313 me.refresh();
1314 });
1315 },
1316
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301317 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301318 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301319 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301320 frappe.throw(__("Please select customer"))
1321 }
1322 },
1323
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301324 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301325 var me = this;
1326 var caught = false;
1327 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1328
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301329 this.customer_validate();
1330 this.mandatory_batch_no();
1331 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301332 this.validate_warehouse();
1333
1334 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301335 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301336 if (d.item_code == me.items[0].item_code) {
1337 caught = true;
1338 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301339 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301341 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1342 d.warehouse = me.item_serial_no[d.item_code][1]
1343 }
1344
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301345 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301346 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 }
1348 }
1349 });
1350 }
1351
1352 // if item not found then add new item
1353 if (!caught)
1354 this.add_new_item_to_grid();
1355
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301356 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301357 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301358 },
1359
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301360 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301361 var me = this;
1362 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1363 this.child.item_code = this.items[0].item_code;
1364 this.child.item_name = this.items[0].item_name;
1365 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301366 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301367 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301368 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301369 this.child.qty = 1;
1370 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301371 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1372 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301373 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301374 ? 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 +05301375 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1376 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1377 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301378 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301379 this.child.batch_no = this.item_batch_no[this.child.item_code];
1380 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1381 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301382 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301383 },
1384
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301385 update_paid_amount_status: function (update_paid_amount) {
1386 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301387 update_paid_amount = update_paid_amount ? false : true;
1388 }
1389
1390 this.refresh(update_paid_amount);
1391 },
1392
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301393 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301394 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301395 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301396 this.set_primary_action();
1397 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301399 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301400 this.apply_pricing_rule();
1401 this.discount_amount_applied = false;
1402 this._calculate_taxes_and_totals();
1403 this.calculate_discount_amount();
1404 this.show_items_in_item_cart();
1405 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301406 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301407 this.set_totals();
1408 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301409
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301410 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301411 return erpnext.get_currency(this.frm.doc.company);
1412 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301413
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301414 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301415 var me = this;
1416 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301417 var $no_items_message = this.wrapper.find(".no-items-message");
1418 $no_items_message.toggle(this.frm.doc.items.length === 0);
1419
1420 var $totals_area = this.wrapper.find('.totals-area');
1421 $totals_area.toggle(this.frm.doc.items.length > 0);
1422
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301423 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301424 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301425 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301426 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301427 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301428 discount_percentage: d.discount_percentage || 0.0,
1429 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301430 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301431 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301432 amount: format_currency(d.amount, me.frm.doc.currency),
1433 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301434 })).appendTo($items);
1435 });
1436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301437 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301438 $(this).select();
1439 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301440
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301441 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301442 $(this).select();
1443 });
1444
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301445 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301446 $(this).select();
1447 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 },
1449
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301450 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301451 var me = this;
1452 me.frm.doc.total_taxes_and_charges = 0.0
1453
1454 var taxes = this.frm.doc.taxes || [];
1455 $(this.wrapper)
1456 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1457 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301458
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301459 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301460 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1461 $(frappe.render_template("pos_tax_row", {
1462 description: d.description,
1463 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1464 me.frm.doc.currency)
1465 })).appendTo(me.wrapper.find(".tax-table"));
1466 }
1467 });
1468 },
1469
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301470 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301471 var me = this;
1472 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1473 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1474 },
1475
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301476 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301477 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301478 this.page.set_primary_action(__("New Cart"), function () {
1479 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301480 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301481 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301482
1483 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301484 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301485 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301486 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301487 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301488 this.page.add_menu_item(__("Email"), function () {
1489 me.email_prompt()
1490 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301491 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301492 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301493
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301494 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301495 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301496 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301497 this.save_previous_entry();
1498 this.create_new();
1499 this.refresh();
1500 this.toggle_input_field();
1501 this.render_list_customers();
1502 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301503 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301504
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301505 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301506 var me = this;
1507
mbauskar66e2a512017-06-22 16:18:12 +05301508 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301509 `<a class="btn btn-primary print_doc"
1510 style="margin-right: 5px;">${__('Print')}</a>
1511 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301512
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301513 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301514 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301515 me.print_document(html)
1516 })
1517
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301518 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301519 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301520 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301521 })
1522 },
1523
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301524 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301525 var w = window.open();
1526 w.document.write(html);
1527 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301528 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301529 w.print();
1530 w.close();
1531 }, 1000)
1532 },
1533
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301534 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301535 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301536 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301537 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301538 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301539 this.print_dialog()
1540 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301541 },
1542
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301543 update_serial_no: function() {
1544 var me = this;
1545
1546 //Remove the sold serial no from the cache
1547 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301548 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301549 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301550 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301551 if(serial_no_list) {
1552 $.each(sn, function(i, serial_no) {
1553 if(in_list(Object.keys(serial_no_list), serial_no)) {
1554 delete serial_no_list[serial_no]
1555 }
1556 })
1557 me.serial_no_data[data.item_code] = serial_no_list;
1558 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301559 }
1560 })
1561 },
1562
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301563 change_status: function () {
1564 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301565 this.frm.doc.docstatus = 1;
1566 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301567 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301568 }
1569 },
1570
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301571 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301572 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301573 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301574 $(this.wrapper).find('input').attr("disabled", disabled);
1575 $(this.wrapper).find('select').attr("disabled", disabled);
1576 $(this.wrapper).find('input').attr("disabled", disabled);
1577 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301578 $(this.wrapper).find('button').attr("disabled", disabled);
1579 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301580
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301581 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301582 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301583 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301584
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301585 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301586 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1587 this.set_primary_action();
1588 },
1589
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301590 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301591 var me = this;
1592 var invoice_data = {}
1593 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301594 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301595 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301596 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301597 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301598 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301599 this.frm.doc.posting_date = frappe.datetime.get_today();
1600 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001601 this.frm.doc.pos_profile = this.pos_profile_data['name'];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301602 invoice_data[this.name] = this.frm.doc
1603 this.si_docs.push(invoice_data)
1604 this.update_localstorage();
1605 this.set_primary_action();
1606 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301607 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301608 },
1609
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301610 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301611 var me = this;
1612 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301613 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301614 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301615 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301616 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301617 me.update_localstorage();
1618 }
1619 }
1620 })
1621 },
1622
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301623 update_localstorage: function () {
1624 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301625 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301626 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301627 frappe.throw(__("LocalStorage is full , did not save"))
1628 }
1629 },
1630
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301631 get_doc_from_localstorage: function () {
1632 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301633 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301634 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301635 return []
1636 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301637 },
1638
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301639 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301640 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301641 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301642 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301643 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301644 },
1645
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301646 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301647 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301648 this.si_docs = this.get_submitted_invoice() || [];
1649 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301650 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301651 if(this.customer_doc) {
1652 this.freeze = this.customer_doc.display
1653 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301654
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301655 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301656 frappe.call({
1657 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1658 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301659 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301660 email_queue_list: me.email_queue_list,
1661 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301662 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301663 callback: function (r) {
1664 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301665 me.customers = r.message.synced_customers_list;
1666 me.address = r.message.synced_address;
1667 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301668 me.removed_items = r.message.invoice;
1669 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301670 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301671 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301672 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301673 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301674 me.prepare_customer_mapper()
1675 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301676 }
1677 }
1678 })
1679 }
1680 },
1681
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301682 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301683 var invoices = [];
1684 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301685 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301686 if (docs) {
1687 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301688 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301689 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301690 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301691 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301692 return data
1693 }
1694 }
1695 });
1696 }
1697
1698 return invoices
1699 },
1700
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301701 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301702 var me = this;
1703 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301704 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301705 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301706 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301707 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301708 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301709 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301710 }
1711 }
1712 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301713 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301714 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301715 this.update_localstorage();
1716 }
1717 },
1718
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301719 remove_email_queue_from_localstorage: function() {
1720 var me = this;
1721 this.email_queue = this.get_email_queue()
1722 if (this.removed_email) {
1723 $.each(this.email_queue_list, function (index, data) {
1724 if (in_list(me.removed_email, index)) {
1725 delete me.email_queue[index]
1726 }
1727 })
1728 this.update_email_queue();
1729 }
1730 },
1731
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301732 remove_customer_from_localstorage: function() {
1733 var me = this;
1734 this.customer_details = this.get_customers_details()
1735 if (this.removed_customers) {
1736 $.each(this.customers_list, function (index, data) {
1737 if (in_list(me.removed_customers, index)) {
1738 delete me.customer_details[index]
1739 }
1740 })
1741 this.update_customer_in_localstorage();
1742 }
1743 },
1744
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301745 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301746 var me = this;
1747 this.customer_validate();
1748 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301749 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301750 },
1751
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301752 item_validate: function () {
1753 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301754 frappe.throw(__("Select items to save the invoice"))
1755 }
1756 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301757
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301758 validate_mode_of_payments: function () {
1759 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301760 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1761 }
1762 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301763
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301764 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301765 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301766 var item_code = ''
1767 var serial_no = '';
1768 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301769 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301770 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301771 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301772
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301773 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301774 this.refresh();
1775 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1776 'item': this.items[0].item_code
1777 })))
1778 }
1779
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301780 if (item_code && serial_no) {
1781 $.each(this.frm.doc.items, function (index, data) {
1782 if (data.item_code == item_code) {
1783 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301784 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1785 'serial_no': serial_no
1786 })))
1787 }
1788 }
1789 })
1790 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301791 },
1792
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301793 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301794 var me = this;
1795 if (args.item_code == item_code && args.serial_no
1796 && field == 'qty' && cint(value) != value) {
1797 args.qty = 0.0;
1798 this.refresh();
1799 frappe.throw(__("Serial no item cannot be a fraction"))
1800 }
1801
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301802 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301803 args.qty = 0.0;
1804 args.serial_no = ''
1805 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301806 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1807 'item': item_code
1808 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301809 }
1810 },
1811
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301812 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301813 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301814 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301815 frappe.prompt([{
1816 'fieldname': 'batch',
1817 'fieldtype': 'Select',
1818 'label': __('Batch No'),
1819 'reqd': 1,
1820 'options': this.batch_no_data[this.items[0].item_code]
1821 }],
1822 function(values){
1823 me.item_batch_no[me.items[0].item_code] = values.batch;
1824 },
1825 __('Select Batch No'))
1826 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301827 },
1828
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301829 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301830 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301831 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301832 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301833 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301834 if (pricing_rule.length) {
1835 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301836 item.margin_type = pricing_rule[0].margin_type;
1837 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1838 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1839 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301840 me.apply_pricing_rule_on_item(item)
1841 } else if (item.pricing_rule) {
1842 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301843 item.margin_rate_or_amount = 0.0;
1844 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301845 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301846 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301847 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001848
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301849 if(item.discount_percentage > 0) {
1850 me.apply_pricing_rule_on_item(item)
1851 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301852 })
1853 },
1854
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301855 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301856 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301857 return $.grep(this.pricing_rules, function (data) {
1858 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301859 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301860 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301861 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301862 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301863 return true
1864 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301865 }
1866 }
1867 })
1868 },
1869
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301870 validate_item_condition: function (data, item) {
1871 var apply_on = frappe.model.scrub(data.apply_on);
1872
1873 return (data.apply_on == 'Item Group')
1874 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1875 },
1876
1877 validate_item_group: function (pr_item_group, cart_item_group) {
1878 //pr_item_group = pricing rule's item group
1879 //cart_item_group = cart item's item group
1880 //this.item_groups has information about item group's lft and rgt
1881 //for example: {'Foods': [12, 19]}
1882
1883 pr_item_group = this.item_groups[pr_item_group]
1884 cart_item_group = this.item_groups[cart_item_group]
1885
1886 return (cart_item_group[0] >= pr_item_group[0] &&
1887 cart_item_group[1] <= pr_item_group[1])
1888 },
1889
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301890 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301891 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301892 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301893 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301894 return true
1895 }
1896 },
1897
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301898 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301899 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301900 'Customer': [data.customer, [this.frm.doc.customer]],
1901 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1902 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301903 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301904 }
1905 },
1906
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301907 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301908 //This method validate duplicate pricing rule
1909 var pricing_rule_name = '';
1910 var priority = 0;
1911 var pricing_rule_list = [];
1912 var priority_list = []
1913
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301914 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301915
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301916 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301917 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301918 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301919 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301920 priority = data.priority
1921 pricing_rule_list.push(data)
1922 }
1923 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301924
Faris Ansariab74ca72017-05-30 12:54:42 +05301925 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301926 $.each(priority_list, function (index, value) {
1927 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301928 count++
1929 }
1930 })
1931
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301932 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301933 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1934 'pricing_rule': pricing_rule_name
1935 })))
1936 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301937
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301938 return pricing_rule_list
1939 }
1940 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301941
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301942 validate_warehouse: function () {
1943 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 +05301944 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301945 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301946 },
1947
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301948 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301949 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301950
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301951 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301952 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301953 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301954 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301955 }
1956
1957 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301958 },
1959
1960 update_customer_in_localstorage: function() {
1961 var me = this;
1962 try {
1963 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1964 } catch (e) {
1965 frappe.throw(__("LocalStorage is full , did not save"))
1966 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301967 }
Faris Ansari185762a2017-04-13 12:36:08 +05301968})