blob: 1031b77596f855d38ee5c45cbfe241b1596488e7 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301frappe.provide("erpnext.pos");
2{% include "erpnext/public/js/controllers/taxes_and_totals.js" %}
3
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05304frappe.pages['pos'].on_page_load = function (wrapper) {
Rushabh Mehta4c36d732015-01-01 15:59:34 +05305 var page = frappe.ui.make_app_page({
Rushabh Mehta72e17192014-08-08 15:30:49 +05306 parent: wrapper,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05307 title: __('Point of Sale'),
Rushabh Mehta72e17192014-08-08 15:30:49 +05308 single_column: true
9 });
Rohit Waghchauree0934d12016-05-11 15:04:57 +053010
11 wrapper.pos = new erpnext.pos.PointOfSale(wrapper)
Rushabh Mehta72e17192014-08-08 15:30:49 +053012}
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053013
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053014frappe.pages['pos'].refresh = function (wrapper) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053015 window.onbeforeunload = function () {
16 return wrapper.pos.beforeunload()
17 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +053018}
19
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053020erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053021 init: function (wrapper) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +053022 this.page_len = 20;
Rohit Waghchauree0934d12016-05-11 15:04:57 +053023 this.page = wrapper.page;
24 this.wrapper = $(wrapper).find('.page-content');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053025 this.set_indicator();
26 this.onload();
27 this.make_menu_list();
Rohit Waghchaure017f5002017-03-08 17:34:39 +053028 this.bind_events();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053029 this.si_docs = this.get_doc_from_localstorage();
30 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053031
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053032 beforeunload: function (e) {
33 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053034 e = e || window.event;
35
36 // For IE and Firefox prior to version 4
37 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053038 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 +053039 return
40 }
41
42 // For Safari
43 return __("You are in offline mode. You will not be able to reload until you have network.");
44 }
45 },
46
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053047 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053048 var me = this;
49 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053050 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053051 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053052 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053053 },
54
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053055 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053056 var me = this;
57 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053058 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053059 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053060 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053061 method: "frappe.handler.ping",
62 callback: function (r) {
63 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053065 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 }
67 }
68 })
69 },
70
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053071 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053072 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053073 this.get_data_from_server(function () {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053074 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053076 },
77
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053078 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053079 var me = this;
80
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053081 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053082 me.save_previous_entry();
83 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053084 })
85
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053086 this.page.add_menu_item(__("Sync Master Data"), function () {
87 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +053088 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053089 me.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +053090 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +053091 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053092 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053093 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053094
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053095 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +053096 me.sync_sales_invoice()
97 });
98
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +053099 this.page.add_menu_item(__("Email"), function () {
100 if(me.frm.doc.docstatus == 1) {
101 me.email_prompt()
102 }
103 });
104
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530105 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530106 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530107 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530108 },
109
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530110 email_prompt: function() {
111 var me = this;
112 fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
113 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
114 {fieldtype: "Section Break"},
115 {label:__("Subject"), fieldtype:"Data", reqd: 1,
116 fieldname:"subject",length:524288},
117 {fieldtype: "Section Break"},
118 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
119 fieldname:"content"},
120 {fieldtype: "Section Break"},
121 {fieldtype: "Column Break"}];
122
123 this.email_dialog = new frappe.ui.Dialog({
124 title: "Email",
125 fields: fields,
126 primary_action_label: __("Send"),
127 primary_action: function() {
128 me.send_action();
129 }
130 });
131
132 this.email_dialog.show()
133 },
134
135 send_action: function() {
136 this.email_queue = this.get_email_queue()
137 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
138 this.update_email_queue()
139 this.email_dialog.hide()
140 },
141
142 update_email_queue: function () {
143 try {
144 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
145 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530146 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530147 }
148 },
149
150 get_email_queue: function () {
151 try {
152 return JSON.parse(localStorage.getItem('email_queue')) || {};
153 } catch (e) {
154 return {}
155 }
156 },
157
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530158 get_customers_details: function () {
159 try {
160 return JSON.parse(localStorage.getItem('customer_details')) || {};
161 } catch (e) {
162 return {}
163 }
164 },
165
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530166 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530167 var me = this;
168
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530169 $(this.list_body).find('.list-column').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530170 me.name = $(this).parents().attr('invoice-name')
171 me.edit_record();
172 })
173
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530174 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530175 me.removed_items = [];
176 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530177 if ($(this).is(":checked")) {
178 $.each(me.si_docs, function (index, data) {
179 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530180 me.removed_items.push(key)
181 }
182 })
183 }
184
185 me.toggle_primary_action();
186 })
187
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530188 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530189 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530190 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530191 me.removed_items.push(me.name);
192 } else {
193 me.removed_items.pop(me.name)
194 }
195
196 me.toggle_primary_action();
197 })
198 },
199
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 var me = this;
202
203 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530205 this.frm.doc = doc_data[0][this.name];
206 this.set_missing_values();
207 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530208 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530209 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530210 }
211 },
212
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530213 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 var me = this;
215 this.remove_doc_from_localstorage()
216 this.update_localstorage();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530217 this.dialog_actions();
218 this.toggle_primary_action();
219 },
220
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530221 toggle_primary_action: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530222 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530223 if(this.frm.doc.allow_delete) {
224 if (this.removed_items && this.removed_items.length > 0) {
225 $(this.wrapper).find('.btn-danger').show();
226 } else {
227 $(this.wrapper).find('.btn-danger').hide();
228 }
229 }
230 },
231
232 get_doctype_status: function (doc) {
233 if (doc.docstatus == 0) {
234 return { status: "Draft", indicator: "red" }
235 } else if (doc.outstanding_amount == 0) {
236 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530237 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530238 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530239 }
240 },
241
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530242 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530243 var me = this;
244 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530245 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530246 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530247 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530248 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530249
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530250 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530251 this.party_field.$input.val(this.frm.doc.customer);
252 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530253
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530254 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530255 this.frm.doc.write_off_account = doc.write_off_account
256 }
257
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530258 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530259 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530260 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530261 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530262
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530263 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530264 var me = this;
265 this.si_docs = this.get_doc_from_localstorage();
266
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530267 return $.grep(this.si_docs, function (data) {
268 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530269 return key == me.name
270 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530271 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530272 },
273
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530274 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530275 var me = this;
276 frappe.call({
277 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
278 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530279 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530280 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530281 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530282 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530283 me.set_interval_for_si_sync();
284 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530285 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530286 callback();
287 }
288 }
289 })
290 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530291
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530292 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530293 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530294 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530295 this.meta = r.message.meta;
296 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530297 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530298 this.customers = r.message.customers;
299 this.serial_no_data = r.message.serial_no_data;
300 this.batch_no_data = r.message.batch_no_data;
301 this.tax_data = r.message.tax_data;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530302 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530303 this.price_list_data = r.message.price_list_data;
304 this.bin_data = r.message.bin_data;
305 this.pricing_rules = r.message.pricing_rules;
306 this.print_template = r.message.print_template;
307 this.pos_profile_data = r.message.pos_profile;
308 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530309 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530310 this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {};
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530311 this.make_control()
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530312 },
313
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530314 save_previous_entry: function () {
315 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
316 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530317 }
318 },
319
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530320 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530321 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530322 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530323 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530324 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530325 this.setup();
326 },
327
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530328 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530329 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530330
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530331 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530332 this.actual_qty_dict = {};
333
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530334 if (load_doc) {
335 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530336 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530337
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530338 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530339 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530340 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530341 })
342
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530343 this.print_template_data = frappe.render_template("print_template", {
344 content: this.print_template,
345 title: "POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css,
346 print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer
347 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530348 },
349
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530350 setup: function () {
351 this.frm.doc.allow_delete = this.pos_profile_data["allow_delete"];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530352 this.make();
353 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530354 this.party_field.$input.attr('disabled', false);
355 if(this.selected_row) {
356 this.selected_row.hide()
357 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530358 },
359
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530360 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530361 var me = this;
362 this.party = party;
363 this.price_list = (party == "Customer" ?
364 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
365 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
366 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
367 },
368
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530369 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530370 this.make_item_list();
371 this.make_discount_field()
372 },
373
374 make_control: function() {
375 this.frm = {}
376 this.frm.doc = this.doc
377 this.set_transaction_defaults("Customer");
378 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530379 this.make_search();
380 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530381 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530382 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530383 },
384
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530385 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530386 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530387 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530388 df: {
389 "fieldtype": "Data",
390 "label": "Item",
391 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530392 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530394 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530395 only_input: true,
396 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530397
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530398 this.serach_item.make_input();
399 this.serach_item.$input.on("keyup", function () {
400 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530401 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530402 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530403 }, 1000);
404 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530405
Faris Ansari339d9c92017-03-07 18:14:06 +0530406 // this.search_item_group = frappe.ui.form.make_control({
407 // df: {
408 // "fieldtype": "Select",
409 // "options": me.item_groups,
410 // "label": __("Item Group"),
411 // "fieldname": "item_group",
412 // "placeholder": __("Item Group")
413 // },
414 // parent: this.wrapper.find(".search-item-group"),
415 // only_input: true,
416 // });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530417
Faris Ansari339d9c92017-03-07 18:14:06 +0530418 this.search_item_group = this.wrapper.find('.search-item-group');
419
420 var dropdown_html = me.item_groups.map(function(item_group) {
421 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
422 }).join("");
423
424 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
425
426 this.search_item_group.on('click', '.dropdown-menu a', function() {
427 me.selected_item_group = $(this).attr('data-value');
428 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
429
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530430 me.page_len = 20;
431 me.items = me.get_items();
432 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530433 })
434
435 // this.search_item_group.make_input();
436 // this.search_item_group.$input.on("change", function () {
437 // me.page_len = 20;
438 // me.items = me.get_items();
439 // me.make_item_list();
440 // });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530441
Faris Ansari45510102017-03-09 14:43:37 +0530442 me.toggle_more_btn();
443
444 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530445 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530446 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530447 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530448 me.toggle_more_btn();
449 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530450
Faris Ansari45510102017-03-09 14:43:37 +0530451 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530452 me.update_customer()
453 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530454 },
455
Faris Ansari45510102017-03-09 14:43:37 +0530456 toggle_more_btn: function() {
457 if(!this.items || this.items.length <= this.page_len) {
458 this.wrapper.find(".btn-more").hide();
459 } else {
460 this.wrapper.find(".btn-more").show();
461 }
462 },
463
464 toggle_totals_area: function(show) {
465
Faris Ansari07c9f352017-03-09 17:03:14 +0530466 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530467 show = this.is_totals_area_collapsed;
468 }
469
470 var totals_area = this.wrapper.find('.totals-area');
471 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
472 .toggle(show);
473
474 if(show) {
475 totals_area.find('.collapse-btn i')
476 .removeClass('octicon-chevron-down')
477 .addClass('octicon-chevron-up');
478 } else {
479 totals_area.find('.collapse-btn i')
480 .removeClass('octicon-chevron-up')
481 .addClass('octicon-chevron-down');
482 }
483
484 this.is_totals_area_collapsed = !show;
485 },
486
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530487 make_list_customers: function () {
488 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530489 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530490 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530491 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530492 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530493 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530494
Faris Ansari45510102017-03-09 14:43:37 +0530495 me.render_list_customers();
496 me.toggle_totals_area(false);
497
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530498 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530499 $(this).toggleClass("view_customer");
500 if($(this).hasClass("view_customer")) {
501 me.render_list_customers();
502 me.bind_delete_event()
503 me.party_field.$input.attr('disabled', true);
504 me.list_customers.show();
505 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530506 me.numeric_keypad.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530507 } else {
508 if(me.frm.doc.docstatus == 0) {
509 me.party_field.$input.attr('disabled', false);
510 }
511 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530512 me.toggle_totals_area(false);
513 me.list_customers.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530514 if(me.frm.doc.items.length > 0) {
515 me.numeric_keypad.show();
516 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530517 }
518 });
519 this.add_customer_btn.on('click', function() {
520 me.save_previous_entry();
521 me.create_new();
522 me.refresh();
523 me.set_focus();
524 });
Faris Ansari45510102017-03-09 14:43:37 +0530525 this.pos_bill.on('click', '.collapse-btn', function() {
526 me.toggle_totals_area();
527 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530528 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530529
530 bind_numeric_keypad: function() {
531 var me = this;
532 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
533 me.numeric_val = '';
534 })
535
536 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
537 me.numeric_id = $(this).attr("id") || me.numeric_id;
538 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530539 if(me.numeric_id) {
540 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
541 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530542
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530543 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530544 me.numeric_val += me.val;
545 me.selected_field.val(flt(me.numeric_val))
546 me.selected_field.trigger("change")
547 me.render_selected_item()
548 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530549
550 if(me.numeric_id && $(this).hasClass('pos-operation')) {
551 me.numeric_keypad.find('button.pos-operation').removeClass('active');
552 $(this).addClass('active');
553
554 me.selected_row.find('.pos-list-row').removeClass('active');
555 me.selected_field.closest('.pos-list-row').addClass('active');
556 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530557 })
558
559 $(this.numeric_keypad).find('.numeric-del').click(function(){
560 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
561 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
562 me.selected_field.val(me.numeric_val);
563 me.selected_field.trigger("change")
564 me.render_selected_item()
565 })
566
567 $(this.numeric_keypad).find('.pos-pay').click(function(){
568 me.validate();
569 me.update_paid_amount_status(true);
570 me.create_invoice();
571 me.make_payment();
572 })
573 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530574
575 render_list_customers: function () {
576 var me = this;
577
578 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530579 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530580 this.si_docs = this.get_doc_from_localstorage();
581
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530582 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530583 return;
584 }
Faris Ansari45510102017-03-09 14:43:37 +0530585
586 var html = "";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530587 this.si_docs.forEach(function (data, i) {
588 for (key in data) {
589 html += frappe.render_template("pos_invoice_list", {
590 sr: i + 1,
591 name: key,
592 customer: data[key].customer,
593 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
594 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
595 data: me.get_doctype_status(data[key])
596 });
597 }
598 });
Faris Ansari45510102017-03-09 14:43:37 +0530599 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530600
601 this.list_customers.find('.list-column').click(function () {
602 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530603 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530604 me.list_customers_btn.toggleClass("view_customer");
605 me.pos_bill.show();
606 me.list_customers_btn.show();
607 me.name = $(this).parents().attr('invoice-name')
608 me.edit_record();
609 })
610
611 //actions
612 $(this.wrapper).find('.list-select-all').click(function () {
613 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
614 me.removed_items = [];
615 if ($(this).is(":checked")) {
616 $.each(me.si_docs, function (index, data) {
617 for (key in data) {
618 me.removed_items.push(key)
619 }
620 });
621 }
622
623 me.toggle_primary_action();
624 });
625
626 $(this.wrapper).find('.list-delete').click(function () {
627 me.name = $(this).parent().parent().attr('invoice-name');
628 if ($(this).is(":checked")) {
629 me.removed_items.push(me.name);
630 } else {
631 me.removed_items.pop(me.name)
632 }
633
634 me.toggle_primary_action();
635 });
636 },
637
638 bind_delete_event: function() {
639 var me = this;
640
641 $(this.wrapper).find('.btn-danger').click(function(){
642 frappe.confirm(__("Delete permanently?"), function () {
643 me.delete_records();
644 me.render_list_customers();
645 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530646 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530647 },
648
649 set_focus: function () {
650 if (this.default_customer || this.frm.doc.customer) {
651 this.serach_item.$input.focus();
652 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530653 this.party_field.$input.focus();
654 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530655 },
656
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530657 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530658 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530659
660 if(!this.party_field) {
661 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
662 $(frappe.render_template('customer_toolbar', {
663 allow_delete: this.frm.doc.allow_delete
664 })).insertAfter(this.page.$title_area.hide());
665 }
666
667 this.party_field = frappe.ui.form.make_control({
668 df: {
669 "fieldtype": "Data",
670 "options": this.party,
671 "label": this.party,
672 "fieldname": this.party.toLowerCase(),
673 "placeholder": __("Select or add new customer")
674 },
675 parent: this.page.wrapper.find(".party-area"),
676 only_input: true,
677 });
678
679 this.party_field.make_input();
680 setTimeout(this.set_focus.bind(this), 500);
Faris Ansari339d9c92017-03-07 18:14:06 +0530681 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530682
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530683 if (this.default_customer && !this.frm.doc.customer) {
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530684 this.party_field.$input.val(this.default_customer);
685 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530686 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530687
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530688 this.party_field.awesomeplete =
689 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530690 minChars: 0,
691 maxItems: 99,
692 autoFirst: true,
693 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530694 filter: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530695 var value = item.value.toLowerCase();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530696 if (value.indexOf('is_action') !== -1 ||
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530697 value.indexOf(input) !== -1) {
698 return true;
699 }
700 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530701 item: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530702 var d = item;
Faris Ansari45510102017-03-09 14:43:37 +0530703 var html = "<span>" + __(d.label || d.value) + "</span>";
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530704 return $('<li></li>')
705 .data('item.autocomplete', d)
706 .html('<a><p>' + html + '</p></a>')
707 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530708 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530709 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530710 var customers = this.customers.map(function (c) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530711 return {
712 label: c.name,
713 value: c.name,
714 customer_group: c.customer_group,
715 territory: c.territory
716 }
717 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530718
719 customers.push({
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530720 label: "<span class='text-primary link-option'>"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530721 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
722 + __("Create a new Customer")
723 + "</span>",
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530724 value: 'is_action',
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530725 action: me.add_customer
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530726 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530727 this.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530728
729 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530730 .on('input', function (e) {
731 me.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530732 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530733 .on('awesomplete-select', function (e) {
734 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530735 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530736 if (!customer) return;
737 // create customer link
738 if (customer.action) {
739 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530740 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530741 }
Faris Ansari45510102017-03-09 14:43:37 +0530742 me.toggle_list_customer(false);
743 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530744 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530745 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530746 me.set_focus();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530747 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530748 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530749 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530750 me.toggle_edit_button(false);
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530751 if(me.frm.doc.items.length){
752 me.toggle_list_customer(false)
753 me.toggle_item_cart(true)
754 } else {
755 me.toggle_list_customer(true)
756 me.toggle_item_cart(false)
757 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530758 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530759 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530760 var item = me.party_field.awesomeplete
761 .get_item(e.originalEvent.text.value);
762 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530763 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530764 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530765 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530766 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530767 },
768
Faris Ansari45510102017-03-09 14:43:37 +0530769 toggle_edit_button: function(flag) {
770 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
771 },
772
773 toggle_list_customer: function(flag) {
774 this.list_customers.toggle(flag);
775 },
776
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530777 toggle_item_cart: function(flag) {
778 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
779 },
780
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530781 add_customer: function() {
782 this.frm.doc.customer = "";
783 this.update_customer()
784 },
785
786 update_customer: function () {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530787 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530788 if (!this.connection_status) return;
Rushabh Mehta37146262017-02-01 18:17:35 +0530789
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530790 this.customer_doc = new frappe.ui.Dialog({
791 'title': 'Customer',
792 fields: [
793 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530794 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530795 "fieldname": "full_name",
796 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530797 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530798 },
799 {
800 "fieldtype": "Section Break"
801 },
802 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530803 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530804 "fieldname": "email_id",
805 "fieldtype": "Data"
806 },
807 {
808 "fieldtype": "Column Break"
809 },
810 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530811 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530812 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530813 "fieldtype": "Data"
814 },
815 {
816 "fieldtype": "Section Break"
817 },
818 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530819 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530820 "fieldname": "address_line1",
821 "fieldtype": "Data"
822 },
823 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530824 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530825 "fieldname": "address_line2",
826 "fieldtype": "Data"
827 },
828 {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530829 "label": __("Fax"),
830 "fieldname": "fax",
831 "fieldtype": "Data"
832 },
833 {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530834 "fieldtype": "Column Break"
835 },
836 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530837 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530838 "fieldname": "city",
839 "fieldtype": "Data"
840 },
841 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530842 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530843 "fieldname": "state",
844 "fieldtype": "Data"
845 },
846 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530847 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530848 "fieldname": "pincode",
849 "fieldtype": "Data"
850 },
851 {
852 "label": __("ZIP Code"),
853 "hidden": 1,
854 "fieldname": "name",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530855 "fieldtype": "Data"
856 }
857 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530858 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530859
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530860 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530861 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530862
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530863 this.customer_doc.set_primary_action(__("Save"), function () {
864 me.make_offline_customer();
865 me.pos_bill.show();
866 });
867 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530868
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530869 render_address_data: function() {
870 var me = this;
871 this.address_data = this.address[this.frm.doc.customer] || this.get_address_from_localstorage();
872 prompt_obj = me.customer_doc.fields_dict;
873 $.each(this.address_data, function(key, value){
874 if(prompt_obj[key] && key!='name') {
875 prompt_obj[key].$input.val(value)
876 }
877 })
878
879 if(!prompt_obj.full_name.$input.val()) {
880 prompt_obj.full_name.$input.val(this.frm.doc.customer)
881 }
882 },
883
884 get_address_from_localstorage: function() {
885 this.address_details = this.get_customers_details()
886 return this.address_details[this.frm.doc.customer]
887 },
888
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530889 make_offline_customer: function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530890 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
891 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530892 this.party_field.$input.val(this.frm.doc.customer);
893 this.customers.push({
894 name: this.frm.doc.customer,
895 customer_name: this.frm.doc.customer
896 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530897 this.update_customer_in_localstorage()
898 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530899 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530900 },
901
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530902 get_prompt_details: function() {
903 this.prompt_details = this.customer_doc.get_values();
904 this.prompt_details['country'] = this.frm.doc.country;
905 return JSON.stringify(this.prompt_details)
906 },
907
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530908 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530909 var me = this;
910 this.frm.doc.customer = doc.label || doc.name;
911 this.frm.doc.customer_name = doc.customer_name;
912 this.frm.doc.customer_group = doc.customer_group;
913 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530914 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530915 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530916 },
917
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530918 get_customers: function (key) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530919 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530920 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530921 var re = new RegExp('%', 'g');
922 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
923
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530924 if (key) {
925 return $.grep(this.customers, function (data) {
926 if (reg.test(data.name.toLowerCase())
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530927 || reg.test(data.customer_name.toLowerCase())
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530928 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530929 return data
930 }
931 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530932 } else {
933 customers = this.customers.sort(function (a, b) { return a.idx < b.idx })
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530934 return customers.slice(0, 20)
935 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530936 },
937
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530938 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530939 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530940 if (!this.price_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530941 msgprint(__("Price List not found or disabled"));
942 return;
943 }
944
945 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530946
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530947 var $wrap = me.wrapper.find(".item-list");
948 me.wrapper.find(".item-list").empty();
949
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530950 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530951 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530952 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530953 $(frappe.render_template("pos_item", {
954 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530955 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530956 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +0530957 item_image: obj.image,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530958 color: frappe.get_palette(obj.item_name),
959 abbr: frappe.get_abbr(obj.item_name)
960 })).tooltip().appendTo($wrap);
961 }
962 });
Faris Ansari45510102017-03-09 14:43:37 +0530963
964 $wrap.append(`
965 <div class="image-view-item btn-more text-muted text-center">
966 <div class="image-view-body">
967 <i class="mega-octicon octicon-package"></i>
968 <div>Load more items</div>
969 </div>
970 </div>
971 `);
972
973 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530974 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +0530975 $("<p class='text-muted small' style='padding-left: 10px'>"
976 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530977 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530978
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530979 if (this.items.length == 1
980 && this.serach_item.$input.val()) {
981 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530982 this.add_to_cart();
983 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530984 },
985
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530986 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530987 // To search item as per the key enter
988
989 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530990 this.item_serial_no = {};
991 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530992
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530993 if (item_code) {
994 return $.grep(this.item_data, function (item) {
995 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530996 return true
997 }
998 })
999 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301000
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301001 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301002
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301003 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301004 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301005 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301006 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301007
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301008 if (key) {
1009 return $.grep(this.items_list, function (item) {
1010 if (search_status) {
1011 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301012 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301013 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1014 } else if (me.serial_no_data[item.item_code]
1015 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301016 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301017 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 +05301018 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301019 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301020 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301021 return item.barcode == me.serach_item.$input.val();
1022 } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
1023 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301024 return true
1025 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301026 }
1027 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301028 } else {
1029 return this.items_list;
1030 }
1031 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301032
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301033 apply_category: function() {
1034 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301035 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301036
1037 if(category == 'All Item Groups') {
1038 return this.item_data
1039 } else {
1040 return this.item_data.filter(function(element, index, array){
1041 return element.item_group == category;
1042 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301043 }
1044 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301045
1046 bind_items_event: function() {
1047 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301048 $(this.wrapper).on('click', '.pos-bill-item', function() {
1049 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1050 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301051 me.numeric_val = "";
1052 me.numeric_id = ""
1053 me.item_code = $(this).attr("data-item-code");
1054 me.render_selected_item()
1055 me.bind_qty_event()
1056 me.update_rate()
1057 $(me.wrapper).find(".selected-item").scrollTop(1000);
1058 })
1059 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301060
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301061 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301062 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301063
1064 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301065 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301066 var qty = $(this).val();
1067 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301068 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301069
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301070 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301071 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1072 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301073 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301074 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301075
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301076 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301077 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1078 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301079 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301080 })
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301081
1082 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301083 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301084 var discount = $(this).val();
1085 me.update_discount(item_code, discount)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301086 })
1087 },
1088
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301089 bind_events: function() {
1090 var me = this;
1091 // if form is local then allow this function
1092 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1093 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
1094 if(me.list_customers_btn.hasClass("view_customer")) return;
1095
1096 me.customer_validate();
1097 if (me.frm.doc.docstatus == 0) {
1098 me.items = me.get_items($(this).attr("data-item-code"))
1099 me.add_to_cart();
1100 }
1101 });
1102 },
1103
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301104 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301105 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301106 this.items = this.get_items(item_code);
1107 this.validate_serial_no()
1108 this.set_item_details(item_code, "qty", qty);
1109 },
1110
1111 update_discount: function(item_code, discount) {
1112 var me = this;
1113 this.items = this.get_items(item_code);
1114 this.set_item_details(item_code, "discount_percentage", discount);
1115 },
1116
1117 update_rate: function () {
1118 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301119 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301120 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301121 me.set_item_details(item_code, "rate", $(this).val());
1122 })
1123 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301124
1125 render_selected_item: function() {
1126 doc = this.get_child_item(this.item_code);
1127 $(this.wrapper).find('.selected-item').empty();
1128 if(doc.length) {
Faris Ansari339d9c92017-03-07 18:14:06 +05301129 this.selected_row = $(frappe.render_template("pos_selected_item", doc[0]))
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301130 $(this.wrapper).find('.selected-item').html(this.selected_row)
1131 }
1132 },
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301133
1134 get_child_item: function(item_code) {
1135 var me = this;
1136 return $.map(me.frm.doc.items, function(doc){
1137 if(doc.item_code == item_code) {
1138 return doc
1139 }
1140 })
1141 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301142
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301143 set_item_details: function (item_code, field, value) {
1144 var me = this;
1145 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301146 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301147 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301148
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301149 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301150 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301151 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301152 if (d.serial_no && field == 'qty') {
1153 me.validate_serial_no_qty(d, item_code, field, value)
1154 }
1155
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301156 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301157 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301158 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301159 me.remove_item.push(d.idx)
1160 }
1161 }
1162 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301163
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301164 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301165 this.remove_zero_qty_item();
1166 }
1167
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301168 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301169 },
1170
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301171 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301172 var me = this;
1173 idx = 0
1174 this.items = []
1175 idx = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301176 $.each(this.frm.doc["items"] || [], function (i, d) {
1177 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301178 d.idx = idx;
1179 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301180 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301181 }
1182 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301183
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301184 this.frm.doc["items"] = this.items;
1185 },
1186
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301187 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301188 var me = this;
1189
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301190 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301191 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
1192 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301193
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301194 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301195 total = me.frm.doc.net_total
1196 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301197
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301198 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 +05301199 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1200 me.refresh();
1201 });
1202
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301203 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301204 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1205 me.frm.doc.additional_discount_percentage = 0.0;
1206 me.wrapper.find('input.discount-percentage').val(0);
1207 me.refresh();
1208 });
1209 },
1210
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301211 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301212 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301213 if (!this.frm.doc.customer) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301214 frappe.throw(__("Please select customer"))
1215 }
1216 },
1217
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301218 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301219 var me = this;
1220 var caught = false;
1221 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1222
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301223 this.customer_validate();
1224 this.mandatory_batch_no();
1225 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301226 this.validate_warehouse();
1227
1228 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301229 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301230 if (d.item_code == me.items[0].item_code) {
1231 caught = true;
1232 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301233 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301234 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301235 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1236 d.warehouse = me.item_serial_no[d.item_code][1]
1237 }
1238
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301239 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301240 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301241 }
1242 }
1243 });
1244 }
1245
1246 // if item not found then add new item
1247 if (!caught)
1248 this.add_new_item_to_grid();
1249
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301250 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301251 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301252 },
1253
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301254 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301255 var me = this;
1256 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1257 this.child.item_code = this.items[0].item_code;
1258 this.child.item_name = this.items[0].item_name;
1259 this.child.stock_uom = this.items[0].stock_uom;
1260 this.child.description = this.items[0].description;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301261 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301262 this.child.qty = 1;
1263 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301264 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1265 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301266 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301267 ? 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 +05301268 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1269 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1270 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301271 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301272 this.child.batch_no = this.item_batch_no[this.child.item_code];
1273 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1274 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301275 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301276 },
1277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301278 update_paid_amount_status: function (update_paid_amount) {
1279 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301280 update_paid_amount = update_paid_amount ? false : true;
1281 }
1282
1283 this.refresh(update_paid_amount);
1284 },
1285
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301286 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301287 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301288 this.refresh_fields(update_paid_amount);
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301289 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301290 this.set_primary_action();
1291 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301292
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301293 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301294 this.apply_pricing_rule();
1295 this.discount_amount_applied = false;
1296 this._calculate_taxes_and_totals();
1297 this.calculate_discount_amount();
1298 this.show_items_in_item_cart();
1299 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301300 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 this.set_totals();
1302 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301303
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301304 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301305 return erpnext.get_currency(this.frm.doc.company);
1306 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301307
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301308 show_item_wise_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301309 return null;
1310 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301311
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301312 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301313 var me = this;
1314 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301315 var $no_items_message = this.wrapper.find(".no-items-message");
1316 $no_items_message.toggle(this.frm.doc.items.length === 0);
1317
1318 var $totals_area = this.wrapper.find('.totals-area');
1319 $totals_area.toggle(this.frm.doc.items.length > 0);
1320
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301321 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301322 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301323 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301324 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301325 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301326 discount_percentage: d.discount_percentage || 0.0,
1327 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301328 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301329 rate: format_number(d.rate, me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301330 enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301331 amount: format_currency(d.amount, me.frm.doc.currency)
1332 })).appendTo($items);
1333 });
1334
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301335 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301336 $(this).select();
1337 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301338
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301339 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301340 $(this).select();
1341 });
1342
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301343 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301344 $(this).select();
1345 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301346 },
1347
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301348 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301349 var me = this;
1350 me.frm.doc.total_taxes_and_charges = 0.0
1351
1352 var taxes = this.frm.doc.taxes || [];
1353 $(this.wrapper)
1354 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1355 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301356
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301357 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301358 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1359 $(frappe.render_template("pos_tax_row", {
1360 description: d.description,
1361 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1362 me.frm.doc.currency)
1363 })).appendTo(me.wrapper.find(".tax-table"));
1364 }
1365 });
1366 },
1367
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301368 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301369 var me = this;
1370 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1371 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1372 },
1373
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301374 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301375 var me = this;
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301376 this.page.set_primary_action(__("New Order"), function () {
Faris Ansari339d9c92017-03-07 18:14:06 +05301377 me.save_previous_entry();
1378 me.create_new();
1379 me.refresh();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301380 me.toggle_input_field();
Faris Ansari339d9c92017-03-07 18:14:06 +05301381 me.set_focus();
Faris Ansari45510102017-03-09 14:43:37 +05301382 }, "fa fa-plus")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301383 if (this.frm.doc.docstatus == 0) {
Faris Ansari339d9c92017-03-07 18:14:06 +05301384 // this.page.set_primary_action(__("Pay"), function () {
1385 // me.validate();
1386 // me.update_paid_amount_status(true);
1387 // me.create_invoice();
1388 // me.make_payment();
1389 // }, "fa fa-credit-card");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301390 } else if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301391 this.page.set_secondary_action(__("Print"), function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301392 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301393 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301394 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301395 } else {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301396 this.page.clear_primary_action()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301397 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301399 // this.page.set_secondary_action(__("New"), function () {
1400 // me.save_previous_entry();
1401 // me.create_new();
1402 // }, "fa fa-plus").addClass("btn-primary");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301403 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301404
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301405 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301406 var me = this;
1407
1408 msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
1409 style="margin-right: 5px;">{0}</a>\
1410 <a class="btn btn-default new_doc">{1}</a>', [
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301411 __('Print'), __('New')
1412 ]));
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301413
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301414 $('.print_doc').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301415 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301416 me.print_document(html)
1417 })
1418
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301419 $('.new_doc').click(function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301420 msgprint.hide()
1421 me.create_new();
1422 })
1423 },
1424
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301425 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301426 var w = window.open();
1427 w.document.write(html);
1428 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301429 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301430 w.print();
1431 w.close();
1432 }, 1000)
1433 },
1434
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301435 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301436 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301437 this.change_status();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301438 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301439 this.print_dialog()
1440 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301441 },
1442
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301443 change_status: function () {
1444 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301445 this.frm.doc.docstatus = 1;
1446 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301447 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 }
1449 },
1450
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301451 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301452 var pointer_events = 'inherit'
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301453 disabled = this.frm.doc.docstatus == 1 ? true: false;
1454 $(this.wrapper).find('input').attr("disabled", disabled);
1455 $(this.wrapper).find('select').attr("disabled", disabled);
1456 $(this.wrapper).find('input').attr("disabled", disabled);
1457 $(this.wrapper).find('select').attr("disabled", disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301458
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301459 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301460 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301461 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301462
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301463 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301464 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1465 this.set_primary_action();
1466 },
1467
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301468 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301469 var me = this;
1470 var invoice_data = {}
1471 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301472 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301473 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301474 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301476 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301477 this.frm.doc.posting_date = frappe.datetime.get_today();
1478 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 invoice_data[this.name] = this.frm.doc
1480 this.si_docs.push(invoice_data)
1481 this.update_localstorage();
1482 this.set_primary_action();
1483 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301484 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301485 },
1486
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301487 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301488 var me = this;
1489 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301490 $.each(this.si_docs, function (index, data) {
1491 for (key in data) {
1492 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301493 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301494 me.update_localstorage();
1495 }
1496 }
1497 })
1498 },
1499
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301500 update_localstorage: function () {
1501 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301502 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301503 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301504 frappe.throw(__("LocalStorage is full , did not save"))
1505 }
1506 },
1507
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301508 get_doc_from_localstorage: function () {
1509 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301510 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301511 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301512 return []
1513 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301514 },
1515
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301516 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301517 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301518 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301519 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301520 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301521 },
1522
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301523 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301524 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301525 this.si_docs = this.get_submitted_invoice() || [];
1526 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301527 this.customers_list = this.get_customers_details() || {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301528
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301529 if (this.si_docs.length || this.email_queue_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301530 frappe.call({
1531 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1532 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301533 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301534 email_queue_list: me.email_queue_list,
1535 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301536 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301537 callback: function (r) {
1538 if (r.message) {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301539 me.removed_items = r.message.invoice;
1540 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301541 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301542 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301543 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301544 me.remove_customer_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301545 }
1546 }
1547 })
1548 }
1549 },
1550
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301551 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301552 var invoices = [];
1553 var index = 1;
1554 docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301555 if (docs) {
1556 invoices = $.map(docs, function (data) {
1557 for (key in data) {
1558 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301559 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301560 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301561 return data
1562 }
1563 }
1564 });
1565 }
1566
1567 return invoices
1568 },
1569
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301570 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301571 var me = this;
1572 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301573 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301574 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301575 $.each(this.si_docs, function (index, data) {
1576 for (key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301577 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301578 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301579 }
1580 }
1581 })
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301582 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301583 this.update_localstorage();
1584 }
1585 },
1586
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301587 remove_email_queue_from_localstorage: function() {
1588 var me = this;
1589 this.email_queue = this.get_email_queue()
1590 if (this.removed_email) {
1591 $.each(this.email_queue_list, function (index, data) {
1592 if (in_list(me.removed_email, index)) {
1593 delete me.email_queue[index]
1594 }
1595 })
1596 this.update_email_queue();
1597 }
1598 },
1599
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301600 remove_customer_from_localstorage: function() {
1601 var me = this;
1602 this.customer_details = this.get_customers_details()
1603 if (this.removed_customers) {
1604 $.each(this.customers_list, function (index, data) {
1605 if (in_list(me.removed_customers, index)) {
1606 delete me.customer_details[index]
1607 }
1608 })
1609 this.update_customer_in_localstorage();
1610 }
1611 },
1612
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301613 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301614 var me = this;
1615 this.customer_validate();
1616 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301617 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301618 },
1619
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301620 item_validate: function () {
1621 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301622 frappe.throw(__("Select items to save the invoice"))
1623 }
1624 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301625
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301626 validate_mode_of_payments: function () {
1627 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301628 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1629 }
1630 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301631
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301632 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301633 var me = this;
1634 var item_code = serial_no = '';
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301635 for (key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301636 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301637 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301638 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301639
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301640 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301641 this.refresh();
1642 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1643 'item': this.items[0].item_code
1644 })))
1645 }
1646
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301647 if (item_code && serial_no) {
1648 $.each(this.frm.doc.items, function (index, data) {
1649 if (data.item_code == item_code) {
1650 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301651 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1652 'serial_no': serial_no
1653 })))
1654 }
1655 }
1656 })
1657 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301658 },
1659
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301660 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301661 var me = this;
1662 if (args.item_code == item_code && args.serial_no
1663 && field == 'qty' && cint(value) != value) {
1664 args.qty = 0.0;
1665 this.refresh();
1666 frappe.throw(__("Serial no item cannot be a fraction"))
1667 }
1668
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301669 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301670 args.qty = 0.0;
1671 args.serial_no = ''
1672 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301673 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1674 'item': item_code
1675 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301676 }
1677 },
1678
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301679 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301680 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301681 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301682 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1683 'item': this.items[0].item_code
1684 })))
1685 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301686 },
1687
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301688 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301689 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301690 $.each(this.frm.doc["items"], function (n, item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301691 pricing_rule = me.get_pricing_rule(item)
1692 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301693 if (pricing_rule.length) {
1694 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301695 item.margin_type = pricing_rule[0].margin_type;
1696 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1697 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1698 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301699 me.apply_pricing_rule_on_item(item)
1700 } else if (item.pricing_rule) {
1701 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301702 item.margin_rate_or_amount = 0.0;
1703 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301704 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301705 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301706 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301707
1708 if(item.discount_percentage > 0) {
1709 me.apply_pricing_rule_on_item(item)
1710 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301711 })
1712 },
1713
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301714 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301715 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301716 return $.grep(this.pricing_rules, function (data) {
1717 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
1718 if (data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) {
1719 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301720 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301721 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301722 return true
1723 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301724 }
1725 }
1726 })
1727 },
1728
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301729 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301730 //This method check condition based on applicable for
1731 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301732 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301733 return true
1734 }
1735 },
1736
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301737 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301738 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301739 'Customer': [data.customer, [this.frm.doc.customer]],
1740 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1741 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301742 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301743 }
1744 },
1745
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301746 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301747 //This method validate duplicate pricing rule
1748 var pricing_rule_name = '';
1749 var priority = 0;
1750 var pricing_rule_list = [];
1751 var priority_list = []
1752
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301753 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301754
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301755 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301756 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301757 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301758 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301759 priority = data.priority
1760 pricing_rule_list.push(data)
1761 }
1762 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301763
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301764 count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301765 $.each(priority_list, function (index, value) {
1766 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301767 count++
1768 }
1769 })
1770
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301771 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301772 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1773 'pricing_rule': pricing_rule_name
1774 })))
1775 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301776
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301777 return pricing_rule_list
1778 }
1779 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301780
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301781 validate_warehouse: function () {
1782 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 +05301783 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301784 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301785 },
1786
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301787 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301788 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301789
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301790 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301791 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301792 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301793 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301794 }
1795
1796 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301797 },
1798
1799 update_customer_in_localstorage: function() {
1800 var me = this;
1801 try {
1802 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1803 } catch (e) {
1804 frappe.throw(__("LocalStorage is full , did not save"))
1805 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301806 }
1807})