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 | wrapper.pos.on_refresh_page() |
| 19 | } |
| 20 | |
| 21 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 22 | erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 23 | init: function(wrapper){ |
| 24 | this.load = true; |
| 25 | this.page = wrapper.page; |
| 26 | this.wrapper = $(wrapper).find('.page-content'); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 27 | this.set_indicator(); |
| 28 | this.onload(); |
| 29 | this.make_menu_list(); |
| 30 | this.set_interval_for_si_sync(); |
| 31 | this.si_docs = this.get_doc_from_localstorage(); |
| 32 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 33 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 34 | on_refresh_page: function() { |
| 35 | var me = this; |
| 36 | if(this.load){ |
| 37 | this.load = false; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 38 | }else if(this.connection_status){ |
| 39 | this.onload(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 40 | }else{ |
| 41 | this.create_new(); |
| 42 | } |
| 43 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 44 | |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 45 | beforeunload: function(e){ |
| 46 | if(this.connection_status == false && frappe.get_route()[0] == "pos"){ |
| 47 | e = e || window.event; |
| 48 | |
| 49 | // For IE and Firefox prior to version 4 |
| 50 | if (e) { |
| 51 | e.returnValue = __("You are in offline mode. You will not be able to reload until you have network."); |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | // For Safari |
| 56 | return __("You are in offline mode. You will not be able to reload until you have network."); |
| 57 | } |
| 58 | }, |
| 59 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 60 | check_internet_connection: function(){ |
| 61 | var me = this; |
| 62 | //Check Internet connection after every 30 seconds |
| 63 | setInterval(function(){ |
| 64 | me.set_indicator(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 65 | }, 5000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 66 | }, |
| 67 | |
| 68 | set_indicator: function(){ |
| 69 | var me = this; |
| 70 | // navigator.onLine |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 71 | this.connection_status = false; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 72 | this.page.set_indicator("Offline", "grey") |
| 73 | frappe.call({ |
| 74 | method:"frappe.handler.ping", |
| 75 | callback: function(r){ |
| 76 | if(r.message){ |
| 77 | me.connection_status = true; |
| 78 | me.page.set_indicator("Online", "green") |
| 79 | } |
| 80 | } |
| 81 | }) |
| 82 | }, |
| 83 | |
| 84 | onload: function(){ |
| 85 | var me = this; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 86 | this.get_data_from_server(function(){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 87 | me.create_new(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 88 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 89 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 90 | this.check_internet_connection(); |
| 91 | }, |
| 92 | |
| 93 | make_menu_list: function(){ |
| 94 | var me = this; |
| 95 | |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 96 | this.page.add_menu_item(__("New Sales Invoice"), function() { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 97 | me.save_previous_entry(); |
| 98 | me.create_new(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 99 | }) |
| 100 | |
| 101 | this.page.add_menu_item(__("View Offline Records"), function(){ |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 102 | me.show_unsync_invoice_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 103 | }); |
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 | this.page.add_menu_item(__("Sync Master Data"), function(){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 106 | me.get_data_from_server(function(){ |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 107 | me.load_data(); |
| 108 | me.make_customer(); |
| 109 | me.make_item_list(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 110 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 111 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 112 | |
| 113 | this.page.add_menu_item(__("POS Profile"), function() { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 114 | frappe.set_route('List', 'POS Profile'); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 115 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 116 | }, |
| 117 | |
| 118 | show_unsync_invoice_list: function(){ |
| 119 | var me = this; |
| 120 | this.si_docs = this.get_doc_from_localstorage(); |
| 121 | |
| 122 | this.list_dialog = new frappe.ui.Dialog({ |
| 123 | title: 'Invoice List' |
| 124 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 125 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 126 | this.list_dialog.show(); |
| 127 | this.list_body = this.list_dialog.body; |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 128 | if(this.si_docs.length > 0){ |
| 129 | $(this.list_body).append('<div class="row list-row list-row-head pos-invoice-list">\ |
| 130 | <div class="col-xs-2">Sr</div>\ |
| 131 | <div class="col-xs-4">Customer</div>\ |
| 132 | <div class="col-xs-2 text-left">Status</div>\ |
| 133 | <div class="col-xs-4 text-right">Grand Total</div>\ |
| 134 | </div>') |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 135 | |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 136 | $.each(this.si_docs, function(index, data){ |
| 137 | for(key in data) { |
| 138 | $(frappe.render_template("pos_invoice_list", { |
| 139 | sr: index + 1, |
| 140 | name: key, |
| 141 | customer: data[key].customer, |
| 142 | grand_total: format_currency(data[key].grand_total, me.frm.doc.currency), |
| 143 | data: me.get_doctype_status(data[key]) |
| 144 | })).appendTo($(me.list_body)); |
| 145 | } |
| 146 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 147 | |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 148 | $(this.list_body).find('.list-row').click(function() { |
| 149 | me.name = $(this).attr('invoice-name') |
| 150 | doc_data = me.get_invoice_doc(me.si_docs) |
| 151 | if(doc_data){ |
| 152 | me.frm.doc = doc_data[0][me.name]; |
| 153 | me.set_missing_values(); |
| 154 | me.refresh(); |
| 155 | me.disable_input_field(); |
| 156 | me.list_dialog.hide(); |
| 157 | } |
| 158 | }) |
| 159 | }else{ |
| 160 | $(this.list_body).append(repl('<div class="media-heading">%(message)s</div>', {'message': __("All records are synced.")})) |
| 161 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 162 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 163 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 164 | get_doctype_status: function(doc){ |
| 165 | if(doc.outstanding_amount == 0){ |
| 166 | return {status: "Paid", indicator: "green"} |
| 167 | }else if(doc.docstatus == 0){ |
| 168 | return {status: "Draft", indicator: "red"} |
| 169 | }else if(doc.paid_amount >= 0){ |
| 170 | return {status: "Unpaid", indicator: "orange"} |
| 171 | } |
| 172 | }, |
| 173 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 174 | set_missing_values: function(){ |
| 175 | var me = this; |
| 176 | doc = JSON.parse(localStorage.getItem('doc')) |
| 177 | if(this.frm.doc.payments.length == 0){ |
| 178 | this.frm.doc.payments = doc.payments; |
| 179 | } |
| 180 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 181 | |
| 182 | get_invoice_doc: function(si_docs){ |
| 183 | var me = this; |
| 184 | this.si_docs = this.get_doc_from_localstorage(); |
| 185 | |
| 186 | return $.grep(this.si_docs, function(data){ |
| 187 | for(key in data){ |
| 188 | return key == me.name |
| 189 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 190 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 191 | }, |
| 192 | |
| 193 | get_data_from_server: function(callback){ |
| 194 | var me = this; |
| 195 | frappe.call({ |
| 196 | method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data", |
| 197 | freeze: true, |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 198 | freeze_message: __("Master data syncing, it might take some time"), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 199 | callback: function(r){ |
| 200 | window.items = r.message.items; |
| 201 | window.customers = r.message.customers; |
| 202 | window.pricing_rules = r.message.pricing_rules; |
| 203 | window.meta = r.message.meta; |
| 204 | window.print_template = r.message.print_template; |
Rohit Waghchaure | 539e913 | 2016-07-07 16:44:40 +0530 | [diff] [blame] | 205 | me.default_customer = r.message.default_customer || null; |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 206 | me.write_off_account = r.message.write_off_account; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 207 | localStorage.setItem('doc', JSON.stringify(r.message.doc)); |
| 208 | if(callback){ |
| 209 | callback(); |
| 210 | } |
| 211 | } |
| 212 | }) |
| 213 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 214 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 215 | save_previous_entry : function(){ |
| 216 | if(this.frm.doc.items.length > 0){ |
| 217 | this.create_invoice() |
| 218 | } |
| 219 | }, |
| 220 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 221 | create_new: function(){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 222 | var me = this; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 223 | this.frm = {} |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 224 | this.name = ''; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 225 | this.load_data(); |
| 226 | this.setup(); |
| 227 | }, |
| 228 | |
| 229 | load_data: function(){ |
| 230 | this.items = window.items; |
| 231 | this.customers = window.customers; |
| 232 | this.pricing_rules = window.pricing_rules; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 233 | this.frm.doc = JSON.parse(localStorage.getItem('doc')); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 234 | |
| 235 | $.each(window.meta, function(i, data){ |
| 236 | frappe.meta.sync(data) |
| 237 | }) |
| 238 | |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 239 | this.print_template = frappe.render_template("print_template", |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 240 | {content: window.print_template, title:"POS"}) |
| 241 | }, |
| 242 | |
| 243 | setup: function(){ |
| 244 | this.wrapper.html(frappe.render_template("pos", this.frm.doc)); |
| 245 | this.set_transaction_defaults("Customer"); |
| 246 | this.make(); |
| 247 | this.set_primary_action(); |
| 248 | }, |
| 249 | |
| 250 | set_transaction_defaults: function(party) { |
| 251 | var me = this; |
| 252 | this.party = party; |
| 253 | this.price_list = (party == "Customer" ? |
| 254 | this.frm.doc.selling_price_list : this.frm.doc.buying_price_list); |
| 255 | this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list"); |
| 256 | this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase"); |
| 257 | }, |
| 258 | |
| 259 | make: function() { |
| 260 | this.make_search(); |
| 261 | this.make_customer(); |
| 262 | this.make_item_list(); |
| 263 | this.make_discount_field() |
| 264 | }, |
| 265 | |
| 266 | make_search: function() { |
| 267 | var me = this; |
| 268 | this.search = frappe.ui.form.make_control({ |
| 269 | df: { |
| 270 | "fieldtype": "Data", |
| 271 | "label": "Item", |
| 272 | "fieldname": "pos_item", |
| 273 | "placeholder": "Search Item" |
| 274 | }, |
| 275 | parent: this.wrapper.find(".search-area"), |
| 276 | only_input: true, |
| 277 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 278 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 279 | this.search.make_input(); |
| 280 | this.search.$input.on("keyup", function() { |
| 281 | setTimeout(function() { |
| 282 | me.items = me.get_items(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 283 | me.make_item_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 284 | }, 1000); |
| 285 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 286 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 287 | this.party_field = frappe.ui.form.make_control({ |
| 288 | df: { |
| 289 | "fieldtype": "Data", |
| 290 | "options": this.party, |
| 291 | "label": this.party, |
| 292 | "fieldname": this.party.toLowerCase(), |
Rushabh Mehta | e0686b3 | 2016-06-10 12:34:41 +0530 | [diff] [blame] | 293 | "placeholder": __("Select or add new customer") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 294 | }, |
| 295 | parent: this.wrapper.find(".party-area"), |
| 296 | only_input: true, |
| 297 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 298 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 299 | this.party_field.make_input(); |
| 300 | }, |
| 301 | |
| 302 | make_customer: function() { |
| 303 | var me = this; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 304 | |
Rohit Waghchaure | 539e913 | 2016-07-07 16:44:40 +0530 | [diff] [blame] | 305 | if(this.default_customer){ |
| 306 | this.party_field.$input.val(this.default_customer); |
| 307 | this.frm.doc.customer = this.default_customer; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 308 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 309 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 310 | this.party_field.$input.autocomplete({ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 311 | autoFocus: true, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 312 | source: function (request, response) { |
| 313 | me.customer_data = me.get_customers(request.term) |
| 314 | response($.map(me.customer_data, function(data){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 315 | return {label: data.name, value: data.name, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 316 | customer_group: data.customer_group, territory: data.territory} |
| 317 | })) |
| 318 | }, |
| 319 | change: function(event, ui){ |
| 320 | if(ui.item){ |
| 321 | me.frm.doc.customer = ui.item.label; |
| 322 | me.frm.doc.customer_name = ui.item.customer_name; |
| 323 | me.frm.doc.customer_group = ui.item.customer_group; |
| 324 | me.frm.doc.territory = ui.item.territory; |
| 325 | }else{ |
| 326 | me.frm.doc.customer = me.party_field.$input.val(); |
| 327 | } |
| 328 | me.refresh(); |
| 329 | } |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 330 | }).on("focus", function(){ |
| 331 | setTimeout(function() { |
| 332 | if(!me.party_field.$input.val()) { |
| 333 | me.party_field.$input.autocomplete( "search", " " ); |
| 334 | } |
| 335 | }, 500); |
| 336 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 337 | }, |
| 338 | |
| 339 | get_customers: function(key){ |
| 340 | var me = this; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 341 | key = key.toLowerCase().trim() |
| 342 | if(key){ |
| 343 | return $.grep(this.customers, function(data) { |
| 344 | if(data.name.toLowerCase().match(key) |
| 345 | || data.customer_name.toLowerCase().match(key) |
| 346 | || (data.customer_group && data.customer_group.toLowerCase().match(key))){ |
| 347 | return data |
| 348 | } |
| 349 | }) |
| 350 | }else{ |
| 351 | customers = this.customers.sort(function(a,b){ return a.idx < b.idx }) |
| 352 | return customers.slice(0, 20) |
| 353 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 354 | }, |
| 355 | |
| 356 | make_item_list: function() { |
| 357 | var me = this; |
| 358 | if(!this.price_list) { |
| 359 | msgprint(__("Price List not found or disabled")); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | me.item_timeout = null; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 364 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 365 | var $wrap = me.wrapper.find(".item-list"); |
| 366 | me.wrapper.find(".item-list").empty(); |
| 367 | |
| 368 | if (this.items) { |
| 369 | $.each(this.items, function(index, obj) { |
| 370 | if(index < 16){ |
| 371 | $(frappe.render_template("pos_item", { |
| 372 | item_code: obj.name, |
| 373 | item_price: format_currency(obj.price_list_rate, obj.currency), |
| 374 | item_name: obj.name===obj.item_name ? "" : obj.item_name, |
| 375 | item_image: obj.image ? "url('" + obj.image + "')" : null, |
| 376 | color: frappe.get_palette(obj.item_name), |
| 377 | abbr: frappe.get_abbr(obj.item_name) |
| 378 | })).tooltip().appendTo($wrap); |
| 379 | } |
| 380 | }); |
| 381 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 382 | |
Rohit Waghchaure | 28c02fe | 2016-06-10 00:48:03 +0530 | [diff] [blame] | 383 | if(this.items.length == 1 |
| 384 | && this.search.$input.val()) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 385 | this.search.$input.val(""); |
| 386 | this.add_to_cart(); |
| 387 | } |
| 388 | |
| 389 | // if form is local then allow this function |
| 390 | $(me.wrapper).find("div.pos-item").on("click", function() { |
| 391 | me.customer_validate(); |
| 392 | if(me.frm.doc.docstatus==0) { |
| 393 | me.items = me.get_items($(this).attr("data-item-code")) |
| 394 | me.add_to_cart(); |
| 395 | } |
| 396 | }); |
| 397 | }, |
| 398 | |
| 399 | get_items: function(item_code){ |
| 400 | // To search item as per the key enter |
| 401 | |
| 402 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 403 | this.item_serial_no = {}; |
| 404 | this.item_batch_no = {}; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 405 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 406 | if(item_code){ |
| 407 | return $.grep(window.items, function(item){ |
| 408 | if(item.item_code == item_code ){ |
| 409 | return true |
| 410 | } |
| 411 | }) |
| 412 | } |
| 413 | |
| 414 | key = this.search.$input.val().toLowerCase(); |
| 415 | |
| 416 | if(key){ |
| 417 | return $.grep(window.items, function(item){ |
| 418 | if( (item.item_code.toLowerCase().match(key)) || |
| 419 | (item.item_name.toLowerCase().match(key)) || (item.item_group.toLowerCase().match(key)) ){ |
| 420 | return true |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 421 | }else if(item.barcode == me.search.$input.val()){ |
| 422 | return item.barcode == me.search.$input.val(); |
| 423 | } else if (in_list(Object.keys(item.serial_nos), me.search.$input.val())){ |
| 424 | me.item_serial_no[item.item_code] = [me.search.$input.val(), item.serial_nos[me.search.$input.val()]] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 425 | return true |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 426 | } else if(in_list(item.batch_nos, me.search.$input.val())){ |
| 427 | return me.item_batch_no[item.item_code] = me.search.$input.val() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 428 | } |
| 429 | }) |
| 430 | }else{ |
| 431 | return window.items; |
| 432 | } |
| 433 | }, |
| 434 | |
| 435 | update_qty: function() { |
| 436 | var me = this; |
| 437 | |
| 438 | $(this.wrapper).find(".pos-item-qty").on("change", function(){ |
| 439 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 440 | me.update_qty_rate_against_item_code(item_code, "qty", $(this).val()); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 441 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 442 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 443 | $(this.wrapper).find("[data-action='increase-qty']").on("click", function(){ |
| 444 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 445 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 446 | me.update_qty_rate_against_item_code(item_code, "qty", qty); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 447 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 448 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 449 | $(this.wrapper).find("[data-action='decrease-qty']").on("click", function(){ |
| 450 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 451 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 452 | me.update_qty_rate_against_item_code(item_code, "qty", qty); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 453 | }) |
| 454 | }, |
| 455 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 456 | update_rate: function() { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 457 | var me = this; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 458 | |
| 459 | $(this.wrapper).find(".pos-item-rate").on("change", function(){ |
| 460 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 461 | me.update_qty_rate_against_item_code(item_code, "rate", $(this).val()); |
| 462 | }) |
| 463 | }, |
| 464 | |
| 465 | update_qty_rate_against_item_code: function(item_code, field, value){ |
| 466 | var me = this; |
| 467 | if(value < 0){ |
| 468 | frappe.throw(__("Enter value must be positive")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 469 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 470 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 471 | this.remove_item = [] |
| 472 | $.each(this.frm.doc["items"] || [], function(i, d) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 473 | if (d.item_code == item_code && d.serial_no |
| 474 | && field == 'qty' && cint(value) != value) { |
| 475 | d.qty = 0.0; |
| 476 | me.refresh(); |
| 477 | frappe.throw(__("Serial no item cannot be a fraction")) |
| 478 | } |
| 479 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 480 | if (d.item_code == item_code) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 481 | d[field] = flt(value); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 482 | d.amount = flt(d.rate) * flt(d.qty); |
| 483 | if(d.qty==0){ |
| 484 | me.remove_item.push(d.idx) |
| 485 | } |
| 486 | } |
| 487 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 488 | |
| 489 | if(field == 'qty'){ |
| 490 | this.remove_zero_qty_item(); |
| 491 | } |
| 492 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 493 | this.refresh(); |
| 494 | }, |
| 495 | |
| 496 | remove_zero_qty_item: function(){ |
| 497 | var me = this; |
| 498 | idx = 0 |
| 499 | this.items = [] |
| 500 | idx = 0 |
| 501 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 502 | if(!in_list(me.remove_item, d.idx)){ |
| 503 | d.idx = idx; |
| 504 | me.items.push(d); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 505 | idx++; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 506 | } |
| 507 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 508 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 509 | this.frm.doc["items"] = this.items; |
| 510 | }, |
| 511 | |
| 512 | make_discount_field: function(){ |
| 513 | var me = this; |
| 514 | |
| 515 | this.wrapper.find('input.discount-percentage').on("change", function() { |
| 516 | me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage")); |
| 517 | total = me.frm.doc.grand_total |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 518 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 519 | if(me.frm.doc.apply_discount_on == 'Net Total'){ |
| 520 | total = me.frm.doc.net_total |
| 521 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 522 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 523 | me.frm.doc.discount_amount = flt(total*flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); |
| 524 | me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount) |
| 525 | me.refresh(); |
| 526 | }); |
| 527 | |
| 528 | this.wrapper.find('input.discount-amount').on("change", function() { |
| 529 | me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount")); |
| 530 | me.frm.doc.additional_discount_percentage = 0.0; |
| 531 | me.wrapper.find('input.discount-percentage').val(0); |
| 532 | me.refresh(); |
| 533 | }); |
| 534 | }, |
| 535 | |
| 536 | customer_validate: function(){ |
| 537 | var me = this; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 538 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 539 | if(!this.frm.doc.customer){ |
| 540 | frappe.throw(__("Please select customer")) |
| 541 | } |
| 542 | }, |
| 543 | |
| 544 | add_to_cart: function() { |
| 545 | var me = this; |
| 546 | var caught = false; |
| 547 | var no_of_items = me.wrapper.find(".pos-bill-item").length; |
| 548 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 549 | this.customer_validate(); |
| 550 | this.mandatory_batch_no(); |
| 551 | this.validate_serial_no(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 552 | this.validate_warehouse(); |
| 553 | |
| 554 | if (no_of_items != 0) { |
| 555 | $.each(this.frm.doc["items"] || [], function(i, d) { |
| 556 | if (d.item_code == me.items[0].item_code) { |
| 557 | caught = true; |
| 558 | d.qty += 1; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 559 | d.amount = flt(d.rate) * flt(d.qty); |
| 560 | if(me.item_serial_no[d.item_code]){ |
| 561 | d.serial_no += '\n' + me.item_serial_no[d.item_code][0] |
| 562 | d.warehouse = me.item_serial_no[d.item_code][1] |
| 563 | } |
| 564 | |
| 565 | if(me.item_batch_no.length){ |
| 566 | d.batch_no = me.item_batch_no[d.item_code] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | }); |
| 570 | } |
| 571 | |
| 572 | // if item not found then add new item |
| 573 | if (!caught) |
| 574 | this.add_new_item_to_grid(); |
| 575 | |
| 576 | this.refresh(); |
| 577 | }, |
| 578 | |
| 579 | add_new_item_to_grid: function() { |
| 580 | var me = this; |
| 581 | this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items"); |
| 582 | this.child.item_code = this.items[0].item_code; |
| 583 | this.child.item_name = this.items[0].item_name; |
| 584 | this.child.stock_uom = this.items[0].stock_uom; |
| 585 | this.child.description = this.items[0].description; |
| 586 | this.child.qty = 1; |
| 587 | this.child.item_group = this.items[0].item_group; |
| 588 | this.child.cost_center = this.items[0].cost_center; |
| 589 | this.child.income_account = this.items[0].income_account; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 590 | this.child.warehouse = (this.item_serial_no[this.child.item_code] |
| 591 | ? this.item_serial_no[this.child.item_code][1] : this.items[0].default_warehouse); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 592 | this.child.price_list_rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9); |
| 593 | this.child.rate = flt(this.items[0].price_list_rate, 9) / flt(this.frm.doc.conversion_rate, 9); |
| 594 | this.child.actual_qty = this.items[0].actual_qty; |
| 595 | this.child.amount = flt(this.child.qty) * flt(this.child.rate); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 596 | this.child.batch_no = this.item_batch_no[this.child.item_code]; |
| 597 | this.child.serial_no = (this.item_serial_no[this.child.item_code] |
| 598 | ? this.item_serial_no[this.child.item_code][0] : ''); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 599 | }, |
| 600 | |
| 601 | refresh: function() { |
| 602 | var me = this; |
| 603 | this.refresh_fields(); |
| 604 | this.update_qty(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 605 | this.update_rate(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 606 | this.set_primary_action(); |
| 607 | }, |
| 608 | refresh_fields: function() { |
| 609 | this.apply_pricing_rule(); |
| 610 | this.discount_amount_applied = false; |
| 611 | this._calculate_taxes_and_totals(); |
| 612 | this.calculate_discount_amount(); |
| 613 | this.show_items_in_item_cart(); |
| 614 | this.set_taxes(); |
| 615 | this.calculate_outstanding_amount(); |
| 616 | this.set_totals(); |
| 617 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 618 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 619 | get_company_currency: function() { |
| 620 | return erpnext.get_currency(this.frm.doc.company); |
| 621 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 622 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 623 | show_item_wise_taxes: function(){ |
| 624 | return null; |
| 625 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 626 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 627 | show_items_in_item_cart: function() { |
| 628 | var me = this; |
| 629 | var $items = this.wrapper.find(".items").empty(); |
| 630 | me.frm.doc.net_total = 0.0 |
| 631 | $.each(this.frm.doc.items|| [], function(i, d) { |
| 632 | $(frappe.render_template("pos_bill_item", { |
| 633 | item_code: d.item_code, |
| 634 | item_name: (d.item_name===d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name), |
| 635 | qty: d.qty, |
| 636 | actual_qty: d.actual_qty, |
| 637 | projected_qty: d.projected_qty, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 638 | rate: format_number(d.rate, me.frm.doc.currency), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 639 | amount: format_currency(d.amount, me.frm.doc.currency) |
| 640 | })).appendTo($items); |
| 641 | }); |
| 642 | |
| 643 | this.wrapper.find("input.pos-item-qty").on("focus", function() { |
| 644 | $(this).select(); |
| 645 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 646 | |
| 647 | this.wrapper.find("input.pos-item-rate").on("focus", function() { |
| 648 | $(this).select(); |
| 649 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 650 | }, |
| 651 | |
| 652 | set_taxes: function(){ |
| 653 | var me = this; |
| 654 | me.frm.doc.total_taxes_and_charges = 0.0 |
| 655 | |
| 656 | var taxes = this.frm.doc.taxes || []; |
| 657 | $(this.wrapper) |
| 658 | .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true) |
| 659 | .find(".tax-table").empty(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 660 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 661 | $.each(taxes, function(i, d) { |
| 662 | if (d.tax_amount && cint(d.included_in_print_rate) == 0) { |
| 663 | $(frappe.render_template("pos_tax_row", { |
| 664 | description: d.description, |
| 665 | tax_amount: format_currency(flt(d.tax_amount_after_discount_amount), |
| 666 | me.frm.doc.currency) |
| 667 | })).appendTo(me.wrapper.find(".tax-table")); |
| 668 | } |
| 669 | }); |
| 670 | }, |
| 671 | |
| 672 | set_totals: function() { |
| 673 | var me = this; |
| 674 | this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency)); |
| 675 | this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency)); |
| 676 | }, |
| 677 | |
| 678 | set_primary_action: function() { |
| 679 | var me = this; |
| 680 | |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 681 | if (this.frm.doc.docstatus==0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 682 | this.page.set_primary_action(__("Pay"), function() { |
| 683 | me.validate() |
| 684 | me.create_invoice(); |
| 685 | me.make_payment(); |
Faris Ansari | 8908d19 | 2016-07-20 13:59:19 +0530 | [diff] [blame] | 686 | }, "octicon octicon-credit-card"); |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 687 | }else if(this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 688 | this.page.set_primary_action(__("Print"), function() { |
| 689 | html = frappe.render(me.print_template, me.frm.doc) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 690 | me.print_document(html) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 691 | }) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 692 | }else { |
| 693 | this.page.clear_primary_action() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 694 | } |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 695 | |
| 696 | this.page.set_secondary_action(__("New"), function() { |
| 697 | me.save_previous_entry(); |
| 698 | me.create_new(); |
Faris Ansari | 8908d19 | 2016-07-20 13:59:19 +0530 | [diff] [blame] | 699 | }, "octicon octicon-plus").addClass("btn-primary"); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 700 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 701 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 702 | print_document: function(html){ |
| 703 | var w = window.open(); |
| 704 | w.document.write(html); |
| 705 | w.document.close(); |
| 706 | setTimeout(function(){ |
| 707 | w.print(); |
| 708 | w.close(); |
| 709 | }, 1000) |
| 710 | }, |
| 711 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 712 | write_off_amount: function(){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 713 | var me = this; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 714 | var value = 0.0; |
| 715 | |
| 716 | if(this.frm.doc.outstanding_amount > 0){ |
| 717 | dialog = new frappe.ui.Dialog({ |
| 718 | title: 'Write Off Amount', |
| 719 | fields: [ |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 720 | {fieldtype: "Check", fieldname: "write_off_amount", label: __("Write off Outstanding Amount")}, |
| 721 | {fieldtype: "Link", options:"Account", default:this.write_off_account, fieldname: "write_off_account", |
| 722 | label: __("Write off Account"), get_query: function() { |
| 723 | return { |
| 724 | filters: {'is_group': 0, 'report_type': 'Profit and Loss'} |
| 725 | } |
| 726 | }} |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 727 | ] |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 728 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 729 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 730 | dialog.show(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 731 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 732 | dialog.fields_dict.write_off_amount.$input.change(function(){ |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 733 | write_off_amount = dialog.get_values().write_off_amount; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 734 | me.frm.doc.write_off_outstanding_amount_automatically = write_off_amount; |
| 735 | me.frm.doc.base_write_off_amount = (write_off_amount==1) ? flt(me.frm.doc.grand_total - me.frm.doc.paid_amount, precision("outstanding_amount")) : 0; |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 736 | me.frm.doc.write_off_account = (write_off_amount==1) ? dialog.get_values().write_off_account : ''; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 737 | me.frm.doc.write_off_amount = flt(me.frm.doc.base_write_off_amount * me.frm.doc.conversion_rate, precision("write_off_amount")) |
| 738 | me.calculate_outstanding_amount(); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 739 | me.set_primary_action(); |
| 740 | }) |
| 741 | |
| 742 | dialog.fields_dict.write_off_account.$input.change(function(){ |
| 743 | me.frm.doc.write_off_account = dialog.get_values().write_off_account; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 744 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 745 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 746 | dialog.set_primary_action(__("Submit"), function(){ |
| 747 | dialog.hide() |
| 748 | me.submit_invoice() |
| 749 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 750 | }else{ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 751 | this.submit_invoice() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 752 | } |
| 753 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 754 | |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 755 | submit_invoice: function(){ |
| 756 | var me = this; |
| 757 | frappe.confirm(__("Do you really want to submit the invoice?"), function () { |
| 758 | me.change_status(); |
Rohit Waghchaure | 53bea82 | 2016-07-06 16:09:26 +0530 | [diff] [blame] | 759 | frappe.msgprint(__("Sales invoice submitted sucessfully.")) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 760 | }) |
| 761 | }, |
| 762 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 763 | change_status: function(){ |
| 764 | if(this.frm.doc.docstatus == 0){ |
| 765 | this.frm.doc.docstatus = 1; |
| 766 | this.update_invoice(); |
| 767 | this.disable_input_field(); |
| 768 | } |
| 769 | }, |
| 770 | |
| 771 | disable_input_field: function(){ |
| 772 | var pointer_events = 'inherit' |
| 773 | $(this.wrapper).find('input').attr("disabled", false); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 774 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 775 | if(this.frm.doc.docstatus == 1){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 776 | pointer_events = 'none'; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 777 | $(this.wrapper).find('input').attr("disabled", true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 778 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 779 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 780 | $(this.wrapper).find('.pos-bill-wrapper').css('pointer-events', pointer_events); |
| 781 | $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events); |
| 782 | this.set_primary_action(); |
| 783 | }, |
| 784 | |
| 785 | create_invoice: function(){ |
| 786 | var me = this; |
| 787 | var invoice_data = {} |
| 788 | this.si_docs = this.get_doc_from_localstorage(); |
| 789 | if(this.name){ |
| 790 | this.update_invoice() |
| 791 | }else{ |
| 792 | this.name = $.now(); |
| 793 | invoice_data[this.name] = this.frm.doc |
| 794 | this.si_docs.push(invoice_data) |
| 795 | this.update_localstorage(); |
| 796 | this.set_primary_action(); |
| 797 | } |
| 798 | }, |
| 799 | |
| 800 | update_invoice: function(){ |
| 801 | var me = this; |
| 802 | this.si_docs = this.get_doc_from_localstorage(); |
| 803 | $.each(this.si_docs, function(index, data){ |
| 804 | for(key in data){ |
| 805 | if(key == me.name){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 806 | me.si_docs[index][key] = me.frm.doc; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 807 | me.update_localstorage(); |
| 808 | } |
| 809 | } |
| 810 | }) |
| 811 | }, |
| 812 | |
| 813 | update_localstorage: function(){ |
| 814 | try{ |
| 815 | localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs)); |
| 816 | }catch(e){ |
| 817 | frappe.throw(__("LocalStorage is full , did not save")) |
| 818 | } |
| 819 | }, |
| 820 | |
| 821 | get_doc_from_localstorage: function(){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 822 | try{ |
| 823 | return JSON.parse(localStorage.getItem('sales_invoice_doc')) || []; |
| 824 | }catch(e){ |
| 825 | return [] |
| 826 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 827 | }, |
| 828 | |
| 829 | set_interval_for_si_sync: function(){ |
| 830 | var me = this; |
| 831 | setInterval(function(){ |
| 832 | me.sync_sales_invoice() |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 833 | }, 60000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 834 | }, |
| 835 | |
| 836 | sync_sales_invoice: function(){ |
| 837 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 838 | this.si_docs = this.get_submitted_invoice(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 839 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 840 | if(this.si_docs.length){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 841 | frappe.call({ |
| 842 | method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", |
| 843 | args: { |
| 844 | doc_list: me.si_docs |
| 845 | }, |
| 846 | callback: function(r){ |
| 847 | if(r.message){ |
| 848 | me.removed_items = r.message; |
| 849 | me.remove_doc_from_localstorage(); |
| 850 | } |
| 851 | } |
| 852 | }) |
| 853 | } |
| 854 | }, |
| 855 | |
| 856 | get_submitted_invoice: function(){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 857 | var invoices = []; |
| 858 | var index = 1; |
| 859 | docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 860 | if(docs){ |
| 861 | invoices = $.map(docs, function(data){ |
| 862 | for(key in data){ |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 863 | if(data[key].docstatus == 1 && index < 50){ |
| 864 | index++ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 865 | return data |
| 866 | } |
| 867 | } |
| 868 | }); |
| 869 | } |
| 870 | |
| 871 | return invoices |
| 872 | }, |
| 873 | |
| 874 | remove_doc_from_localstorage: function(){ |
| 875 | var me = this; |
| 876 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 877 | this.new_si_docs = []; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 878 | if(this.removed_items){ |
| 879 | $.each(this.si_docs, function(index, data){ |
| 880 | for(key in data){ |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 881 | if(!in_list(me.removed_items, key)){ |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 882 | me.new_si_docs.push(data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | }) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 886 | this.si_docs = this.new_si_docs; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 887 | this.update_localstorage(); |
| 888 | } |
| 889 | }, |
| 890 | |
| 891 | validate: function(){ |
| 892 | var me = this; |
| 893 | this.customer_validate(); |
| 894 | this.item_validate(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 895 | this.validate_mode_of_payments(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 896 | }, |
| 897 | |
| 898 | item_validate: function(){ |
| 899 | if(this.frm.doc.items.length == 0){ |
| 900 | frappe.throw(__("Select items to save the invoice")) |
| 901 | } |
| 902 | }, |
Saurabh | 9a6c666 | 2016-06-27 16:51:44 +0530 | [diff] [blame] | 903 | |
| 904 | validate_mode_of_payments: function(){ |
| 905 | if (this.frm.doc.payments.length === 0){ |
| 906 | frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile.")) |
| 907 | } |
| 908 | }, |
| 909 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 910 | validate_serial_no: function(){ |
| 911 | var me = this; |
| 912 | var item_code = serial_no = ''; |
| 913 | for (key in this.item_serial_no){ |
| 914 | item_code = key; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 915 | serial_no = me.item_serial_no[key][0]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 916 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 917 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 918 | if(item_code && serial_no){ |
| 919 | $.each(this.frm.doc.items, function(index, data){ |
| 920 | if(data.item_code == item_code){ |
| 921 | if(in_list(data.serial_no.split('\n'), serial_no)){ |
| 922 | frappe.throw(__(repl("Serial no %(serial_no)s is already taken", { |
| 923 | 'serial_no': serial_no |
| 924 | }))) |
| 925 | } |
| 926 | } |
| 927 | }) |
| 928 | } |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 929 | |
| 930 | if(this.items[0].has_serial_no && serial_no == ""){ |
| 931 | frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", { |
| 932 | 'item': this.items[0].item_code |
| 933 | }))) |
| 934 | } |
| 935 | }, |
| 936 | |
| 937 | mandatory_batch_no: function(){ |
| 938 | var me = this; |
| 939 | if(this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]){ |
| 940 | frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", { |
| 941 | 'item': this.items[0].item_code |
| 942 | }))) |
| 943 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 944 | }, |
| 945 | |
| 946 | apply_pricing_rule: function(){ |
| 947 | var me = this; |
| 948 | $.each(this.frm.doc["items"], function(n, item) { |
| 949 | pricing_rule = me.get_pricing_rule(item) |
| 950 | me.validate_pricing_rule(pricing_rule) |
| 951 | if(pricing_rule.length){ |
| 952 | item.margin_type = pricing_rule[0].margin_type; |
| 953 | item.price_list_rate = pricing_rule[0].price || item.price_list_rate; |
| 954 | item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount; |
| 955 | item.discount_percentage = pricing_rule[0].discount_percentage || 0.0; |
| 956 | me.apply_pricing_rule_on_item(item) |
| 957 | } |
| 958 | }) |
| 959 | }, |
| 960 | |
| 961 | get_pricing_rule: function(item){ |
| 962 | var me = this; |
| 963 | return $.grep(this.pricing_rules, function(data){ |
| 964 | if(data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) { |
| 965 | if(in_list(['Customer', 'Customer Group', 'Territory'], data.applicable_for)){ |
| 966 | return me.validate_condition(data) |
| 967 | }else{ |
| 968 | return true |
| 969 | } |
| 970 | } |
| 971 | }) |
| 972 | }, |
| 973 | |
| 974 | validate_condition: function(data){ |
| 975 | //This method check condition based on applicable for |
| 976 | condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for] |
| 977 | if(in_list(condition[1], condition[0])){ |
| 978 | return true |
| 979 | } |
| 980 | }, |
| 981 | |
| 982 | get_mapper_for_pricing_rule: function(data){ |
| 983 | return { |
Rohit Waghchaure | 89ee309 | 2016-07-08 15:17:04 +0530 | [diff] [blame] | 984 | 'Customer': [data.customer, [this.frm.doc.customer]], |
| 985 | 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']], |
| 986 | 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']], |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 987 | } |
| 988 | }, |
| 989 | |
| 990 | validate_pricing_rule: function(pricing_rule){ |
| 991 | //This method validate duplicate pricing rule |
| 992 | var pricing_rule_name = ''; |
| 993 | var priority = 0; |
| 994 | var pricing_rule_list = []; |
| 995 | var priority_list = [] |
| 996 | |
| 997 | if(pricing_rule.length > 1){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 998 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 999 | $.each(pricing_rule, function(index, data){ |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1000 | pricing_rule_name += data.name + ',' |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1001 | priority_list.push(data.priority) |
| 1002 | if(priority <= data.priority){ |
| 1003 | priority = data.priority |
| 1004 | pricing_rule_list.push(data) |
| 1005 | } |
| 1006 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1007 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1008 | count = 0 |
| 1009 | $.each(priority_list, function(index, value){ |
| 1010 | if(value == priority){ |
| 1011 | count++ |
| 1012 | } |
| 1013 | }) |
| 1014 | |
| 1015 | if(priority == 0 || count > 1){ |
| 1016 | frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", { |
| 1017 | 'pricing_rule': pricing_rule_name |
| 1018 | }))) |
| 1019 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1020 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1021 | return pricing_rule_list |
| 1022 | } |
| 1023 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1024 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1025 | validate_warehouse: function(){ |
| 1026 | if(!this.items[0].default_warehouse){ |
| 1027 | frappe.throw(__("Deafault warehouse is required for selected item")) |
| 1028 | } |
| 1029 | } |
| 1030 | }) |