blob: c442062ab6ffd9160bf7064441304001a1ac9c79 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
Faris Ansaric70bbac2017-08-29 15:27:17 +053011 frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => {
Rohit Waghchaure1a779222017-09-11 11:49:25 +053012 if (r && r.use_pos_in_offline_mode && cint(r.use_pos_in_offline_mode)) {
Faris Ansaric70bbac2017-08-29 15:27:17 +053013 // offline
14 wrapper.pos = new erpnext.pos.PointOfSale(wrapper);
15 cur_pos = wrapper.pos;
16 } else {
17 // online
18 frappe.set_route('point-of-sale');
19 }
20 });
Rushabh Mehta72e17192014-08-08 15:30:49 +053021}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053022
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053023frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053024 window.onbeforeunload = function () {
25 return wrapper.pos.beforeunload()
26 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053027}
28
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053029erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053030 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053031 this.page_len = 20;
rohitwaghchaured2be55b2017-06-07 12:04:01 +053032 this.freeze = false;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053033 this.page = wrapper.page;
34 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053035 this.set_indicator();
36 this.onload();
37 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053038 this.bind_events();
Rohit Waghchaure75177c02017-03-16 15:21:21 +053039 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053040 this.si_docs = this.get_doc_from_localstorage();
41 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053042
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053043 beforeunload: function (e) {
44 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053045 e = e || window.event;
46
47 // For IE and Firefox prior to version 4
48 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053049 e.returnValue = __("You are in offline mode. You will not be able to reload until you have network.");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053050 return
51 }
52
53 // For Safari
54 return __("You are in offline mode. You will not be able to reload until you have network.");
55 }
56 },
57
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053058 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 var me = this;
60 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053061 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053063 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 },
65
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053066 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 var me = this;
68 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053069 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053070 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053071 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 method: "frappe.handler.ping",
73 callback: function (r) {
74 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053076 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 }
78 }
79 })
80 },
81
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053082 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053083 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053084 this.get_data_from_server(function () {
rohitwaghchaured2be55b2017-06-07 12:04:01 +053085 me.make_control();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 me.create_new();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +053087 me.make();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053088 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053089 },
90
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053091 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053092 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +053093 this.page.clear_menu();
Faris Ansari769b6ba2017-05-16 07:31:10 +053094
95 // for mobile
96 this.page.add_menu_item(__("Pay"), function () {
97 me.validate();
98 me.update_paid_amount_status(true);
99 me.create_invoice();
100 me.make_payment();
101 }).addClass('visible-xs');
102
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530103 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530104 me.save_previous_entry();
105 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530106 })
107
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530108 this.page.add_menu_item(__("Sync Master Data"), function () {
109 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530110 me.load_data(false);
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530111 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530112 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530113 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530114 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530115
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530116 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
rohitwaghchaure88491712017-10-02 12:13:36 +0530117 me.freeze_screen = true;
Rohit Waghchaure82be0202016-10-04 12:46:37 +0530118 me.sync_sales_invoice()
119 });
120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530121 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530122 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530123 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530124 },
125
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530126 email_prompt: function() {
127 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530128 var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530129 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
130 {fieldtype: "Section Break"},
131 {label:__("Subject"), fieldtype:"Data", reqd: 1,
132 fieldname:"subject",length:524288},
133 {fieldtype: "Section Break"},
134 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
135 fieldname:"content"},
136 {fieldtype: "Section Break"},
137 {fieldtype: "Column Break"}];
138
139 this.email_dialog = new frappe.ui.Dialog({
140 title: "Email",
141 fields: fields,
142 primary_action_label: __("Send"),
143 primary_action: function() {
144 me.send_action();
145 }
146 });
147
148 this.email_dialog.show()
149 },
150
151 send_action: function() {
152 this.email_queue = this.get_email_queue()
153 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
154 this.update_email_queue()
155 this.email_dialog.hide()
156 },
157
158 update_email_queue: function () {
159 try {
160 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
161 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530162 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530163 }
164 },
165
166 get_email_queue: function () {
167 try {
168 return JSON.parse(localStorage.getItem('email_queue')) || {};
169 } catch (e) {
170 return {}
171 }
172 },
173
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530174 get_customers_details: function () {
175 try {
176 return JSON.parse(localStorage.getItem('customer_details')) || {};
177 } catch (e) {
178 return {}
179 }
180 },
181
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530182 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530183 var me = this;
184
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530185 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530186 me.removed_items = [];
187 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530188 if ($(this).is(":checked")) {
189 $.each(me.si_docs, function (index, data) {
190 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530191 me.removed_items.push(key)
192 }
193 })
194 }
195
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530196 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530197 })
198
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530199 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530200 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530201 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530202 me.removed_items.push(me.name);
203 } else {
204 me.removed_items.pop(me.name)
205 }
206
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530207 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530208 })
209 },
210
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530211 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530212 var me = this;
213
214 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530215 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530216 this.frm.doc = doc_data[0][this.name];
217 this.set_missing_values();
218 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530219 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530220 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530221 }
222 },
223
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530224 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530225 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530226 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530227 this.remove_doc_from_localstorage()
228 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530229 // this.dialog_actions();
230 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530231 },
232
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530233 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530234 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530235 this.si_docs = this.get_submitted_invoice()
236 $.each(this.removed_items, function(index, name){
237 $.each(me.si_docs, function(key, data){
238 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
239 frappe.throw(__("Submitted orders can not be deleted"))
240 }
241 })
242 })
243 },
244
245 toggle_delete_button: function () {
246 var me = this;
247 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530248 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530249 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530250 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530251 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530252 }
253 }
254 },
255
256 get_doctype_status: function (doc) {
257 if (doc.docstatus == 0) {
258 return { status: "Draft", indicator: "red" }
259 } else if (doc.outstanding_amount == 0) {
260 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530261 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530262 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530263 }
264 },
265
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530266 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530267 var me = this;
268 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530269 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530270 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530271 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530272 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530273
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530274 this.set_customer_value_in_party_field();
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
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530285 set_customer_value_in_party_field: function() {
286 if (this.frm.doc.customer) {
287 this.party_field.$input.val(this.frm.doc.customer);
288 }
289 },
290
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530291 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530292 var me = this;
293 this.si_docs = this.get_doc_from_localstorage();
294
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530295 return $.grep(this.si_docs, function (data) {
296 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530297 return key == me.name
298 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530299 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530300 },
301
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530302 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530303 var me = this;
304 frappe.call({
305 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
306 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530307 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530308 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530309 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530310 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530311 me.set_interval_for_si_sync();
312 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530313 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530314 callback();
315 }
316 }
317 })
318 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530319
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530320 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530321 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530322 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530323 this.meta = r.message.meta;
324 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530325 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530326 this.customers = r.message.customers;
327 this.serial_no_data = r.message.serial_no_data;
328 this.batch_no_data = r.message.batch_no_data;
329 this.tax_data = r.message.tax_data;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530330 this.contacts = r.message.contacts;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530331 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530332 this.price_list_data = r.message.price_list_data;
333 this.bin_data = r.message.bin_data;
334 this.pricing_rules = r.message.pricing_rules;
335 this.print_template = r.message.print_template;
336 this.pos_profile_data = r.message.pos_profile;
337 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530338 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530339 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 +0530340 },
341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 save_previous_entry: function () {
343 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
344 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530345 }
346 },
347
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530348 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530349 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530350 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530351 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530352 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 this.setup();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530354 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530355 },
356
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530357 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530358 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530359
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530360 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530361 this.actual_qty_dict = {};
362
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530363 if (load_doc) {
364 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530365 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530366
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530367 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530368 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530369 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530370 })
371
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530372 this.print_template_data = frappe.render_template("print_template", {
373 content: this.print_template,
Faris Ansari29d64ca2017-05-23 17:12:51 +0530374 title: "POS",
375 base_url: frappe.urllib.get_base_url(),
376 print_css: frappe.boot.print_css,
377 print_settings: this.print_settings,
378 header: this.letter_head.header,
379 footer: this.letter_head.footer,
Makarand Bauskar724cc352017-05-23 17:43:42 +0530380 landscape: false,
381 columns: []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530382 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530383 },
384
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530385 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530386 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530387 this.party_field.$input.attr('disabled', false);
388 if(this.selected_row) {
389 this.selected_row.hide()
390 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530391 },
392
Rohit Waghchaure61165122017-05-02 13:04:08 +0530393 set_default_customer: function() {
394 if (this.default_customer && !this.frm.doc.customer) {
395 this.party_field.$input.val(this.default_customer);
396 this.frm.doc.customer = this.default_customer;
397 this.numeric_keypad.show();
398 this.toggle_list_customer(false)
399 this.toggle_item_cart(true)
400 }
401 },
402
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530403 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530404 var me = this;
405 this.party = party;
406 this.price_list = (party == "Customer" ?
407 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
408 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
409 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
410 },
411
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530412 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530413 this.make_item_list();
414 this.make_discount_field()
415 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200416
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530417 make_control: function() {
418 this.frm = {}
419 this.frm.doc = this.doc
420 this.set_transaction_defaults("Customer");
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530421 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 +0530422 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530423 this.make_search();
424 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530425 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530426 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530427 },
428
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530429 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530430 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530431 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530432 df: {
433 "fieldtype": "Data",
434 "label": "Item",
435 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530436 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530437 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530438 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530439 only_input: true,
440 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530441
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530442 this.serach_item.make_input();
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530443
Revant Nandgaonkar2f7cb822017-08-26 09:58:15 +0530444 this.serach_item.$input.on("keypress", function (event) {
Faris Ansari8fbc08f2017-08-28 16:52:20 +0530445
446 clearTimeout(me.last_search_timeout);
447 me.last_search_timeout = setTimeout(() => {
448 if((me.serach_item.$input.val() != "") || (event.which == 13)) {
449 me.items = me.get_items();
450 me.make_item_list();
451 }
452 }, 400);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530453 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530454
Faris Ansari339d9c92017-03-07 18:14:06 +0530455 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530456 sorted_item_groups = this.get_sorted_item_groups()
457 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530458 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
459 }).join("");
460
461 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
462
463 this.search_item_group.on('click', '.dropdown-menu a', function() {
464 me.selected_item_group = $(this).attr('data-value');
465 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
466
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530467 me.page_len = 20;
468 me.items = me.get_items();
469 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530470 })
471
Faris Ansari45510102017-03-09 14:43:37 +0530472 me.toggle_more_btn();
473
474 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530475 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530476 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530477 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530478 me.toggle_more_btn();
479 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530480
Faris Ansari45510102017-03-09 14:43:37 +0530481 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530482 me.update_customer()
483 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530484 },
485
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530486 get_sorted_item_groups: function() {
487 list = {}
488 $.each(this.item_groups, function(i, data) {
489 list[i] = data[0]
490 })
491
492 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
493 },
494
Faris Ansari45510102017-03-09 14:43:37 +0530495 toggle_more_btn: function() {
496 if(!this.items || this.items.length <= this.page_len) {
497 this.wrapper.find(".btn-more").hide();
498 } else {
499 this.wrapper.find(".btn-more").show();
500 }
501 },
502
503 toggle_totals_area: function(show) {
504
Faris Ansari07c9f352017-03-09 17:03:14 +0530505 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530506 show = this.is_totals_area_collapsed;
507 }
508
509 var totals_area = this.wrapper.find('.totals-area');
510 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
511 .toggle(show);
512
513 if(show) {
514 totals_area.find('.collapse-btn i')
515 .removeClass('octicon-chevron-down')
516 .addClass('octicon-chevron-up');
517 } else {
518 totals_area.find('.collapse-btn i')
519 .removeClass('octicon-chevron-up')
520 .addClass('octicon-chevron-down');
521 }
522
523 this.is_totals_area_collapsed = !show;
524 },
525
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530526 make_list_customers: function () {
527 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530528 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530529 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530530 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530531 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530532 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530533 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530534
Faris Ansari45510102017-03-09 14:43:37 +0530535 me.render_list_customers();
536 me.toggle_totals_area(false);
537
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530538 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530539 $(this).toggleClass("view_customer");
540 if($(this).hasClass("view_customer")) {
541 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530542 me.list_customers.show();
543 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530544 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530545 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530546 } else {
547 if(me.frm.doc.docstatus == 0) {
548 me.party_field.$input.attr('disabled', false);
549 }
550 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530551 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530552 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530553 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530554 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530555 }
556 });
557 this.add_customer_btn.on('click', function() {
558 me.save_previous_entry();
559 me.create_new();
560 me.refresh();
561 me.set_focus();
562 });
Faris Ansari45510102017-03-09 14:43:37 +0530563 this.pos_bill.on('click', '.collapse-btn', function() {
564 me.toggle_totals_area();
565 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530566 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200567
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530568 bind_numeric_keypad: function() {
569 var me = this;
570 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
571 me.numeric_val = '';
572 })
573
574 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
575 me.numeric_id = $(this).attr("id") || me.numeric_id;
576 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530577 if(me.numeric_id) {
578 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
579 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530580
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530581 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530582 me.numeric_val += me.val;
583 me.selected_field.val(flt(me.numeric_val))
584 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530585 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530586 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530587
588 if(me.numeric_id && $(this).hasClass('pos-operation')) {
589 me.numeric_keypad.find('button.pos-operation').removeClass('active');
590 $(this).addClass('active');
591
592 me.selected_row.find('.pos-list-row').removeClass('active');
593 me.selected_field.closest('.pos-list-row').addClass('active');
594 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530595 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200596
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530597 $(this.numeric_keypad).find('.numeric-del').click(function(){
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530598 if(me.numeric_id) {
599 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
600 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
601 me.selected_field.val(me.numeric_val);
602 me.selected_field.trigger("change")
603 } else {
604 //Remove an item from the cart, if focus is at selected item
605 me.remove_selected_item()
606 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530607 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +0200608
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530609 $(this.numeric_keypad).find('.pos-pay').click(function(){
610 me.validate();
611 me.update_paid_amount_status(true);
612 me.create_invoice();
613 me.make_payment();
614 })
615 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530616
rohitwaghchaure8f51a5e2017-06-23 18:17:04 +0530617 remove_selected_item: function() {
618 this.remove_item = []
619 idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx")
620 this.remove_item.push(idx)
621 this.remove_zero_qty_item()
622 this.update_paid_amount_status(false)
623 },
624
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530625 render_list_customers: function () {
626 var me = this;
627
628 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530629 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530630 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530631 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530632 return;
633 }
Faris Ansari45510102017-03-09 14:43:37 +0530634
635 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530636 if(this.si_docs.length) {
637 this.si_docs.forEach(function (data, i) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530638 for (var key in data) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530639 html += frappe.render_template("pos_invoice_list", {
640 sr: i + 1,
641 name: key,
642 customer: data[key].customer,
643 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
644 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
645 data: me.get_doctype_status(data[key])
646 });
647 }
648 });
649 }
Faris Ansari45510102017-03-09 14:43:37 +0530650 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530651
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530652 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530653 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530654 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530655 me.list_customers_btn.toggleClass("view_customer");
656 me.pos_bill.show();
657 me.list_customers_btn.show();
658 me.name = $(this).parents().attr('invoice-name')
659 me.edit_record();
660 })
661
662 //actions
663 $(this.wrapper).find('.list-select-all').click(function () {
664 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
665 me.removed_items = [];
666 if ($(this).is(":checked")) {
667 $.each(me.si_docs, function (index, data) {
668 for (key in data) {
669 me.removed_items.push(key)
670 }
671 });
672 }
673
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530674 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530675 });
676
677 $(this.wrapper).find('.list-delete').click(function () {
678 me.name = $(this).parent().parent().attr('invoice-name');
679 if ($(this).is(":checked")) {
680 me.removed_items.push(me.name);
681 } else {
682 me.removed_items.pop(me.name)
683 }
684
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530685 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530686 });
687 },
688
689 bind_delete_event: function() {
690 var me = this;
691
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530692 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530693 frappe.confirm(__("Delete permanently?"), function () {
694 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530695 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530696 me.render_list_customers();
697 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530698 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530699 },
700
701 set_focus: function () {
702 if (this.default_customer || this.frm.doc.customer) {
rohitwaghchaure9a0db392017-09-14 10:51:21 +0530703 this.set_customer_value_in_party_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530704 this.serach_item.$input.focus();
705 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530706 this.party_field.$input.focus();
707 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 },
709
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530710 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530711 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530712
713 if(!this.party_field) {
714 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
715 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530716 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530717 })).insertAfter(this.page.$title_area.hide());
718 }
719
720 this.party_field = frappe.ui.form.make_control({
721 df: {
722 "fieldtype": "Data",
723 "options": this.party,
724 "label": this.party,
725 "fieldname": this.party.toLowerCase(),
726 "placeholder": __("Select or add new customer")
727 },
728 parent: this.page.wrapper.find(".party-area"),
729 only_input: true,
730 });
731
732 this.party_field.make_input();
733 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530734 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530735 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530736
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530737 this.party_field.awesomeplete =
738 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530739 minChars: 0,
740 maxItems: 99,
741 autoFirst: true,
742 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530743 filter: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530744 if (item.value.includes('is_action')) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530745 return true;
746 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530747
748 input = input.toLowerCase();
749 item = this.get_item(item.value);
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530750 result = item ? item.searchtext.includes(input) : '';
751 if(!result) {
752 me.prepare_customer_mapper(input);
753 } else {
754 return result;
755 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530756 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530757 item: function (item, input) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530758 var d = this.get_item(item.value);
Faris Ansari45510102017-03-09 14:43:37 +0530759 var html = "<span>" + __(d.label || d.value) + "</span>";
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530760 if(d.customer_name) {
761 html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>';
762 }
763
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530764 return $('<li></li>')
765 .data('item.autocomplete', d)
766 .html('<a><p>' + html + '</p></a>')
767 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530768 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530769 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530770
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530771 this.prepare_customer_mapper()
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530772 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530773
774 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530775 .on('input', function (e) {
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530776 if(me.customers_mapper.length <= 1) {
777 me.prepare_customer_mapper(e.target.value);
778 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530779 me.party_field.awesomeplete.list = me.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530780 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530781 .on('awesomplete-select', function (e) {
782 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530783 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530784 if (!customer) return;
785 // create customer link
786 if (customer.action) {
787 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530788 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530789 }
Faris Ansari45510102017-03-09 14:43:37 +0530790 me.toggle_list_customer(false);
791 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530792 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530793 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530794 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530795 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530796 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530797 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530798 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530799 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530800
801 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530802 me.toggle_list_customer(false)
803 me.toggle_item_cart(true)
804 } else {
805 me.toggle_list_customer(true)
806 me.toggle_item_cart(false)
807 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530808 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530809 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530810 var item = me.party_field.awesomeplete
811 .get_item(e.originalEvent.text.value);
812 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530813 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530814 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530815 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530816 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530817 },
818
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530819 prepare_customer_mapper: function(key) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530820 var me = this;
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530821 var customer_data = '';
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530822
Rohit Waghchaure06b22e92017-09-06 19:45:36 +0530823 if (key) {
824 key = key.toLowerCase().trim();
825 var re = new RegExp('%', 'g');
826 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'));
827
828 customer_data = $.grep(this.customers, function(data) {
829 contact = me.contacts[data.name];
830 if(reg.test(data.name.toLowerCase())
831 || reg.test(data.customer_name.toLowerCase())
832 || (contact && reg.test(contact["mobile_no"]))
833 || (contact && reg.test(contact["phone"]))
834 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){
835 return data;
836 }
837 })
838 } else {
839 customer_data = this.customers;
840 }
841
842 this.customers_mapper = [];
843
844 customer_data.forEach(function (c, index) {
845 if(index < 30) {
846 contact = me.contacts[c.name];
847 if(contact && !c['phone']) {
848 c["phone"] = contact["phone"];
849 c["email_id"] = contact["email_id"];
850 c["mobile_no"] = contact["mobile_no"];
851 }
852
853 me.customers_mapper.push({
854 label: c.name,
855 value: c.name,
856 customer_name: c.customer_name,
857 customer_group: c.customer_group,
858 territory: c.territory,
859 phone: contact ? contact["phone"] : '',
860 mobile_no: contact ? contact["mobile_no"] : '',
861 email_id: contact ? contact["email_id"] : '',
862 searchtext: ['customer_name', 'customer_group', 'name', 'value',
863 'label', 'email_id', 'phone', 'mobile_no']
864 .map(key => c[key]).join(' ')
865 .toLowerCase()
866 });
867 } else {
868 return;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530869 }
870 });
871
872 this.customers_mapper.push({
873 label: "<span class='text-primary link-option'>"
874 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
875 + __("Create a new Customer")
876 + "</span>",
877 value: 'is_action',
878 action: me.add_customer
879 });
880 },
881
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530882 autocomplete_customers: function() {
883 this.party_field.awesomeplete.list = this.customers_mapper;
884 },
885
Faris Ansari45510102017-03-09 14:43:37 +0530886 toggle_edit_button: function(flag) {
887 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
888 },
889
890 toggle_list_customer: function(flag) {
891 this.list_customers.toggle(flag);
892 },
893
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530894 toggle_item_cart: function(flag) {
895 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
896 },
897
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530898 add_customer: function() {
899 this.frm.doc.customer = "";
rohitwaghchaure09483d32017-05-16 08:51:24 +0530900 this.update_customer(true);
901 this.numeric_keypad.show();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530902 },
903
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530904 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530905 var me = this;
Rushabh Mehta37146262017-02-01 18:17:35 +0530906
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530907 this.customer_doc = new frappe.ui.Dialog({
908 'title': 'Customer',
909 fields: [
910 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530911 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530912 "fieldname": "full_name",
913 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530914 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530915 },
916 {
917 "fieldtype": "Section Break"
918 },
919 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530920 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530921 "fieldname": "email_id",
922 "fieldtype": "Data"
923 },
924 {
925 "fieldtype": "Column Break"
926 },
927 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530928 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530929 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530930 "fieldtype": "Data"
931 },
932 {
933 "fieldtype": "Section Break"
934 },
935 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530936 "label": __("Address Name"),
937 "read_only": 1,
938 "fieldname": "name",
939 "fieldtype": "Data"
940 },
941 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530942 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530943 "fieldname": "address_line1",
944 "fieldtype": "Data"
945 },
946 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530947 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530948 "fieldname": "address_line2",
949 "fieldtype": "Data"
950 },
951 {
952 "fieldtype": "Column Break"
953 },
954 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530955 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530956 "fieldname": "city",
957 "fieldtype": "Data"
958 },
959 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530960 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530961 "fieldname": "state",
962 "fieldtype": "Data"
963 },
964 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530965 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530966 "fieldname": "pincode",
967 "fieldtype": "Data"
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530968 },
969 {
970 "label": __("Customer POS Id"),
971 "fieldname": "customer_pos_id",
972 "fieldtype": "Data",
973 "hidden": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530974 }
975 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530976 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530977 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530978 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530979
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530980 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530981 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530982 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530983 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530984 });
985 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530986
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530987 render_address_data: function() {
988 var me = this;
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530989 this.address_data = this.address[this.frm.doc.customer] || {};
990 if(!this.address_data.email_id || !this.address_data.phone) {
991 this.address_data = this.contacts[this.frm.doc.customer];
992 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530993
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530994 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530995 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
996 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530997 }
rohitwaghchaured2be55b2017-06-07 12:04:01 +0530998
999 if(!this.customer_doc.fields_dict.customer_pos_id.value) {
1000 this.customer_doc.set_value("customer_pos_id", $.now())
1001 }
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301002 },
1003
1004 get_address_from_localstorage: function() {
1005 this.address_details = this.get_customers_details()
1006 return this.address_details[this.frm.doc.customer]
1007 },
1008
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301009 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301010 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301011 this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301012 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301013 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301014 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301015 this.update_address_and_customer_list(new_customer)
1016 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301017 this.update_customer_in_localstorage()
1018 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301019 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301020 },
1021
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301022 update_address_and_customer_list: function(new_customer) {
1023 var me = this;
1024 if(new_customer) {
1025 this.customers_mapper.push({
1026 label: this.frm.doc.customer,
1027 value: this.frm.doc.customer,
1028 customer_group: "",
1029 territory: ""
1030 });
1031 }
1032
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301033 this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details())
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301034 },
1035
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301036 get_prompt_details: function() {
1037 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301038 this.prompt_details['country'] = this.pos_profile_data.country;
rohitwaghchauree2176b82017-07-28 20:52:02 +05301039 this.prompt_details['territory'] = this.pos_profile_data["territory"];
1040 this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"];
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301041 this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301042 return JSON.stringify(this.prompt_details)
1043 },
1044
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301045 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +05301046 var me = this;
1047 this.frm.doc.customer = doc.label || doc.name;
1048 this.frm.doc.customer_name = doc.customer_name;
1049 this.frm.doc.customer_group = doc.customer_group;
1050 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301051 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301052 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301053 },
1054
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301055 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301056 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301057 if (!this.price_list) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301058 frappe.msgprint(__("Price List not found or disabled"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301059 return;
1060 }
1061
1062 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301063
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301064 var $wrap = me.wrapper.find(".item-list");
1065 me.wrapper.find(".item-list").empty();
1066
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301067 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301068 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301069 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301070 $(frappe.render_template("pos_item", {
1071 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301072 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301073 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +05301074 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +05301075 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301076 color: frappe.get_palette(obj.item_name),
1077 abbr: frappe.get_abbr(obj.item_name)
1078 })).tooltip().appendTo($wrap);
1079 }
1080 });
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001081
Faris Ansari45510102017-03-09 14:43:37 +05301082 $wrap.append(`
1083 <div class="image-view-item btn-more text-muted text-center">
1084 <div class="image-view-body">
1085 <i class="mega-octicon octicon-package"></i>
1086 <div>Load more items</div>
1087 </div>
1088 </div>
1089 `);
1090
1091 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +05301092 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +05301093 $("<p class='text-muted small' style='padding-left: 10px'>"
1094 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301095 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301096
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301097 if (this.items.length == 1
1098 && this.serach_item.$input.val()) {
1099 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301100 this.add_to_cart();
1101 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301102 },
1103
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301104 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301105 // To search item as per the key enter
1106
1107 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301108 this.item_serial_no = {};
1109 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301110
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301111 if (item_code) {
1112 return $.grep(this.item_data, function (item) {
1113 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301114 return true
1115 }
1116 })
1117 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301118
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301119 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301120
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301121 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301122 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301123 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301124 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301125
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301126 if (key) {
1127 return $.grep(this.items_list, function (item) {
1128 if (search_status) {
1129 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301130 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301131 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1132 } else if (me.serial_no_data[item.item_code]
1133 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301134 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301135 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 +05301136 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301137 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301138 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301139 return item.barcode == me.serach_item.$input.val();
rohitwaghchaure860144f2017-07-12 15:01:56 +05301140 } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) ||
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301141 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301142 return true
1143 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301144 }
1145 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301146 } else {
1147 return this.items_list;
1148 }
1149 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301150
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301151 apply_category: function() {
1152 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301153 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301154
1155 if(category == 'All Item Groups') {
1156 return this.item_data
1157 } else {
1158 return this.item_data.filter(function(element, index, array){
1159 return element.item_group == category;
1160 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301161 }
1162 },
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001163
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301164 bind_items_event: function() {
1165 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301166 $(this.wrapper).on('click', '.pos-bill-item', function() {
1167 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1168 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301169 me.numeric_val = "";
1170 me.numeric_id = ""
1171 me.item_code = $(this).attr("data-item-code");
1172 me.render_selected_item()
1173 me.bind_qty_event()
1174 me.update_rate()
1175 $(me.wrapper).find(".selected-item").scrollTop(1000);
1176 })
1177 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301178
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301179 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301180 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301181
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301182 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301183 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301184 var qty = $(this).val();
1185 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301186 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301187 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301188
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301189 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301190 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1191 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301192 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301193 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301194
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301195 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301196 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1197 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301198 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301199 })
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001200
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301201 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301202 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301203 var discount = $(this).val();
1204 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301205 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301206 })
1207 },
1208
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301209 bind_events: function() {
1210 var me = this;
1211 // if form is local then allow this function
1212 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1213 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301214 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301215 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301216 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301217
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301218 if (me.frm.doc.docstatus == 0) {
1219 me.items = me.get_items($(this).attr("data-item-code"))
1220 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301221 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301222 }
1223 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301224
1225 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301226 },
1227
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301228 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301229 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301230 this.items = this.get_items(item_code);
1231 this.validate_serial_no()
1232 this.set_item_details(item_code, "qty", qty);
1233 },
1234
1235 update_discount: function(item_code, discount) {
1236 var me = this;
1237 this.items = this.get_items(item_code);
1238 this.set_item_details(item_code, "discount_percentage", discount);
1239 },
1240
1241 update_rate: function () {
1242 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301243 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301244 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301245 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301246 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301247 })
1248 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301249
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301250 update_value: function() {
1251 var me = this;
1252 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1253 "rate": ".pos-item-price", "amount": ".pos-amount"}
1254 this.child_doc = this.get_child_item(this.item_code);
1255
1256 if(me.child_doc.length) {
1257 $.each(fields, function(key, field) {
1258 $(me.selected_row).find(field).val(me.child_doc[0][key])
1259 })
1260 } else {
1261 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301262 }
1263 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301264
1265 clear_selected_row: function() {
1266 $(this.wrapper).find('.selected-item').empty();
1267 },
1268
1269 render_selected_item: function() {
1270 this.child_doc = this.get_child_item(this.item_code);
1271 $(this.wrapper).find('.selected-item').empty();
1272 if(this.child_doc.length) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301273 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 +05301274 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1275 $(this.wrapper).find('.selected-item').html(this.selected_row)
1276 }
1277
1278 $(this.selected_row).find('.form-control').click(function(){
1279 $(this).select();
1280 })
1281 },
1282
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301283 get_child_item: function(item_code) {
1284 var me = this;
1285 return $.map(me.frm.doc.items, function(doc){
1286 if(doc.item_code == item_code) {
1287 return doc
1288 }
1289 })
1290 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301291
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301292 set_item_details: function (item_code, field, value) {
1293 var me = this;
1294 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301295 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301296 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301297
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301298 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301299 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301300 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301301 if (d.serial_no && field == 'qty') {
1302 me.validate_serial_no_qty(d, item_code, field, value)
1303 }
1304
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301305 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301306 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301307 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301308 me.remove_item.push(d.idx)
1309 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301310
1311 if(field=="discount_percentage" && value == 0) {
1312 d.rate = d.price_list_rate;
1313 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301314 }
1315 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301316
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301317 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301318 this.remove_zero_qty_item();
1319 }
1320
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301321 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301322 },
1323
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301324 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301325 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301326 var idx = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301327 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301328 $.each(this.frm.doc["items"] || [], function (i, d) {
1329 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301330 d.idx = idx;
1331 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301332 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301333 }
1334 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301335
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301336 this.frm.doc["items"] = this.items;
1337 },
1338
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301339 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301340 var me = this;
1341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301342 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301343 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301344
1345 if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) {
1346 // Reset discount amount
1347 me.frm.doc.discount_amount = 0;
1348 }
1349
Faris Ansariab74ca72017-05-30 12:54:42 +05301350 var total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301351
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301352 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301353 total = me.frm.doc.net_total
1354 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301355
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301356 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 +05301357 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301358 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301359 });
1360
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301361 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301362 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1363 me.frm.doc.additional_discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301364 me.refresh();
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301365 me.wrapper.find('input.discount-percentage').val(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301366 });
1367 },
1368
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301369 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301370 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301371 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301372 frappe.throw(__("Please select customer"))
1373 }
1374 },
1375
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301376 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301377 var me = this;
1378 var caught = false;
1379 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1380
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301381 this.customer_validate();
1382 this.mandatory_batch_no();
1383 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301384 this.validate_warehouse();
1385
1386 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301387 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301388 if (d.item_code == me.items[0].item_code) {
1389 caught = true;
1390 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301391 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301392 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301393 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1394 d.warehouse = me.item_serial_no[d.item_code][1]
1395 }
1396
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301397 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301398 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301399 }
1400 }
1401 });
1402 }
1403
1404 // if item not found then add new item
1405 if (!caught)
1406 this.add_new_item_to_grid();
1407
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301408 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301409 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301410 },
1411
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301412 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301413 var me = this;
1414 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1415 this.child.item_code = this.items[0].item_code;
1416 this.child.item_name = this.items[0].item_name;
1417 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301418 this.child.brand = this.items[0].brand;
rohitwaghchaure860144f2017-07-12 15:01:56 +05301419 this.child.description = this.items[0].description || this.items[0].item_name;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301420 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301421 this.child.qty = 1;
1422 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301423 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1424 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301425 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301426 ? 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 +05301427 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1428 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1429 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301430 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301431 this.child.batch_no = this.item_batch_no[this.child.item_code];
1432 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1433 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301434 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301435 },
1436
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301437 update_paid_amount_status: function (update_paid_amount) {
1438 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301439 update_paid_amount = update_paid_amount ? false : true;
1440 }
1441
1442 this.refresh(update_paid_amount);
1443 },
1444
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301445 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301446 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301447 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 this.set_primary_action();
1449 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301450
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301451 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301452 this.apply_pricing_rule();
1453 this.discount_amount_applied = false;
1454 this._calculate_taxes_and_totals();
1455 this.calculate_discount_amount();
1456 this.show_items_in_item_cart();
1457 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301458 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301459 this.set_totals();
1460 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301461
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301462 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301463 return erpnext.get_currency(this.frm.doc.company);
1464 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301465
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301466 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301467 var me = this;
1468 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301469 var $no_items_message = this.wrapper.find(".no-items-message");
1470 $no_items_message.toggle(this.frm.doc.items.length === 0);
1471
1472 var $totals_area = this.wrapper.find('.totals-area');
1473 $totals_area.toggle(this.frm.doc.items.length > 0);
1474
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301475 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301476 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301477 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301478 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301480 discount_percentage: d.discount_percentage || 0.0,
1481 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301482 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301483 rate: format_currency(d.rate, me.frm.doc.currency),
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301484 amount: format_currency(d.amount, me.frm.doc.currency),
1485 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301486 })).appendTo($items);
1487 });
1488
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301489 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301490 $(this).select();
1491 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301492
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301493 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301494 $(this).select();
1495 });
1496
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301497 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301498 $(this).select();
1499 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301500 },
1501
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301502 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301503 var me = this;
1504 me.frm.doc.total_taxes_and_charges = 0.0
1505
1506 var taxes = this.frm.doc.taxes || [];
1507 $(this.wrapper)
1508 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1509 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301510
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301511 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301512 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1513 $(frappe.render_template("pos_tax_row", {
1514 description: d.description,
1515 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1516 me.frm.doc.currency)
1517 })).appendTo(me.wrapper.find(".tax-table"));
1518 }
1519 });
1520 },
1521
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301522 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301523 var me = this;
1524 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1525 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
rohitwaghchaure5fdd26f2017-10-21 11:23:45 +05301526 this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
1527 this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301528 },
1529
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301530 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301531 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301532 this.page.set_primary_action(__("New Cart"), function () {
1533 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301534 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301535 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301536
1537 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301538 this.page.set_secondary_action(__("Print"), function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301539 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301540 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301541 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301542 this.page.add_menu_item(__("Email"), function () {
1543 me.email_prompt()
1544 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301545 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301546 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301547
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301548 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301549 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301550 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301551 this.save_previous_entry();
1552 this.create_new();
1553 this.refresh();
1554 this.toggle_input_field();
1555 this.render_list_customers();
1556 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301557 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301558
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301559 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301560 var me = this;
1561
mbauskar66e2a512017-06-22 16:18:12 +05301562 this.msgprint = frappe.msgprint(
Faris Ansariab74ca72017-05-30 12:54:42 +05301563 `<a class="btn btn-primary print_doc"
1564 style="margin-right: 5px;">${__('Print')}</a>
1565 <a class="btn btn-default new_doc">${__('New')}</a>`);
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301566
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301567 $('.print_doc').click(function () {
Faris Ansariab74ca72017-05-30 12:54:42 +05301568 var html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301569 me.print_document(html)
1570 })
1571
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301572 $('.new_doc').click(function () {
mbauskar66e2a512017-06-22 16:18:12 +05301573 me.msgprint.hide()
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301574 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301575 })
1576 },
1577
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301578 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301579 var w = window.open();
1580 w.document.write(html);
1581 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301582 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301583 w.print();
1584 w.close();
1585 }, 1000)
1586 },
1587
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301588 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301589 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301590 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301591 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301592 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301593 this.print_dialog()
1594 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301595 },
1596
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301597 update_serial_no: function() {
1598 var me = this;
1599
1600 //Remove the sold serial no from the cache
1601 $.each(this.frm.doc.items, function(index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301602 var sn = data.serial_no.split('\n')
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301603 if(sn.length) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301604 var serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301605 if(serial_no_list) {
1606 $.each(sn, function(i, serial_no) {
1607 if(in_list(Object.keys(serial_no_list), serial_no)) {
1608 delete serial_no_list[serial_no]
1609 }
1610 })
1611 me.serial_no_data[data.item_code] = serial_no_list;
1612 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301613 }
1614 })
1615 },
1616
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301617 change_status: function () {
1618 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301619 this.frm.doc.docstatus = 1;
1620 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301621 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301622 }
1623 },
1624
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301625 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301626 var pointer_events = 'inherit'
Faris Ansariab74ca72017-05-30 12:54:42 +05301627 var disabled = this.frm.doc.docstatus == 1 ? true: false;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301628 $(this.wrapper).find('input').attr("disabled", disabled);
1629 $(this.wrapper).find('select').attr("disabled", disabled);
1630 $(this.wrapper).find('input').attr("disabled", disabled);
1631 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301632 $(this.wrapper).find('button').attr("disabled", disabled);
1633 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301634
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301635 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301636 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301637 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301638
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301639 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301640 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1641 this.set_primary_action();
1642 },
1643
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301644 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301645 var me = this;
1646 var invoice_data = {}
1647 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301648 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301649 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301650 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301651 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301652 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301653 this.frm.doc.posting_date = frappe.datetime.get_today();
1654 this.frm.doc.posting_time = frappe.datetime.now_time();
Charles-Henri Decultot2305eda2017-06-19 05:55:54 +02001655 this.frm.doc.pos_profile = this.pos_profile_data['name'];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301656 invoice_data[this.name] = this.frm.doc
1657 this.si_docs.push(invoice_data)
1658 this.update_localstorage();
1659 this.set_primary_action();
1660 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301661 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301662 },
1663
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301664 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301665 var me = this;
1666 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301667 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301668 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301669 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301670 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301671 me.update_localstorage();
1672 }
1673 }
1674 })
1675 },
1676
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301677 update_localstorage: function () {
1678 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301679 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301680 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301681 frappe.throw(__("LocalStorage is full , did not save"))
1682 }
1683 },
1684
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301685 get_doc_from_localstorage: function () {
1686 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301687 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301688 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301689 return []
1690 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301691 },
1692
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301693 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301694 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301695 setInterval(function () {
rohitwaghchaure88491712017-10-02 12:13:36 +05301696 me.freeze_screen = false;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301697 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301698 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301699 },
1700
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301701 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301702 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301703 this.si_docs = this.get_submitted_invoice() || [];
1704 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301705 this.customers_list = this.get_customers_details() || {};
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301706 if(this.customer_doc) {
1707 this.freeze = this.customer_doc.display
1708 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301709
rohitwaghchaure88491712017-10-02 12:13:36 +05301710 freeze_screen = this.freeze_screen || false;
1711
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301712 if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301713 frappe.call({
1714 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
rohitwaghchaure88491712017-10-02 12:13:36 +05301715 freeze: freeze_screen,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301716 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301717 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301718 email_queue_list: me.email_queue_list,
1719 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301720 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301721 callback: function (r) {
1722 if (r.message) {
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301723 me.customers = r.message.synced_customers_list;
1724 me.address = r.message.synced_address;
1725 me.contacts = r.message.synced_contacts;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301726 me.removed_items = r.message.invoice;
1727 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301728 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301729 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301730 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301731 me.remove_customer_from_localstorage();
rohitwaghchaured2be55b2017-06-07 12:04:01 +05301732 me.prepare_customer_mapper()
1733 me.autocomplete_customers()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301734 }
1735 }
1736 })
1737 }
1738 },
1739
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301740 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301741 var invoices = [];
1742 var index = 1;
Faris Ansariab74ca72017-05-30 12:54:42 +05301743 var docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301744 if (docs) {
1745 invoices = $.map(docs, function (data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301746 for (var key in data) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301747 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301748 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301749 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301750 return data
1751 }
1752 }
1753 });
1754 }
1755
1756 return invoices
1757 },
1758
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301759 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301760 var me = this;
1761 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301762 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301763 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301764 $.each(this.si_docs, function (index, data) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301765 for (var key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301766 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301767 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301768 }
1769 }
1770 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301771 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301772 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301773 this.update_localstorage();
1774 }
1775 },
1776
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301777 remove_email_queue_from_localstorage: function() {
1778 var me = this;
1779 this.email_queue = this.get_email_queue()
1780 if (this.removed_email) {
1781 $.each(this.email_queue_list, function (index, data) {
1782 if (in_list(me.removed_email, index)) {
1783 delete me.email_queue[index]
1784 }
1785 })
1786 this.update_email_queue();
1787 }
1788 },
1789
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301790 remove_customer_from_localstorage: function() {
1791 var me = this;
1792 this.customer_details = this.get_customers_details()
1793 if (this.removed_customers) {
1794 $.each(this.customers_list, function (index, data) {
1795 if (in_list(me.removed_customers, index)) {
1796 delete me.customer_details[index]
1797 }
1798 })
1799 this.update_customer_in_localstorage();
1800 }
1801 },
1802
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301803 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301804 var me = this;
1805 this.customer_validate();
1806 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301807 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301808 },
1809
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301810 item_validate: function () {
1811 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301812 frappe.throw(__("Select items to save the invoice"))
1813 }
1814 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301815
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301816 validate_mode_of_payments: function () {
1817 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301818 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1819 }
1820 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301821
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301822 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301823 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +05301824 var item_code = ''
1825 var serial_no = '';
1826 for (var key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301827 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301828 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301829 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301830
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301831 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301832 this.refresh();
1833 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1834 'item': this.items[0].item_code
1835 })))
1836 }
1837
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301838 if (item_code && serial_no) {
1839 $.each(this.frm.doc.items, function (index, data) {
1840 if (data.item_code == item_code) {
1841 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301842 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1843 'serial_no': serial_no
1844 })))
1845 }
1846 }
1847 })
1848 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301849 },
1850
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301851 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301852 var me = this;
1853 if (args.item_code == item_code && args.serial_no
1854 && field == 'qty' && cint(value) != value) {
1855 args.qty = 0.0;
1856 this.refresh();
1857 frappe.throw(__("Serial no item cannot be a fraction"))
1858 }
1859
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301860 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301861 args.qty = 0.0;
1862 args.serial_no = ''
1863 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301864 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1865 'item': item_code
1866 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301867 }
1868 },
1869
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301870 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301871 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301872 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301873 frappe.prompt([{
1874 'fieldname': 'batch',
1875 'fieldtype': 'Select',
1876 'label': __('Batch No'),
1877 'reqd': 1,
1878 'options': this.batch_no_data[this.items[0].item_code]
1879 }],
1880 function(values){
1881 me.item_batch_no[me.items[0].item_code] = values.batch;
1882 },
1883 __('Select Batch No'))
1884 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301885 },
1886
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301887 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301888 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301889 $.each(this.frm.doc["items"], function (n, item) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301890 var pricing_rule = me.get_pricing_rule(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301891 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301892 if (pricing_rule.length) {
1893 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301894 item.margin_type = pricing_rule[0].margin_type;
1895 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1896 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1897 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301898 me.apply_pricing_rule_on_item(item)
1899 } else if (item.pricing_rule) {
1900 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301901 item.margin_rate_or_amount = 0.0;
1902 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301903 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301904 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301905 }
Charles-Henri Decultot8bec0232017-05-19 12:32:30 +02001906
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301907 if(item.discount_percentage > 0) {
1908 me.apply_pricing_rule_on_item(item)
1909 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301910 })
1911 },
1912
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301913 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301914 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301915 return $.grep(this.pricing_rules, function (data) {
1916 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301917 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301918 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301919 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301920 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301921 return true
1922 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301923 }
1924 }
1925 })
1926 },
1927
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301928 validate_item_condition: function (data, item) {
1929 var apply_on = frappe.model.scrub(data.apply_on);
1930
1931 return (data.apply_on == 'Item Group')
1932 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1933 },
1934
1935 validate_item_group: function (pr_item_group, cart_item_group) {
1936 //pr_item_group = pricing rule's item group
1937 //cart_item_group = cart item's item group
1938 //this.item_groups has information about item group's lft and rgt
1939 //for example: {'Foods': [12, 19]}
1940
1941 pr_item_group = this.item_groups[pr_item_group]
1942 cart_item_group = this.item_groups[cart_item_group]
1943
1944 return (cart_item_group[0] >= pr_item_group[0] &&
1945 cart_item_group[1] <= pr_item_group[1])
1946 },
1947
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301948 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301949 //This method check condition based on applicable for
Faris Ansariab74ca72017-05-30 12:54:42 +05301950 var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301951 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301952 return true
1953 }
1954 },
1955
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301956 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301957 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301958 'Customer': [data.customer, [this.frm.doc.customer]],
1959 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1960 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301961 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301962 }
1963 },
1964
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301965 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301966 //This method validate duplicate pricing rule
1967 var pricing_rule_name = '';
1968 var priority = 0;
1969 var pricing_rule_list = [];
1970 var priority_list = []
1971
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301972 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301973
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301974 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301975 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301976 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301977 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301978 priority = data.priority
1979 pricing_rule_list.push(data)
1980 }
1981 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301982
Faris Ansariab74ca72017-05-30 12:54:42 +05301983 var count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301984 $.each(priority_list, function (index, value) {
1985 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301986 count++
1987 }
1988 })
1989
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301990 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301991 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1992 'pricing_rule': pricing_rule_name
1993 })))
1994 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301995
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301996 return pricing_rule_list
1997 }
1998 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301999
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302000 validate_warehouse: function () {
2001 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 +05302002 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302003 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302004 },
2005
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302006 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302007 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302008
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302009 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05302010 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302011 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05302012 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05302013 }
2014
2015 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05302016 },
2017
2018 update_customer_in_localstorage: function() {
2019 var me = this;
2020 try {
2021 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
2022 } catch (e) {
2023 frappe.throw(__("LocalStorage is full , did not save"))
2024 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05302025 }
Faris Ansari185762a2017-04-13 12:36:08 +05302026})