blob: c02100029b56f3f7b8b713fe47e54b169200a570 [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 Waghchaure75177c02017-03-16 15:21:21 +053029 this.bind_items_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053030 this.si_docs = this.get_doc_from_localstorage();
31 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053032
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053033 beforeunload: function (e) {
34 if (this.connection_status == false && frappe.get_route()[0] == "pos") {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053035 e = e || window.event;
36
37 // For IE and Firefox prior to version 4
38 if (e) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053039 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 +053040 return
41 }
42
43 // For Safari
44 return __("You are in offline mode. You will not be able to reload until you have network.");
45 }
46 },
47
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053048 check_internet_connection: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053049 var me = this;
50 //Check Internet connection after every 30 seconds
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053051 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053052 me.set_indicator();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053053 }, 5000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053054 },
55
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053056 set_indicator: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053057 var me = this;
58 // navigator.onLine
Rohit Waghchauref2aa1762016-05-20 23:55:45 +053059 this.connection_status = false;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053060 this.page.set_indicator(__("Offline"), "grey")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053061 frappe.call({
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053062 method: "frappe.handler.ping",
63 callback: function (r) {
64 if (r.message) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053065 me.connection_status = true;
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +053066 me.page.set_indicator(__("Online"), "green")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053067 }
68 }
69 })
70 },
71
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053072 onload: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053073 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053074 this.get_data_from_server(function () {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053075 me.create_new();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053076 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053077 },
78
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053079 make_menu_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053080 var me = this;
81
Rohit Waghchaure75177c02017-03-16 15:21:21 +053082 this.page.clear_menu();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053083 this.page.add_menu_item(__("New Sales Invoice"), function () {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053084 me.save_previous_entry();
85 me.create_new();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053086 })
87
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053088 this.page.add_menu_item(__("Sync Master Data"), function () {
89 me.get_data_from_server(function () {
Rohit Waghchaureea278b52016-07-21 23:28:04 +053090 me.load_data(false);
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053091 me.make_customer();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +053092 me.make_item_list();
Rohit Waghchaureea278b52016-07-21 23:28:04 +053093 me.set_missing_values();
Rohit Waghchauree0934d12016-05-11 15:04:57 +053094 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053095 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +053096
rohitwaghchaurec13dbd42017-02-01 18:07:22 +053097 this.page.add_menu_item(__("Sync Offline Invoices"), function () {
Rohit Waghchaure82be0202016-10-04 12:46:37 +053098 me.sync_sales_invoice()
99 });
100
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530101 this.page.add_menu_item(__("POS Profile"), function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530102 frappe.set_route('List', 'POS Profile');
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530103 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 },
105
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530106 email_prompt: function() {
107 var me = this;
108 fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288},
109 {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"},
110 {fieldtype: "Section Break"},
111 {label:__("Subject"), fieldtype:"Data", reqd: 1,
112 fieldname:"subject",length:524288},
113 {fieldtype: "Section Break"},
114 {label:__("Message"), fieldtype:"Text Editor", reqd: 1,
115 fieldname:"content"},
116 {fieldtype: "Section Break"},
117 {fieldtype: "Column Break"}];
118
119 this.email_dialog = new frappe.ui.Dialog({
120 title: "Email",
121 fields: fields,
122 primary_action_label: __("Send"),
123 primary_action: function() {
124 me.send_action();
125 }
126 });
127
128 this.email_dialog.show()
129 },
130
131 send_action: function() {
132 this.email_queue = this.get_email_queue()
133 this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values())
134 this.update_email_queue()
135 this.email_dialog.hide()
136 },
137
138 update_email_queue: function () {
139 try {
140 localStorage.setItem('email_queue', JSON.stringify(this.email_queue));
141 } catch (e) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530142 frappe.throw(__("LocalStorage is full, did not save"))
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +0530143 }
144 },
145
146 get_email_queue: function () {
147 try {
148 return JSON.parse(localStorage.getItem('email_queue')) || {};
149 } catch (e) {
150 return {}
151 }
152 },
153
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530154 get_customers_details: function () {
155 try {
156 return JSON.parse(localStorage.getItem('customer_details')) || {};
157 } catch (e) {
158 return {}
159 }
160 },
161
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530162 dialog_actions: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530163 var me = this;
164
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530165 $(this.list_body).find('.list-select-all').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530166 me.removed_items = [];
167 $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked"))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530168 if ($(this).is(":checked")) {
169 $.each(me.si_docs, function (index, data) {
170 for (key in data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530171 me.removed_items.push(key)
172 }
173 })
174 }
175
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530176 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530177 })
178
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530179 $(this.list_body).find('.list-delete').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530180 me.name = $(this).parent().parent().attr('invoice-name');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530181 if ($(this).is(":checked")) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530182 me.removed_items.push(me.name);
183 } else {
184 me.removed_items.pop(me.name)
185 }
186
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530187 me.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530188 })
189 },
190
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530191 edit_record: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530192 var me = this;
193
194 doc_data = this.get_invoice_doc(this.si_docs);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530195 if (doc_data) {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530196 this.frm.doc = doc_data[0][this.name];
197 this.set_missing_values();
198 this.refresh(false);
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530199 this.toggle_input_field();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530200 this.list_dialog && this.list_dialog.hide();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530201 }
202 },
203
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530204 delete_records: function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530205 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530206 this.validate_list()
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530207 this.remove_doc_from_localstorage()
208 this.update_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530209 // this.dialog_actions();
210 this.toggle_delete_button();
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530211 },
212
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530213 validate_list: function() {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530214 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530215 this.si_docs = this.get_submitted_invoice()
216 $.each(this.removed_items, function(index, name){
217 $.each(me.si_docs, function(key, data){
218 if(me.si_docs[key][name] && me.si_docs[key][name].offline_pos_name == name ){
219 frappe.throw(__("Submitted orders can not be deleted"))
220 }
221 })
222 })
223 },
224
225 toggle_delete_button: function () {
226 var me = this;
227 if(this.pos_profile_data["allow_delete"]) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530228 if (this.removed_items && this.removed_items.length > 0) {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530229 $(this.page.wrapper).find('.btn-danger').show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530230 } else {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530231 $(this.page.wrapper).find('.btn-danger').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530232 }
233 }
234 },
235
236 get_doctype_status: function (doc) {
237 if (doc.docstatus == 0) {
238 return { status: "Draft", indicator: "red" }
239 } else if (doc.outstanding_amount == 0) {
240 return { status: "Paid", indicator: "green" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530241 } else {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530242 return { status: "Submitted", indicator: "blue" }
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +0530243 }
244 },
245
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530246 set_missing_values: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530247 var me = this;
248 doc = JSON.parse(localStorage.getItem('doc'))
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530249 if (this.frm.doc.payments.length == 0) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530250 this.frm.doc.payments = doc.payments;
Rohit Waghchaureb77a1052016-08-01 18:35:18 +0530251 this.calculate_outstanding_amount();
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530252 }
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530253
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530254 if (this.frm.doc.customer) {
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530255 this.party_field.$input.val(this.frm.doc.customer);
256 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530257
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530258 if (!this.frm.doc.write_off_account) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530259 this.frm.doc.write_off_account = doc.write_off_account
260 }
261
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530262 if (!this.frm.doc.account_for_change_amount) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +0530263 this.frm.doc.account_for_change_amount = doc.account_for_change_amount
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530264 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530265 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530266
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530267 get_invoice_doc: function (si_docs) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530268 var me = this;
269 this.si_docs = this.get_doc_from_localstorage();
270
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530271 return $.grep(this.si_docs, function (data) {
272 for (key in data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530273 return key == me.name
274 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530275 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530276 },
277
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530278 get_data_from_server: function (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530279 var me = this;
280 frappe.call({
281 method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data",
282 freeze: true,
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530283 freeze_message: __("Master data syncing, it might take some time"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530284 callback: function (r) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530285 localStorage.setItem('doc', JSON.stringify(r.message.doc));
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530286 me.init_master_data(r)
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530287 me.set_interval_for_si_sync();
288 me.check_internet_connection();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530289 if (callback) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530290 callback();
291 }
292 }
293 })
294 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530295
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530296 init_master_data: function (r) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530297 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530298 this.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530299 this.meta = r.message.meta;
300 this.item_data = r.message.items;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530301 this.item_groups = r.message.item_groups;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530302 this.customers = r.message.customers;
303 this.serial_no_data = r.message.serial_no_data;
304 this.batch_no_data = r.message.batch_no_data;
305 this.tax_data = r.message.tax_data;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530306 this.address = r.message.address || {};
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530307 this.price_list_data = r.message.price_list_data;
308 this.bin_data = r.message.bin_data;
309 this.pricing_rules = r.message.pricing_rules;
310 this.print_template = r.message.print_template;
311 this.pos_profile_data = r.message.pos_profile;
312 this.default_customer = r.message.default_customer || null;
rohitwaghchaure34e820d2016-12-20 16:54:24 +0530313 this.print_settings = locals[":Print Settings"]["Print Settings"];
Rohit Waghchaured567e572016-12-21 13:18:36 +0530314 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 +0530315 this.make_control()
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530316 },
317
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530318 save_previous_entry: function () {
319 if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) {
320 this.create_invoice();
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530321 }
322 },
323
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530324 create_new: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530325 var me = this;
Rohit Waghchauree0934d12016-05-11 15:04:57 +0530326 this.frm = {}
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530327 this.name = null;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530328 this.load_data(true);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530329 this.setup();
330 },
331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530332 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530333 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530334
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530335 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530336 this.actual_qty_dict = {};
337
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530338 if (load_doc) {
339 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530340 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530341
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530342 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530343 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530344 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530345 })
346
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530347 this.print_template_data = frappe.render_template("print_template", {
348 content: this.print_template,
349 title: "POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css,
350 print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer
351 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530352 },
353
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530354 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530355 this.make();
356 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530357 this.party_field.$input.attr('disabled', false);
358 if(this.selected_row) {
359 this.selected_row.hide()
360 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530361 },
362
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530363 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530364 var me = this;
365 this.party = party;
366 this.price_list = (party == "Customer" ?
367 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
368 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
369 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
370 },
371
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530372 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530373 this.make_item_list();
374 this.make_discount_field()
375 },
376
377 make_control: function() {
378 this.frm = {}
379 this.frm.doc = this.doc
380 this.set_transaction_defaults("Customer");
381 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530382 this.make_search();
383 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530384 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530385 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530386 },
387
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530388 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530389 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530390 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530391 df: {
392 "fieldtype": "Data",
393 "label": "Item",
394 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530395 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530396 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530397 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530398 only_input: true,
399 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530400
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530401 this.serach_item.make_input();
402 this.serach_item.$input.on("keyup", function () {
403 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530404 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530405 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530406 }, 1000);
407 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530408
Faris Ansari339d9c92017-03-07 18:14:06 +0530409 this.search_item_group = this.wrapper.find('.search-item-group');
410
411 var dropdown_html = me.item_groups.map(function(item_group) {
412 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
413 }).join("");
414
415 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
416
417 this.search_item_group.on('click', '.dropdown-menu a', function() {
418 me.selected_item_group = $(this).attr('data-value');
419 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
420
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530421 me.page_len = 20;
422 me.items = me.get_items();
423 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530424 })
425
Faris Ansari45510102017-03-09 14:43:37 +0530426 me.toggle_more_btn();
427
428 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530429 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530430 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530431 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530432 me.toggle_more_btn();
433 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530434
Faris Ansari45510102017-03-09 14:43:37 +0530435 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530436 me.update_customer()
437 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530438 },
439
Faris Ansari45510102017-03-09 14:43:37 +0530440 toggle_more_btn: function() {
441 if(!this.items || this.items.length <= this.page_len) {
442 this.wrapper.find(".btn-more").hide();
443 } else {
444 this.wrapper.find(".btn-more").show();
445 }
446 },
447
448 toggle_totals_area: function(show) {
449
Faris Ansari07c9f352017-03-09 17:03:14 +0530450 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530451 show = this.is_totals_area_collapsed;
452 }
453
454 var totals_area = this.wrapper.find('.totals-area');
455 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
456 .toggle(show);
457
458 if(show) {
459 totals_area.find('.collapse-btn i')
460 .removeClass('octicon-chevron-down')
461 .addClass('octicon-chevron-up');
462 } else {
463 totals_area.find('.collapse-btn i')
464 .removeClass('octicon-chevron-up')
465 .addClass('octicon-chevron-down');
466 }
467
468 this.is_totals_area_collapsed = !show;
469 },
470
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530471 make_list_customers: function () {
472 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530473 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530474 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530475 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530476 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530477 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530478
Faris Ansari45510102017-03-09 14:43:37 +0530479 me.render_list_customers();
480 me.toggle_totals_area(false);
481
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530482 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530483 $(this).toggleClass("view_customer");
484 if($(this).hasClass("view_customer")) {
485 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530486 me.list_customers.show();
487 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530488 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530489 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530490 } else {
491 if(me.frm.doc.docstatus == 0) {
492 me.party_field.$input.attr('disabled', false);
493 }
494 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530495 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530496 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530497 me.list_customers.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530498 if(me.frm.doc.items.length > 0) {
499 me.numeric_keypad.show();
500 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530501 }
502 });
503 this.add_customer_btn.on('click', function() {
504 me.save_previous_entry();
505 me.create_new();
506 me.refresh();
507 me.set_focus();
508 });
Faris Ansari45510102017-03-09 14:43:37 +0530509 this.pos_bill.on('click', '.collapse-btn', function() {
510 me.toggle_totals_area();
511 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530512 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530513
514 bind_numeric_keypad: function() {
515 var me = this;
516 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
517 me.numeric_val = '';
518 })
519
520 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
521 me.numeric_id = $(this).attr("id") || me.numeric_id;
522 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530523 if(me.numeric_id) {
524 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
525 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530526
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530527 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530528 me.numeric_val += me.val;
529 me.selected_field.val(flt(me.numeric_val))
530 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530531 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530532 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530533
534 if(me.numeric_id && $(this).hasClass('pos-operation')) {
535 me.numeric_keypad.find('button.pos-operation').removeClass('active');
536 $(this).addClass('active');
537
538 me.selected_row.find('.pos-list-row').removeClass('active');
539 me.selected_field.closest('.pos-list-row').addClass('active');
540 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530541 })
542
543 $(this.numeric_keypad).find('.numeric-del').click(function(){
544 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
545 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
546 me.selected_field.val(me.numeric_val);
547 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530548 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530549 })
550
551 $(this.numeric_keypad).find('.pos-pay').click(function(){
552 me.validate();
553 me.update_paid_amount_status(true);
554 me.create_invoice();
555 me.make_payment();
556 })
557 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530558
559 render_list_customers: function () {
560 var me = this;
561
562 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530563 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530564 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530565 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530566 return;
567 }
Faris Ansari45510102017-03-09 14:43:37 +0530568
569 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530570 if(this.si_docs.length) {
571 this.si_docs.forEach(function (data, i) {
572 for (key in data) {
573 html += frappe.render_template("pos_invoice_list", {
574 sr: i + 1,
575 name: key,
576 customer: data[key].customer,
577 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
578 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
579 data: me.get_doctype_status(data[key])
580 });
581 }
582 });
583 }
Faris Ansari45510102017-03-09 14:43:37 +0530584 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530585
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530586 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530587 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530588 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530589 me.list_customers_btn.toggleClass("view_customer");
590 me.pos_bill.show();
591 me.list_customers_btn.show();
592 me.name = $(this).parents().attr('invoice-name')
593 me.edit_record();
594 })
595
596 //actions
597 $(this.wrapper).find('.list-select-all').click(function () {
598 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
599 me.removed_items = [];
600 if ($(this).is(":checked")) {
601 $.each(me.si_docs, function (index, data) {
602 for (key in data) {
603 me.removed_items.push(key)
604 }
605 });
606 }
607
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530608 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530609 });
610
611 $(this.wrapper).find('.list-delete').click(function () {
612 me.name = $(this).parent().parent().attr('invoice-name');
613 if ($(this).is(":checked")) {
614 me.removed_items.push(me.name);
615 } else {
616 me.removed_items.pop(me.name)
617 }
618
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530619 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530620 });
621 },
622
623 bind_delete_event: function() {
624 var me = this;
625
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530626 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530627 frappe.confirm(__("Delete permanently?"), function () {
628 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530629 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530630 me.render_list_customers();
631 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530632 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530633 },
634
635 set_focus: function () {
636 if (this.default_customer || this.frm.doc.customer) {
637 this.serach_item.$input.focus();
638 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530639 this.party_field.$input.focus();
640 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530641 },
642
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530643 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530644 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530645
646 if(!this.party_field) {
647 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
648 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530649 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530650 })).insertAfter(this.page.$title_area.hide());
651 }
652
653 this.party_field = frappe.ui.form.make_control({
654 df: {
655 "fieldtype": "Data",
656 "options": this.party,
657 "label": this.party,
658 "fieldname": this.party.toLowerCase(),
659 "placeholder": __("Select or add new customer")
660 },
661 parent: this.page.wrapper.find(".party-area"),
662 only_input: true,
663 });
664
665 this.party_field.make_input();
666 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530667 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530668 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530669
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530670 if (this.default_customer && !this.frm.doc.customer) {
Rohit Waghchaure539e9132016-07-07 16:44:40 +0530671 this.party_field.$input.val(this.default_customer);
672 this.frm.doc.customer = this.default_customer;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530673 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530674
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530675 this.party_field.awesomeplete =
676 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530677 minChars: 0,
678 maxItems: 99,
679 autoFirst: true,
680 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530681 filter: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530682 var value = item.value.toLowerCase();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530683 if (value.indexOf('is_action') !== -1 ||
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530684 value.indexOf(input) !== -1) {
685 return true;
686 }
687 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530688 item: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530689 var d = item;
Faris Ansari45510102017-03-09 14:43:37 +0530690 var html = "<span>" + __(d.label || d.value) + "</span>";
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530691 return $('<li></li>')
692 .data('item.autocomplete', d)
693 .html('<a><p>' + html + '</p></a>')
694 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530695 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530696 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530697 var customers = this.customers.map(function (c) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530698 return {
699 label: c.name,
700 value: c.name,
701 customer_group: c.customer_group,
702 territory: c.territory
703 }
704 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530705
706 customers.push({
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530707 label: "<span class='text-primary link-option'>"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530708 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
709 + __("Create a new Customer")
710 + "</span>",
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530711 value: 'is_action',
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530712 action: me.add_customer
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530713 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530714 this.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530715
716 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530717 .on('input', function (e) {
718 me.party_field.awesomeplete.list = customers;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530719 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530720 .on('awesomplete-select', function (e) {
721 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530722 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530723 if (!customer) return;
724 // create customer link
725 if (customer.action) {
726 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530727 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530728 }
Faris Ansari45510102017-03-09 14:43:37 +0530729 me.toggle_list_customer(false);
730 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530731 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530732 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530733 me.set_focus();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530734 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530735 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530736 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530737 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530738
739 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530740 me.toggle_list_customer(false)
741 me.toggle_item_cart(true)
742 } else {
743 me.toggle_list_customer(true)
744 me.toggle_item_cart(false)
745 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530746 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530747 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530748 var item = me.party_field.awesomeplete
749 .get_item(e.originalEvent.text.value);
750 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530751 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530752 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530753 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530754 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530755 },
756
Faris Ansari45510102017-03-09 14:43:37 +0530757 toggle_edit_button: function(flag) {
758 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
759 },
760
761 toggle_list_customer: function(flag) {
762 this.list_customers.toggle(flag);
763 },
764
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530765 toggle_item_cart: function(flag) {
766 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
767 },
768
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530769 add_customer: function() {
770 this.frm.doc.customer = "";
771 this.update_customer()
772 },
773
774 update_customer: function () {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530775 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530776 if (!this.connection_status) return;
Rushabh Mehta37146262017-02-01 18:17:35 +0530777
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530778 this.customer_doc = new frappe.ui.Dialog({
779 'title': 'Customer',
780 fields: [
781 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530782 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530783 "fieldname": "full_name",
784 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530785 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530786 },
787 {
788 "fieldtype": "Section Break"
789 },
790 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530791 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530792 "fieldname": "email_id",
793 "fieldtype": "Data"
794 },
795 {
796 "fieldtype": "Column Break"
797 },
798 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530799 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530800 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530801 "fieldtype": "Data"
802 },
803 {
804 "fieldtype": "Section Break"
805 },
806 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530807 "label": __("Address Name"),
808 "read_only": 1,
809 "fieldname": "name",
810 "fieldtype": "Data"
811 },
812 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530813 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530814 "fieldname": "address_line1",
815 "fieldtype": "Data"
816 },
817 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530818 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530819 "fieldname": "address_line2",
820 "fieldtype": "Data"
821 },
822 {
823 "fieldtype": "Column Break"
824 },
825 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530826 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530827 "fieldname": "city",
828 "fieldtype": "Data"
829 },
830 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530831 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530832 "fieldname": "state",
833 "fieldtype": "Data"
834 },
835 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530836 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530837 "fieldname": "pincode",
838 "fieldtype": "Data"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530839 }
840 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530841 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530842
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530843 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530844 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530845
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530846 this.customer_doc.set_primary_action(__("Save"), function () {
847 me.make_offline_customer();
848 me.pos_bill.show();
849 });
850 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530851
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530852 render_address_data: function() {
853 var me = this;
854 this.address_data = this.address[this.frm.doc.customer] || this.get_address_from_localstorage();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530855 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530856
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530857 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
858 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530859 }
860 },
861
862 get_address_from_localstorage: function() {
863 this.address_details = this.get_customers_details()
864 return this.address_details[this.frm.doc.customer]
865 },
866
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530867 make_offline_customer: function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530868 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530869 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530870 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530871 this.party_field.$input.val(this.frm.doc.customer);
872 this.customers.push({
873 name: this.frm.doc.customer,
874 customer_name: this.frm.doc.customer
875 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530876 this.update_customer_in_localstorage()
877 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530878 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530879 },
880
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530881 get_prompt_details: function() {
882 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530883 this.prompt_details['country'] = this.pos_profile_data.country;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530884 return JSON.stringify(this.prompt_details)
885 },
886
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530887 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530888 var me = this;
889 this.frm.doc.customer = doc.label || doc.name;
890 this.frm.doc.customer_name = doc.customer_name;
891 this.frm.doc.customer_group = doc.customer_group;
892 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530893 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530894 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530895 },
896
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530897 get_customers: function (key) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530898 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530899 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530900 var re = new RegExp('%', 'g');
901 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
902
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530903 if (key) {
904 return $.grep(this.customers, function (data) {
905 if (reg.test(data.name.toLowerCase())
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530906 || reg.test(data.customer_name.toLowerCase())
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530907 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530908 return data
909 }
910 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530911 } else {
912 customers = this.customers.sort(function (a, b) { return a.idx < b.idx })
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530913 return customers.slice(0, 20)
914 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530915 },
916
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530917 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530918 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530919 if (!this.price_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530920 msgprint(__("Price List not found or disabled"));
921 return;
922 }
923
924 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530925
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530926 var $wrap = me.wrapper.find(".item-list");
927 me.wrapper.find(".item-list").empty();
928
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530929 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530930 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530931 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530932 $(frappe.render_template("pos_item", {
933 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530934 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530935 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +0530936 item_image: obj.image,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530937 color: frappe.get_palette(obj.item_name),
938 abbr: frappe.get_abbr(obj.item_name)
939 })).tooltip().appendTo($wrap);
940 }
941 });
Faris Ansari45510102017-03-09 14:43:37 +0530942
943 $wrap.append(`
944 <div class="image-view-item btn-more text-muted text-center">
945 <div class="image-view-body">
946 <i class="mega-octicon octicon-package"></i>
947 <div>Load more items</div>
948 </div>
949 </div>
950 `);
951
952 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530953 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +0530954 $("<p class='text-muted small' style='padding-left: 10px'>"
955 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530956 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530957
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530958 if (this.items.length == 1
959 && this.serach_item.$input.val()) {
960 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530961 this.add_to_cart();
962 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530963 },
964
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530965 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530966 // To search item as per the key enter
967
968 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +0530969 this.item_serial_no = {};
970 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530971
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530972 if (item_code) {
973 return $.grep(this.item_data, function (item) {
974 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530975 return true
976 }
977 })
978 }
Rushabh Mehta37146262017-02-01 18:17:35 +0530979
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530980 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530981
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530982 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530983 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530984 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530985 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530986
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530987 if (key) {
988 return $.grep(this.items_list, function (item) {
989 if (search_status) {
990 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530991 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530992 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
993 } else if (me.serial_no_data[item.item_code]
994 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530995 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530996 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 +0530997 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530998 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +0530999 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301000 return item.barcode == me.serach_item.$input.val();
1001 } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
1002 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301003 return true
1004 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301005 }
1006 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301007 } else {
1008 return this.items_list;
1009 }
1010 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301011
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301012 apply_category: function() {
1013 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301014 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301015
1016 if(category == 'All Item Groups') {
1017 return this.item_data
1018 } else {
1019 return this.item_data.filter(function(element, index, array){
1020 return element.item_group == category;
1021 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301022 }
1023 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301024
1025 bind_items_event: function() {
1026 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301027 $(this.wrapper).on('click', '.pos-bill-item', function() {
1028 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1029 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301030 me.numeric_val = "";
1031 me.numeric_id = ""
1032 me.item_code = $(this).attr("data-item-code");
1033 me.render_selected_item()
1034 me.bind_qty_event()
1035 me.update_rate()
1036 $(me.wrapper).find(".selected-item").scrollTop(1000);
1037 })
1038 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301039
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301040 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301041 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301042
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301043 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301044 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301045 var qty = $(this).val();
1046 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301047 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301048 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301049
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301050 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301051 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1052 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301053 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301054 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301055
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301056 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301057 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1058 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301059 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301060 })
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301061
1062 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301063 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301064 var discount = $(this).val();
1065 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301066 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301067 })
1068 },
1069
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301070 bind_events: function() {
1071 var me = this;
1072 // if form is local then allow this function
1073 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1074 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
1075 if(me.list_customers_btn.hasClass("view_customer")) return;
1076
1077 me.customer_validate();
1078 if (me.frm.doc.docstatus == 0) {
1079 me.items = me.get_items($(this).attr("data-item-code"))
1080 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301081 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301082 }
1083 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301084
1085 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301086 },
1087
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301088 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301089 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301090 this.items = this.get_items(item_code);
1091 this.validate_serial_no()
1092 this.set_item_details(item_code, "qty", qty);
1093 },
1094
1095 update_discount: function(item_code, discount) {
1096 var me = this;
1097 this.items = this.get_items(item_code);
1098 this.set_item_details(item_code, "discount_percentage", discount);
1099 },
1100
1101 update_rate: function () {
1102 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301103 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301104 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301105 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301106 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301107 })
1108 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301109
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301110 update_value: function() {
1111 var me = this;
1112 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1113 "rate": ".pos-item-price", "amount": ".pos-amount"}
1114 this.child_doc = this.get_child_item(this.item_code);
1115
1116 if(me.child_doc.length) {
1117 $.each(fields, function(key, field) {
1118 $(me.selected_row).find(field).val(me.child_doc[0][key])
1119 })
1120 } else {
1121 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301122 }
1123 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301124
1125 clear_selected_row: function() {
1126 $(this.wrapper).find('.selected-item').empty();
1127 },
1128
1129 render_selected_item: function() {
1130 this.child_doc = this.get_child_item(this.item_code);
1131 $(this.wrapper).find('.selected-item').empty();
1132 if(this.child_doc.length) {
1133 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1134 $(this.wrapper).find('.selected-item').html(this.selected_row)
1135 }
1136
1137 $(this.selected_row).find('.form-control').click(function(){
1138 $(this).select();
1139 })
1140 },
1141
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301142 get_child_item: function(item_code) {
1143 var me = this;
1144 return $.map(me.frm.doc.items, function(doc){
1145 if(doc.item_code == item_code) {
1146 return doc
1147 }
1148 })
1149 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301150
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301151 set_item_details: function (item_code, field, value) {
1152 var me = this;
1153 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301154 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301155 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301156
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301157 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301158 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301159 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301160 if (d.serial_no && field == 'qty') {
1161 me.validate_serial_no_qty(d, item_code, field, value)
1162 }
1163
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301164 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301165 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301166 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301167 me.remove_item.push(d.idx)
1168 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301169
1170 if(field=="discount_percentage" && value == 0) {
1171 d.rate = d.price_list_rate;
1172 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301173 }
1174 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301175
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301176 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301177 this.remove_zero_qty_item();
1178 }
1179
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301180 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301181 },
1182
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301183 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301184 var me = this;
1185 idx = 0
1186 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301187 $.each(this.frm.doc["items"] || [], function (i, d) {
1188 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301189 d.idx = idx;
1190 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301191 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301192 }
1193 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301194
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301195 this.frm.doc["items"] = this.items;
1196 },
1197
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301198 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301199 var me = this;
1200
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301201 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301202 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
1203 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301204
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301205 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301206 total = me.frm.doc.net_total
1207 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301208
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301209 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 +05301210 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1211 me.refresh();
1212 });
1213
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301214 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301215 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1216 me.frm.doc.additional_discount_percentage = 0.0;
1217 me.wrapper.find('input.discount-percentage').val(0);
1218 me.refresh();
1219 });
1220 },
1221
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301222 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301223 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301224 if (!this.frm.doc.customer) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301225 frappe.throw(__("Please select customer"))
1226 }
1227 },
1228
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301229 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301230 var me = this;
1231 var caught = false;
1232 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1233
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301234 this.customer_validate();
1235 this.mandatory_batch_no();
1236 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301237 this.validate_warehouse();
1238
1239 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301240 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301241 if (d.item_code == me.items[0].item_code) {
1242 caught = true;
1243 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301244 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301245 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301246 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1247 d.warehouse = me.item_serial_no[d.item_code][1]
1248 }
1249
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301250 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301251 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301252 }
1253 }
1254 });
1255 }
1256
1257 // if item not found then add new item
1258 if (!caught)
1259 this.add_new_item_to_grid();
1260
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301261 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301262 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301263 },
1264
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301265 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301266 var me = this;
1267 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1268 this.child.item_code = this.items[0].item_code;
1269 this.child.item_name = this.items[0].item_name;
1270 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301271 this.child.brand = this.items[0].brand;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301272 this.child.description = this.items[0].description;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301273 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301274 this.child.qty = 1;
1275 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301276 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1277 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301278 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301279 ? 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 +05301280 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1281 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1282 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301283 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301284 this.child.batch_no = this.item_batch_no[this.child.item_code];
1285 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1286 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301287 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301288 },
1289
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301290 update_paid_amount_status: function (update_paid_amount) {
1291 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301292 update_paid_amount = update_paid_amount ? false : true;
1293 }
1294
1295 this.refresh(update_paid_amount);
1296 },
1297
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301298 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301299 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301300 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 this.set_primary_action();
1302 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301303
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301304 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301305 this.apply_pricing_rule();
1306 this.discount_amount_applied = false;
1307 this._calculate_taxes_and_totals();
1308 this.calculate_discount_amount();
1309 this.show_items_in_item_cart();
1310 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301311 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301312 this.set_totals();
1313 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301314
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301315 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301316 return erpnext.get_currency(this.frm.doc.company);
1317 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301318
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301319 show_item_wise_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301320 return null;
1321 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301322
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301323 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301324 var me = this;
1325 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301326 var $no_items_message = this.wrapper.find(".no-items-message");
1327 $no_items_message.toggle(this.frm.doc.items.length === 0);
1328
1329 var $totals_area = this.wrapper.find('.totals-area');
1330 $totals_area.toggle(this.frm.doc.items.length > 0);
1331
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301332 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301333 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301334 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301335 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301336 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301337 discount_percentage: d.discount_percentage || 0.0,
1338 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301339 projected_qty: d.projected_qty,
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301340 rate: format_number(d.rate, me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301341 enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301342 amount: format_currency(d.amount, me.frm.doc.currency)
1343 })).appendTo($items);
1344 });
1345
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301346 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 $(this).select();
1348 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301349
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301350 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301351 $(this).select();
1352 });
1353
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301354 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301355 $(this).select();
1356 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301357 },
1358
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301359 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301360 var me = this;
1361 me.frm.doc.total_taxes_and_charges = 0.0
1362
1363 var taxes = this.frm.doc.taxes || [];
1364 $(this.wrapper)
1365 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1366 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301367
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301368 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301369 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1370 $(frappe.render_template("pos_tax_row", {
1371 description: d.description,
1372 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1373 me.frm.doc.currency)
1374 })).appendTo(me.wrapper.find(".tax-table"));
1375 }
1376 });
1377 },
1378
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301379 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301380 var me = this;
1381 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1382 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1383 },
1384
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301385 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301386 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301387 this.page.set_primary_action(__("New Cart"), function () {
1388 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301389 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301390 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301391
1392 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301393 this.page.set_secondary_action(__("Print"), function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301394 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301395 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301396 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301397 this.page.add_menu_item(__("Email"), function () {
1398 me.email_prompt()
1399 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301400 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301401 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301402
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301403 make_new_cart: function (){
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301404 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301405 this.save_previous_entry();
1406 this.create_new();
1407 this.refresh();
1408 this.toggle_input_field();
1409 this.render_list_customers();
1410 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301411 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301412
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301413 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301414 var me = this;
1415
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301416 this.msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301417 style="margin-right: 5px;">{0}</a>\
1418 <a class="btn btn-default new_doc">{1}</a>', [
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301419 __('Print'), __('New')
1420 ]));
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301421
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301422 $('.print_doc').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301423 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301424 me.print_document(html)
1425 })
1426
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301427 $('.new_doc').click(function () {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301428 me.msgprint.hide()
1429 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301430 })
1431 },
1432
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301433 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301434 var w = window.open();
1435 w.document.write(html);
1436 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301437 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301438 w.print();
1439 w.close();
1440 }, 1000)
1441 },
1442
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301443 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301444 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301445 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301446 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301447 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301448 this.print_dialog()
1449 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301450 },
1451
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301452 update_serial_no: function() {
1453 var me = this;
1454
1455 //Remove the sold serial no from the cache
1456 $.each(this.frm.doc.items, function(index, data) {
1457 sn = data.serial_no.split('\n')
1458 if(sn.length) {
1459 serial_no_list = me.serial_no_data[data.item_code]
1460 $.each(sn, function(i, serial_no) {
1461 if(in_list(Object.keys(serial_no_list), serial_no)) {
1462 delete serial_no_list[serial_no]
1463 }
1464 })
1465 me.serial_no_data[data.item_code] = serial_no_list;
1466 }
1467 })
1468 },
1469
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301470 change_status: function () {
1471 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301472 this.frm.doc.docstatus = 1;
1473 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301474 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301475 }
1476 },
1477
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301478 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301479 var pointer_events = 'inherit'
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301480 disabled = this.frm.doc.docstatus == 1 ? true: false;
1481 $(this.wrapper).find('input').attr("disabled", disabled);
1482 $(this.wrapper).find('select').attr("disabled", disabled);
1483 $(this.wrapper).find('input').attr("disabled", disabled);
1484 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301485 $(this.wrapper).find('button').attr("disabled", disabled);
1486 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301487
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301488 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301489 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301490 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301491
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301492 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301493 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1494 this.set_primary_action();
1495 },
1496
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301497 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301498 var me = this;
1499 var invoice_data = {}
1500 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301501 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301502 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301503 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301504 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301505 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301506 this.frm.doc.posting_date = frappe.datetime.get_today();
1507 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301508 invoice_data[this.name] = this.frm.doc
1509 this.si_docs.push(invoice_data)
1510 this.update_localstorage();
1511 this.set_primary_action();
1512 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301513 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301514 },
1515
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301516 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301517 var me = this;
1518 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301519 $.each(this.si_docs, function (index, data) {
1520 for (key in data) {
1521 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301522 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301523 me.update_localstorage();
1524 }
1525 }
1526 })
1527 },
1528
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301529 update_localstorage: function () {
1530 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301531 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301532 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301533 frappe.throw(__("LocalStorage is full , did not save"))
1534 }
1535 },
1536
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301537 get_doc_from_localstorage: function () {
1538 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301539 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301540 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301541 return []
1542 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301543 },
1544
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301545 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301546 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301547 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301548 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301549 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301550 },
1551
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301552 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301553 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301554 this.si_docs = this.get_submitted_invoice() || [];
1555 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301556 this.customers_list = this.get_customers_details() || {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301557
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301558 if (this.si_docs.length || this.email_queue_list || this.customers_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301559 frappe.call({
1560 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1561 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301562 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301563 email_queue_list: me.email_queue_list,
1564 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301565 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301566 callback: function (r) {
1567 if (r.message) {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301568 me.removed_items = r.message.invoice;
1569 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301570 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301571 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301572 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301573 me.remove_customer_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301574 }
1575 }
1576 })
1577 }
1578 },
1579
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301580 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301581 var invoices = [];
1582 var index = 1;
1583 docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301584 if (docs) {
1585 invoices = $.map(docs, function (data) {
1586 for (key in data) {
1587 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301588 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301589 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301590 return data
1591 }
1592 }
1593 });
1594 }
1595
1596 return invoices
1597 },
1598
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301599 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301600 var me = this;
1601 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301602 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301603 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301604 $.each(this.si_docs, function (index, data) {
1605 for (key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301606 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301607 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301608 }
1609 }
1610 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301611 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301612 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301613 this.update_localstorage();
1614 }
1615 },
1616
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301617 remove_email_queue_from_localstorage: function() {
1618 var me = this;
1619 this.email_queue = this.get_email_queue()
1620 if (this.removed_email) {
1621 $.each(this.email_queue_list, function (index, data) {
1622 if (in_list(me.removed_email, index)) {
1623 delete me.email_queue[index]
1624 }
1625 })
1626 this.update_email_queue();
1627 }
1628 },
1629
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301630 remove_customer_from_localstorage: function() {
1631 var me = this;
1632 this.customer_details = this.get_customers_details()
1633 if (this.removed_customers) {
1634 $.each(this.customers_list, function (index, data) {
1635 if (in_list(me.removed_customers, index)) {
1636 delete me.customer_details[index]
1637 }
1638 })
1639 this.update_customer_in_localstorage();
1640 }
1641 },
1642
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301643 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301644 var me = this;
1645 this.customer_validate();
1646 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301647 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301648 },
1649
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301650 item_validate: function () {
1651 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301652 frappe.throw(__("Select items to save the invoice"))
1653 }
1654 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301655
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301656 validate_mode_of_payments: function () {
1657 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301658 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1659 }
1660 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301661
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301662 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301663 var me = this;
1664 var item_code = serial_no = '';
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301665 for (key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301666 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301667 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301668 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301669
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301670 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301671 this.refresh();
1672 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1673 'item': this.items[0].item_code
1674 })))
1675 }
1676
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301677 if (item_code && serial_no) {
1678 $.each(this.frm.doc.items, function (index, data) {
1679 if (data.item_code == item_code) {
1680 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301681 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1682 'serial_no': serial_no
1683 })))
1684 }
1685 }
1686 })
1687 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301688 },
1689
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301690 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301691 var me = this;
1692 if (args.item_code == item_code && args.serial_no
1693 && field == 'qty' && cint(value) != value) {
1694 args.qty = 0.0;
1695 this.refresh();
1696 frappe.throw(__("Serial no item cannot be a fraction"))
1697 }
1698
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301699 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301700 args.qty = 0.0;
1701 args.serial_no = ''
1702 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301703 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1704 'item': item_code
1705 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301706 }
1707 },
1708
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301709 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301710 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301711 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301712 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1713 'item': this.items[0].item_code
1714 })))
1715 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301716 },
1717
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301718 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301719 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301720 $.each(this.frm.doc["items"], function (n, item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301721 pricing_rule = me.get_pricing_rule(item)
1722 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301723 if (pricing_rule.length) {
1724 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301725 item.margin_type = pricing_rule[0].margin_type;
1726 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1727 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1728 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301729 me.apply_pricing_rule_on_item(item)
1730 } else if (item.pricing_rule) {
1731 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301732 item.margin_rate_or_amount = 0.0;
1733 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301734 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301735 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301736 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301737
1738 if(item.discount_percentage > 0) {
1739 me.apply_pricing_rule_on_item(item)
1740 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301741 })
1742 },
1743
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301744 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301745 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301746 return $.grep(this.pricing_rules, function (data) {
1747 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301748 if (data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group) || item.brand == data.brand) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301749 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301750 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301751 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301752 return true
1753 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301754 }
1755 }
1756 })
1757 },
1758
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301759 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301760 //This method check condition based on applicable for
1761 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301762 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301763 return true
1764 }
1765 },
1766
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301767 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301768 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301769 'Customer': [data.customer, [this.frm.doc.customer]],
1770 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1771 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301772 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301773 }
1774 },
1775
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301776 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301777 //This method validate duplicate pricing rule
1778 var pricing_rule_name = '';
1779 var priority = 0;
1780 var pricing_rule_list = [];
1781 var priority_list = []
1782
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301783 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301784
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301785 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301786 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301787 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301788 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301789 priority = data.priority
1790 pricing_rule_list.push(data)
1791 }
1792 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301793
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301794 count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301795 $.each(priority_list, function (index, value) {
1796 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301797 count++
1798 }
1799 })
1800
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301801 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301802 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1803 'pricing_rule': pricing_rule_name
1804 })))
1805 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301806
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301807 return pricing_rule_list
1808 }
1809 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301810
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301811 validate_warehouse: function () {
1812 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 +05301813 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301814 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301815 },
1816
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301817 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301818 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301819
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301820 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301821 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301822 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301823 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301824 }
1825
1826 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301827 },
1828
1829 update_customer_in_localstorage: function() {
1830 var me = this;
1831 try {
1832 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1833 } catch (e) {
1834 frappe.throw(__("LocalStorage is full , did not save"))
1835 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301836 }
1837})