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