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