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