Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | erpnext.payments = erpnext.stock.StockController.extend({ |
| 5 | make_payment: function() { |
| 6 | var me = this; |
| 7 | |
| 8 | this.dialog = new frappe.ui.Dialog({ |
| 9 | title: 'Payment' |
| 10 | }); |
| 11 | |
| 12 | this.dialog.show(); |
| 13 | this.$body = this.dialog.body; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 14 | this.set_payment_primary_action(); |
| 15 | this.make_keyboard(); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 16 | this.select_text() |
| 17 | }, |
| 18 | |
| 19 | select_text: function(){ |
| 20 | var me = this; |
| 21 | $(this.$body).find('.form-control').click(function(){ |
| 22 | $(this).select(); |
| 23 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 24 | }, |
| 25 | |
| 26 | set_payment_primary_action: function(){ |
| 27 | var me = this; |
| 28 | |
| 29 | this.dialog.set_primary_action(__("Submit"), function() { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 30 | me.dialog.hide() |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 31 | me.submit_invoice() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 32 | }) |
| 33 | }, |
| 34 | |
| 35 | make_keyboard: function(){ |
| 36 | var me = this; |
| 37 | $(this.$body).empty(); |
| 38 | $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc)) |
| 39 | this.show_payment_details(); |
| 40 | this.bind_keyboard_event() |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 41 | this.clear_amount() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 42 | }, |
| 43 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 44 | make_multimode_payment: function(){ |
| 45 | var me = this; |
| 46 | |
| 47 | if(this.frm.doc.change_amount > 0){ |
| 48 | me.payment_val = me.doc.outstanding_amount |
| 49 | } |
| 50 | |
| 51 | this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments"); |
| 52 | this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value(); |
| 53 | this.payments.amount = flt(this.payment_val); |
| 54 | }, |
| 55 | |
| 56 | show_payment_details: function(){ |
| 57 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 58 | var multimode_payments = $(this.$body).find('.multimode-payments').empty(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 59 | if(this.frm.doc.payments.length){ |
| 60 | $.each(this.frm.doc.payments, function(index, data){ |
| 61 | $(frappe.render_template('payment_details', { |
| 62 | mode_of_payment: data.mode_of_payment, |
| 63 | amount: data.amount, |
| 64 | idx: data.idx, |
| 65 | currency: me.frm.doc.currency, |
| 66 | type: data.type |
| 67 | })).appendTo(multimode_payments) |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 68 | |
| 69 | if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) { |
| 70 | me.idx = data.idx; |
| 71 | me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx})); |
| 72 | me.highlight_selected_row(); |
| 73 | me.bind_amount_change_event(); |
| 74 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 75 | }) |
| 76 | }else{ |
| 77 | $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments) |
| 78 | } |
| 79 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 80 | |
| 81 | set_outstanding_amount: function(){ |
| 82 | this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx})); |
| 83 | this.highlight_selected_row() |
| 84 | this.payment_val = 0.0 |
| 85 | if(this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0){ |
Rohit Waghchaure | 88514a2 | 2016-07-19 20:04:44 +0530 | [diff] [blame] | 86 | //When user first time click on row |
Rohit Waghchaure | a57bf5e | 2016-08-30 00:41:33 +0530 | [diff] [blame] | 87 | this.payment_val = flt(this.frm.doc.outstanding_amount / this.frm.doc.conversion_rate, precision("outstanding_amount")) |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 88 | this.selected_mode.val(format_currency(this.payment_val, this.frm.doc.currency)); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 89 | this.update_payment_amount() |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 90 | }else if(flt(this.selected_mode.val()) > 0){ |
| 91 | //If user click on existing row which has value |
| 92 | this.payment_val = flt(this.selected_mode.val()); |
| 93 | } |
| 94 | this.selected_mode.select() |
| 95 | this.bind_amount_change_event(); |
| 96 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 97 | |
| 98 | bind_keyboard_event: function(){ |
| 99 | var me = this; |
| 100 | this.payment_val = ''; |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 101 | this.bind_form_control_event(); |
| 102 | this.bind_numeric_keys_event(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 103 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 104 | |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 105 | bind_form_control_event: function(){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 106 | var me = this; |
| 107 | $(this.$body).find('.pos-payment-row').click(function(){ |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 108 | me.idx = $(this).attr("idx"); |
| 109 | me.set_outstanding_amount() |
| 110 | }) |
| 111 | |
| 112 | $(this.$body).find('.form-control').click(function(){ |
| 113 | me.idx = $(this).attr("idx"); |
| 114 | me.set_outstanding_amount(); |
Rohit Waghchaure | ea6d7e9 | 2016-08-22 19:49:17 +0530 | [diff] [blame] | 115 | me.update_paid_amount(true); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 116 | }) |
| 117 | |
| 118 | $(this.$body).find('.write_off_amount').change(function(){ |
Rohit Waghchaure | ea6d7e9 | 2016-08-22 19:49:17 +0530 | [diff] [blame] | 119 | me.write_off_amount(flt($(this).val()), precision("write_off_amount")); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 120 | }) |
| 121 | |
| 122 | $(this.$body).find('.change_amount').change(function(){ |
Rohit Waghchaure | ea6d7e9 | 2016-08-22 19:49:17 +0530 | [diff] [blame] | 123 | me.change_amount(flt($(this).val()), precision("change_amount")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 124 | }) |
| 125 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 126 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 127 | highlight_selected_row: function(){ |
| 128 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 129 | var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx})); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 130 | $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode') |
| 131 | selected_row.addClass('selected-payment-mode') |
| 132 | $(this.$body).find('.amount').attr('disabled', true); |
| 133 | this.selected_mode.attr('disabled', false); |
| 134 | }, |
| 135 | |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 136 | bind_numeric_keys_event: function(){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 137 | var me = this; |
| 138 | $(this.$body).find('.pos-keyboard-key').click(function(){ |
| 139 | me.payment_val += $(this).text(); |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 140 | me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 141 | me.idx = me.selected_mode.attr("idx") |
Rohit Waghchaure | ea6d7e9 | 2016-08-22 19:49:17 +0530 | [diff] [blame] | 142 | me.update_paid_amount() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 143 | }) |
| 144 | |
| 145 | $(this.$body).find('.delete-btn').click(function(){ |
| 146 | me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1); |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 147 | me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 148 | me.idx = me.selected_mode.attr("idx") |
| 149 | me.update_paid_amount(); |
| 150 | }) |
| 151 | |
| 152 | }, |
| 153 | |
| 154 | bind_amount_change_event: function(){ |
| 155 | var me = this; |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 156 | this.selected_mode.change(function(){ |
Rohit Waghchaure | 53bea82 | 2016-07-06 16:09:26 +0530 | [diff] [blame] | 157 | me.payment_val = flt($(this).val()) || 0.0; |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 158 | me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 159 | me.idx = me.selected_mode.attr("idx") |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 160 | me.update_payment_amount() |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 161 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 162 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 163 | |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 164 | clear_amount: function() { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 165 | var me = this; |
| 166 | $(this.$body).find('.clr').click(function(e){ |
| 167 | e.stopPropagation(); |
| 168 | me.idx = $(this).attr("idx"); |
| 169 | me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx})); |
| 170 | me.payment_val = 0.0; |
| 171 | me.selected_mode.val(0.0); |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 172 | me.highlight_selected_row(); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 173 | me.update_payment_amount(); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 174 | }) |
| 175 | }, |
| 176 | |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 177 | write_off_amount: function(write_off_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 178 | var me = this; |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 179 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 180 | this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount")); |
| 181 | this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate, |
| 182 | precision("base_write_off_amount")); |
| 183 | this.calculate_outstanding_amount(false) |
| 184 | this.show_amounts() |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 185 | }, |
| 186 | |
| 187 | change_amount: function(change_amount) { |
| 188 | var me = this; |
| 189 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 190 | this.frm.doc.change_amount = flt(change_amount, precision("change_amount")); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 191 | this.calculate_write_off_amount() |
| 192 | this.show_amounts() |
| 193 | }, |
| 194 | |
Rohit Waghchaure | ea6d7e9 | 2016-08-22 19:49:17 +0530 | [diff] [blame] | 195 | update_paid_amount: function(update_write_off) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 196 | var me = this; |
| 197 | if(in_list(['change_amount', 'write_off_amount'], this.idx)){ |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 198 | var value = me.selected_mode.val(); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 199 | if(me.idx == 'change_amount'){ |
| 200 | me.change_amount(value) |
| 201 | } else{ |
Rohit Waghchaure | d39f533 | 2016-08-31 02:09:15 +0530 | [diff] [blame] | 202 | if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) { |
Rohit Waghchaure | a57bf5e | 2016-08-30 00:41:33 +0530 | [diff] [blame] | 203 | value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx)); |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 204 | } |
| 205 | me.write_off_amount(value) |
| 206 | } |
| 207 | }else{ |
| 208 | this.update_payment_amount() |
| 209 | } |
| 210 | }, |
| 211 | |
| 212 | update_payment_amount: function(){ |
| 213 | var me = this; |
| 214 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 215 | $.each(this.frm.doc.payments, function(index, data){ |
| 216 | if(cint(me.idx) == cint(data.idx)){ |
| 217 | data.amount = flt(me.selected_mode.val(), 2) |
| 218 | } |
| 219 | }) |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 220 | |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 221 | this.calculate_outstanding_amount(false); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 222 | this.show_amounts(); |
| 223 | }, |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 224 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 225 | show_amounts: function(){ |
| 226 | var me = this; |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 227 | $(this.$body).find(".write_off_amount").val(format_currency(this.frm.doc.write_off_amount, this.frm.doc.currency)); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 228 | $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency)); |
Rohit Waghchaure | 609e2b4 | 2016-08-31 02:04:37 +0530 | [diff] [blame] | 229 | $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency)) |
Rohit Waghchaure | a57bf5e | 2016-08-30 00:41:33 +0530 | [diff] [blame] | 230 | $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, frappe.get_doc(":Company", this.frm.doc.company).default_currency)) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 231 | this.update_invoice(); |
| 232 | } |
| 233 | }) |