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