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