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