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