Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.pos"); |
| 2 | {% include "erpnext/public/js/controllers/taxes_and_totals.js" %} |
| 3 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 4 | frappe.pages['pos'].on_page_load = function (wrapper) { |
Rushabh Mehta | 4c36d73 | 2015-01-01 15:59:34 +0530 | [diff] [blame] | 5 | var page = frappe.ui.make_app_page({ |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 6 | parent: wrapper, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 7 | title: __('Point of Sale'), |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 8 | single_column: true |
| 9 | }); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 10 | |
Faris Ansari | c70bbac | 2017-08-29 15:27:17 +0530 | [diff] [blame] | 11 | frappe.db.get_value('POS Settings', {name: 'POS Settings'}, 'is_online', (r) => { |
Rohit Waghchaure | 1a77922 | 2017-09-11 11:49:25 +0530 | [diff] [blame] | 12 | if (r && r.use_pos_in_offline_mode && cint(r.use_pos_in_offline_mode)) { |
Faris Ansari | c70bbac | 2017-08-29 15:27:17 +0530 | [diff] [blame] | 13 | // offline |
| 14 | wrapper.pos = new erpnext.pos.PointOfSale(wrapper); |
| 15 | cur_pos = wrapper.pos; |
| 16 | } else { |
| 17 | // online |
| 18 | frappe.set_route('point-of-sale'); |
| 19 | } |
| 20 | }); |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 21 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 22 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 23 | frappe.pages['pos'].refresh = function (wrapper) { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 24 | window.onbeforeunload = function () { |
| 25 | return wrapper.pos.beforeunload() |
| 26 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 27 | } |
| 28 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 29 | erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 30 | init: function (wrapper) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 31 | this.page_len = 20; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 32 | this.freeze = false; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 33 | this.page = wrapper.page; |
| 34 | this.wrapper = $(wrapper).find('.page-content'); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 35 | this.set_indicator(); |
| 36 | this.onload(); |
| 37 | this.make_menu_list(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 38 | this.bind_events(); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 39 | this.bind_items_event(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 40 | this.si_docs = this.get_doc_from_localstorage(); |
| 41 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 42 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 43 | beforeunload: function (e) { |
| 44 | if (this.connection_status == false && frappe.get_route()[0] == "pos") { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 45 | e = e || window.event; |
| 46 | |
| 47 | // For IE and Firefox prior to version 4 |
| 48 | if (e) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 49 | e.returnValue = __("You are in offline mode. You will not be able to reload until you have network."); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 50 | return |
| 51 | } |
| 52 | |
| 53 | // For Safari |
| 54 | return __("You are in offline mode. You will not be able to reload until you have network."); |
| 55 | } |
| 56 | }, |
| 57 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 58 | check_internet_connection: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 59 | var me = this; |
| 60 | //Check Internet connection after every 30 seconds |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 61 | setInterval(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 62 | me.set_indicator(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 63 | }, 5000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 64 | }, |
| 65 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 66 | set_indicator: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 67 | var me = this; |
| 68 | // navigator.onLine |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 69 | this.connection_status = false; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 70 | this.page.set_indicator(__("Offline"), "grey") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 71 | frappe.call({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 72 | method: "frappe.handler.ping", |
| 73 | callback: function (r) { |
| 74 | if (r.message) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 75 | me.connection_status = true; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 76 | me.page.set_indicator(__("Online"), "green") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | }) |
| 80 | }, |
| 81 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 82 | onload: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 83 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 84 | this.get_data_from_server(function () { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 85 | me.make_control(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 86 | me.create_new(); |
rohitwaghchaure | 5fdd26f | 2017-10-21 11:23:45 +0530 | [diff] [blame] | 87 | me.make(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 88 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 89 | }, |
| 90 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 91 | make_menu_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 92 | var me = this; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 93 | this.page.clear_menu(); |
Faris Ansari | 769b6ba | 2017-05-16 07:31:10 +0530 | [diff] [blame] | 94 | |
| 95 | // for mobile |
| 96 | this.page.add_menu_item(__("Pay"), function () { |
| 97 | me.validate(); |
| 98 | me.update_paid_amount_status(true); |
| 99 | me.create_invoice(); |
| 100 | me.make_payment(); |
| 101 | }).addClass('visible-xs'); |
| 102 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 103 | this.page.add_menu_item(__("New Sales Invoice"), function () { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 104 | me.save_previous_entry(); |
| 105 | me.create_new(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 106 | }) |
| 107 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 108 | this.page.add_menu_item(__("Sync Master Data"), function () { |
| 109 | me.get_data_from_server(function () { |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 110 | me.load_data(false); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 111 | me.make_item_list(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 112 | me.set_missing_values(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 113 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 114 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 115 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 116 | this.page.add_menu_item(__("Sync Offline Invoices"), function () { |
rohitwaghchaure | 8849171 | 2017-10-02 12:13:36 +0530 | [diff] [blame] | 117 | me.freeze_screen = true; |
Rohit Waghchaure | 82be020 | 2016-10-04 12:46:37 +0530 | [diff] [blame] | 118 | me.sync_sales_invoice() |
| 119 | }); |
| 120 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 121 | this.page.add_menu_item(__("POS Profile"), function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 122 | frappe.set_route('List', 'POS Profile'); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 123 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 124 | }, |
| 125 | |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 126 | email_prompt: function() { |
| 127 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 128 | var fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288}, |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 129 | {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"}, |
| 130 | {fieldtype: "Section Break"}, |
| 131 | {label:__("Subject"), fieldtype:"Data", reqd: 1, |
| 132 | fieldname:"subject",length:524288}, |
| 133 | {fieldtype: "Section Break"}, |
| 134 | {label:__("Message"), fieldtype:"Text Editor", reqd: 1, |
| 135 | fieldname:"content"}, |
| 136 | {fieldtype: "Section Break"}, |
| 137 | {fieldtype: "Column Break"}]; |
| 138 | |
| 139 | this.email_dialog = new frappe.ui.Dialog({ |
| 140 | title: "Email", |
| 141 | fields: fields, |
| 142 | primary_action_label: __("Send"), |
| 143 | primary_action: function() { |
| 144 | me.send_action(); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | this.email_dialog.show() |
| 149 | }, |
| 150 | |
| 151 | send_action: function() { |
| 152 | this.email_queue = this.get_email_queue() |
| 153 | this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values()) |
| 154 | this.update_email_queue() |
| 155 | this.email_dialog.hide() |
| 156 | }, |
| 157 | |
| 158 | update_email_queue: function () { |
| 159 | try { |
| 160 | localStorage.setItem('email_queue', JSON.stringify(this.email_queue)); |
| 161 | } catch (e) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 162 | frappe.throw(__("LocalStorage is full, did not save")) |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 163 | } |
| 164 | }, |
| 165 | |
| 166 | get_email_queue: function () { |
| 167 | try { |
| 168 | return JSON.parse(localStorage.getItem('email_queue')) || {}; |
| 169 | } catch (e) { |
| 170 | return {} |
| 171 | } |
| 172 | }, |
| 173 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 174 | get_customers_details: function () { |
| 175 | try { |
| 176 | return JSON.parse(localStorage.getItem('customer_details')) || {}; |
| 177 | } catch (e) { |
| 178 | return {} |
| 179 | } |
| 180 | }, |
| 181 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 182 | edit_record: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 183 | var me = this; |
| 184 | |
| 185 | doc_data = this.get_invoice_doc(this.si_docs); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 186 | if (doc_data) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 187 | this.frm.doc = doc_data[0][this.frm.doc.offline_pos_name]; |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 188 | this.set_missing_values(); |
| 189 | this.refresh(false); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 190 | this.toggle_input_field(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 191 | this.list_dialog && this.list_dialog.hide(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 192 | } |
| 193 | }, |
| 194 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 195 | delete_records: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 196 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 197 | this.validate_list() |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 198 | this.remove_doc_from_localstorage() |
| 199 | this.update_localstorage(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 200 | this.toggle_delete_button(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 201 | }, |
| 202 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 203 | validate_list: function() { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 204 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 205 | this.si_docs = this.get_submitted_invoice() |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 206 | $.each(this.removed_items, function(index, pos_name){ |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 207 | $.each(me.si_docs, function(key, data){ |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 208 | if(me.si_docs[key][pos_name] && me.si_docs[key][pos_name].offline_pos_name == pos_name ){ |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 209 | frappe.throw(__("Submitted orders can not be deleted")) |
| 210 | } |
| 211 | }) |
| 212 | }) |
| 213 | }, |
| 214 | |
| 215 | toggle_delete_button: function () { |
| 216 | var me = this; |
| 217 | if(this.pos_profile_data["allow_delete"]) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 218 | if (this.removed_items && this.removed_items.length > 0) { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 219 | $(this.page.wrapper).find('.btn-danger').show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 220 | } else { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 221 | $(this.page.wrapper).find('.btn-danger').hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | }, |
| 225 | |
| 226 | get_doctype_status: function (doc) { |
| 227 | if (doc.docstatus == 0) { |
| 228 | return { status: "Draft", indicator: "red" } |
| 229 | } else if (doc.outstanding_amount == 0) { |
| 230 | return { status: "Paid", indicator: "green" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 231 | } else { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 232 | return { status: "Submitted", indicator: "blue" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 233 | } |
| 234 | }, |
| 235 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 236 | set_missing_values: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 237 | var me = this; |
| 238 | doc = JSON.parse(localStorage.getItem('doc')) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 239 | if (this.frm.doc.payments.length == 0) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 240 | this.frm.doc.payments = doc.payments; |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 241 | this.calculate_outstanding_amount(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 242 | } |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 243 | |
rohitwaghchaure | 9a0db39 | 2017-09-14 10:51:21 +0530 | [diff] [blame] | 244 | this.set_customer_value_in_party_field(); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 245 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 246 | if (!this.frm.doc.write_off_account) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 247 | this.frm.doc.write_off_account = doc.write_off_account |
| 248 | } |
| 249 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 250 | if (!this.frm.doc.account_for_change_amount) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 251 | this.frm.doc.account_for_change_amount = doc.account_for_change_amount |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 252 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 253 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 254 | |
rohitwaghchaure | 9a0db39 | 2017-09-14 10:51:21 +0530 | [diff] [blame] | 255 | set_customer_value_in_party_field: function() { |
| 256 | if (this.frm.doc.customer) { |
| 257 | this.party_field.$input.val(this.frm.doc.customer); |
| 258 | } |
| 259 | }, |
| 260 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 261 | get_invoice_doc: function (si_docs) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 262 | var me = this; |
| 263 | this.si_docs = this.get_doc_from_localstorage(); |
| 264 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 265 | return $.grep(this.si_docs, function (data) { |
| 266 | for (key in data) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 267 | return key == me.frm.doc.offline_pos_name; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 268 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 269 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 270 | }, |
| 271 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 272 | get_data_from_server: function (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 273 | var me = this; |
| 274 | frappe.call({ |
| 275 | method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data", |
| 276 | freeze: true, |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 277 | freeze_message: __("Master data syncing, it might take some time"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 278 | callback: function (r) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 279 | localStorage.setItem('doc', JSON.stringify(r.message.doc)); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 280 | me.init_master_data(r) |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 281 | me.set_interval_for_si_sync(); |
| 282 | me.check_internet_connection(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 283 | if (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 284 | callback(); |
| 285 | } |
| 286 | } |
| 287 | }) |
| 288 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 289 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 290 | init_master_data: function (r) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 291 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 292 | this.doc = JSON.parse(localStorage.getItem('doc')); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 293 | this.meta = r.message.meta; |
| 294 | this.item_data = r.message.items; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 295 | this.item_groups = r.message.item_groups; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 296 | this.customers = r.message.customers; |
| 297 | this.serial_no_data = r.message.serial_no_data; |
| 298 | this.batch_no_data = r.message.batch_no_data; |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 299 | this.barcode_data = r.message.barcode_data; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 300 | this.tax_data = r.message.tax_data; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 301 | this.contacts = r.message.contacts; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 302 | this.address = r.message.address || {}; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 303 | this.price_list_data = r.message.price_list_data; |
| 304 | this.bin_data = r.message.bin_data; |
| 305 | this.pricing_rules = r.message.pricing_rules; |
| 306 | this.print_template = r.message.print_template; |
| 307 | this.pos_profile_data = r.message.pos_profile; |
| 308 | this.default_customer = r.message.default_customer || null; |
rohitwaghchaure | 34e820d | 2016-12-20 16:54:24 +0530 | [diff] [blame] | 309 | this.print_settings = locals[":Print Settings"]["Print Settings"]; |
Rohit Waghchaure | d567e57 | 2016-12-21 13:18:36 +0530 | [diff] [blame] | 310 | this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {}; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 311 | }, |
| 312 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 313 | save_previous_entry: function () { |
| 314 | if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) { |
| 315 | this.create_invoice(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 316 | } |
| 317 | }, |
| 318 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 319 | create_new: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 320 | var me = this; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 321 | this.frm = {} |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 322 | this.load_data(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 323 | this.setup(); |
Rohit Waghchaure | 6116512 | 2017-05-02 13:04:08 +0530 | [diff] [blame] | 324 | this.set_default_customer() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 325 | }, |
| 326 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 327 | load_data: function (load_doc) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 328 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 329 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 330 | this.items = this.item_data; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 331 | this.actual_qty_dict = {}; |
| 332 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 333 | if (load_doc) { |
| 334 | this.frm.doc = JSON.parse(localStorage.getItem('doc')); |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 335 | this.frm.doc.offline_pos_name = null; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 336 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 337 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 338 | $.each(this.meta, function (i, data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 339 | frappe.meta.sync(data) |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 340 | locals["DocType"][data.name] = data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 341 | }) |
| 342 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 343 | this.print_template_data = frappe.render_template("print_template", { |
| 344 | content: this.print_template, |
Faris Ansari | 29d64ca | 2017-05-23 17:12:51 +0530 | [diff] [blame] | 345 | title: "POS", |
| 346 | base_url: frappe.urllib.get_base_url(), |
| 347 | print_css: frappe.boot.print_css, |
| 348 | print_settings: this.print_settings, |
| 349 | header: this.letter_head.header, |
| 350 | footer: this.letter_head.footer, |
Makarand Bauskar | 724cc35 | 2017-05-23 17:43:42 +0530 | [diff] [blame] | 351 | landscape: false, |
| 352 | columns: [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 353 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 354 | }, |
| 355 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 356 | setup: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 357 | this.set_primary_action(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 358 | this.party_field.$input.attr('disabled', false); |
| 359 | if(this.selected_row) { |
| 360 | this.selected_row.hide() |
| 361 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 362 | }, |
| 363 | |
Rohit Waghchaure | 6116512 | 2017-05-02 13:04:08 +0530 | [diff] [blame] | 364 | 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 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 374 | set_transaction_defaults: function (party) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 375 | 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 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 383 | make: function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 384 | this.make_item_list(); |
| 385 | this.make_discount_field() |
| 386 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 387 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 388 | make_control: function() { |
| 389 | this.frm = {} |
| 390 | this.frm.doc = this.doc |
| 391 | this.set_transaction_defaults("Customer"); |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 392 | this.frm.doc["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false, |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 393 | this.wrapper.html(frappe.render_template("pos", this.frm.doc)); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 394 | this.make_search(); |
| 395 | this.make_customer(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 396 | this.make_list_customers(); |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 397 | this.bind_numeric_keypad(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 398 | }, |
| 399 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 400 | make_search: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 401 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 402 | this.serach_item = frappe.ui.form.make_control({ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 403 | df: { |
| 404 | "fieldtype": "Data", |
| 405 | "label": "Item", |
| 406 | "fieldname": "pos_item", |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 407 | "placeholder": __("Search Item") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 408 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 409 | parent: this.wrapper.find(".search-item"), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 410 | only_input: true, |
| 411 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 412 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 413 | this.serach_item.make_input(); |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 414 | |
Revant Nandgaonkar | 2f7cb82 | 2017-08-26 09:58:15 +0530 | [diff] [blame] | 415 | this.serach_item.$input.on("keypress", function (event) { |
Faris Ansari | 8fbc08f | 2017-08-28 16:52:20 +0530 | [diff] [blame] | 416 | |
| 417 | clearTimeout(me.last_search_timeout); |
| 418 | me.last_search_timeout = setTimeout(() => { |
| 419 | if((me.serach_item.$input.val() != "") || (event.which == 13)) { |
| 420 | me.items = me.get_items(); |
| 421 | me.make_item_list(); |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 422 | } |
Faris Ansari | 8fbc08f | 2017-08-28 16:52:20 +0530 | [diff] [blame] | 423 | }, 400); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 424 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 425 | |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 426 | this.search_item_group = this.wrapper.find('.search-item-group'); |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 427 | sorted_item_groups = this.get_sorted_item_groups() |
| 428 | var dropdown_html = sorted_item_groups.map(function(item_group) { |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 429 | return "<li><a class='option' data-value='"+item_group+"'>"+item_group+"</a></li>"; |
| 430 | }).join(""); |
| 431 | |
| 432 | this.search_item_group.find('.dropdown-menu').html(dropdown_html); |
| 433 | |
| 434 | this.search_item_group.on('click', '.dropdown-menu a', function() { |
| 435 | me.selected_item_group = $(this).attr('data-value'); |
| 436 | me.search_item_group.find('.dropdown-text').text(me.selected_item_group); |
| 437 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 438 | me.page_len = 20; |
| 439 | me.items = me.get_items(); |
| 440 | me.make_item_list(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 441 | }) |
| 442 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 443 | me.toggle_more_btn(); |
| 444 | |
| 445 | this.wrapper.on("click", ".btn-more", function() { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 446 | me.page_len += 20; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 447 | me.items = me.get_items(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 448 | me.make_item_list(); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 449 | me.toggle_more_btn(); |
| 450 | }); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 451 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 452 | this.page.wrapper.on("click", ".edit-customer-btn", function() { |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 453 | me.update_customer() |
| 454 | }) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 455 | }, |
| 456 | |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 457 | get_sorted_item_groups: function() { |
| 458 | list = {} |
| 459 | $.each(this.item_groups, function(i, data) { |
| 460 | list[i] = data[0] |
| 461 | }) |
| 462 | |
| 463 | return Object.keys(list).sort(function(a,b){return list[a]-list[b]}) |
| 464 | }, |
| 465 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 466 | toggle_more_btn: function() { |
| 467 | if(!this.items || this.items.length <= this.page_len) { |
| 468 | this.wrapper.find(".btn-more").hide(); |
| 469 | } else { |
| 470 | this.wrapper.find(".btn-more").show(); |
| 471 | } |
| 472 | }, |
| 473 | |
| 474 | toggle_totals_area: function(show) { |
| 475 | |
Faris Ansari | 07c9f35 | 2017-03-09 17:03:14 +0530 | [diff] [blame] | 476 | if(show === undefined) { |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 477 | show = this.is_totals_area_collapsed; |
| 478 | } |
| 479 | |
| 480 | var totals_area = this.wrapper.find('.totals-area'); |
| 481 | totals_area.find('.net-total-area, .tax-area, .discount-amount-area') |
| 482 | .toggle(show); |
| 483 | |
| 484 | if(show) { |
| 485 | totals_area.find('.collapse-btn i') |
| 486 | .removeClass('octicon-chevron-down') |
| 487 | .addClass('octicon-chevron-up'); |
| 488 | } else { |
| 489 | totals_area.find('.collapse-btn i') |
| 490 | .removeClass('octicon-chevron-up') |
| 491 | .addClass('octicon-chevron-down'); |
| 492 | } |
| 493 | |
| 494 | this.is_totals_area_collapsed = !show; |
| 495 | }, |
| 496 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 497 | make_list_customers: function () { |
| 498 | var me = this; |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 499 | this.list_customers_btn = this.page.wrapper.find('.list-customers-btn'); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 500 | this.add_customer_btn = this.wrapper.find('.add-customer-btn'); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 501 | this.pos_bill = this.wrapper.find('.pos-bill-wrapper').hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 502 | this.list_customers = this.wrapper.find('.list-customers'); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 503 | this.numeric_keypad = this.wrapper.find('.numeric_keypad'); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 504 | this.list_customers_btn.addClass("view_customer") |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 505 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 506 | me.render_list_customers(); |
| 507 | me.toggle_totals_area(false); |
| 508 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 509 | this.page.wrapper.on('click', '.list-customers-btn', function() { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 510 | $(this).toggleClass("view_customer"); |
| 511 | if($(this).hasClass("view_customer")) { |
| 512 | me.render_list_customers(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 513 | me.list_customers.show(); |
| 514 | me.pos_bill.hide(); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 515 | me.numeric_keypad.hide(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 516 | me.toggle_delete_button() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 517 | } else { |
| 518 | if(me.frm.doc.docstatus == 0) { |
| 519 | me.party_field.$input.attr('disabled', false); |
| 520 | } |
| 521 | me.pos_bill.show(); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 522 | me.toggle_totals_area(false); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 523 | me.toggle_delete_button() |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 524 | me.list_customers.hide(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 525 | me.numeric_keypad.show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 526 | } |
| 527 | }); |
| 528 | this.add_customer_btn.on('click', function() { |
| 529 | me.save_previous_entry(); |
| 530 | me.create_new(); |
| 531 | me.refresh(); |
| 532 | me.set_focus(); |
| 533 | }); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 534 | this.pos_bill.on('click', '.collapse-btn', function() { |
| 535 | me.toggle_totals_area(); |
| 536 | }); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 537 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 538 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 539 | bind_numeric_keypad: function() { |
| 540 | var me = this; |
| 541 | $(this.numeric_keypad).find('.pos-operation').on('click', function(){ |
| 542 | me.numeric_val = ''; |
| 543 | }) |
| 544 | |
| 545 | $(this.numeric_keypad).find('.numeric-keypad').on('click', function(){ |
| 546 | me.numeric_id = $(this).attr("id") || me.numeric_id; |
| 547 | me.val = $(this).attr("val") |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 548 | if(me.numeric_id) { |
| 549 | me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id) |
| 550 | } |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 551 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 552 | if(me.val && me.numeric_id) { |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 553 | me.numeric_val += me.val; |
| 554 | me.selected_field.val(flt(me.numeric_val)) |
| 555 | me.selected_field.trigger("change") |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 556 | // me.render_selected_item() |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 557 | } |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 558 | |
| 559 | if(me.numeric_id && $(this).hasClass('pos-operation')) { |
| 560 | me.numeric_keypad.find('button.pos-operation').removeClass('active'); |
| 561 | $(this).addClass('active'); |
| 562 | |
| 563 | me.selected_row.find('.pos-list-row').removeClass('active'); |
| 564 | me.selected_field.closest('.pos-list-row').addClass('active'); |
| 565 | } |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 566 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 567 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 568 | $(this.numeric_keypad).find('.numeric-del').click(function(){ |
rohitwaghchaure | 8f51a5e | 2017-06-23 18:17:04 +0530 | [diff] [blame] | 569 | if(me.numeric_id) { |
| 570 | me.selected_field = $(me.wrapper).find('.selected-item').find('.' + me.numeric_id) |
| 571 | me.numeric_val = cstr(flt(me.selected_field.val())).slice(0, -1); |
| 572 | me.selected_field.val(me.numeric_val); |
| 573 | me.selected_field.trigger("change") |
| 574 | } else { |
| 575 | //Remove an item from the cart, if focus is at selected item |
| 576 | me.remove_selected_item() |
| 577 | } |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 578 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 579 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 580 | $(this.numeric_keypad).find('.pos-pay').click(function(){ |
| 581 | me.validate(); |
| 582 | me.update_paid_amount_status(true); |
| 583 | me.create_invoice(); |
| 584 | me.make_payment(); |
| 585 | }) |
| 586 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 587 | |
rohitwaghchaure | 8f51a5e | 2017-06-23 18:17:04 +0530 | [diff] [blame] | 588 | remove_selected_item: function() { |
| 589 | this.remove_item = [] |
| 590 | idx = $(this.wrapper).find(".pos-selected-item-action").attr("data-idx") |
| 591 | this.remove_item.push(idx) |
| 592 | this.remove_zero_qty_item() |
| 593 | this.update_paid_amount_status(false) |
| 594 | }, |
| 595 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 596 | render_list_customers: function () { |
| 597 | var me = this; |
| 598 | |
| 599 | this.removed_items = []; |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 600 | // this.list_customers.empty(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 601 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 602 | if (!this.si_docs.length) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 603 | this.list_customers.find('.list-customers-table').html(""); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 604 | return; |
| 605 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 606 | |
| 607 | var html = ""; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 608 | if(this.si_docs.length) { |
| 609 | this.si_docs.forEach(function (data, i) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 610 | for (var key in data) { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 611 | html += frappe.render_template("pos_invoice_list", { |
| 612 | sr: i + 1, |
| 613 | name: key, |
| 614 | customer: data[key].customer, |
| 615 | paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency), |
| 616 | grand_total: format_currency(data[key].grand_total, me.frm.doc.currency), |
| 617 | data: me.get_doctype_status(data[key]) |
| 618 | }); |
| 619 | } |
| 620 | }); |
| 621 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 622 | this.list_customers.find('.list-customers-table').html(html); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 623 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 624 | this.list_customers.on('click', '.customer-row', function () { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 625 | me.list_customers.hide(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 626 | me.numeric_keypad.show(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 627 | me.list_customers_btn.toggleClass("view_customer"); |
| 628 | me.pos_bill.show(); |
| 629 | me.list_customers_btn.show(); |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 630 | me.frm.doc.offline_pos_name = $(this).parents().attr('invoice-name') |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 631 | me.edit_record(); |
| 632 | }) |
| 633 | |
| 634 | //actions |
| 635 | $(this.wrapper).find('.list-select-all').click(function () { |
| 636 | me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked")) |
| 637 | me.removed_items = []; |
| 638 | if ($(this).is(":checked")) { |
| 639 | $.each(me.si_docs, function (index, data) { |
| 640 | for (key in data) { |
| 641 | me.removed_items.push(key) |
| 642 | } |
| 643 | }); |
| 644 | } |
| 645 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 646 | me.toggle_delete_button(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 647 | }); |
| 648 | |
| 649 | $(this.wrapper).find('.list-delete').click(function () { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 650 | me.frm.doc.offline_pos_name = $(this).parent().parent().attr('invoice-name'); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 651 | if ($(this).is(":checked")) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 652 | me.removed_items.push(me.frm.doc.offline_pos_name); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 653 | } else { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 654 | me.removed_items.pop(me.frm.doc.offline_pos_name) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 655 | } |
| 656 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 657 | me.toggle_delete_button(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 658 | }); |
| 659 | }, |
| 660 | |
| 661 | bind_delete_event: function() { |
| 662 | var me = this; |
| 663 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 664 | $(this.page.wrapper).on('click', '.btn-danger', function(){ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 665 | frappe.confirm(__("Delete permanently?"), function () { |
| 666 | me.delete_records(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 667 | me.list_customers.find('.list-customers-table').html(""); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 668 | me.render_list_customers(); |
| 669 | }) |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 670 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 671 | }, |
| 672 | |
| 673 | set_focus: function () { |
| 674 | if (this.default_customer || this.frm.doc.customer) { |
rohitwaghchaure | 9a0db39 | 2017-09-14 10:51:21 +0530 | [diff] [blame] | 675 | this.set_customer_value_in_party_field(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 676 | this.serach_item.$input.focus(); |
| 677 | } else { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 678 | this.party_field.$input.focus(); |
| 679 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 680 | }, |
| 681 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 682 | make_customer: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 683 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 684 | |
| 685 | if(!this.party_field) { |
| 686 | if(this.page.wrapper.find('.pos-bill-toolbar').length === 0) { |
| 687 | $(frappe.render_template('customer_toolbar', { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 688 | allow_delete: this.pos_profile_data["allow_delete"] |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 689 | })).insertAfter(this.page.$title_area.hide()); |
| 690 | } |
| 691 | |
| 692 | this.party_field = frappe.ui.form.make_control({ |
| 693 | df: { |
| 694 | "fieldtype": "Data", |
| 695 | "options": this.party, |
| 696 | "label": this.party, |
| 697 | "fieldname": this.party.toLowerCase(), |
| 698 | "placeholder": __("Select or add new customer") |
| 699 | }, |
| 700 | parent: this.page.wrapper.find(".party-area"), |
| 701 | only_input: true, |
| 702 | }); |
| 703 | |
| 704 | this.party_field.make_input(); |
| 705 | setTimeout(this.set_focus.bind(this), 500); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 706 | me.toggle_delete_button(); |
Faris Ansari | 339d9c9 | 2017-03-07 18:14:06 +0530 | [diff] [blame] | 707 | } |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 708 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 709 | this.party_field.awesomeplete = |
| 710 | new Awesomplete(this.party_field.$input.get(0), { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 711 | minChars: 0, |
| 712 | maxItems: 99, |
| 713 | autoFirst: true, |
| 714 | list: [], |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 715 | filter: function (item, input) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 716 | if (item.value.includes('is_action')) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 717 | return true; |
| 718 | } |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 719 | |
| 720 | input = input.toLowerCase(); |
| 721 | item = this.get_item(item.value); |
Rohit Waghchaure | 06b22e9 | 2017-09-06 19:45:36 +0530 | [diff] [blame] | 722 | result = item ? item.searchtext.includes(input) : ''; |
| 723 | if(!result) { |
| 724 | me.prepare_customer_mapper(input); |
| 725 | } else { |
| 726 | return result; |
| 727 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 728 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 729 | item: function (item, input) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 730 | var d = this.get_item(item.value); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 731 | var html = "<span>" + __(d.label || d.value) + "</span>"; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 732 | if(d.customer_name) { |
| 733 | html += '<br><span class="text-muted ellipsis">' + __(d.customer_name) + '</span>'; |
| 734 | } |
| 735 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 736 | return $('<li></li>') |
| 737 | .data('item.autocomplete', d) |
| 738 | .html('<a><p>' + html + '</p></a>') |
| 739 | .get(0); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 740 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 741 | }); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 742 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 743 | this.prepare_customer_mapper() |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 744 | this.autocomplete_customers(); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 745 | |
| 746 | this.party_field.$input |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 747 | .on('input', function (e) { |
Rohit Waghchaure | 06b22e9 | 2017-09-06 19:45:36 +0530 | [diff] [blame] | 748 | if(me.customers_mapper.length <= 1) { |
| 749 | me.prepare_customer_mapper(e.target.value); |
| 750 | } |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 751 | me.party_field.awesomeplete.list = me.customers_mapper; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 752 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 753 | .on('awesomplete-select', function (e) { |
| 754 | var customer = me.party_field.awesomeplete |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 755 | .get_item(e.originalEvent.text.value); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 756 | if (!customer) return; |
| 757 | // create customer link |
| 758 | if (customer.action) { |
| 759 | customer.action.apply(me); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 760 | return; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 761 | } |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 762 | me.toggle_list_customer(false); |
| 763 | me.toggle_edit_button(true); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 764 | me.update_customer_data(customer); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 765 | me.refresh(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 766 | me.set_focus(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 767 | me.list_customers_btn.removeClass("view_customer"); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 768 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 769 | .on('focus', function (e) { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 770 | $(e.target).val('').trigger('input'); |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 771 | me.toggle_edit_button(false); |
Faris Ansari | 1fe1518 | 2017-03-10 14:50:39 +0530 | [diff] [blame] | 772 | |
| 773 | if(me.frm.doc.items.length) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 774 | me.toggle_list_customer(false) |
| 775 | me.toggle_item_cart(true) |
| 776 | } else { |
| 777 | me.toggle_list_customer(true) |
| 778 | me.toggle_item_cart(false) |
| 779 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 780 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 781 | .on("awesomplete-selectcomplete", function (e) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 782 | var item = me.party_field.awesomeplete |
| 783 | .get_item(e.originalEvent.text.value); |
| 784 | // clear text input if item is action |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 785 | if (item.action) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 786 | $(this).val(""); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 787 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 788 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 789 | }, |
| 790 | |
Rohit Waghchaure | 06b22e9 | 2017-09-06 19:45:36 +0530 | [diff] [blame] | 791 | prepare_customer_mapper: function(key) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 792 | var me = this; |
Rohit Waghchaure | 06b22e9 | 2017-09-06 19:45:36 +0530 | [diff] [blame] | 793 | var customer_data = ''; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 794 | |
Rohit Waghchaure | 06b22e9 | 2017-09-06 19:45:36 +0530 | [diff] [blame] | 795 | if (key) { |
| 796 | key = key.toLowerCase().trim(); |
| 797 | var re = new RegExp('%', 'g'); |
| 798 | var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*')); |
| 799 | |
| 800 | customer_data = $.grep(this.customers, function(data) { |
| 801 | contact = me.contacts[data.name]; |
| 802 | if(reg.test(data.name.toLowerCase()) |
| 803 | || reg.test(data.customer_name.toLowerCase()) |
| 804 | || (contact && reg.test(contact["mobile_no"])) |
| 805 | || (contact && reg.test(contact["phone"])) |
| 806 | || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){ |
| 807 | return data; |
| 808 | } |
| 809 | }) |
| 810 | } else { |
| 811 | customer_data = this.customers; |
| 812 | } |
| 813 | |
| 814 | this.customers_mapper = []; |
| 815 | |
| 816 | customer_data.forEach(function (c, index) { |
| 817 | if(index < 30) { |
| 818 | contact = me.contacts[c.name]; |
| 819 | if(contact && !c['phone']) { |
| 820 | c["phone"] = contact["phone"]; |
| 821 | c["email_id"] = contact["email_id"]; |
| 822 | c["mobile_no"] = contact["mobile_no"]; |
| 823 | } |
| 824 | |
| 825 | me.customers_mapper.push({ |
| 826 | label: c.name, |
| 827 | value: c.name, |
| 828 | customer_name: c.customer_name, |
| 829 | customer_group: c.customer_group, |
| 830 | territory: c.territory, |
| 831 | phone: contact ? contact["phone"] : '', |
| 832 | mobile_no: contact ? contact["mobile_no"] : '', |
| 833 | email_id: contact ? contact["email_id"] : '', |
| 834 | searchtext: ['customer_name', 'customer_group', 'name', 'value', |
| 835 | 'label', 'email_id', 'phone', 'mobile_no'] |
| 836 | .map(key => c[key]).join(' ') |
| 837 | .toLowerCase() |
| 838 | }); |
| 839 | } else { |
| 840 | return; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 841 | } |
| 842 | }); |
| 843 | |
| 844 | this.customers_mapper.push({ |
| 845 | label: "<span class='text-primary link-option'>" |
| 846 | + "<i class='fa fa-plus' style='margin-right: 5px;'></i> " |
| 847 | + __("Create a new Customer") |
| 848 | + "</span>", |
| 849 | value: 'is_action', |
| 850 | action: me.add_customer |
| 851 | }); |
| 852 | }, |
| 853 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 854 | autocomplete_customers: function() { |
| 855 | this.party_field.awesomeplete.list = this.customers_mapper; |
| 856 | }, |
| 857 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 858 | toggle_edit_button: function(flag) { |
| 859 | this.page.wrapper.find('.edit-customer-btn').toggle(flag); |
| 860 | }, |
| 861 | |
| 862 | toggle_list_customer: function(flag) { |
| 863 | this.list_customers.toggle(flag); |
| 864 | }, |
| 865 | |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 866 | toggle_item_cart: function(flag) { |
| 867 | this.wrapper.find('.pos-bill-wrapper').toggle(flag); |
| 868 | }, |
| 869 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 870 | add_customer: function() { |
| 871 | this.frm.doc.customer = ""; |
rohitwaghchaure | 09483d3 | 2017-05-16 08:51:24 +0530 | [diff] [blame] | 872 | this.update_customer(true); |
| 873 | this.numeric_keypad.show(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 874 | }, |
| 875 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 876 | update_customer: function (new_customer) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 877 | var me = this; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 878 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 879 | this.customer_doc = new frappe.ui.Dialog({ |
| 880 | 'title': 'Customer', |
| 881 | fields: [ |
| 882 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 883 | "label": __("Full Name"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 884 | "fieldname": "full_name", |
| 885 | "fieldtype": "Data", |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 886 | "reqd": 1 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 887 | }, |
| 888 | { |
| 889 | "fieldtype": "Section Break" |
| 890 | }, |
| 891 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 892 | "label": __("Email Id"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 893 | "fieldname": "email_id", |
| 894 | "fieldtype": "Data" |
| 895 | }, |
| 896 | { |
| 897 | "fieldtype": "Column Break" |
| 898 | }, |
| 899 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 900 | "label": __("Contact Number"), |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 901 | "fieldname": "phone", |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 902 | "fieldtype": "Data" |
| 903 | }, |
| 904 | { |
| 905 | "fieldtype": "Section Break" |
| 906 | }, |
| 907 | { |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 908 | "label": __("Address Name"), |
| 909 | "read_only": 1, |
| 910 | "fieldname": "name", |
| 911 | "fieldtype": "Data" |
| 912 | }, |
| 913 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 914 | "label": __("Address Line 1"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 915 | "fieldname": "address_line1", |
| 916 | "fieldtype": "Data" |
| 917 | }, |
| 918 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 919 | "label": __("Address Line 2"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 920 | "fieldname": "address_line2", |
| 921 | "fieldtype": "Data" |
| 922 | }, |
| 923 | { |
| 924 | "fieldtype": "Column Break" |
| 925 | }, |
| 926 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 927 | "label": __("City"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 928 | "fieldname": "city", |
| 929 | "fieldtype": "Data" |
| 930 | }, |
| 931 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 932 | "label": __("State"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 933 | "fieldname": "state", |
| 934 | "fieldtype": "Data" |
| 935 | }, |
| 936 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 937 | "label": __("ZIP Code"), |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 938 | "fieldname": "pincode", |
| 939 | "fieldtype": "Data" |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 940 | }, |
| 941 | { |
| 942 | "label": __("Customer POS Id"), |
| 943 | "fieldname": "customer_pos_id", |
| 944 | "fieldtype": "Data", |
| 945 | "hidden": 1 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 946 | } |
| 947 | ] |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 948 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 949 | this.customer_doc.show() |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 950 | this.render_address_data() |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 951 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 952 | this.customer_doc.set_primary_action(__("Save"), function () { |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 953 | me.make_offline_customer(new_customer); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 954 | me.pos_bill.show(); |
Rohit Waghchaure | fd37516 | 2017-05-02 18:27:09 +0530 | [diff] [blame] | 955 | me.list_customers.hide(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 956 | }); |
| 957 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 958 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 959 | render_address_data: function() { |
| 960 | var me = this; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 961 | this.address_data = this.address[this.frm.doc.customer] || {}; |
| 962 | if(!this.address_data.email_id || !this.address_data.phone) { |
| 963 | this.address_data = this.contacts[this.frm.doc.customer]; |
| 964 | } |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 965 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 966 | this.customer_doc.set_values(this.address_data) |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 967 | if(!this.customer_doc.fields_dict.full_name.$input.val()) { |
| 968 | this.customer_doc.set_value("full_name", this.frm.doc.customer) |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 969 | } |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 970 | |
| 971 | if(!this.customer_doc.fields_dict.customer_pos_id.value) { |
| 972 | this.customer_doc.set_value("customer_pos_id", $.now()) |
| 973 | } |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 974 | }, |
| 975 | |
| 976 | get_address_from_localstorage: function() { |
| 977 | this.address_details = this.get_customers_details() |
| 978 | return this.address_details[this.frm.doc.customer] |
| 979 | }, |
| 980 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 981 | make_offline_customer: function(new_customer) { |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 982 | this.frm.doc.customer = this.frm.doc.customer || this.customer_doc.get_values().full_name; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 983 | this.frm.doc.customer_pos_id = this.customer_doc.fields_dict.customer_pos_id.value; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 984 | this.customer_details = this.get_customers_details(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 985 | this.customer_details[this.frm.doc.customer] = this.get_prompt_details(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 986 | this.party_field.$input.val(this.frm.doc.customer); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 987 | this.update_address_and_customer_list(new_customer) |
| 988 | this.autocomplete_customers(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 989 | this.update_customer_in_localstorage() |
| 990 | this.update_customer_in_localstorage() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 991 | this.customer_doc.hide() |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 992 | }, |
| 993 | |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 994 | update_address_and_customer_list: function(new_customer) { |
| 995 | var me = this; |
| 996 | if(new_customer) { |
| 997 | this.customers_mapper.push({ |
| 998 | label: this.frm.doc.customer, |
| 999 | value: this.frm.doc.customer, |
| 1000 | customer_group: "", |
| 1001 | territory: "" |
| 1002 | }); |
| 1003 | } |
| 1004 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1005 | this.address[this.frm.doc.customer] = JSON.parse(this.get_prompt_details()) |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1006 | }, |
| 1007 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1008 | get_prompt_details: function() { |
| 1009 | this.prompt_details = this.customer_doc.get_values(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1010 | this.prompt_details['country'] = this.pos_profile_data.country; |
rohitwaghchaure | e2176b8 | 2017-07-28 20:52:02 +0530 | [diff] [blame] | 1011 | this.prompt_details['territory'] = this.pos_profile_data["territory"]; |
| 1012 | this.prompt_details['customer_group'] = this.pos_profile_data["customer_group"]; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1013 | this.prompt_details['customer_pos_id'] = this.customer_doc.fields_dict.customer_pos_id.value; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1014 | return JSON.stringify(this.prompt_details) |
| 1015 | }, |
| 1016 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1017 | update_customer_data: function (doc) { |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 1018 | var me = this; |
| 1019 | this.frm.doc.customer = doc.label || doc.name; |
| 1020 | this.frm.doc.customer_name = doc.customer_name; |
| 1021 | this.frm.doc.customer_group = doc.customer_group; |
| 1022 | this.frm.doc.territory = doc.territory; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1023 | this.pos_bill.show(); |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 1024 | this.numeric_keypad.show(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1025 | }, |
| 1026 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1027 | make_item_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1028 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1029 | if (!this.price_list) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1030 | frappe.msgprint(__("Price List not found or disabled")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | me.item_timeout = null; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1035 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1036 | var $wrap = me.wrapper.find(".item-list"); |
| 1037 | me.wrapper.find(".item-list").empty(); |
| 1038 | |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 1039 | if (this.items.length > 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1040 | $.each(this.items, function(index, obj) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1041 | if(index < me.page_len) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1042 | $(frappe.render_template("pos_item", { |
| 1043 | item_code: obj.name, |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1044 | item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1045 | item_name: obj.name === obj.item_name ? "" : obj.item_name, |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1046 | item_image: obj.image, |
Faris Ansari | 185762a | 2017-04-13 12:36:08 +0530 | [diff] [blame] | 1047 | item_stock: __('Stock Qty') + ": " + me.get_actual_qty(obj), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1048 | color: frappe.get_palette(obj.item_name), |
| 1049 | abbr: frappe.get_abbr(obj.item_name) |
| 1050 | })).tooltip().appendTo($wrap); |
| 1051 | } |
| 1052 | }); |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1053 | |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1054 | $wrap.append(` |
| 1055 | <div class="image-view-item btn-more text-muted text-center"> |
| 1056 | <div class="image-view-body"> |
| 1057 | <i class="mega-octicon octicon-package"></i> |
| 1058 | <div>Load more items</div> |
| 1059 | </div> |
| 1060 | </div> |
| 1061 | `); |
| 1062 | |
| 1063 | me.toggle_more_btn(); |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 1064 | } else { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1065 | $("<p class='text-muted small' style='padding-left: 10px'>" |
| 1066 | +__("Not items found")+"</p>").appendTo($wrap) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1067 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1068 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1069 | if (this.items.length == 1 |
| 1070 | && this.serach_item.$input.val()) { |
| 1071 | this.serach_item.$input.val(""); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1072 | this.add_to_cart(); |
| 1073 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1074 | }, |
| 1075 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1076 | get_items: function (item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1077 | // To search item as per the key enter |
| 1078 | |
| 1079 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1080 | this.item_serial_no = {}; |
| 1081 | this.item_batch_no = {}; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1082 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1083 | if (item_code) { |
| 1084 | return $.grep(this.item_data, function (item) { |
| 1085 | if (item.item_code == item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1086 | return true |
| 1087 | } |
| 1088 | }) |
| 1089 | } |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1090 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1091 | this.items_list = this.apply_category(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1092 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1093 | key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&'); |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 1094 | var re = new RegExp('%', 'g'); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 1095 | var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*')) |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1096 | search_status = true |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1097 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1098 | if (key) { |
| 1099 | return $.grep(this.items_list, function (item) { |
| 1100 | if (search_status) { |
| 1101 | if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1102 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1103 | return me.item_batch_no[item.item_code] = me.serach_item.$input.val() |
| 1104 | } else if (me.serial_no_data[item.item_code] |
| 1105 | && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1106 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1107 | 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 Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1108 | return true |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 1109 | } else if (in_list(me.barcode_data[item.item_code], me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1110 | search_status = false; |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 1111 | return true; |
rohitwaghchaure | 860144f | 2017-07-12 15:01:56 +0530 | [diff] [blame] | 1112 | } else if (reg.test(item.item_code.toLowerCase()) || (item.description && reg.test(item.description.toLowerCase())) || |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1113 | reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 1114 | return true |
| 1115 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1116 | } |
| 1117 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1118 | } else { |
| 1119 | return this.items_list; |
| 1120 | } |
| 1121 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 1122 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1123 | apply_category: function() { |
| 1124 | var me = this; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1125 | category = this.selected_item_group || "All Item Groups"; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1126 | |
| 1127 | if(category == 'All Item Groups') { |
| 1128 | return this.item_data |
| 1129 | } else { |
| 1130 | return this.item_data.filter(function(element, index, array){ |
| 1131 | return element.item_group == category; |
| 1132 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1133 | } |
| 1134 | }, |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1135 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1136 | bind_items_event: function() { |
| 1137 | var me = this; |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1138 | $(this.wrapper).on('click', '.pos-bill-item', function() { |
| 1139 | $(me.wrapper).find('.pos-bill-item').removeClass('active'); |
| 1140 | $(this).addClass('active'); |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1141 | me.numeric_val = ""; |
| 1142 | me.numeric_id = "" |
| 1143 | me.item_code = $(this).attr("data-item-code"); |
| 1144 | me.render_selected_item() |
| 1145 | me.bind_qty_event() |
| 1146 | me.update_rate() |
| 1147 | $(me.wrapper).find(".selected-item").scrollTop(1000); |
| 1148 | }) |
| 1149 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1150 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1151 | bind_qty_event: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1152 | var me = this; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1153 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1154 | $(this.wrapper).on("change", ".pos-item-qty", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1155 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1156 | var qty = $(this).val(); |
| 1157 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1158 | me.update_value() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1159 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1160 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1161 | $(this.wrapper).find("[data-action='increase-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1162 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 1163 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1164 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1165 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1166 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1167 | $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1168 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 1169 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1170 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1171 | }) |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1172 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1173 | $(this.wrapper).on("change", ".pos-item-disc", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1174 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1175 | var discount = $(this).val(); |
| 1176 | me.update_discount(item_code, discount) |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1177 | me.update_value() |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1178 | }) |
| 1179 | }, |
| 1180 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1181 | bind_events: function() { |
| 1182 | var me = this; |
| 1183 | // if form is local then allow this function |
| 1184 | // $(me.wrapper).find(".pos-item-wrapper").on("click", function () { |
| 1185 | $(this.wrapper).on("click", ".pos-item-wrapper", function () { |
Rohit Waghchaure | a86bb69 | 2017-03-30 19:16:38 +0530 | [diff] [blame] | 1186 | me.item_code = ''; |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1187 | me.customer_validate(); |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1188 | if($(me.pos_bill).is(":hidden")) return; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1189 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1190 | if (me.frm.doc.docstatus == 0) { |
| 1191 | me.items = me.get_items($(this).attr("data-item-code")) |
| 1192 | me.add_to_cart(); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1193 | me.clear_selected_row(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1194 | } |
| 1195 | }); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1196 | |
| 1197 | me.bind_delete_event() |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1198 | }, |
| 1199 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1200 | update_qty: function (item_code, qty) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1201 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1202 | this.items = this.get_items(item_code); |
| 1203 | this.validate_serial_no() |
| 1204 | this.set_item_details(item_code, "qty", qty); |
| 1205 | }, |
| 1206 | |
| 1207 | update_discount: function(item_code, discount) { |
| 1208 | var me = this; |
| 1209 | this.items = this.get_items(item_code); |
| 1210 | this.set_item_details(item_code, "discount_percentage", discount); |
| 1211 | }, |
| 1212 | |
| 1213 | update_rate: function () { |
| 1214 | var me = this; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1215 | $(this.wrapper).on("change", ".pos-item-price", function () { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1216 | var item_code = $(this).parents(".pos-selected-item-action").attr("data-item-code"); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1217 | me.set_item_details(item_code, "rate", $(this).val()); |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1218 | me.update_value() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1219 | }) |
| 1220 | }, |
Rohit Waghchaure | 56a7974 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1221 | |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1222 | update_value: function() { |
| 1223 | var me = this; |
| 1224 | var fields = {qty: ".pos-item-qty", "discount_percentage": ".pos-item-disc", |
| 1225 | "rate": ".pos-item-price", "amount": ".pos-amount"} |
| 1226 | this.child_doc = this.get_child_item(this.item_code); |
| 1227 | |
| 1228 | if(me.child_doc.length) { |
| 1229 | $.each(fields, function(key, field) { |
| 1230 | $(me.selected_row).find(field).val(me.child_doc[0][key]) |
| 1231 | }) |
| 1232 | } else { |
| 1233 | this.clear_selected_row(); |
Rohit Waghchaure | 56a7974 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1234 | } |
| 1235 | }, |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1236 | |
| 1237 | clear_selected_row: function() { |
| 1238 | $(this.wrapper).find('.selected-item').empty(); |
| 1239 | }, |
| 1240 | |
| 1241 | render_selected_item: function() { |
| 1242 | this.child_doc = this.get_child_item(this.item_code); |
| 1243 | $(this.wrapper).find('.selected-item').empty(); |
| 1244 | if(this.child_doc.length) { |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1245 | this.child_doc[0]["allow_user_to_edit_rate"] = this.pos_profile_data["allow_user_to_edit_rate"] ? true : false, |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1246 | this.selected_row = $(frappe.render_template("pos_selected_item", this.child_doc[0])) |
| 1247 | $(this.wrapper).find('.selected-item').html(this.selected_row) |
| 1248 | } |
| 1249 | |
| 1250 | $(this.selected_row).find('.form-control').click(function(){ |
| 1251 | $(this).select(); |
| 1252 | }) |
| 1253 | }, |
| 1254 | |
Rohit Waghchaure | 86ab6a9 | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 1255 | get_child_item: function(item_code) { |
| 1256 | var me = this; |
| 1257 | return $.map(me.frm.doc.items, function(doc){ |
| 1258 | if(doc.item_code == item_code) { |
| 1259 | return doc |
| 1260 | } |
| 1261 | }) |
| 1262 | }, |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1263 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1264 | set_item_details: function (item_code, field, value) { |
| 1265 | var me = this; |
| 1266 | if (value < 0) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1267 | frappe.throw(__("Enter value must be positive")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1268 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1269 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1270 | this.remove_item = [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1271 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1272 | if (d.item_code == item_code) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1273 | if (d.serial_no && field == 'qty') { |
| 1274 | me.validate_serial_no_qty(d, item_code, field, value) |
| 1275 | } |
| 1276 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1277 | d[field] = flt(value); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1278 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1279 | if (d.qty == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1280 | me.remove_item.push(d.idx) |
| 1281 | } |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1282 | |
| 1283 | if(field=="discount_percentage" && value == 0) { |
| 1284 | d.rate = d.price_list_rate; |
| 1285 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1286 | } |
| 1287 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1288 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1289 | if (field == 'qty') { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1290 | this.remove_zero_qty_item(); |
| 1291 | } |
| 1292 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1293 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1294 | }, |
| 1295 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1296 | remove_zero_qty_item: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1297 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1298 | var idx = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1299 | this.items = [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1300 | $.each(this.frm.doc["items"] || [], function (i, d) { |
| 1301 | if (!in_list(me.remove_item, d.idx)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1302 | d.idx = idx; |
| 1303 | me.items.push(d); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1304 | idx++; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1305 | } |
| 1306 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1307 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1308 | this.frm.doc["items"] = this.items; |
| 1309 | }, |
| 1310 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1311 | make_discount_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1312 | var me = this; |
| 1313 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1314 | this.wrapper.find('input.discount-percentage').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1315 | me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage")); |
rohitwaghchaure | 5fdd26f | 2017-10-21 11:23:45 +0530 | [diff] [blame] | 1316 | |
| 1317 | if(me.frm.doc.additional_discount_percentage && me.frm.doc.discount_amount) { |
| 1318 | // Reset discount amount |
| 1319 | me.frm.doc.discount_amount = 0; |
| 1320 | } |
| 1321 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1322 | var total = me.frm.doc.grand_total |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1323 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1324 | if (me.frm.doc.apply_discount_on == 'Net Total') { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1325 | total = me.frm.doc.net_total |
| 1326 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1327 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1328 | me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1329 | me.refresh(); |
rohitwaghchaure | 5fdd26f | 2017-10-21 11:23:45 +0530 | [diff] [blame] | 1330 | me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1331 | }); |
| 1332 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1333 | this.wrapper.find('input.discount-amount').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1334 | me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount")); |
| 1335 | me.frm.doc.additional_discount_percentage = 0.0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1336 | me.refresh(); |
rohitwaghchaure | 5fdd26f | 2017-10-21 11:23:45 +0530 | [diff] [blame] | 1337 | me.wrapper.find('input.discount-percentage').val(0); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1338 | }); |
| 1339 | }, |
| 1340 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1341 | customer_validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1342 | var me = this; |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1343 | if (!this.frm.doc.customer || this.party_field.get_value() == "") { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1344 | frappe.throw(__("Please select customer")) |
| 1345 | } |
| 1346 | }, |
| 1347 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1348 | add_to_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1349 | var me = this; |
| 1350 | var caught = false; |
| 1351 | var no_of_items = me.wrapper.find(".pos-bill-item").length; |
| 1352 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1353 | this.customer_validate(); |
| 1354 | this.mandatory_batch_no(); |
| 1355 | this.validate_serial_no(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1356 | this.validate_warehouse(); |
| 1357 | |
| 1358 | if (no_of_items != 0) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1359 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1360 | if (d.item_code == me.items[0].item_code) { |
| 1361 | caught = true; |
| 1362 | d.qty += 1; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1363 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1364 | if (me.item_serial_no[d.item_code]) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1365 | d.serial_no += '\n' + me.item_serial_no[d.item_code][0] |
| 1366 | d.warehouse = me.item_serial_no[d.item_code][1] |
| 1367 | } |
| 1368 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1369 | if (me.item_batch_no.length) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1370 | d.batch_no = me.item_batch_no[d.item_code] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | }); |
| 1374 | } |
| 1375 | |
| 1376 | // if item not found then add new item |
| 1377 | if (!caught) |
| 1378 | this.add_new_item_to_grid(); |
| 1379 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1380 | this.update_paid_amount_status(false) |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1381 | this.wrapper.find(".item-cart-items").scrollTop(1000); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1382 | }, |
| 1383 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1384 | add_new_item_to_grid: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1385 | var me = this; |
| 1386 | this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items"); |
| 1387 | this.child.item_code = this.items[0].item_code; |
| 1388 | this.child.item_name = this.items[0].item_name; |
| 1389 | this.child.stock_uom = this.items[0].stock_uom; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1390 | this.child.brand = this.items[0].brand; |
rohitwaghchaure | 860144f | 2017-07-12 15:01:56 +0530 | [diff] [blame] | 1391 | this.child.description = this.items[0].description || this.items[0].item_name; |
Rohit Waghchaure | e30f83a | 2017-02-24 18:02:50 +0530 | [diff] [blame] | 1392 | this.child.discount_percentage = 0.0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1393 | this.child.qty = 1; |
| 1394 | this.child.item_group = this.items[0].item_group; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1395 | this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center; |
| 1396 | this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1397 | this.child.warehouse = (this.item_serial_no[this.child.item_code] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1398 | ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse)); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1399 | this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 1400 | this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 1401 | this.child.actual_qty = me.get_actual_qty(this.items[0]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1402 | this.child.amount = flt(this.child.qty) * flt(this.child.rate); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1403 | this.child.batch_no = this.item_batch_no[this.child.item_code]; |
| 1404 | this.child.serial_no = (this.item_serial_no[this.child.item_code] |
| 1405 | ? this.item_serial_no[this.child.item_code][0] : ''); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1406 | this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1407 | }, |
| 1408 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1409 | update_paid_amount_status: function (update_paid_amount) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1410 | if (this.frm.doc.offline_pos_name) { |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1411 | update_paid_amount = update_paid_amount ? false : true; |
| 1412 | } |
| 1413 | |
| 1414 | this.refresh(update_paid_amount); |
| 1415 | }, |
| 1416 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1417 | refresh: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1418 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 1419 | this.refresh_fields(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1420 | this.set_primary_action(); |
| 1421 | }, |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1422 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1423 | refresh_fields: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1424 | this.apply_pricing_rule(); |
| 1425 | this.discount_amount_applied = false; |
| 1426 | this._calculate_taxes_and_totals(); |
| 1427 | this.calculate_discount_amount(); |
| 1428 | this.show_items_in_item_cart(); |
| 1429 | this.set_taxes(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 1430 | this.calculate_outstanding_amount(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1431 | this.set_totals(); |
| 1432 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1433 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1434 | get_company_currency: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1435 | return erpnext.get_currency(this.frm.doc.company); |
| 1436 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1437 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1438 | show_items_in_item_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1439 | var me = this; |
| 1440 | var $items = this.wrapper.find(".items").empty(); |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1441 | var $no_items_message = this.wrapper.find(".no-items-message"); |
| 1442 | $no_items_message.toggle(this.frm.doc.items.length === 0); |
| 1443 | |
| 1444 | var $totals_area = this.wrapper.find('.totals-area'); |
| 1445 | $totals_area.toggle(this.frm.doc.items.length > 0); |
| 1446 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1447 | $.each(this.frm.doc.items || [], function (i, d) { |
Faris Ansari | 1f261a8 | 2017-03-06 18:01:58 +0530 | [diff] [blame] | 1448 | $(frappe.render_template("pos_bill_item_new", { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1449 | item_code: d.item_code, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1450 | item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1451 | qty: d.qty, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1452 | discount_percentage: d.discount_percentage || 0.0, |
| 1453 | actual_qty: me.actual_qty_dict[d.item_code] || 0.0, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1454 | projected_qty: d.projected_qty, |
mbauskar | 22cedeb | 2017-04-17 12:24:11 +0530 | [diff] [blame] | 1455 | rate: format_currency(d.rate, me.frm.doc.currency), |
Rohit Waghchaure | 6ee91ec | 2017-03-21 15:55:09 +0530 | [diff] [blame] | 1456 | amount: format_currency(d.amount, me.frm.doc.currency), |
| 1457 | selected_class: (me.item_code == d.item_code) ? "active" : "" |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1458 | })).appendTo($items); |
| 1459 | }); |
| 1460 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1461 | this.wrapper.find("input.pos-item-qty").on("focus", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1462 | $(this).select(); |
| 1463 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1464 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1465 | this.wrapper.find("input.pos-item-disc").on("focus", function () { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1466 | $(this).select(); |
| 1467 | }); |
| 1468 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1469 | this.wrapper.find("input.pos-item-price").on("focus", function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1470 | $(this).select(); |
| 1471 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1472 | }, |
| 1473 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1474 | set_taxes: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1475 | var me = this; |
| 1476 | me.frm.doc.total_taxes_and_charges = 0.0 |
| 1477 | |
| 1478 | var taxes = this.frm.doc.taxes || []; |
| 1479 | $(this.wrapper) |
| 1480 | .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true) |
| 1481 | .find(".tax-table").empty(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1482 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1483 | $.each(taxes, function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1484 | if (d.tax_amount && cint(d.included_in_print_rate) == 0) { |
| 1485 | $(frappe.render_template("pos_tax_row", { |
| 1486 | description: d.description, |
| 1487 | tax_amount: format_currency(flt(d.tax_amount_after_discount_amount), |
| 1488 | me.frm.doc.currency) |
| 1489 | })).appendTo(me.wrapper.find(".tax-table")); |
| 1490 | } |
| 1491 | }); |
| 1492 | }, |
| 1493 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1494 | set_totals: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1495 | var me = this; |
| 1496 | this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency)); |
| 1497 | this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency)); |
rohitwaghchaure | 5fdd26f | 2017-10-21 11:23:45 +0530 | [diff] [blame] | 1498 | this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage); |
| 1499 | this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1500 | }, |
| 1501 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1502 | set_primary_action: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1503 | var me = this; |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1504 | this.page.set_primary_action(__("New Cart"), function () { |
| 1505 | me.make_new_cart() |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1506 | me.make_menu_list() |
Faris Ansari | 4551010 | 2017-03-09 14:43:37 +0530 | [diff] [blame] | 1507 | }, "fa fa-plus") |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1508 | |
augustinfotech | 135a54c | 2017-11-16 15:28:42 +0530 | [diff] [blame] | 1509 | if (this.frm.doc.docstatus == 1 || this.pos_profile_data["allow_print_before_pay"]) { |
Rohit Waghchaure | 6068aec | 2017-03-10 11:40:28 +0530 | [diff] [blame] | 1510 | this.page.set_secondary_action(__("Print"), function () { |
augustinfotech | 135a54c | 2017-11-16 15:28:42 +0530 | [diff] [blame] | 1511 | me.create_invoice(); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1512 | var html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1513 | me.print_document(html) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1514 | }) |
augustinfotech | 135a54c | 2017-11-16 15:28:42 +0530 | [diff] [blame] | 1515 | } |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 1516 | |
| 1517 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1518 | this.page.add_menu_item(__("Email"), function () { |
| 1519 | me.email_prompt() |
| 1520 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1521 | } |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1522 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 1523 | |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1524 | make_new_cart: function (){ |
Rohit Waghchaure | a86bb69 | 2017-03-30 19:16:38 +0530 | [diff] [blame] | 1525 | this.item_code = ''; |
Rohit Waghchaure | 75177c0 | 2017-03-16 15:21:21 +0530 | [diff] [blame] | 1526 | this.page.clear_secondary_action(); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1527 | this.save_previous_entry(); |
| 1528 | this.create_new(); |
| 1529 | this.refresh(); |
| 1530 | this.toggle_input_field(); |
| 1531 | this.render_list_customers(); |
| 1532 | this.set_focus(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1533 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1534 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1535 | print_dialog: function () { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1536 | var me = this; |
| 1537 | |
mbauskar | 66e2a51 | 2017-06-22 16:18:12 +0530 | [diff] [blame] | 1538 | this.msgprint = frappe.msgprint( |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1539 | `<a class="btn btn-primary print_doc" |
| 1540 | style="margin-right: 5px;">${__('Print')}</a> |
| 1541 | <a class="btn btn-default new_doc">${__('New')}</a>`); |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1542 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1543 | $('.print_doc').click(function () { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1544 | var html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1545 | me.print_document(html) |
| 1546 | }) |
| 1547 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1548 | $('.new_doc').click(function () { |
mbauskar | 66e2a51 | 2017-06-22 16:18:12 +0530 | [diff] [blame] | 1549 | me.msgprint.hide() |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1550 | me.make_new_cart() |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1551 | }) |
| 1552 | }, |
| 1553 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1554 | print_document: function (html) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1555 | var w = window.open(); |
| 1556 | w.document.write(html); |
| 1557 | w.document.close(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1558 | setTimeout(function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1559 | w.print(); |
| 1560 | w.close(); |
| 1561 | }, 1000) |
| 1562 | }, |
| 1563 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1564 | submit_invoice: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1565 | var me = this; |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1566 | this.change_status(); |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1567 | this.update_serial_no() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1568 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1569 | this.print_dialog() |
| 1570 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1571 | }, |
| 1572 | |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1573 | update_serial_no: function() { |
| 1574 | var me = this; |
| 1575 | |
| 1576 | //Remove the sold serial no from the cache |
| 1577 | $.each(this.frm.doc.items, function(index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1578 | var sn = data.serial_no.split('\n') |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1579 | if(sn.length) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1580 | var serial_no_list = me.serial_no_data[data.item_code] |
Rohit Waghchaure | b719dc5 | 2017-03-23 17:04:00 +0530 | [diff] [blame] | 1581 | if(serial_no_list) { |
| 1582 | $.each(sn, function(i, serial_no) { |
| 1583 | if(in_list(Object.keys(serial_no_list), serial_no)) { |
| 1584 | delete serial_no_list[serial_no] |
| 1585 | } |
| 1586 | }) |
| 1587 | me.serial_no_data[data.item_code] = serial_no_list; |
| 1588 | } |
Rohit Waghchaure | e259a9b | 2017-03-14 19:03:49 +0530 | [diff] [blame] | 1589 | } |
| 1590 | }) |
| 1591 | }, |
| 1592 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1593 | change_status: function () { |
| 1594 | if (this.frm.doc.docstatus == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1595 | this.frm.doc.docstatus = 1; |
| 1596 | this.update_invoice(); |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1597 | this.toggle_input_field(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1598 | } |
| 1599 | }, |
| 1600 | |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1601 | toggle_input_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1602 | var pointer_events = 'inherit' |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1603 | var disabled = this.frm.doc.docstatus == 1 ? true: false; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1604 | $(this.wrapper).find('input').attr("disabled", disabled); |
| 1605 | $(this.wrapper).find('select').attr("disabled", disabled); |
| 1606 | $(this.wrapper).find('input').attr("disabled", disabled); |
| 1607 | $(this.wrapper).find('select').attr("disabled", disabled); |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1608 | $(this.wrapper).find('button').attr("disabled", disabled); |
| 1609 | this.party_field.$input.attr('disabled', disabled); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1610 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1611 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1612 | pointer_events = 'none'; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1613 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1614 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1615 | $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1616 | $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events); |
| 1617 | this.set_primary_action(); |
| 1618 | }, |
| 1619 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1620 | create_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1621 | var me = this; |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1622 | var invoice_data = {}; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1623 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1624 | if (this.frm.doc.offline_pos_name) { |
| 1625 | this.update_invoice(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1626 | } else { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1627 | this.frm.doc.offline_pos_name = $.now(); |
Rohit Waghchaure | 9cd356c | 2016-08-11 16:54:30 +0530 | [diff] [blame] | 1628 | this.frm.doc.posting_date = frappe.datetime.get_today(); |
| 1629 | this.frm.doc.posting_time = frappe.datetime.now_time(); |
Charles-Henri Decultot | 2305eda | 2017-06-19 05:55:54 +0200 | [diff] [blame] | 1630 | this.frm.doc.pos_profile = this.pos_profile_data['name']; |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1631 | invoice_data[this.frm.doc.offline_pos_name] = this.frm.doc; |
| 1632 | this.si_docs.push(invoice_data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1633 | this.update_localstorage(); |
| 1634 | this.set_primary_action(); |
| 1635 | } |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1636 | return invoice_data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1637 | }, |
| 1638 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1639 | update_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1640 | var me = this; |
| 1641 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1642 | $.each(this.si_docs, function (index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1643 | for (var key in data) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1644 | if (key == me.frm.doc.offline_pos_name) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1645 | me.si_docs[index][key] = me.frm.doc; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1646 | me.update_localstorage(); |
| 1647 | } |
| 1648 | } |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1649 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1650 | }, |
| 1651 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1652 | update_localstorage: function () { |
| 1653 | try { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1654 | localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs)); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1655 | } catch (e) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1656 | frappe.throw(__("LocalStorage is full , did not save")) |
| 1657 | } |
| 1658 | }, |
| 1659 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1660 | get_doc_from_localstorage: function () { |
| 1661 | try { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1662 | return JSON.parse(localStorage.getItem('sales_invoice_doc')) || []; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1663 | } catch (e) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1664 | return [] |
| 1665 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1666 | }, |
| 1667 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1668 | set_interval_for_si_sync: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1669 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1670 | setInterval(function () { |
rohitwaghchaure | 8849171 | 2017-10-02 12:13:36 +0530 | [diff] [blame] | 1671 | me.freeze_screen = false; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1672 | me.sync_sales_invoice() |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1673 | }, 60000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1674 | }, |
| 1675 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1676 | sync_sales_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1677 | var me = this; |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1678 | this.si_docs = this.get_submitted_invoice() || []; |
| 1679 | this.email_queue_list = this.get_email_queue() || {}; |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1680 | this.customers_list = this.get_customers_details() || {}; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1681 | if(this.customer_doc) { |
| 1682 | this.freeze = this.customer_doc.display |
| 1683 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1684 | |
rohitwaghchaure | 8849171 | 2017-10-02 12:13:36 +0530 | [diff] [blame] | 1685 | freeze_screen = this.freeze_screen || false; |
| 1686 | |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1687 | if ((this.si_docs.length || this.email_queue_list || this.customers_list) && !this.freeze) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1688 | this.freeze = true; |
| 1689 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1690 | frappe.call({ |
| 1691 | method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", |
rohitwaghchaure | 8849171 | 2017-10-02 12:13:36 +0530 | [diff] [blame] | 1692 | freeze: freeze_screen, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1693 | args: { |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1694 | doc_list: me.si_docs, |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1695 | email_queue_list: me.email_queue_list, |
| 1696 | customers_list: me.customers_list |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1697 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1698 | callback: function (r) { |
| 1699 | if (r.message) { |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1700 | me.freeze = false; |
rohitwaghchaure | d2be55b | 2017-06-07 12:04:01 +0530 | [diff] [blame] | 1701 | me.customers = r.message.synced_customers_list; |
| 1702 | me.address = r.message.synced_address; |
| 1703 | me.contacts = r.message.synced_contacts; |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1704 | me.removed_items = r.message.invoice; |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1705 | me.removed_email = r.message.email_queue; |
| 1706 | me.removed_customers = r.message.customers; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1707 | me.remove_doc_from_localstorage(); |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1708 | me.remove_email_queue_from_localstorage(); |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1709 | me.remove_customer_from_localstorage(); |
rohitwaghchaure | f688af3 | 2017-11-08 11:33:39 +0530 | [diff] [blame] | 1710 | me.prepare_customer_mapper(); |
| 1711 | me.autocomplete_customers(); |
| 1712 | me.render_list_customers(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | }) |
| 1716 | } |
| 1717 | }, |
| 1718 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1719 | get_submitted_invoice: function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1720 | var invoices = []; |
| 1721 | var index = 1; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1722 | var docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1723 | if (docs) { |
| 1724 | invoices = $.map(docs, function (data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1725 | for (var key in data) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1726 | if (data[key].docstatus == 1 && index < 50) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1727 | index++ |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 1728 | data[key].docstatus = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1729 | return data |
| 1730 | } |
| 1731 | } |
| 1732 | }); |
| 1733 | } |
| 1734 | |
| 1735 | return invoices |
| 1736 | }, |
| 1737 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1738 | remove_doc_from_localstorage: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1739 | var me = this; |
| 1740 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1741 | this.new_si_docs = []; |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1742 | if (this.removed_items) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1743 | $.each(this.si_docs, function (index, data) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1744 | for (var key in data) { |
Rohit Waghchaure | 017f500 | 2017-03-08 17:34:39 +0530 | [diff] [blame] | 1745 | if (!in_list(me.removed_items, key)) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1746 | me.new_si_docs.push(data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | }) |
Rohit Waghchaure | 87fa59a | 2017-03-10 14:56:19 +0530 | [diff] [blame] | 1750 | this.removed_items = []; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1751 | this.si_docs = this.new_si_docs; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1752 | this.update_localstorage(); |
| 1753 | } |
| 1754 | }, |
| 1755 | |
Rohit Waghchaure | b5097ec | 2017-03-01 01:13:09 +0530 | [diff] [blame] | 1756 | remove_email_queue_from_localstorage: function() { |
| 1757 | var me = this; |
| 1758 | this.email_queue = this.get_email_queue() |
| 1759 | if (this.removed_email) { |
| 1760 | $.each(this.email_queue_list, function (index, data) { |
| 1761 | if (in_list(me.removed_email, index)) { |
| 1762 | delete me.email_queue[index] |
| 1763 | } |
| 1764 | }) |
| 1765 | this.update_email_queue(); |
| 1766 | } |
| 1767 | }, |
| 1768 | |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1769 | remove_customer_from_localstorage: function() { |
| 1770 | var me = this; |
| 1771 | this.customer_details = this.get_customers_details() |
| 1772 | if (this.removed_customers) { |
| 1773 | $.each(this.customers_list, function (index, data) { |
| 1774 | if (in_list(me.removed_customers, index)) { |
| 1775 | delete me.customer_details[index] |
| 1776 | } |
| 1777 | }) |
| 1778 | this.update_customer_in_localstorage(); |
| 1779 | } |
| 1780 | }, |
| 1781 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1782 | validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1783 | var me = this; |
| 1784 | this.customer_validate(); |
| 1785 | this.item_validate(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1786 | this.validate_mode_of_payments(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1787 | }, |
| 1788 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1789 | item_validate: function () { |
| 1790 | if (this.frm.doc.items.length == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1791 | frappe.throw(__("Select items to save the invoice")) |
| 1792 | } |
| 1793 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1794 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1795 | validate_mode_of_payments: function () { |
| 1796 | if (this.frm.doc.payments.length === 0) { |
Saurabh | 9a6c666 | 2016-06-27 16:51:44 +0530 | [diff] [blame] | 1797 | frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile.")) |
| 1798 | } |
| 1799 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1800 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1801 | validate_serial_no: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1802 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1803 | var item_code = '' |
| 1804 | var serial_no = ''; |
| 1805 | for (var key in this.item_serial_no) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1806 | item_code = key; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1807 | serial_no = me.item_serial_no[key][0]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1808 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1809 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1810 | if (this.items[0].has_serial_no && serial_no == "") { |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1811 | this.refresh(); |
| 1812 | frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", { |
| 1813 | 'item': this.items[0].item_code |
| 1814 | }))) |
| 1815 | } |
| 1816 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1817 | if (item_code && serial_no) { |
| 1818 | $.each(this.frm.doc.items, function (index, data) { |
| 1819 | if (data.item_code == item_code) { |
| 1820 | if (in_list(data.serial_no.split('\n'), serial_no)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1821 | frappe.throw(__(repl("Serial no %(serial_no)s is already taken", { |
| 1822 | 'serial_no': serial_no |
| 1823 | }))) |
| 1824 | } |
| 1825 | } |
| 1826 | }) |
| 1827 | } |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1828 | }, |
| 1829 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1830 | validate_serial_no_qty: function (args, item_code, field, value) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1831 | var me = this; |
| 1832 | if (args.item_code == item_code && args.serial_no |
| 1833 | && field == 'qty' && cint(value) != value) { |
| 1834 | args.qty = 0.0; |
| 1835 | this.refresh(); |
| 1836 | frappe.throw(__("Serial no item cannot be a fraction")) |
| 1837 | } |
| 1838 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1839 | if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1840 | args.qty = 0.0; |
| 1841 | args.serial_no = '' |
| 1842 | this.refresh(); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1843 | frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", { |
| 1844 | 'item': item_code |
| 1845 | }))) |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1846 | } |
| 1847 | }, |
| 1848 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1849 | mandatory_batch_no: function () { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1850 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1851 | if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1852 | frappe.prompt([{ |
| 1853 | 'fieldname': 'batch', |
| 1854 | 'fieldtype': 'Select', |
| 1855 | 'label': __('Batch No'), |
| 1856 | 'reqd': 1, |
| 1857 | 'options': this.batch_no_data[this.items[0].item_code] |
| 1858 | }], |
| 1859 | function(values){ |
| 1860 | me.item_batch_no[me.items[0].item_code] = values.batch; |
| 1861 | }, |
| 1862 | __('Select Batch No')) |
| 1863 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1864 | }, |
| 1865 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1866 | apply_pricing_rule: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1867 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1868 | $.each(this.frm.doc["items"], function (n, item) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1869 | var pricing_rule = me.get_pricing_rule(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1870 | me.validate_pricing_rule(pricing_rule) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1871 | if (pricing_rule.length) { |
| 1872 | item.pricing_rule = pricing_rule[0].name; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1873 | item.margin_type = pricing_rule[0].margin_type; |
| 1874 | item.price_list_rate = pricing_rule[0].price || item.price_list_rate; |
| 1875 | item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount; |
| 1876 | item.discount_percentage = pricing_rule[0].discount_percentage || 0.0; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1877 | me.apply_pricing_rule_on_item(item) |
| 1878 | } else if (item.pricing_rule) { |
| 1879 | item.price_list_rate = me.price_list_data[item.item_code] |
Rohit Waghchaure | 619c40b | 2016-11-07 15:29:17 +0530 | [diff] [blame] | 1880 | item.margin_rate_or_amount = 0.0; |
| 1881 | item.discount_percentage = 0.0; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1882 | item.pricing_rule = null; |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1883 | me.apply_pricing_rule_on_item(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1884 | } |
Charles-Henri Decultot | 8bec023 | 2017-05-19 12:32:30 +0200 | [diff] [blame] | 1885 | |
Rohit Waghchaure | f0c7ba4 | 2017-02-28 16:51:17 +0530 | [diff] [blame] | 1886 | if(item.discount_percentage > 0) { |
| 1887 | me.apply_pricing_rule_on_item(item) |
| 1888 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1889 | }) |
| 1890 | }, |
| 1891 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1892 | get_pricing_rule: function (item) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1893 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1894 | return $.grep(this.pricing_rules, function (data) { |
| 1895 | if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) { |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1896 | if (me.validate_item_condition(data, item)) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1897 | if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1898 | return me.validate_condition(data) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1899 | } else { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1900 | return true |
| 1901 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | }) |
| 1905 | }, |
| 1906 | |
Rohit Waghchaure | d7de3c6 | 2017-04-17 13:36:08 +0530 | [diff] [blame] | 1907 | validate_item_condition: function (data, item) { |
| 1908 | var apply_on = frappe.model.scrub(data.apply_on); |
| 1909 | |
| 1910 | return (data.apply_on == 'Item Group') |
| 1911 | ? this.validate_item_group(data.item_group, item.item_group) : (data[apply_on] == item[apply_on]); |
| 1912 | }, |
| 1913 | |
| 1914 | validate_item_group: function (pr_item_group, cart_item_group) { |
| 1915 | //pr_item_group = pricing rule's item group |
| 1916 | //cart_item_group = cart item's item group |
| 1917 | //this.item_groups has information about item group's lft and rgt |
| 1918 | //for example: {'Foods': [12, 19]} |
| 1919 | |
| 1920 | pr_item_group = this.item_groups[pr_item_group] |
| 1921 | cart_item_group = this.item_groups[cart_item_group] |
| 1922 | |
| 1923 | return (cart_item_group[0] >= pr_item_group[0] && |
| 1924 | cart_item_group[1] <= pr_item_group[1]) |
| 1925 | }, |
| 1926 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1927 | validate_condition: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1928 | //This method check condition based on applicable for |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1929 | var condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1930 | if (in_list(condition[1], condition[0])) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1931 | return true |
| 1932 | } |
| 1933 | }, |
| 1934 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1935 | get_mapper_for_pricing_rule: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1936 | return { |
Rohit Waghchaure | 89ee309 | 2016-07-08 15:17:04 +0530 | [diff] [blame] | 1937 | 'Customer': [data.customer, [this.frm.doc.customer]], |
| 1938 | 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']], |
| 1939 | 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']], |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1940 | 'Campaign': [data.campaign, [this.frm.doc.campaign]], |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1941 | } |
| 1942 | }, |
| 1943 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1944 | validate_pricing_rule: function (pricing_rule) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1945 | //This method validate duplicate pricing rule |
| 1946 | var pricing_rule_name = ''; |
| 1947 | var priority = 0; |
| 1948 | var pricing_rule_list = []; |
| 1949 | var priority_list = [] |
| 1950 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1951 | if (pricing_rule.length > 1) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1952 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1953 | $.each(pricing_rule, function (index, data) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1954 | pricing_rule_name += data.name + ',' |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1955 | priority_list.push(data.priority) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1956 | if (priority <= data.priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1957 | priority = data.priority |
| 1958 | pricing_rule_list.push(data) |
| 1959 | } |
| 1960 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1961 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 1962 | var count = 0 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1963 | $.each(priority_list, function (index, value) { |
| 1964 | if (value == priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1965 | count++ |
| 1966 | } |
| 1967 | }) |
| 1968 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1969 | if (priority == 0 || count > 1) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1970 | frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", { |
| 1971 | 'pricing_rule': pricing_rule_name |
| 1972 | }))) |
| 1973 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1974 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1975 | return pricing_rule_list |
| 1976 | } |
| 1977 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1978 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1979 | validate_warehouse: function () { |
| 1980 | if (this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']) { |
Neil Trini Lasrado | 5ddd7ac | 2016-10-24 15:25:33 +0530 | [diff] [blame] | 1981 | frappe.throw(__("Default warehouse is required for selected item")) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1982 | } |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1983 | }, |
| 1984 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1985 | get_actual_qty: function (item) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1986 | this.actual_qty = 0.0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1987 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1988 | var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1989 | if (warehouse && this.bin_data[item.item_code]) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1990 | this.actual_qty = this.bin_data[item.item_code][warehouse] || 0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1991 | this.actual_qty_dict[item.item_code] = this.actual_qty |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | return this.actual_qty |
Rohit Waghchaure | 1312fe3 | 2017-03-07 14:01:04 +0530 | [diff] [blame] | 1995 | }, |
| 1996 | |
| 1997 | update_customer_in_localstorage: function() { |
| 1998 | var me = this; |
| 1999 | try { |
| 2000 | localStorage.setItem('customer_details', JSON.stringify(this.customer_details)); |
| 2001 | } catch (e) { |
| 2002 | frappe.throw(__("LocalStorage is full , did not save")) |
| 2003 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 2004 | } |
Faris Ansari | 185762a | 2017-04-13 12:36:08 +0530 | [diff] [blame] | 2005 | }) |