blob: c632e459e34528526bf56046563c5be8f151fba7 [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();
Rohit Waghchaure61165122017-05-02 13:04:08 +0530330 this.set_default_customer()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530331 },
332
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530333 load_data: function (load_doc) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530334 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530335
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530336 this.items = this.item_data;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +0530337 this.actual_qty_dict = {};
338
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530339 if (load_doc) {
340 this.frm.doc = JSON.parse(localStorage.getItem('doc'));
Rohit Waghchaureea278b52016-07-21 23:28:04 +0530341 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530342
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530343 $.each(this.meta, function (i, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530344 frappe.meta.sync(data)
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530345 locals["DocType"][data.name] = data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530346 })
347
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530348 this.print_template_data = frappe.render_template("print_template", {
349 content: this.print_template,
350 title: "POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css,
351 print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer
352 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530353 },
354
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530355 setup: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530356 this.make();
357 this.set_primary_action();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530358 this.party_field.$input.attr('disabled', false);
359 if(this.selected_row) {
360 this.selected_row.hide()
361 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530362 },
363
Rohit Waghchaure61165122017-05-02 13:04:08 +0530364 set_default_customer: function() {
365 if (this.default_customer && !this.frm.doc.customer) {
366 this.party_field.$input.val(this.default_customer);
367 this.frm.doc.customer = this.default_customer;
368 this.numeric_keypad.show();
369 this.toggle_list_customer(false)
370 this.toggle_item_cart(true)
371 }
372 },
373
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530374 set_transaction_defaults: function (party) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530375 var me = this;
376 this.party = party;
377 this.price_list = (party == "Customer" ?
378 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list);
379 this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list");
380 this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase");
381 },
382
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530383 make: function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530384 this.make_item_list();
385 this.make_discount_field()
386 },
387
388 make_control: function() {
389 this.frm = {}
390 this.frm.doc = this.doc
391 this.set_transaction_defaults("Customer");
392 this.wrapper.html(frappe.render_template("pos", this.frm.doc));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530393 this.make_search();
394 this.make_customer();
Faris Ansari339d9c92017-03-07 18:14:06 +0530395 this.make_list_customers();
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530396 this.bind_numeric_keypad();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530397 },
398
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530399 make_search: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530400 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530401 this.serach_item = frappe.ui.form.make_control({
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530402 df: {
403 "fieldtype": "Data",
404 "label": "Item",
405 "fieldname": "pos_item",
Rohit Waghchaure26cf01a2016-07-22 13:24:33 +0530406 "placeholder": __("Search Item")
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530407 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530408 parent: this.wrapper.find(".search-item"),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530409 only_input: true,
410 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530411
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530412 this.serach_item.make_input();
413 this.serach_item.$input.on("keyup", function () {
414 setTimeout(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530415 me.items = me.get_items();
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +0530416 me.make_item_list();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530417 }, 1000);
418 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530419
Faris Ansari339d9c92017-03-07 18:14:06 +0530420 this.search_item_group = this.wrapper.find('.search-item-group');
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530421 sorted_item_groups = this.get_sorted_item_groups()
422 var dropdown_html = sorted_item_groups.map(function(item_group) {
Faris Ansari339d9c92017-03-07 18:14:06 +0530423 return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>";
424 }).join("");
425
426 this.search_item_group.find('.dropdown-menu').html(dropdown_html);
427
428 this.search_item_group.on('click', '.dropdown-menu a', function() {
429 me.selected_item_group = $(this).attr('data-value');
430 me.search_item_group.find('.dropdown-text').text(me.selected_item_group);
431
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530432 me.page_len = 20;
433 me.items = me.get_items();
434 me.make_item_list();
Faris Ansari339d9c92017-03-07 18:14:06 +0530435 })
436
Faris Ansari45510102017-03-09 14:43:37 +0530437 me.toggle_more_btn();
438
439 this.wrapper.on("click", ".btn-more", function() {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530440 me.page_len += 20;
Rushabh Mehta37146262017-02-01 18:17:35 +0530441 me.items = me.get_items();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530442 me.make_item_list();
Faris Ansari45510102017-03-09 14:43:37 +0530443 me.toggle_more_btn();
444 });
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530445
Faris Ansari45510102017-03-09 14:43:37 +0530446 this.page.wrapper.on("click", ".edit-customer-btn", function() {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530447 me.update_customer()
448 })
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530449 },
450
Rohit Waghchaured7de3c62017-04-17 13:36:08 +0530451 get_sorted_item_groups: function() {
452 list = {}
453 $.each(this.item_groups, function(i, data) {
454 list[i] = data[0]
455 })
456
457 return Object.keys(list).sort(function(a,b){return list[a]-list[b]})
458 },
459
Faris Ansari45510102017-03-09 14:43:37 +0530460 toggle_more_btn: function() {
461 if(!this.items || this.items.length <= this.page_len) {
462 this.wrapper.find(".btn-more").hide();
463 } else {
464 this.wrapper.find(".btn-more").show();
465 }
466 },
467
468 toggle_totals_area: function(show) {
469
Faris Ansari07c9f352017-03-09 17:03:14 +0530470 if(show === undefined) {
Faris Ansari45510102017-03-09 14:43:37 +0530471 show = this.is_totals_area_collapsed;
472 }
473
474 var totals_area = this.wrapper.find('.totals-area');
475 totals_area.find('.net-total-area, .tax-area, .discount-amount-area')
476 .toggle(show);
477
478 if(show) {
479 totals_area.find('.collapse-btn i')
480 .removeClass('octicon-chevron-down')
481 .addClass('octicon-chevron-up');
482 } else {
483 totals_area.find('.collapse-btn i')
484 .removeClass('octicon-chevron-up')
485 .addClass('octicon-chevron-down');
486 }
487
488 this.is_totals_area_collapsed = !show;
489 },
490
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530491 make_list_customers: function () {
492 var me = this;
Faris Ansari339d9c92017-03-07 18:14:06 +0530493 this.list_customers_btn = this.page.wrapper.find('.list-customers-btn');
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530494 this.add_customer_btn = this.wrapper.find('.add-customer-btn');
Faris Ansari339d9c92017-03-07 18:14:06 +0530495 this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530496 this.list_customers = this.wrapper.find('.list-customers');
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530497 this.numeric_keypad = this.wrapper.find('.numeric_keypad');
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530498 this.list_customers_btn.addClass("view_customer")
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530499
Faris Ansari45510102017-03-09 14:43:37 +0530500 me.render_list_customers();
501 me.toggle_totals_area(false);
502
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530503 this.page.wrapper.on('click', '.list-customers-btn', function() {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530504 $(this).toggleClass("view_customer");
505 if($(this).hasClass("view_customer")) {
506 me.render_list_customers();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530507 me.list_customers.show();
508 me.pos_bill.hide();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530509 me.numeric_keypad.hide();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530510 me.toggle_delete_button()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530511 } else {
512 if(me.frm.doc.docstatus == 0) {
513 me.party_field.$input.attr('disabled', false);
514 }
515 me.pos_bill.show();
Faris Ansari45510102017-03-09 14:43:37 +0530516 me.toggle_totals_area(false);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530517 me.toggle_delete_button()
Faris Ansari45510102017-03-09 14:43:37 +0530518 me.list_customers.hide();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530519 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530520 }
521 });
522 this.add_customer_btn.on('click', function() {
523 me.save_previous_entry();
524 me.create_new();
525 me.refresh();
526 me.set_focus();
527 });
Faris Ansari45510102017-03-09 14:43:37 +0530528 this.pos_bill.on('click', '.collapse-btn', function() {
529 me.toggle_totals_area();
530 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530531 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530532
533 bind_numeric_keypad: function() {
534 var me = this;
535 $(this.numeric_keypad).find('.pos-operation').on('click', function(){
536 me.numeric_val = '';
537 })
538
539 $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){
540 me.numeric_id = $(this).attr("id") || me.numeric_id;
541 me.val = $(this).attr("val")
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530542 if(me.numeric_id) {
543 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
544 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530545
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530546 if(me.val && me.numeric_id) {
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530547 me.numeric_val += me.val;
548 me.selected_field.val(flt(me.numeric_val))
549 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530550 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530551 }
Faris Ansari339d9c92017-03-07 18:14:06 +0530552
553 if(me.numeric_id && $(this).hasClass('pos-operation')) {
554 me.numeric_keypad.find('button.pos-operation').removeClass('active');
555 $(this).addClass('active');
556
557 me.selected_row.find('.pos-list-row').removeClass('active');
558 me.selected_field.closest('.pos-list-row').addClass('active');
559 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530560 })
561
562 $(this.numeric_keypad).find('.numeric-del').click(function(){
563 me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id)
564 me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1);
565 me.selected_field.val(me.numeric_val);
566 me.selected_field.trigger("change")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530567 // me.render_selected_item()
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +0530568 })
569
570 $(this.numeric_keypad).find('.pos-pay').click(function(){
571 me.validate();
572 me.update_paid_amount_status(true);
573 me.create_invoice();
574 me.make_payment();
575 })
576 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530577
578 render_list_customers: function () {
579 var me = this;
580
581 this.removed_items = [];
Faris Ansari45510102017-03-09 14:43:37 +0530582 // this.list_customers.empty();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530583 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530584 if (!this.si_docs.length) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530585 return;
586 }
Faris Ansari45510102017-03-09 14:43:37 +0530587
588 var html = "";
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530589 if(this.si_docs.length) {
590 this.si_docs.forEach(function (data, i) {
591 for (key in data) {
592 html += frappe.render_template("pos_invoice_list", {
593 sr: i + 1,
594 name: key,
595 customer: data[key].customer,
596 paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency),
597 grand_total: format_currency(data[key].grand_total, me.frm.doc.currency),
598 data: me.get_doctype_status(data[key])
599 });
600 }
601 });
602 }
Faris Ansari45510102017-03-09 14:43:37 +0530603 this.list_customers.find('.list-customers-table').html(html);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530604
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530605 this.list_customers.on('click', '.customer-row', function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530606 me.list_customers.hide();
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530607 me.numeric_keypad.show();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530608 me.list_customers_btn.toggleClass("view_customer");
609 me.pos_bill.show();
610 me.list_customers_btn.show();
611 me.name = $(this).parents().attr('invoice-name')
612 me.edit_record();
613 })
614
615 //actions
616 $(this.wrapper).find('.list-select-all').click(function () {
617 me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked"))
618 me.removed_items = [];
619 if ($(this).is(":checked")) {
620 $.each(me.si_docs, function (index, data) {
621 for (key in data) {
622 me.removed_items.push(key)
623 }
624 });
625 }
626
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530627 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530628 });
629
630 $(this.wrapper).find('.list-delete').click(function () {
631 me.name = $(this).parent().parent().attr('invoice-name');
632 if ($(this).is(":checked")) {
633 me.removed_items.push(me.name);
634 } else {
635 me.removed_items.pop(me.name)
636 }
637
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530638 me.toggle_delete_button();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530639 });
640 },
641
642 bind_delete_event: function() {
643 var me = this;
644
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530645 $(this.page.wrapper).on('click', '.btn-danger', function(){
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530646 frappe.confirm(__("Delete permanently?"), function () {
647 me.delete_records();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530648 me.list_customers.find('.list-customers-table').html("");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530649 me.render_list_customers();
650 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530651 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530652 },
653
654 set_focus: function () {
655 if (this.default_customer || this.frm.doc.customer) {
656 this.serach_item.$input.focus();
657 } else {
Rohit Waghchaure60a05322016-09-12 01:37:46 +0530658 this.party_field.$input.focus();
659 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530660 },
661
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530662 make_customer: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530663 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530664
665 if(!this.party_field) {
666 if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) {
667 $(frappe.render_template('customer_toolbar', {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530668 allow_delete: this.pos_profile_data["allow_delete"]
Rohit Waghchaure017f5002017-03-08 17:34:39 +0530669 })).insertAfter(this.page.$title_area.hide());
670 }
671
672 this.party_field = frappe.ui.form.make_control({
673 df: {
674 "fieldtype": "Data",
675 "options": this.party,
676 "label": this.party,
677 "fieldname": this.party.toLowerCase(),
678 "placeholder": __("Select or add new customer")
679 },
680 parent: this.page.wrapper.find(".party-area"),
681 only_input: true,
682 });
683
684 this.party_field.make_input();
685 setTimeout(this.set_focus.bind(this), 500);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530686 me.toggle_delete_button();
Faris Ansari339d9c92017-03-07 18:14:06 +0530687 }
Faris Ansari1f261a82017-03-06 18:01:58 +0530688
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530689 this.party_field.awesomeplete =
690 new Awesomplete(this.party_field.$input.get(0), {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530691 minChars: 0,
692 maxItems: 99,
693 autoFirst: true,
694 list: [],
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530695 filter: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530696 var value = item.value.toLowerCase();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530697 if (value.indexOf('is_action') !== -1 ||
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530698 value.indexOf(input) !== -1) {
699 return true;
700 }
701 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530702 item: function (item, input) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530703 var d = item;
Faris Ansari45510102017-03-09 14:43:37 +0530704 var html = "<span>" + __(d.label || d.value) + "</span>";
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530705 return $('<li></li>')
706 .data('item.autocomplete', d)
707 .html('<a><p>' + html + '</p></a>')
708 .get(0);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530709 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530710 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530711
712 this.customers_mapper = this.customers.map(function (c) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530713 return {
714 label: c.name,
715 value: c.name,
716 customer_group: c.customer_group,
717 territory: c.territory
718 }
719 });
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530720
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530721 this.customers_mapper.push({
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530722 label: "<span class='text-primary link-option'>"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530723 + "<i class='fa fa-plus' style='margin-right: 5px;'></i> "
724 + __("Create a new Customer")
725 + "</span>",
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530726 value: 'is_action',
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530727 action: me.add_customer
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530728 });
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530729 this.autocomplete_customers();
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530730
731 this.party_field.$input
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530732 .on('input', function (e) {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530733 me.party_field.awesomeplete.list = this.customers_mapper;
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530734 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530735 .on('awesomplete-select', function (e) {
736 var customer = me.party_field.awesomeplete
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530737 .get_item(e.originalEvent.text.value);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530738 if (!customer) return;
739 // create customer link
740 if (customer.action) {
741 customer.action.apply(me);
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530742 return;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530743 }
Faris Ansari45510102017-03-09 14:43:37 +0530744 me.toggle_list_customer(false);
745 me.toggle_edit_button(true);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530746 me.update_customer_data(customer);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530747 me.refresh();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530748 me.set_focus();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530749 me.list_customers_btn.removeClass("view_customer");
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530750 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530751 .on('focus', function (e) {
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530752 $(e.target).val('').trigger('input');
Faris Ansari45510102017-03-09 14:43:37 +0530753 me.toggle_edit_button(false);
Faris Ansari1fe15182017-03-10 14:50:39 +0530754
755 if(me.frm.doc.items.length) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530756 me.toggle_list_customer(false)
757 me.toggle_item_cart(true)
758 } else {
759 me.toggle_list_customer(true)
760 me.toggle_item_cart(false)
761 }
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530762 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530763 .on("awesomplete-selectcomplete", function (e) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530764 var item = me.party_field.awesomeplete
765 .get_item(e.originalEvent.text.value);
766 // clear text input if item is action
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530767 if (item.action) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530768 $(this).val("");
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530769 }
Rushabh Mehta512b85e2016-12-27 12:14:26 +0530770 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530771 },
772
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530773 autocomplete_customers: function() {
774 this.party_field.awesomeplete.list = this.customers_mapper;
775 },
776
Faris Ansari45510102017-03-09 14:43:37 +0530777 toggle_edit_button: function(flag) {
778 this.page.wrapper.find('.edit-customer-btn').toggle(flag);
779 },
780
781 toggle_list_customer: function(flag) {
782 this.list_customers.toggle(flag);
783 },
784
Rohit Waghchaure6068aec2017-03-10 11:40:28 +0530785 toggle_item_cart: function(flag) {
786 this.wrapper.find('.pos-bill-wrapper').toggle(flag);
787 },
788
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530789 add_customer: function() {
790 this.frm.doc.customer = "";
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530791 this.update_customer(true)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530792 },
793
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530794 update_customer: function (new_customer) {
Nabin Hait6e9d2a32017-01-09 17:21:34 +0530795 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530796 if (!this.connection_status) return;
Rushabh Mehta37146262017-02-01 18:17:35 +0530797
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530798 this.customer_doc = new frappe.ui.Dialog({
799 'title': 'Customer',
800 fields: [
801 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530802 "label": __("Full Name"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530803 "fieldname": "full_name",
804 "fieldtype": "Data",
Rushabh Mehta37146262017-02-01 18:17:35 +0530805 "reqd": 1
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530806 },
807 {
808 "fieldtype": "Section Break"
809 },
810 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530811 "label": __("Email Id"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530812 "fieldname": "email_id",
813 "fieldtype": "Data"
814 },
815 {
816 "fieldtype": "Column Break"
817 },
818 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530819 "label": __("Contact Number"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530820 "fieldname": "phone",
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530821 "fieldtype": "Data"
822 },
823 {
824 "fieldtype": "Section Break"
825 },
826 {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530827 "label": __("Address Name"),
828 "read_only": 1,
829 "fieldname": "name",
830 "fieldtype": "Data"
831 },
832 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530833 "label": __("Address Line 1"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530834 "fieldname": "address_line1",
835 "fieldtype": "Data"
836 },
837 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530838 "label": __("Address Line 2"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530839 "fieldname": "address_line2",
840 "fieldtype": "Data"
841 },
842 {
843 "fieldtype": "Column Break"
844 },
845 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530846 "label": __("City"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530847 "fieldname": "city",
848 "fieldtype": "Data"
849 },
850 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530851 "label": __("State"),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530852 "fieldname": "state",
853 "fieldtype": "Data"
854 },
855 {
Rushabh Mehta37146262017-02-01 18:17:35 +0530856 "label": __("ZIP Code"),
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530857 "fieldname": "pincode",
858 "fieldtype": "Data"
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530859 }
860 ]
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530861 })
Rushabh Mehta37146262017-02-01 18:17:35 +0530862
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530863 this.customer_doc.show()
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530864 this.render_address_data()
Rushabh Mehta37146262017-02-01 18:17:35 +0530865
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530866 this.customer_doc.set_primary_action(__("Save"), function () {
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530867 me.make_offline_customer(new_customer);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530868 me.pos_bill.show();
Rohit Waghchaurefd375162017-05-02 18:27:09 +0530869 me.list_customers.hide();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530870 });
871 },
Rushabh Mehta37146262017-02-01 18:17:35 +0530872
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530873 render_address_data: function() {
874 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530875 this.address_data = this.address[this.frm.doc.customer];
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530876 this.customer_doc.set_values(this.address_data)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530877
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530878 if(!this.customer_doc.fields_dict.full_name.$input.val()) {
879 this.customer_doc.set_value("full_name", this.frm.doc.customer)
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530880 }
881 },
882
883 get_address_from_localstorage: function() {
884 this.address_details = this.get_customers_details()
885 return this.address_details[this.frm.doc.customer]
886 },
887
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530888 make_offline_customer: function(new_customer) {
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530889 this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530890 this.customer_details = this.get_customers_details();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530891 this.customer_details[this.frm.doc.customer] = this.get_prompt_details();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530892 this.party_field.$input.val(this.frm.doc.customer);
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530893 this.update_address_and_customer_list(new_customer)
894 this.autocomplete_customers();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530895 this.update_customer_in_localstorage()
896 this.update_customer_in_localstorage()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530897 this.customer_doc.hide()
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530898 },
899
Rohit Waghchaureb719dc52017-03-23 17:04:00 +0530900 update_address_and_customer_list: function(new_customer) {
901 var me = this;
902 if(new_customer) {
903 this.customers_mapper.push({
904 label: this.frm.doc.customer,
905 value: this.frm.doc.customer,
906 customer_group: "",
907 territory: ""
908 });
909 }
910
911 this.address[this.frm.doc.customer] = this.customer_doc.get_values();
912 },
913
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530914 get_prompt_details: function() {
915 this.prompt_details = this.customer_doc.get_values();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +0530916 this.prompt_details['country'] = this.pos_profile_data.country;
Rohit Waghchaure1312fe32017-03-07 14:01:04 +0530917 return JSON.stringify(this.prompt_details)
918 },
919
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530920 update_customer_data: function (doc) {
Rohit Waghchaurefe7a5b32016-12-27 14:15:52 +0530921 var me = this;
922 this.frm.doc.customer = doc.label || doc.name;
923 this.frm.doc.customer_name = doc.customer_name;
924 this.frm.doc.customer_group = doc.customer_group;
925 this.frm.doc.territory = doc.territory;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530926 this.pos_bill.show();
Rohit Waghchauree30f83a2017-02-24 18:02:50 +0530927 this.numeric_keypad.show();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530928 },
929
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530930 get_customers: function (key) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530931 var me = this;
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530932 key = key.toLowerCase().trim()
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530933 var re = new RegExp('%', 'g');
934 var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*'))
935
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530936 if (key) {
937 return $.grep(this.customers, function (data) {
938 if (reg.test(data.name.toLowerCase())
Rohit Waghchaure163e3592016-10-05 13:38:02 +0530939 || reg.test(data.customer_name.toLowerCase())
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530940 || (data.customer_group && reg.test(data.customer_group.toLowerCase()))) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530941 return data
942 }
943 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530944 } else {
945 customers = this.customers.sort(function (a, b) { return a.idx < b.idx })
Rohit Waghchauref2aa1762016-05-20 23:55:45 +0530946 return customers.slice(0, 20)
947 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530948 },
949
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530950 make_item_list: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530951 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530952 if (!this.price_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530953 msgprint(__("Price List not found or disabled"));
954 return;
955 }
956
957 me.item_timeout = null;
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530958
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530959 var $wrap = me.wrapper.find(".item-list");
960 me.wrapper.find(".item-list").empty();
961
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530962 if (this.items.length > 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530963 $.each(this.items, function(index, obj) {
Faris Ansari1f261a82017-03-06 18:01:58 +0530964 if(index < me.page_len) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530965 $(frappe.render_template("pos_item", {
966 item_code: obj.name,
Rohit Waghchaurea27c4172016-11-20 23:41:13 +0530967 item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530968 item_name: obj.name === obj.item_name ? "" : obj.item_name,
Faris Ansari1f261a82017-03-06 18:01:58 +0530969 item_image: obj.image,
Faris Ansari185762a2017-04-13 12:36:08 +0530970 item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530971 color: frappe.get_palette(obj.item_name),
972 abbr: frappe.get_abbr(obj.item_name)
973 })).tooltip().appendTo($wrap);
974 }
975 });
Faris Ansari45510102017-03-09 14:43:37 +0530976
977 $wrap.append(`
978 <div class="image-view-item btn-more text-muted text-center">
979 <div class="image-view-body">
980 <i class="mega-octicon octicon-package"></i>
981 <div>Load more items</div>
982 </div>
983 </div>
984 `);
985
986 me.toggle_more_btn();
Rohit Waghchaure801029e2016-11-17 00:14:21 +0530987 } else {
Rushabh Mehta37146262017-02-01 18:17:35 +0530988 $("<p class='text-muted small' style='padding-left: 10px'>"
989 +__("Not items found")+"</p>").appendTo($wrap)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530990 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +0530991
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530992 if (this.items.length == 1
993 && this.serach_item.$input.val()) {
994 this.serach_item.$input.val("");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530995 this.add_to_cart();
996 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530997 },
998
rohitwaghchaurec13dbd42017-02-01 18:07:22 +0530999 get_items: function (item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301000 // To search item as per the key enter
1001
1002 var me = this;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301003 this.item_serial_no = {};
1004 this.item_batch_no = {};
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301005
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301006 if (item_code) {
1007 return $.grep(this.item_data, function (item) {
1008 if (item.item_code == item_code) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301009 return true
1010 }
1011 })
1012 }
Rushabh Mehta37146262017-02-01 18:17:35 +05301013
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301014 this.items_list = this.apply_category();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301015
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301016 key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&');
Rohit Waghchaure163e3592016-10-05 13:38:02 +05301017 var re = new RegExp('%', 'g');
Rohit Waghchaure6f33dfb2016-10-09 15:32:56 +05301018 var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*'))
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301019 search_status = true
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301020
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301021 if (key) {
1022 return $.grep(this.items_list, function (item) {
1023 if (search_status) {
1024 if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301025 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301026 return me.item_batch_no[item.item_code] = me.serach_item.$input.val()
1027 } else if (me.serial_no_data[item.item_code]
1028 && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301029 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301030 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 +05301031 return true
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301032 } else if (item.barcode == me.serach_item.$input.val()) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301033 search_status = false;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301034 return item.barcode == me.serach_item.$input.val();
1035 } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) ||
1036 reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) {
Rohit Waghchaureea5a32d2016-08-10 17:18:52 +05301037 return true
1038 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301039 }
1040 })
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301041 } else {
1042 return this.items_list;
1043 }
1044 },
Rushabh Mehta37146262017-02-01 18:17:35 +05301045
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301046 apply_category: function() {
1047 var me = this;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301048 category = this.selected_item_group || "All Item Groups";
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301049
1050 if(category == 'All Item Groups') {
1051 return this.item_data
1052 } else {
1053 return this.item_data.filter(function(element, index, array){
1054 return element.item_group == category;
1055 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301056 }
1057 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301058
1059 bind_items_event: function() {
1060 var me = this;
Faris Ansari1f261a82017-03-06 18:01:58 +05301061 $(this.wrapper).on('click', '.pos-bill-item', function() {
1062 $(me.wrapper).find('.pos-bill-item').removeClass('active');
1063 $(this).addClass('active');
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301064 me.numeric_val = "";
1065 me.numeric_id = ""
1066 me.item_code = $(this).attr("data-item-code");
1067 me.render_selected_item()
1068 me.bind_qty_event()
1069 me.update_rate()
1070 $(me.wrapper).find(".selected-item").scrollTop(1000);
1071 })
1072 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301073
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301074 bind_qty_event: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301075 var me = this;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301076
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301077 $(this.wrapper).on("change", ".pos-item-qty", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301078 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301079 var qty = $(this).val();
1080 me.update_qty(item_code, qty)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301081 me.update_value()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301082 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301083
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301084 $(this.wrapper).find("[data-action='increase-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301085 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1086 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301087 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301088 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301089
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301090 $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301091 var item_code = $(this).parents(".pos-bill-item").attr("data-item-code");
1092 var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1;
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301093 me.update_qty(item_code, qty)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301094 })
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301095
1096 $(this.wrapper).on("change", ".pos-item-disc", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301097 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301098 var discount = $(this).val();
1099 me.update_discount(item_code, discount)
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301100 me.update_value()
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301101 })
1102 },
1103
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301104 bind_events: function() {
1105 var me = this;
1106 // if form is local then allow this function
1107 // $(me.wrapper).find(".pos-item-wrapper").on("click", function () {
1108 $(this.wrapper).on("click", ".pos-item-wrapper", function () {
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301109 me.item_code = '';
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301110 me.customer_validate();
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301111 if($(me.pos_bill).is(":hidden")) return;
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301112
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301113 if (me.frm.doc.docstatus == 0) {
1114 me.items = me.get_items($(this).attr("data-item-code"))
1115 me.add_to_cart();
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301116 me.clear_selected_row();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301117 }
1118 });
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301119
1120 me.bind_delete_event()
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301121 },
1122
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301123 update_qty: function (item_code, qty) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301124 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301125 this.items = this.get_items(item_code);
1126 this.validate_serial_no()
1127 this.set_item_details(item_code, "qty", qty);
1128 },
1129
1130 update_discount: function(item_code, discount) {
1131 var me = this;
1132 this.items = this.get_items(item_code);
1133 this.set_item_details(item_code, "discount_percentage", discount);
1134 },
1135
1136 update_rate: function () {
1137 var me = this;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301138 $(this.wrapper).on("change", ".pos-item-price", function () {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301139 var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code");
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301140 me.set_item_details(item_code, "rate", $(this).val());
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301141 me.update_value()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301142 })
1143 },
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301144
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301145 update_value: function() {
1146 var me = this;
1147 var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc",
1148 "rate": ".pos-item-price", "amount": ".pos-amount"}
1149 this.child_doc = this.get_child_item(this.item_code);
1150
1151 if(me.child_doc.length) {
1152 $.each(fields, function(key, field) {
1153 $(me.selected_row).find(field).val(me.child_doc[0][key])
1154 })
1155 } else {
1156 this.clear_selected_row();
Rohit Waghchaure56a79742017-02-28 16:51:17 +05301157 }
1158 },
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301159
1160 clear_selected_row: function() {
1161 $(this.wrapper).find('.selected-item').empty();
1162 },
1163
1164 render_selected_item: function() {
1165 this.child_doc = this.get_child_item(this.item_code);
1166 $(this.wrapper).find('.selected-item').empty();
1167 if(this.child_doc.length) {
1168 this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0]))
1169 $(this.wrapper).find('.selected-item').html(this.selected_row)
1170 }
1171
1172 $(this.selected_row).find('.form-control').click(function(){
1173 $(this).select();
1174 })
1175 },
1176
Rohit Waghchaure86ab6a92017-02-24 18:02:50 +05301177 get_child_item: function(item_code) {
1178 var me = this;
1179 return $.map(me.frm.doc.items, function(doc){
1180 if(doc.item_code == item_code) {
1181 return doc
1182 }
1183 })
1184 },
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301185
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301186 set_item_details: function (item_code, field, value) {
1187 var me = this;
1188 if (value < 0) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301189 frappe.throw(__("Enter value must be positive"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301190 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301191
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301192 this.remove_item = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301193 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301194 if (d.item_code == item_code) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301195 if (d.serial_no && field == 'qty') {
1196 me.validate_serial_no_qty(d, item_code, field, value)
1197 }
1198
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301199 d[field] = flt(value);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301200 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301201 if (d.qty == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301202 me.remove_item.push(d.idx)
1203 }
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301204
1205 if(field=="discount_percentage" && value == 0) {
1206 d.rate = d.price_list_rate;
1207 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301208 }
1209 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301210
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301211 if (field == 'qty') {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301212 this.remove_zero_qty_item();
1213 }
1214
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301215 this.update_paid_amount_status(false)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301216 },
1217
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301218 remove_zero_qty_item: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301219 var me = this;
1220 idx = 0
1221 this.items = []
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301222 $.each(this.frm.doc["items"] || [], function (i, d) {
1223 if (!in_list(me.remove_item, d.idx)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301224 d.idx = idx;
1225 me.items.push(d);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301226 idx++;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301227 }
1228 });
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301229
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301230 this.frm.doc["items"] = this.items;
1231 },
1232
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301233 make_discount_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301234 var me = this;
1235
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301236 this.wrapper.find('input.discount-percentage').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301237 me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage"));
1238 total = me.frm.doc.grand_total
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301239
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301240 if (me.frm.doc.apply_discount_on == 'Net Total') {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301241 total = me.frm.doc.net_total
1242 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301243
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301244 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 +05301245 me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount)
1246 me.refresh();
1247 });
1248
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301249 this.wrapper.find('input.discount-amount').on("change", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301250 me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount"));
1251 me.frm.doc.additional_discount_percentage = 0.0;
1252 me.wrapper.find('input.discount-percentage').val(0);
1253 me.refresh();
1254 });
1255 },
1256
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301257 customer_validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301258 var me = this;
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301259 if (!this.frm.doc.customer || this.party_field.get_value() == "") {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301260 frappe.throw(__("Please select customer"))
1261 }
1262 },
1263
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301264 add_to_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301265 var me = this;
1266 var caught = false;
1267 var no_of_items = me.wrapper.find(".pos-bill-item").length;
1268
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301269 this.customer_validate();
1270 this.mandatory_batch_no();
1271 this.validate_serial_no();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301272 this.validate_warehouse();
1273
1274 if (no_of_items != 0) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301275 $.each(this.frm.doc["items"] || [], function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301276 if (d.item_code == me.items[0].item_code) {
1277 caught = true;
1278 d.qty += 1;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301279 d.amount = flt(d.rate) * flt(d.qty);
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301280 if (me.item_serial_no[d.item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301281 d.serial_no += '\n' + me.item_serial_no[d.item_code][0]
1282 d.warehouse = me.item_serial_no[d.item_code][1]
1283 }
1284
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301285 if (me.item_batch_no.length) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301286 d.batch_no = me.item_batch_no[d.item_code]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301287 }
1288 }
1289 });
1290 }
1291
1292 // if item not found then add new item
1293 if (!caught)
1294 this.add_new_item_to_grid();
1295
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301296 this.update_paid_amount_status(false)
Faris Ansari45510102017-03-09 14:43:37 +05301297 this.wrapper.find(".item-cart-items").scrollTop(1000);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301298 },
1299
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301300 add_new_item_to_grid: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301301 var me = this;
1302 this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items");
1303 this.child.item_code = this.items[0].item_code;
1304 this.child.item_name = this.items[0].item_name;
1305 this.child.stock_uom = this.items[0].stock_uom;
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301306 this.child.brand = this.items[0].brand;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301307 this.child.description = this.items[0].description;
Rohit Waghchauree30f83a2017-02-24 18:02:50 +05301308 this.child.discount_percentage = 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301309 this.child.qty = 1;
1310 this.child.item_group = this.items[0].item_group;
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301311 this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center;
1312 this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301313 this.child.warehouse = (this.item_serial_no[this.child.item_code]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301314 ? 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 +05301315 this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1316 this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9);
1317 this.child.actual_qty = me.get_actual_qty(this.items[0]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301318 this.child.amount = flt(this.child.qty) * flt(this.child.rate);
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301319 this.child.batch_no = this.item_batch_no[this.child.item_code];
1320 this.child.serial_no = (this.item_serial_no[this.child.item_code]
1321 ? this.item_serial_no[this.child.item_code][0] : '');
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301322 this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301323 },
1324
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301325 update_paid_amount_status: function (update_paid_amount) {
1326 if (this.name) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301327 update_paid_amount = update_paid_amount ? false : true;
1328 }
1329
1330 this.refresh(update_paid_amount);
1331 },
1332
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301333 refresh: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301334 var me = this;
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301335 this.refresh_fields(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301336 this.set_primary_action();
1337 },
Rohit Waghchaure713e2b72016-08-19 15:11:36 +05301338
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301339 refresh_fields: function (update_paid_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301340 this.apply_pricing_rule();
1341 this.discount_amount_applied = false;
1342 this._calculate_taxes_and_totals();
1343 this.calculate_discount_amount();
1344 this.show_items_in_item_cart();
1345 this.set_taxes();
Rohit Waghchaureea278b52016-07-21 23:28:04 +05301346 this.calculate_outstanding_amount(update_paid_amount);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301347 this.set_totals();
1348 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301349
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301350 get_company_currency: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301351 return erpnext.get_currency(this.frm.doc.company);
1352 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301353
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301354 show_item_wise_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301355 return null;
1356 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301357
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301358 show_items_in_item_cart: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301359 var me = this;
1360 var $items = this.wrapper.find(".items").empty();
Faris Ansari1f261a82017-03-06 18:01:58 +05301361 var $no_items_message = this.wrapper.find(".no-items-message");
1362 $no_items_message.toggle(this.frm.doc.items.length === 0);
1363
1364 var $totals_area = this.wrapper.find('.totals-area');
1365 $totals_area.toggle(this.frm.doc.items.length > 0);
1366
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301367 $.each(this.frm.doc.items || [], function (i, d) {
Faris Ansari1f261a82017-03-06 18:01:58 +05301368 $(frappe.render_template("pos_bill_item_new", {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301369 item_code: d.item_code,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301370 item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name),
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301371 qty: d.qty,
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301372 discount_percentage: d.discount_percentage || 0.0,
1373 actual_qty: me.actual_qty_dict[d.item_code] || 0.0,
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301374 projected_qty: d.projected_qty,
mbauskar22cedeb2017-04-17 12:24:11 +05301375 rate: format_currency(d.rate, me.frm.doc.currency),
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301376 enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false,
Rohit Waghchaure6ee91ec2017-03-21 15:55:09 +05301377 amount: format_currency(d.amount, me.frm.doc.currency),
1378 selected_class: (me.item_code == d.item_code) ? "active" : ""
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301379 })).appendTo($items);
1380 });
1381
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301382 this.wrapper.find("input.pos-item-qty").on("focus", function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301383 $(this).select();
1384 });
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301385
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301386 this.wrapper.find("input.pos-item-disc").on("focus", function () {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301387 $(this).select();
1388 });
1389
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301390 this.wrapper.find("input.pos-item-price").on("focus", function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301391 $(this).select();
1392 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301393 },
1394
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301395 set_taxes: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301396 var me = this;
1397 me.frm.doc.total_taxes_and_charges = 0.0
1398
1399 var taxes = this.frm.doc.taxes || [];
1400 $(this.wrapper)
1401 .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true)
1402 .find(".tax-table").empty();
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301403
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301404 $.each(taxes, function (i, d) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301405 if (d.tax_amount && cint(d.included_in_print_rate) == 0) {
1406 $(frappe.render_template("pos_tax_row", {
1407 description: d.description,
1408 tax_amount: format_currency(flt(d.tax_amount_after_discount_amount),
1409 me.frm.doc.currency)
1410 })).appendTo(me.wrapper.find(".tax-table"));
1411 }
1412 });
1413 },
1414
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301415 set_totals: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301416 var me = this;
1417 this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency));
1418 this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency));
1419 },
1420
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301421 set_primary_action: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301422 var me = this;
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301423 this.page.set_primary_action(__("New Cart"), function () {
1424 me.make_new_cart()
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301425 me.make_menu_list()
Faris Ansari45510102017-03-09 14:43:37 +05301426 }, "fa fa-plus")
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301427
1428 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure6068aec2017-03-10 11:40:28 +05301429 this.page.set_secondary_action(__("Print"), function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301430 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301431 me.print_document(html)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301432 })
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301433 this.page.add_menu_item(__("Email"), function () {
1434 me.email_prompt()
1435 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301436 }
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301437 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +05301438
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301439 make_new_cart: function (){
Rohit Waghchaurea86bb692017-03-30 19:16:38 +05301440 this.item_code = '';
Rohit Waghchaure75177c02017-03-16 15:21:21 +05301441 this.page.clear_secondary_action();
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301442 this.save_previous_entry();
1443 this.create_new();
1444 this.refresh();
1445 this.toggle_input_field();
1446 this.render_list_customers();
1447 this.set_focus();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301448 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301449
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301450 print_dialog: function () {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301451 var me = this;
1452
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301453 this.msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301454 style="margin-right: 5px;">{0}</a>\
1455 <a class="btn btn-default new_doc">{1}</a>', [
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301456 __('Print'), __('New')
1457 ]));
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301458
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301459 $('.print_doc').click(function () {
Rohit Waghchaure9f2cc382016-12-09 11:46:08 +05301460 html = frappe.render(me.print_template_data, me.frm.doc)
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301461 me.print_document(html)
1462 })
1463
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301464 $('.new_doc').click(function () {
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301465 me.msgprint.hide()
1466 me.make_new_cart()
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301467 })
1468 },
1469
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301470 print_document: function (html) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301471 var w = window.open();
1472 w.document.write(html);
1473 w.document.close();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301474 setTimeout(function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301475 w.print();
1476 w.close();
1477 }, 1000)
1478 },
1479
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301480 submit_invoice: function () {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301481 var me = this;
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301482 this.change_status();
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301483 this.update_serial_no()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301484 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure60a05322016-09-12 01:37:46 +05301485 this.print_dialog()
1486 }
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301487 },
1488
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301489 update_serial_no: function() {
1490 var me = this;
1491
1492 //Remove the sold serial no from the cache
1493 $.each(this.frm.doc.items, function(index, data) {
1494 sn = data.serial_no.split('\n')
1495 if(sn.length) {
1496 serial_no_list = me.serial_no_data[data.item_code]
Rohit Waghchaureb719dc52017-03-23 17:04:00 +05301497 if(serial_no_list) {
1498 $.each(sn, function(i, serial_no) {
1499 if(in_list(Object.keys(serial_no_list), serial_no)) {
1500 delete serial_no_list[serial_no]
1501 }
1502 })
1503 me.serial_no_data[data.item_code] = serial_no_list;
1504 }
Rohit Waghchauree259a9b2017-03-14 19:03:49 +05301505 }
1506 })
1507 },
1508
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301509 change_status: function () {
1510 if (this.frm.doc.docstatus == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301511 this.frm.doc.docstatus = 1;
1512 this.update_invoice();
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301513 this.toggle_input_field();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301514 }
1515 },
1516
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301517 toggle_input_field: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301518 var pointer_events = 'inherit'
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301519 disabled = this.frm.doc.docstatus == 1 ? true: false;
1520 $(this.wrapper).find('input').attr("disabled", disabled);
1521 $(this.wrapper).find('select').attr("disabled", disabled);
1522 $(this.wrapper).find('input').attr("disabled", disabled);
1523 $(this.wrapper).find('select').attr("disabled", disabled);
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301524 $(this.wrapper).find('button').attr("disabled", disabled);
1525 this.party_field.$input.attr('disabled', disabled);
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301526
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301527 if (this.frm.doc.docstatus == 1) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301528 pointer_events = 'none';
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301529 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301530
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301531 $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301532 $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events);
1533 this.set_primary_action();
1534 },
1535
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301536 create_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301537 var me = this;
1538 var invoice_data = {}
1539 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301540 if (this.name) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301541 this.update_invoice()
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301542 } else {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301543 this.name = $.now();
Rohit Waghchaure377c7ac2016-09-05 17:56:44 +05301544 this.frm.doc.offline_pos_name = this.name;
Rohit Waghchaure9cd356c2016-08-11 16:54:30 +05301545 this.frm.doc.posting_date = frappe.datetime.get_today();
1546 this.frm.doc.posting_time = frappe.datetime.now_time();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301547 invoice_data[this.name] = this.frm.doc
1548 this.si_docs.push(invoice_data)
1549 this.update_localstorage();
1550 this.set_primary_action();
1551 }
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301552 return invoice_data;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301553 },
1554
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301555 update_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301556 var me = this;
1557 this.si_docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301558 $.each(this.si_docs, function (index, data) {
1559 for (key in data) {
1560 if (key == me.name) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301561 me.si_docs[index][key] = me.frm.doc;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301562 me.update_localstorage();
1563 }
1564 }
1565 })
1566 },
1567
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301568 update_localstorage: function () {
1569 try {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301570 localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs));
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301571 } catch (e) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301572 frappe.throw(__("LocalStorage is full , did not save"))
1573 }
1574 },
1575
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301576 get_doc_from_localstorage: function () {
1577 try {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301578 return JSON.parse(localStorage.getItem('sales_invoice_doc')) || [];
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301579 } catch (e) {
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301580 return []
1581 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301582 },
1583
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301584 set_interval_for_si_sync: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301585 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301586 setInterval(function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301587 me.sync_sales_invoice()
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301588 }, 60000)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301589 },
1590
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301591 sync_sales_invoice: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301592 var me = this;
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301593 this.si_docs = this.get_submitted_invoice() || [];
1594 this.email_queue_list = this.get_email_queue() || {};
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301595 this.customers_list = this.get_customers_details() || {};
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301596
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301597 if (this.si_docs.length || this.email_queue_list || this.customers_list) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301598 frappe.call({
1599 method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice",
1600 args: {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301601 doc_list: me.si_docs,
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301602 email_queue_list: me.email_queue_list,
1603 customers_list: me.customers_list
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301604 },
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301605 callback: function (r) {
1606 if (r.message) {
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301607 me.removed_items = r.message.invoice;
1608 me.removed_email = r.message.email_queue
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301609 me.removed_customers = r.message.customers
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301610 me.remove_doc_from_localstorage();
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301611 me.remove_email_queue_from_localstorage();
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301612 me.remove_customer_from_localstorage();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301613 }
1614 }
1615 })
1616 }
1617 },
1618
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301619 get_submitted_invoice: function () {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301620 var invoices = [];
1621 var index = 1;
1622 docs = this.get_doc_from_localstorage();
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301623 if (docs) {
1624 invoices = $.map(docs, function (data) {
1625 for (key in data) {
1626 if (data[key].docstatus == 1 && index < 50) {
Rohit Waghchauref2aa1762016-05-20 23:55:45 +05301627 index++
Rohit Waghchaureb77a1052016-08-01 18:35:18 +05301628 data[key].docstatus = 0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301629 return data
1630 }
1631 }
1632 });
1633 }
1634
1635 return invoices
1636 },
1637
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301638 remove_doc_from_localstorage: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301639 var me = this;
1640 this.si_docs = this.get_doc_from_localstorage();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301641 this.new_si_docs = [];
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301642 if (this.removed_items) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301643 $.each(this.si_docs, function (index, data) {
1644 for (key in data) {
Rohit Waghchaure017f5002017-03-08 17:34:39 +05301645 if (!in_list(me.removed_items, key)) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301646 me.new_si_docs.push(data);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301647 }
1648 }
1649 })
Rohit Waghchaure87fa59a2017-03-10 14:56:19 +05301650 this.removed_items = [];
Rohit Waghchauree0934d12016-05-11 15:04:57 +05301651 this.si_docs = this.new_si_docs;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301652 this.update_localstorage();
1653 }
1654 },
1655
Rohit Waghchaureb5097ec2017-03-01 01:13:09 +05301656 remove_email_queue_from_localstorage: function() {
1657 var me = this;
1658 this.email_queue = this.get_email_queue()
1659 if (this.removed_email) {
1660 $.each(this.email_queue_list, function (index, data) {
1661 if (in_list(me.removed_email, index)) {
1662 delete me.email_queue[index]
1663 }
1664 })
1665 this.update_email_queue();
1666 }
1667 },
1668
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301669 remove_customer_from_localstorage: function() {
1670 var me = this;
1671 this.customer_details = this.get_customers_details()
1672 if (this.removed_customers) {
1673 $.each(this.customers_list, function (index, data) {
1674 if (in_list(me.removed_customers, index)) {
1675 delete me.customer_details[index]
1676 }
1677 })
1678 this.update_customer_in_localstorage();
1679 }
1680 },
1681
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301682 validate: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301683 var me = this;
1684 this.customer_validate();
1685 this.item_validate();
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301686 this.validate_mode_of_payments();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301687 },
1688
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301689 item_validate: function () {
1690 if (this.frm.doc.items.length == 0) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301691 frappe.throw(__("Select items to save the invoice"))
1692 }
1693 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301694
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301695 validate_mode_of_payments: function () {
1696 if (this.frm.doc.payments.length === 0) {
Saurabh9a6c6662016-06-27 16:51:44 +05301697 frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile."))
1698 }
1699 },
Rushabh Mehta512b85e2016-12-27 12:14:26 +05301700
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301701 validate_serial_no: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301702 var me = this;
1703 var item_code = serial_no = '';
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301704 for (key in this.item_serial_no) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301705 item_code = key;
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301706 serial_no = me.item_serial_no[key][0];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301707 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301708
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301709 if (this.items[0].has_serial_no && serial_no == "") {
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301710 this.refresh();
1711 frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", {
1712 'item': this.items[0].item_code
1713 })))
1714 }
1715
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301716 if (item_code && serial_no) {
1717 $.each(this.frm.doc.items, function (index, data) {
1718 if (data.item_code == item_code) {
1719 if (in_list(data.serial_no.split('\n'), serial_no)) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301720 frappe.throw(__(repl("Serial no %(serial_no)s is already taken", {
1721 'serial_no': serial_no
1722 })))
1723 }
1724 }
1725 })
1726 }
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301727 },
1728
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301729 validate_serial_no_qty: function (args, item_code, field, value) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301730 var me = this;
1731 if (args.item_code == item_code && args.serial_no
1732 && field == 'qty' && cint(value) != value) {
1733 args.qty = 0.0;
1734 this.refresh();
1735 frappe.throw(__("Serial no item cannot be a fraction"))
1736 }
1737
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301738 if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) {
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301739 args.qty = 0.0;
1740 args.serial_no = ''
1741 this.refresh();
Rohit Waghchauredd58d532016-12-21 17:23:01 +05301742 frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", {
1743 'item': item_code
1744 })))
Rohit Waghchauree4e69ec2016-08-17 16:20:13 +05301745 }
1746 },
1747
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301748 mandatory_batch_no: function () {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301749 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301750 if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
Rohit Waghchaure03da40b2016-07-16 03:02:20 +05301751 frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", {
1752 'item': this.items[0].item_code
1753 })))
1754 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301755 },
1756
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301757 apply_pricing_rule: function () {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301758 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301759 $.each(this.frm.doc["items"], function (n, item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301760 pricing_rule = me.get_pricing_rule(item)
1761 me.validate_pricing_rule(pricing_rule)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301762 if (pricing_rule.length) {
1763 item.pricing_rule = pricing_rule[0].name;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301764 item.margin_type = pricing_rule[0].margin_type;
1765 item.price_list_rate = pricing_rule[0].price || item.price_list_rate;
1766 item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount;
1767 item.discount_percentage = pricing_rule[0].discount_percentage || 0.0;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301768 me.apply_pricing_rule_on_item(item)
1769 } else if (item.pricing_rule) {
1770 item.price_list_rate = me.price_list_data[item.item_code]
Rohit Waghchaure619c40b2016-11-07 15:29:17 +05301771 item.margin_rate_or_amount = 0.0;
1772 item.discount_percentage = 0.0;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301773 item.pricing_rule = null;
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301774 me.apply_pricing_rule_on_item(item)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301775 }
Rohit Waghchauref0c7ba42017-02-28 16:51:17 +05301776
1777 if(item.discount_percentage > 0) {
1778 me.apply_pricing_rule_on_item(item)
1779 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301780 })
1781 },
1782
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301783 get_pricing_rule: function (item) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301784 var me = this;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301785 return $.grep(this.pricing_rules, function (data) {
1786 if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) {
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301787 if (me.validate_item_condition(data, item)) {
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301788 if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301789 return me.validate_condition(data)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301790 } else {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301791 return true
1792 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301793 }
1794 }
1795 })
1796 },
1797
Rohit Waghchaured7de3c62017-04-17 13:36:08 +05301798 validate_item_condition: function (data, item) {
1799 var apply_on = frappe.model.scrub(data.apply_on);
1800
1801 return (data.apply_on == 'Item Group')
1802 ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]);
1803 },
1804
1805 validate_item_group: function (pr_item_group, cart_item_group) {
1806 //pr_item_group = pricing rule's item group
1807 //cart_item_group = cart item's item group
1808 //this.item_groups has information about item group's lft and rgt
1809 //for example: {'Foods': [12, 19]}
1810
1811 pr_item_group = this.item_groups[pr_item_group]
1812 cart_item_group = this.item_groups[cart_item_group]
1813
1814 return (cart_item_group[0] >= pr_item_group[0] &&
1815 cart_item_group[1] <= pr_item_group[1])
1816 },
1817
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301818 validate_condition: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301819 //This method check condition based on applicable for
1820 condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for]
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301821 if (in_list(condition[1], condition[0])) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301822 return true
1823 }
1824 },
1825
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301826 get_mapper_for_pricing_rule: function (data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301827 return {
Rohit Waghchaure89ee3092016-07-08 15:17:04 +05301828 'Customer': [data.customer, [this.frm.doc.customer]],
1829 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']],
1830 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']],
Rohit Waghchaurebaef2622016-08-05 15:41:36 +05301831 'Campaign': [data.campaign, [this.frm.doc.campaign]],
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301832 }
1833 },
1834
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301835 validate_pricing_rule: function (pricing_rule) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301836 //This method validate duplicate pricing rule
1837 var pricing_rule_name = '';
1838 var priority = 0;
1839 var pricing_rule_list = [];
1840 var priority_list = []
1841
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301842 if (pricing_rule.length > 1) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301843
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301844 $.each(pricing_rule, function (index, data) {
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301845 pricing_rule_name += data.name + ','
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301846 priority_list.push(data.priority)
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301847 if (priority <= data.priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301848 priority = data.priority
1849 pricing_rule_list.push(data)
1850 }
1851 })
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301852
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301853 count = 0
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301854 $.each(priority_list, function (index, value) {
1855 if (value == priority) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301856 count++
1857 }
1858 })
1859
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301860 if (priority == 0 || count > 1) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301861 frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", {
1862 'pricing_rule': pricing_rule_name
1863 })))
1864 }
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301865
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301866 return pricing_rule_list
1867 }
1868 },
Rushabh Mehtad0cee1b2016-05-20 12:54:44 +05301869
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301870 validate_warehouse: function () {
1871 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 +05301872 frappe.throw(__("Default warehouse is required for selected item"))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301873 }
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301874 },
1875
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301876 get_actual_qty: function (item) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301877 this.actual_qty = 0.0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301878
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301879 var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse;
rohitwaghchaurec13dbd42017-02-01 18:07:22 +05301880 if (warehouse && this.bin_data[item.item_code]) {
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301881 this.actual_qty = this.bin_data[item.item_code][warehouse] || 0;
Rohit Waghchaure80f5bb62016-11-27 12:04:12 +05301882 this.actual_qty_dict[item.item_code] = this.actual_qty
Rohit Waghchaurea27c4172016-11-20 23:41:13 +05301883 }
1884
1885 return this.actual_qty
Rohit Waghchaure1312fe32017-03-07 14:01:04 +05301886 },
1887
1888 update_customer_in_localstorage: function() {
1889 var me = this;
1890 try {
1891 localStorage.setItem('customer_details', JSON.stringify(this.customer_details));
1892 } catch (e) {
1893 frappe.throw(__("LocalStorage is full , did not save"))
1894 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301895 }
Faris Ansari185762a2017-04-13 12:36:08 +05301896})