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