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 | |
Rushabh Mehta | 06fa46f | 2015-01-29 18:09:11 +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 | |
| 11 | wrapper.pos = new erpnext.pos.PointOfSale(wrapper) |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 12 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 13 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 14 | frappe.pages['pos'].refresh = function(wrapper) { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 15 | window.onbeforeunload = function () { |
| 16 | return wrapper.pos.beforeunload() |
| 17 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 21 | erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 22 | init: function(wrapper){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 23 | this.page = wrapper.page; |
| 24 | this.wrapper = $(wrapper).find('.page-content'); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 25 | this.set_indicator(); |
| 26 | this.onload(); |
| 27 | this.make_menu_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 28 | this.si_docs = this.get_doc_from_localstorage(); |
| 29 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 30 | |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 31 | beforeunload: function(e){ |
| 32 | if(this.connection_status == false && frappe.get_route()[0] == "pos"){ |
| 33 | e = e || window.event; |
| 34 | |
| 35 | // For IE and Firefox prior to version 4 |
| 36 | if (e) { |
| 37 | e.returnValue = __("You are in offline mode. You will not be able to reload until you have network."); |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | // For Safari |
| 42 | return __("You are in offline mode. You will not be able to reload until you have network."); |
| 43 | } |
| 44 | }, |
| 45 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 46 | check_internet_connection: function(){ |
| 47 | var me = this; |
| 48 | //Check Internet connection after every 30 seconds |
| 49 | setInterval(function(){ |
| 50 | me.set_indicator(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 51 | }, 5000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 52 | }, |
| 53 | |
| 54 | set_indicator: function(){ |
| 55 | var me = this; |
| 56 | // navigator.onLine |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 57 | this.connection_status = false; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 58 | this.page.set_indicator(__("Offline"), "grey") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 59 | frappe.call({ |
| 60 | method:"frappe.handler.ping", |
| 61 | callback: function(r){ |
| 62 | if(r.message){ |
| 63 | me.connection_status = true; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 64 | me.page.set_indicator(__("Online"), "green") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | }) |
| 68 | }, |
| 69 | |
| 70 | onload: function(){ |
| 71 | var me = this; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 72 | this.get_data_from_server(function(){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 73 | me.create_new(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 74 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 75 | }, |
| 76 | |
| 77 | make_menu_list: function(){ |
| 78 | var me = this; |
| 79 | |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 80 | this.page.add_menu_item(__("New Sales Invoice"), function() { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 81 | me.save_previous_entry(); |
| 82 | me.create_new(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 83 | }) |
| 84 | |
| 85 | this.page.add_menu_item(__("View Offline Records"), function(){ |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 86 | me.show_unsync_invoice_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 87 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 88 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 89 | this.page.add_menu_item(__("Sync Master Data"), function(){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 90 | me.get_data_from_server(function(){ |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 91 | me.load_data(false); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 92 | me.make_customer(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 93 | me.make_item_list(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 94 | me.set_missing_values(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 95 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 96 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 97 | |
Rohit Waghchaure | 82be020 | 2016-10-04 12:46:37 +0530 | [diff] [blame] | 98 | this.page.add_menu_item(__("Sync Offline Invoices"), function(){ |
| 99 | me.sync_sales_invoice() |
| 100 | }); |
| 101 | |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 102 | this.page.add_menu_item(__("POS Profile"), function() { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 103 | frappe.set_route('List', 'POS Profile'); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 104 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 105 | }, |
| 106 | |
| 107 | show_unsync_invoice_list: function(){ |
| 108 | var me = this; |
| 109 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 110 | this.list_dialog = new frappe.ui.Dialog({ |
| 111 | title: 'Invoice List' |
| 112 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 113 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 114 | this.list_dialog.show(); |
| 115 | this.list_body = this.list_dialog.body; |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 116 | if(me.pos_profile_data["allow_delete"]) { |
| 117 | this.list_dialog.set_primary_action(__("Delete"), function() { |
| 118 | frappe.confirm(__("Delete permanently?"), function () { |
| 119 | me.delete_records(); |
| 120 | }) |
| 121 | }).addClass("btn-danger"); |
| 122 | this.toggle_primary_action(); |
| 123 | } |
| 124 | |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 125 | if(this.si_docs.length > 0){ |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 126 | me.render_offline_data(); |
| 127 | me.dialog_actions() |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 128 | }else{ |
| 129 | $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")})) |
| 130 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 131 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 132 | |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 133 | render_offline_data: function() { |
| 134 | var me = this; |
| 135 | |
| 136 | this.removed_items = []; |
| 137 | $(this.list_body).empty(); |
| 138 | |
| 139 | $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\ |
| 140 | <div class="col-xs-1"><input class="list-select-all" type="checkbox"></div>\ |
| 141 | <div class="col-xs-3">Customer</div>\ |
| 142 | <div class="col-xs-2 text-left">Status</div>\ |
| 143 | <div class="col-xs-3 text-right">Paid Amount</div>\ |
| 144 | <div class="col-xs-3 text-right">Grand Total</div>\ |
| 145 | </div>') |
| 146 | |
| 147 | $.each(this.si_docs, function(index, data){ |
| 148 | for(key in data) { |
| 149 | $(frappe.render_template("pos_invoice_list", { |
| 150 | sr: index + 1, |
| 151 | name: key, |
| 152 | customer: data[key].customer, |
| 153 | paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency), |
| 154 | grand_total: format_currency(data[key].grand_total, me.frm.doc.currency), |
| 155 | data: me.get_doctype_status(data[key]) |
| 156 | })).appendTo($(me.list_body)); |
| 157 | } |
| 158 | }) |
| 159 | }, |
| 160 | |
| 161 | dialog_actions: function() { |
| 162 | var me = this; |
| 163 | |
| 164 | $(this.list_body).find('.list-column').click(function() { |
| 165 | me.name = $(this).parents().attr('invoice-name') |
| 166 | me.edit_record(); |
| 167 | }) |
| 168 | |
| 169 | $(this.list_body).find('.list-select-all').click(function() { |
| 170 | me.removed_items = []; |
| 171 | $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked")) |
| 172 | if($(this).is(":checked")) { |
| 173 | $.each(me.si_docs, function(index, data){ |
| 174 | for(key in data) { |
| 175 | me.removed_items.push(key) |
| 176 | } |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | me.toggle_primary_action(); |
| 181 | }) |
| 182 | |
| 183 | $(this.list_body).find('.list-delete').click(function() { |
| 184 | me.name = $(this).parent().parent().attr('invoice-name'); |
| 185 | if($(this).is(":checked")) { |
| 186 | me.removed_items.push(me.name); |
| 187 | } else { |
| 188 | me.removed_items.pop(me.name) |
| 189 | } |
| 190 | |
| 191 | me.toggle_primary_action(); |
| 192 | }) |
| 193 | }, |
| 194 | |
| 195 | edit_record: function() { |
| 196 | var me = this; |
| 197 | |
| 198 | doc_data = this.get_invoice_doc(this.si_docs); |
| 199 | if(doc_data){ |
| 200 | this.frm.doc = doc_data[0][this.name]; |
| 201 | this.set_missing_values(); |
| 202 | this.refresh(false); |
| 203 | this.disable_input_field(); |
| 204 | this.list_dialog.hide(); |
| 205 | } |
| 206 | }, |
| 207 | |
| 208 | delete_records: function() { |
| 209 | var me = this; |
| 210 | this.remove_doc_from_localstorage() |
| 211 | this.update_localstorage(); |
| 212 | this.render_offline_data(); |
| 213 | this.dialog_actions(); |
| 214 | this.toggle_primary_action(); |
| 215 | }, |
| 216 | |
| 217 | toggle_primary_action: function() { |
| 218 | var me = this; |
| 219 | if(this.removed_items && this.removed_items.length > 0) { |
| 220 | $(this.list_dialog.wrapper).find('.btn-danger').show(); |
| 221 | } else { |
| 222 | $(this.list_dialog.wrapper).find('.btn-danger').hide(); |
| 223 | } |
| 224 | }, |
| 225 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 226 | get_doctype_status: function(doc){ |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 227 | if(doc.docstatus == 0) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 228 | return {status: "Draft", indicator: "red"} |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 229 | }else if(doc.outstanding_amount == 0) { |
| 230 | return {status: "Paid", indicator: "green"} |
| 231 | }else { |
| 232 | return {status: "Submitted", indicator: "blue"} |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 233 | } |
| 234 | }, |
| 235 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 236 | set_missing_values: function(){ |
| 237 | var me = this; |
| 238 | doc = JSON.parse(localStorage.getItem('doc')) |
| 239 | if(this.frm.doc.payments.length == 0){ |
| 240 | this.frm.doc.payments = doc.payments; |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 241 | this.calculate_outstanding_amount(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 242 | } |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 243 | |
| 244 | if(this.frm.doc.customer){ |
| 245 | this.party_field.$input.val(this.frm.doc.customer); |
| 246 | } |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 247 | |
| 248 | if(!this.frm.doc.write_off_account){ |
| 249 | this.frm.doc.write_off_account = doc.write_off_account |
| 250 | } |
| 251 | |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 252 | if(!this.frm.doc.account_for_change_amount){ |
| 253 | this.frm.doc.account_for_change_amount = doc.account_for_change_amount |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 254 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 255 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 256 | |
| 257 | get_invoice_doc: function(si_docs){ |
| 258 | var me = this; |
| 259 | this.si_docs = this.get_doc_from_localstorage(); |
| 260 | |
| 261 | return $.grep(this.si_docs, function(data){ |
| 262 | for(key in data){ |
| 263 | return key == me.name |
| 264 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 265 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 266 | }, |
| 267 | |
| 268 | get_data_from_server: function(callback){ |
| 269 | var me = this; |
| 270 | frappe.call({ |
| 271 | method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data", |
| 272 | freeze: true, |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 273 | freeze_message: __("Master data syncing, it might take some time"), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 274 | callback: function(r){ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 275 | me.init_master_data(r) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 276 | localStorage.setItem('doc', JSON.stringify(r.message.doc)); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 277 | me.set_interval_for_si_sync(); |
| 278 | me.check_internet_connection(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 279 | if(callback){ |
| 280 | callback(); |
| 281 | } |
| 282 | } |
| 283 | }) |
| 284 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 285 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 286 | init_master_data: function(r){ |
| 287 | var me = this; |
| 288 | this.meta = r.message.meta; |
| 289 | this.item_data = r.message.items; |
| 290 | this.customers = r.message.customers; |
| 291 | this.serial_no_data = r.message.serial_no_data; |
| 292 | this.batch_no_data = r.message.batch_no_data; |
| 293 | this.tax_data = r.message.tax_data; |
| 294 | this.price_list_data = r.message.price_list_data; |
| 295 | this.bin_data = r.message.bin_data; |
| 296 | this.pricing_rules = r.message.pricing_rules; |
| 297 | this.print_template = r.message.print_template; |
| 298 | this.pos_profile_data = r.message.pos_profile; |
| 299 | this.default_customer = r.message.default_customer || null; |
rohitwaghchaure | 34e820d | 2016-12-20 16:54:24 +0530 | [diff] [blame] | 300 | this.print_settings = locals[":Print Settings"]["Print Settings"]; |
Rohit Waghchaure | d567e57 | 2016-12-21 13:18:36 +0530 | [diff] [blame] | 301 | 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] | 302 | }, |
| 303 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 304 | save_previous_entry : function(){ |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 305 | if(this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 306 | this.create_invoice() |
| 307 | } |
| 308 | }, |
| 309 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 310 | create_new: function(){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 311 | var me = this; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 312 | this.frm = {} |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 313 | this.name = ''; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 314 | this.load_data(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 315 | this.setup(); |
| 316 | }, |
| 317 | |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 318 | load_data: function(load_doc){ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 319 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 320 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 321 | this.items = this.item_data; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 322 | this.actual_qty_dict = {}; |
| 323 | |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 324 | if(load_doc) { |
| 325 | this.frm.doc = JSON.parse(localStorage.getItem('doc')); |
| 326 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 327 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 328 | $.each(this.meta, function(i, data){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 329 | frappe.meta.sync(data) |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 330 | locals["DocType"][data.name] = data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 331 | }) |
| 332 | |
rohitwaghchaure | 34e820d | 2016-12-20 16:54:24 +0530 | [diff] [blame] | 333 | this.print_template_data = frappe.render_template("print_template", {content: this.print_template, |
| 334 | title:"POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css, |
| 335 | print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer}) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 336 | }, |
| 337 | |
| 338 | setup: function(){ |
| 339 | this.wrapper.html(frappe.render_template("pos", this.frm.doc)); |
| 340 | this.set_transaction_defaults("Customer"); |
| 341 | this.make(); |
| 342 | this.set_primary_action(); |
| 343 | }, |
| 344 | |
| 345 | set_transaction_defaults: function(party) { |
| 346 | var me = this; |
| 347 | this.party = party; |
| 348 | this.price_list = (party == "Customer" ? |
| 349 | this.frm.doc.selling_price_list : this.frm.doc.buying_price_list); |
| 350 | this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list"); |
| 351 | this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase"); |
| 352 | }, |
| 353 | |
| 354 | make: function() { |
| 355 | this.make_search(); |
| 356 | this.make_customer(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 357 | this.make_item_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 358 | this.make_discount_field() |
| 359 | }, |
| 360 | |
| 361 | make_search: function() { |
| 362 | var me = this; |
| 363 | this.search = frappe.ui.form.make_control({ |
| 364 | df: { |
| 365 | "fieldtype": "Data", |
| 366 | "label": "Item", |
| 367 | "fieldname": "pos_item", |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 368 | "placeholder": __("Search Item") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 369 | }, |
| 370 | parent: this.wrapper.find(".search-area"), |
| 371 | only_input: true, |
| 372 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 373 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 374 | this.search.make_input(); |
| 375 | this.search.$input.on("keyup", function() { |
| 376 | setTimeout(function() { |
| 377 | me.items = me.get_items(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 378 | me.make_item_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 379 | }, 1000); |
| 380 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 381 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 382 | this.party_field = frappe.ui.form.make_control({ |
| 383 | df: { |
| 384 | "fieldtype": "Data", |
| 385 | "options": this.party, |
| 386 | "label": this.party, |
| 387 | "fieldname": this.party.toLowerCase(), |
Rushabh Mehta | e0686b3 | 2016-06-10 12:34:41 +0530 | [diff] [blame] | 388 | "placeholder": __("Select or add new customer") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 389 | }, |
| 390 | parent: this.wrapper.find(".party-area"), |
| 391 | only_input: true, |
| 392 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 393 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 394 | this.party_field.make_input(); |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 395 | this.set_focus() |
| 396 | }, |
| 397 | |
| 398 | set_focus: function(){ |
| 399 | if(this.default_customer){ |
| 400 | this.search.$input.focus(); |
| 401 | }else{ |
| 402 | this.party_field.$input.focus(); |
| 403 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 404 | }, |
| 405 | |
| 406 | make_customer: function() { |
| 407 | var me = this; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 408 | |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 409 | if(this.default_customer && !this.frm.doc.customer){ |
Rohit Waghchaure | 539e913 | 2016-07-07 16:44:40 +0530 | [diff] [blame] | 410 | this.party_field.$input.val(this.default_customer); |
| 411 | this.frm.doc.customer = this.default_customer; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 412 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 413 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 414 | this.party_field.awesomeplete = |
| 415 | new Awesomplete(this.party_field.$input.get(0), { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 416 | minChars: 0, |
| 417 | maxItems: 99, |
| 418 | autoFirst: true, |
| 419 | list: [], |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 420 | filter: function(item, input) { |
| 421 | var value = item.value.toLowerCase(); |
| 422 | if(value.indexOf('is_action') !== -1 || |
| 423 | value.indexOf(input) !== -1) { |
| 424 | return true; |
| 425 | } |
| 426 | }, |
| 427 | item: function(item, input) { |
| 428 | var d = item; |
| 429 | var html = "<span>" + __(d.label || d.value) + "</span>"; |
| 430 | return $('<li></li>') |
| 431 | .data('item.autocomplete', d) |
| 432 | .html('<a><p>' + html + '</p></a>') |
| 433 | .get(0); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 434 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 435 | }); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 436 | var items = this.customers.map(function(c) { |
| 437 | return { |
| 438 | label: c.name, |
| 439 | value: c.name, |
| 440 | customer_group: c.customer_group, |
| 441 | territory: c.territory |
| 442 | } |
| 443 | }); |
| 444 | items.push({ |
| 445 | label: "<span class='text-primary link-option'>" |
| 446 | + "<i class='fa fa-plus' style='margin-right: 5px;'></i> " |
| 447 | + __("Create a new Customer") |
| 448 | + "</span>", |
| 449 | value: 'is_action', |
| 450 | action: me.new_customer |
| 451 | }); |
| 452 | this.party_field.awesomeplete.list = items; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 453 | |
| 454 | this.party_field.$input |
| 455 | .on('input', function(e) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 456 | me.party_field.awesomeplete.list = items; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 457 | }) |
| 458 | .on('awesomplete-select', function(e) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 459 | var item = me.party_field.awesomeplete |
| 460 | .get_item(e.originalEvent.text.value); |
| 461 | if(!item) return; |
| 462 | if(item.action) { |
| 463 | item.action.apply(me); |
| 464 | return; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 465 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 466 | me.update_customer_data(item); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 467 | me.refresh(); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 468 | }) |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 469 | .on('change', function(e) { |
| 470 | if(!e.originalEvent.text) { |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 471 | me.frm.doc.customer = $(this).val(); |
| 472 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 473 | }) |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 474 | .on('focus', function(e) { |
| 475 | $(e.target).val('').trigger('input'); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 476 | }) |
| 477 | .on("awesomplete-selectcomplete", function(e) { |
| 478 | var item = me.party_field.awesomeplete |
| 479 | .get_item(e.originalEvent.text.value); |
| 480 | // clear text input if item is action |
| 481 | if(item.action) { |
| 482 | $(this).val(""); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 483 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 484 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 485 | }, |
| 486 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 487 | new_customer: function () { |
| 488 | var me = this; |
| 489 | if(!this.connection_status) return; |
| 490 | frappe.ui.form.quick_entry('Customer', function (doc) { |
| 491 | me.customers.push(doc); |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 492 | me.party_field.$input.val(doc.name); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 493 | me.update_customer_data(doc); |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 494 | }) |
| 495 | }, |
| 496 | |
| 497 | update_customer_data: function(doc) { |
| 498 | var me = this; |
| 499 | this.frm.doc.customer = doc.label || doc.name; |
| 500 | this.frm.doc.customer_name = doc.customer_name; |
| 501 | this.frm.doc.customer_group = doc.customer_group; |
| 502 | this.frm.doc.territory = doc.territory; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 503 | }, |
| 504 | |
| 505 | get_customers: function(key){ |
| 506 | var me = this; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 507 | key = key.toLowerCase().trim() |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 508 | var re = new RegExp('%', 'g'); |
| 509 | var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*')) |
| 510 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 511 | if(key){ |
| 512 | return $.grep(this.customers, function(data) { |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 513 | if(reg.test(data.name.toLowerCase()) |
| 514 | || reg.test(data.customer_name.toLowerCase()) |
| 515 | || (data.customer_group && reg.test(data.customer_group.toLowerCase()))){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 516 | return data |
| 517 | } |
| 518 | }) |
| 519 | }else{ |
| 520 | customers = this.customers.sort(function(a,b){ return a.idx < b.idx }) |
| 521 | return customers.slice(0, 20) |
| 522 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 523 | }, |
| 524 | |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 525 | make_item_list: function() { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 526 | var me = this; |
| 527 | if(!this.price_list) { |
| 528 | msgprint(__("Price List not found or disabled")); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | me.item_timeout = null; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 533 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 534 | var $wrap = me.wrapper.find(".item-list"); |
| 535 | me.wrapper.find(".item-list").empty(); |
| 536 | |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 537 | if (this.items.length > 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 538 | $.each(this.items, function(index, obj) { |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 539 | if(index < 30){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 540 | $(frappe.render_template("pos_item", { |
| 541 | item_code: obj.name, |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 542 | item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 543 | item_name: obj.name===obj.item_name ? "" : obj.item_name, |
| 544 | item_image: obj.image ? "url('" + obj.image + "')" : null, |
| 545 | color: frappe.get_palette(obj.item_name), |
| 546 | abbr: frappe.get_abbr(obj.item_name) |
| 547 | })).tooltip().appendTo($wrap); |
| 548 | } |
| 549 | }); |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 550 | } else { |
| 551 | $("<h4>Searching record not found.</h4>").appendTo($wrap) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 552 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 553 | |
Rohit Waghchaure | 28c02fe | 2016-06-10 00:48:03 +0530 | [diff] [blame] | 554 | if(this.items.length == 1 |
| 555 | && this.search.$input.val()) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 556 | this.search.$input.val(""); |
| 557 | this.add_to_cart(); |
| 558 | } |
| 559 | |
| 560 | // if form is local then allow this function |
| 561 | $(me.wrapper).find("div.pos-item").on("click", function() { |
| 562 | me.customer_validate(); |
| 563 | if(me.frm.doc.docstatus==0) { |
| 564 | me.items = me.get_items($(this).attr("data-item-code")) |
| 565 | me.add_to_cart(); |
| 566 | } |
| 567 | }); |
| 568 | }, |
| 569 | |
| 570 | get_items: function(item_code){ |
| 571 | // To search item as per the key enter |
| 572 | |
| 573 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 574 | this.item_serial_no = {}; |
| 575 | this.item_batch_no = {}; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 576 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 577 | if(item_code){ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 578 | return $.grep(this.item_data, function(item){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 579 | if(item.item_code == item_code ){ |
| 580 | return true |
| 581 | } |
| 582 | }) |
| 583 | } |
| 584 | |
Rohit Waghchaure | 9a36083 | 2016-11-24 20:00:23 +0530 | [diff] [blame] | 585 | key = this.search.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g,'\\$&'); |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 586 | var re = new RegExp('%', 'g'); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 587 | 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] | 588 | search_status = true |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 589 | |
| 590 | if(key){ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 591 | return $.grep(this.item_data, function(item){ |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 592 | if(search_status){ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 593 | if(in_list(me.batch_no_data[item.item_code], me.search.$input.val())){ |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 594 | search_status = false; |
| 595 | return me.item_batch_no[item.item_code] = me.search.$input.val() |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 596 | } else if( me.serial_no_data[item.item_code] |
| 597 | && in_list(Object.keys(me.serial_no_data[item.item_code]), me.search.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 598 | search_status = false; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 599 | me.item_serial_no[item.item_code] = [me.search.$input.val(), me.serial_no_data[item.item_code][me.search.$input.val()]] |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 600 | return true |
| 601 | } else if(item.barcode == me.search.$input.val()) { |
| 602 | search_status = false; |
| 603 | return item.barcode == me.search.$input.val(); |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 604 | } else if(reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) || |
| 605 | reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase()) ){ |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 606 | return true |
| 607 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 608 | } |
| 609 | }) |
| 610 | }else{ |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 611 | return this.item_data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 612 | } |
| 613 | }, |
| 614 | |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 615 | bind_qty_event: function() { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 616 | var me = this; |
| 617 | |
| 618 | $(this.wrapper).find(".pos-item-qty").on("change", function(){ |
| 619 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 620 | var qty = $(this).val(); |
| 621 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 622 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 623 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 624 | $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){ |
| 625 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 626 | 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] | 627 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 628 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 629 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 630 | $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){ |
| 631 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 632 | 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] | 633 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 634 | }) |
| 635 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 636 | |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 637 | update_qty: function(item_code, qty) { |
| 638 | var me = this; |
| 639 | this.items = this.get_items(item_code); |
| 640 | this.validate_serial_no() |
| 641 | this.update_qty_rate_against_item_code(item_code, "qty", qty); |
| 642 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 643 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 644 | update_rate: function() { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 645 | var me = this; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 646 | |
| 647 | $(this.wrapper).find(".pos-item-rate").on("change", function(){ |
| 648 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 649 | me.update_qty_rate_against_item_code(item_code, "rate", $(this).val()); |
| 650 | }) |
| 651 | }, |
| 652 | |
| 653 | update_qty_rate_against_item_code: function(item_code, field, value){ |
| 654 | var me = this; |
| 655 | if(value < 0){ |
| 656 | frappe.throw(__("Enter value must be positive")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 657 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 658 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 659 | this.remove_item = [] |
| 660 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Rohit Waghchaure | 8c9c9cf | 2016-12-27 12:31:35 +0530 | [diff] [blame] | 661 | if(d.serial_no && field == 'qty'){ |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 662 | me.validate_serial_no_qty(d, item_code, field, value) |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 663 | } |
| 664 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 665 | if (d.item_code == item_code) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 666 | d[field] = flt(value); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 667 | d.amount = flt(d.rate) * flt(d.qty); |
| 668 | if(d.qty==0){ |
| 669 | me.remove_item.push(d.idx) |
| 670 | } |
| 671 | } |
| 672 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 673 | |
| 674 | if(field == 'qty'){ |
| 675 | this.remove_zero_qty_item(); |
| 676 | } |
| 677 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 678 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 679 | }, |
| 680 | |
| 681 | remove_zero_qty_item: function(){ |
| 682 | var me = this; |
| 683 | idx = 0 |
| 684 | this.items = [] |
| 685 | idx = 0 |
| 686 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 687 | if(!in_list(me.remove_item, d.idx)){ |
| 688 | d.idx = idx; |
| 689 | me.items.push(d); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 690 | idx++; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 691 | } |
| 692 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 693 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 694 | this.frm.doc["items"] = this.items; |
| 695 | }, |
| 696 | |
| 697 | make_discount_field: function(){ |
| 698 | var me = this; |
| 699 | |
| 700 | this.wrapper.find('input.discount-percentage').on("change", function() { |
| 701 | me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage")); |
| 702 | total = me.frm.doc.grand_total |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 703 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 704 | if(me.frm.doc.apply_discount_on == 'Net Total'){ |
| 705 | total = me.frm.doc.net_total |
| 706 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 707 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 708 | me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); |
| 709 | me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount) |
| 710 | me.refresh(); |
| 711 | }); |
| 712 | |
| 713 | this.wrapper.find('input.discount-amount').on("change", function() { |
| 714 | me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount")); |
| 715 | me.frm.doc.additional_discount_percentage = 0.0; |
| 716 | me.wrapper.find('input.discount-percentage').val(0); |
| 717 | me.refresh(); |
| 718 | }); |
| 719 | }, |
| 720 | |
| 721 | customer_validate: function(){ |
| 722 | var me = this; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 723 | if(!this.frm.doc.customer){ |
| 724 | frappe.throw(__("Please select customer")) |
| 725 | } |
| 726 | }, |
| 727 | |
| 728 | add_to_cart: function() { |
| 729 | var me = this; |
| 730 | var caught = false; |
| 731 | var no_of_items = me.wrapper.find(".pos-bill-item").length; |
| 732 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 733 | this.customer_validate(); |
| 734 | this.mandatory_batch_no(); |
| 735 | this.validate_serial_no(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 736 | this.validate_warehouse(); |
| 737 | |
| 738 | if (no_of_items != 0) { |
| 739 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 740 | if (d.item_code == me.items[0].item_code) { |
| 741 | caught = true; |
| 742 | d.qty += 1; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 743 | d.amount = flt(d.rate) * flt(d.qty); |
| 744 | if(me.item_serial_no[d.item_code]){ |
| 745 | d.serial_no += '\n' + me.item_serial_no[d.item_code][0] |
| 746 | d.warehouse = me.item_serial_no[d.item_code][1] |
| 747 | } |
| 748 | |
| 749 | if(me.item_batch_no.length){ |
| 750 | d.batch_no = me.item_batch_no[d.item_code] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | }); |
| 754 | } |
| 755 | |
| 756 | // if item not found then add new item |
| 757 | if (!caught) |
| 758 | this.add_new_item_to_grid(); |
| 759 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 760 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 761 | }, |
| 762 | |
| 763 | add_new_item_to_grid: function() { |
| 764 | var me = this; |
| 765 | this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items"); |
| 766 | this.child.item_code = this.items[0].item_code; |
| 767 | this.child.item_name = this.items[0].item_name; |
| 768 | this.child.stock_uom = this.items[0].stock_uom; |
| 769 | this.child.description = this.items[0].description; |
| 770 | this.child.qty = 1; |
| 771 | this.child.item_group = this.items[0].item_group; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 772 | this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center; |
| 773 | 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] | 774 | this.child.warehouse = (this.item_serial_no[this.child.item_code] |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 775 | ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse) ); |
| 776 | this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 777 | this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 778 | this.child.actual_qty = me.get_actual_qty(this.items[0]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 779 | this.child.amount = flt(this.child.qty) * flt(this.child.rate); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 780 | this.child.batch_no = this.item_batch_no[this.child.item_code]; |
| 781 | this.child.serial_no = (this.item_serial_no[this.child.item_code] |
| 782 | ? this.item_serial_no[this.child.item_code][0] : ''); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 783 | 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] | 784 | }, |
| 785 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 786 | update_paid_amount_status: function(update_paid_amount){ |
| 787 | if(this.name){ |
| 788 | update_paid_amount = update_paid_amount ? false : true; |
| 789 | } |
| 790 | |
| 791 | this.refresh(update_paid_amount); |
| 792 | }, |
| 793 | |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 794 | refresh: function(update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 795 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 796 | this.refresh_fields(update_paid_amount); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 797 | this.bind_qty_event(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 798 | this.update_rate(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 799 | this.set_primary_action(); |
| 800 | }, |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 801 | |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 802 | refresh_fields: function(update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 803 | this.apply_pricing_rule(); |
| 804 | this.discount_amount_applied = false; |
| 805 | this._calculate_taxes_and_totals(); |
| 806 | this.calculate_discount_amount(); |
| 807 | this.show_items_in_item_cart(); |
| 808 | this.set_taxes(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 809 | this.calculate_outstanding_amount(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 810 | this.set_totals(); |
| 811 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 812 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 813 | get_company_currency: function() { |
| 814 | return erpnext.get_currency(this.frm.doc.company); |
| 815 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 816 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 817 | show_item_wise_taxes: function(){ |
| 818 | return null; |
| 819 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 820 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 821 | show_items_in_item_cart: function() { |
| 822 | var me = this; |
| 823 | var $items = this.wrapper.find(".items").empty(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 824 | $.each(this.frm.doc.items|| [], function(i, d) { |
| 825 | $(frappe.render_template("pos_bill_item", { |
| 826 | item_code: d.item_code, |
| 827 | item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name), |
| 828 | qty: d.qty, |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 829 | actual_qty: me.actual_qty_dict[d.item_code] || 0, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 830 | projected_qty: d.projected_qty, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 831 | rate: format_number(d.rate, me.frm.doc.currency), |
Rohit Waghchaure | bef185c | 2016-12-28 15:16:49 +0530 | [diff] [blame] | 832 | enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true: false, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 833 | amount: format_currency(d.amount, me.frm.doc.currency) |
| 834 | })).appendTo($items); |
| 835 | }); |
| 836 | |
| 837 | this.wrapper.find("input.pos-item-qty").on("focus", function() { |
| 838 | $(this).select(); |
| 839 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 840 | |
| 841 | this.wrapper.find("input.pos-item-rate").on("focus", function() { |
| 842 | $(this).select(); |
| 843 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 844 | }, |
| 845 | |
| 846 | set_taxes: function(){ |
| 847 | var me = this; |
| 848 | me.frm.doc.total_taxes_and_charges = 0.0 |
| 849 | |
| 850 | var taxes = this.frm.doc.taxes || []; |
| 851 | $(this.wrapper) |
| 852 | .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true) |
| 853 | .find(".tax-table").empty(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 854 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 855 | $.each(taxes, function(i, d) { |
| 856 | if (d.tax_amount && cint(d.included_in_print_rate) == 0) { |
| 857 | $(frappe.render_template("pos_tax_row", { |
| 858 | description: d.description, |
| 859 | tax_amount: format_currency(flt(d.tax_amount_after_discount_amount), |
| 860 | me.frm.doc.currency) |
| 861 | })).appendTo(me.wrapper.find(".tax-table")); |
| 862 | } |
| 863 | }); |
| 864 | }, |
| 865 | |
| 866 | set_totals: function() { |
| 867 | var me = this; |
| 868 | this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency)); |
| 869 | this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency)); |
| 870 | }, |
| 871 | |
| 872 | set_primary_action: function() { |
| 873 | var me = this; |
| 874 | |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 875 | if (this.frm.doc.docstatus==0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 876 | this.page.set_primary_action(__("Pay"), function() { |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 877 | me.validate(); |
| 878 | me.update_paid_amount_status(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 879 | me.create_invoice(); |
| 880 | me.make_payment(); |
Rushabh Mehta | 323c06e | 2016-12-05 14:17:26 +0530 | [diff] [blame] | 881 | }, "octicon octfa fa-credit-card"); |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 882 | }else if(this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 883 | this.page.set_primary_action(__("Print"), function() { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 884 | html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 885 | me.print_document(html) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 886 | }) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 887 | }else { |
| 888 | this.page.clear_primary_action() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 889 | } |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 890 | |
| 891 | this.page.set_secondary_action(__("New"), function() { |
| 892 | me.save_previous_entry(); |
| 893 | me.create_new(); |
Rushabh Mehta | 323c06e | 2016-12-05 14:17:26 +0530 | [diff] [blame] | 894 | }, "octicon octfa fa-plus").addClass("btn-primary"); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 895 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 896 | |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 897 | print_dialog: function(){ |
| 898 | var me = this; |
| 899 | |
| 900 | msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \ |
| 901 | style="margin-right: 5px;">{0}</a>\ |
| 902 | <a class="btn btn-default new_doc">{1}</a>', [ |
| 903 | __('Print'), __('New') |
| 904 | ])); |
| 905 | |
| 906 | $('.print_doc').click(function(){ |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 907 | html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 908 | me.print_document(html) |
| 909 | }) |
| 910 | |
| 911 | $('.new_doc').click(function(){ |
| 912 | msgprint.hide() |
| 913 | me.create_new(); |
| 914 | }) |
| 915 | }, |
| 916 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 917 | print_document: function(html){ |
| 918 | var w = window.open(); |
| 919 | w.document.write(html); |
| 920 | w.document.close(); |
| 921 | setTimeout(function(){ |
| 922 | w.print(); |
| 923 | w.close(); |
| 924 | }, 1000) |
| 925 | }, |
| 926 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 927 | submit_invoice: function(){ |
| 928 | var me = this; |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 929 | this.change_status(); |
| 930 | if(this.frm.doc.docstatus == 1){ |
| 931 | this.print_dialog() |
| 932 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 933 | }, |
| 934 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 935 | change_status: function(){ |
| 936 | if(this.frm.doc.docstatus == 0){ |
| 937 | this.frm.doc.docstatus = 1; |
| 938 | this.update_invoice(); |
| 939 | this.disable_input_field(); |
| 940 | } |
| 941 | }, |
| 942 | |
| 943 | disable_input_field: function(){ |
| 944 | var pointer_events = 'inherit' |
| 945 | $(this.wrapper).find('input').attr("disabled", false); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 946 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 947 | if(this.frm.doc.docstatus == 1){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 948 | pointer_events = 'none'; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 949 | $(this.wrapper).find('input').attr("disabled", true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 950 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 951 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 952 | $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events); |
| 953 | $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events); |
| 954 | this.set_primary_action(); |
| 955 | }, |
| 956 | |
| 957 | create_invoice: function(){ |
| 958 | var me = this; |
| 959 | var invoice_data = {} |
| 960 | this.si_docs = this.get_doc_from_localstorage(); |
| 961 | if(this.name){ |
| 962 | this.update_invoice() |
| 963 | }else{ |
| 964 | this.name = $.now(); |
Rohit Waghchaure | 377c7ac | 2016-09-05 17:56:44 +0530 | [diff] [blame] | 965 | this.frm.doc.offline_pos_name = this.name; |
Rohit Waghchaure | 9cd356c | 2016-08-11 16:54:30 +0530 | [diff] [blame] | 966 | this.frm.doc.posting_date = frappe.datetime.get_today(); |
| 967 | this.frm.doc.posting_time = frappe.datetime.now_time(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 968 | invoice_data[this.name] = this.frm.doc |
| 969 | this.si_docs.push(invoice_data) |
| 970 | this.update_localstorage(); |
| 971 | this.set_primary_action(); |
| 972 | } |
| 973 | }, |
| 974 | |
| 975 | update_invoice: function(){ |
| 976 | var me = this; |
| 977 | this.si_docs = this.get_doc_from_localstorage(); |
| 978 | $.each(this.si_docs, function(index, data){ |
| 979 | for(key in data){ |
| 980 | if(key == me.name){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 981 | me.si_docs[index][key] = me.frm.doc; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 982 | me.update_localstorage(); |
| 983 | } |
| 984 | } |
| 985 | }) |
| 986 | }, |
| 987 | |
| 988 | update_localstorage: function(){ |
| 989 | try{ |
| 990 | localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs)); |
| 991 | }catch(e){ |
| 992 | frappe.throw(__("LocalStorage is full , did not save")) |
| 993 | } |
| 994 | }, |
| 995 | |
| 996 | get_doc_from_localstorage: function(){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 997 | try{ |
| 998 | return JSON.parse(localStorage.getItem('sales_invoice_doc')) || []; |
| 999 | }catch(e){ |
| 1000 | return [] |
| 1001 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1002 | }, |
| 1003 | |
| 1004 | set_interval_for_si_sync: function(){ |
| 1005 | var me = this; |
| 1006 | setInterval(function(){ |
| 1007 | me.sync_sales_invoice() |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1008 | }, 60000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1009 | }, |
| 1010 | |
| 1011 | sync_sales_invoice: function(){ |
| 1012 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1013 | this.si_docs = this.get_submitted_invoice(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1014 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1015 | if(this.si_docs.length){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1016 | frappe.call({ |
| 1017 | method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", |
| 1018 | args: { |
| 1019 | doc_list: me.si_docs |
| 1020 | }, |
| 1021 | callback: function(r){ |
| 1022 | if(r.message){ |
| 1023 | me.removed_items = r.message; |
| 1024 | me.remove_doc_from_localstorage(); |
| 1025 | } |
| 1026 | } |
| 1027 | }) |
| 1028 | } |
| 1029 | }, |
| 1030 | |
| 1031 | get_submitted_invoice: function(){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1032 | var invoices = []; |
| 1033 | var index = 1; |
| 1034 | docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1035 | if(docs){ |
| 1036 | invoices = $.map(docs, function(data){ |
| 1037 | for(key in data){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1038 | if(data[key].docstatus == 1 && index < 50){ |
| 1039 | index++ |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 1040 | data[key].docstatus = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1041 | return data |
| 1042 | } |
| 1043 | } |
| 1044 | }); |
| 1045 | } |
| 1046 | |
| 1047 | return invoices |
| 1048 | }, |
| 1049 | |
| 1050 | remove_doc_from_localstorage: function(){ |
| 1051 | var me = this; |
| 1052 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1053 | this.new_si_docs = []; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1054 | if(this.removed_items){ |
| 1055 | $.each(this.si_docs, function(index, data){ |
| 1056 | for(key in data){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1057 | if(!in_list(me.removed_items, key)){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1058 | me.new_si_docs.push(data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | }) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1062 | this.si_docs = this.new_si_docs; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1063 | this.update_localstorage(); |
| 1064 | } |
| 1065 | }, |
| 1066 | |
| 1067 | validate: function(){ |
| 1068 | var me = this; |
| 1069 | this.customer_validate(); |
| 1070 | this.item_validate(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1071 | this.validate_mode_of_payments(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1072 | }, |
| 1073 | |
| 1074 | item_validate: function(){ |
| 1075 | if(this.frm.doc.items.length == 0){ |
| 1076 | frappe.throw(__("Select items to save the invoice")) |
| 1077 | } |
| 1078 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1079 | |
Saurabh | 9a6c666 | 2016-06-27 16:51:44 +0530 | [diff] [blame] | 1080 | validate_mode_of_payments: function(){ |
| 1081 | if (this.frm.doc.payments.length === 0){ |
| 1082 | frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile.")) |
| 1083 | } |
| 1084 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1085 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1086 | validate_serial_no: function(){ |
| 1087 | var me = this; |
| 1088 | var item_code = serial_no = ''; |
| 1089 | for (key in this.item_serial_no){ |
| 1090 | item_code = key; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1091 | serial_no = me.item_serial_no[key][0]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1092 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1093 | |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1094 | if(this.items[0].has_serial_no && serial_no == ""){ |
| 1095 | this.refresh(); |
| 1096 | frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", { |
| 1097 | 'item': this.items[0].item_code |
| 1098 | }))) |
| 1099 | } |
| 1100 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1101 | if(item_code && serial_no){ |
| 1102 | $.each(this.frm.doc.items, function(index, data){ |
| 1103 | if(data.item_code == item_code){ |
| 1104 | if(in_list(data.serial_no.split('\n'), serial_no)){ |
| 1105 | frappe.throw(__(repl("Serial no %(serial_no)s is already taken", { |
| 1106 | 'serial_no': serial_no |
| 1107 | }))) |
| 1108 | } |
| 1109 | } |
| 1110 | }) |
| 1111 | } |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1112 | }, |
| 1113 | |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1114 | validate_serial_no_qty: function(args, item_code, field, value){ |
| 1115 | var me = this; |
| 1116 | if (args.item_code == item_code && args.serial_no |
| 1117 | && field == 'qty' && cint(value) != value) { |
| 1118 | args.qty = 0.0; |
| 1119 | this.refresh(); |
| 1120 | frappe.throw(__("Serial no item cannot be a fraction")) |
| 1121 | } |
| 1122 | |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1123 | 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] | 1124 | args.qty = 0.0; |
| 1125 | args.serial_no = '' |
| 1126 | this.refresh(); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1127 | frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", { |
| 1128 | 'item': item_code |
| 1129 | }))) |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1130 | } |
| 1131 | }, |
| 1132 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1133 | mandatory_batch_no: function(){ |
| 1134 | var me = this; |
| 1135 | if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){ |
| 1136 | frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", { |
| 1137 | 'item': this.items[0].item_code |
| 1138 | }))) |
| 1139 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1140 | }, |
| 1141 | |
| 1142 | apply_pricing_rule: function(){ |
| 1143 | var me = this; |
| 1144 | $.each(this.frm.doc["items"], function(n, item) { |
| 1145 | pricing_rule = me.get_pricing_rule(item) |
| 1146 | me.validate_pricing_rule(pricing_rule) |
| 1147 | if(pricing_rule.length){ |
| 1148 | item.margin_type = pricing_rule[0].margin_type; |
| 1149 | item.price_list_rate = pricing_rule[0].price || item.price_list_rate; |
| 1150 | item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount; |
| 1151 | item.discount_percentage = pricing_rule[0].discount_percentage || 0.0; |
| 1152 | me.apply_pricing_rule_on_item(item) |
Rohit Waghchaure | 619c40b | 2016-11-07 15:29:17 +0530 | [diff] [blame] | 1153 | } else if(item.discount_percentage > 0 || item.margin_rate_or_amount > 0) { |
| 1154 | item.margin_rate_or_amount = 0.0; |
| 1155 | item.discount_percentage = 0.0; |
| 1156 | me.apply_pricing_rule_on_item(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1157 | } |
| 1158 | }) |
| 1159 | }, |
| 1160 | |
| 1161 | get_pricing_rule: function(item){ |
| 1162 | var me = this; |
| 1163 | return $.grep(this.pricing_rules, function(data){ |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1164 | if(item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty)) ){ |
| 1165 | if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) { |
| 1166 | if(in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)){ |
| 1167 | return me.validate_condition(data) |
| 1168 | }else{ |
| 1169 | return true |
| 1170 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | }) |
| 1174 | }, |
| 1175 | |
| 1176 | validate_condition: function(data){ |
| 1177 | //This method check condition based on applicable for |
| 1178 | condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for] |
| 1179 | if(in_list(condition[1], condition[0])){ |
| 1180 | return true |
| 1181 | } |
| 1182 | }, |
| 1183 | |
| 1184 | get_mapper_for_pricing_rule: function(data){ |
| 1185 | return { |
Rohit Waghchaure | 89ee309 | 2016-07-08 15:17:04 +0530 | [diff] [blame] | 1186 | 'Customer': [data.customer, [this.frm.doc.customer]], |
| 1187 | 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']], |
| 1188 | 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']], |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1189 | 'Campaign': [data.campaign, [this.frm.doc.campaign]], |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1190 | } |
| 1191 | }, |
| 1192 | |
| 1193 | validate_pricing_rule: function(pricing_rule){ |
| 1194 | //This method validate duplicate pricing rule |
| 1195 | var pricing_rule_name = ''; |
| 1196 | var priority = 0; |
| 1197 | var pricing_rule_list = []; |
| 1198 | var priority_list = [] |
| 1199 | |
| 1200 | if(pricing_rule.length > 1){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1201 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1202 | $.each(pricing_rule, function(index, data){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1203 | pricing_rule_name += data.name + ',' |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1204 | priority_list.push(data.priority) |
| 1205 | if(priority <= data.priority){ |
| 1206 | priority = data.priority |
| 1207 | pricing_rule_list.push(data) |
| 1208 | } |
| 1209 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1210 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1211 | count = 0 |
| 1212 | $.each(priority_list, function(index, value){ |
| 1213 | if(value == priority){ |
| 1214 | count++ |
| 1215 | } |
| 1216 | }) |
| 1217 | |
| 1218 | if(priority == 0 || count > 1){ |
| 1219 | frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", { |
| 1220 | 'pricing_rule': pricing_rule_name |
| 1221 | }))) |
| 1222 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1223 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1224 | return pricing_rule_list |
| 1225 | } |
| 1226 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1227 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1228 | validate_warehouse: function(){ |
Nabin Hait | 977eff9 | 2016-11-21 18:29:16 +0530 | [diff] [blame] | 1229 | 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] | 1230 | frappe.throw(__("Default warehouse is required for selected item")) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1231 | } |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1232 | }, |
| 1233 | |
| 1234 | get_actual_qty: function(item) { |
| 1235 | this.actual_qty = 0.0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1236 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1237 | var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse; |
| 1238 | if(warehouse && this.bin_data[item.item_code]) { |
| 1239 | this.actual_qty = this.bin_data[item.item_code][warehouse] || 0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1240 | this.actual_qty_dict[item.item_code] = this.actual_qty |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | return this.actual_qty |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1244 | } |
| 1245 | }) |