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(); |
| 16 | }, |
| 17 | |
| 18 | set_payment_primary_action: function(){ |
| 19 | var me = this; |
| 20 | |
| 21 | this.dialog.set_primary_action(__("Submit"), function() { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 22 | me.dialog.hide() |
| 23 | me.write_off_amount() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 24 | }) |
| 25 | }, |
| 26 | |
| 27 | make_keyboard: function(){ |
| 28 | var me = this; |
| 29 | $(this.$body).empty(); |
| 30 | $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc)) |
| 31 | this.show_payment_details(); |
| 32 | this.bind_keyboard_event() |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 33 | this.clear_amount() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 34 | }, |
| 35 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 36 | make_multimode_payment: function(){ |
| 37 | var me = this; |
| 38 | |
| 39 | if(this.frm.doc.change_amount > 0){ |
| 40 | me.payment_val = me.doc.outstanding_amount |
| 41 | } |
| 42 | |
| 43 | this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments"); |
| 44 | this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value(); |
| 45 | this.payments.amount = flt(this.payment_val); |
| 46 | }, |
| 47 | |
| 48 | show_payment_details: function(){ |
| 49 | var me = this; |
| 50 | multimode_payments = $(this.$body).find('.multimode-payments').empty(); |
| 51 | if(this.frm.doc.payments.length){ |
| 52 | $.each(this.frm.doc.payments, function(index, data){ |
| 53 | $(frappe.render_template('payment_details', { |
| 54 | mode_of_payment: data.mode_of_payment, |
| 55 | amount: data.amount, |
| 56 | idx: data.idx, |
| 57 | currency: me.frm.doc.currency, |
| 58 | type: data.type |
| 59 | })).appendTo(multimode_payments) |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 60 | |
| 61 | if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) { |
| 62 | me.idx = data.idx; |
| 63 | me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx})); |
| 64 | me.highlight_selected_row(); |
| 65 | me.bind_amount_change_event(); |
| 66 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 67 | }) |
| 68 | }else{ |
| 69 | $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments) |
| 70 | } |
| 71 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 72 | |
| 73 | set_outstanding_amount: function(){ |
| 74 | this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx})); |
| 75 | this.highlight_selected_row() |
| 76 | this.payment_val = 0.0 |
| 77 | 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] | 78 | //When user first time click on row |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 79 | this.payment_val = flt(this.frm.doc.outstanding_amount) |
| 80 | this.selected_mode.val(format_number(this.payment_val, 2)); |
| 81 | this.update_paid_amount() |
| 82 | }else if(flt(this.selected_mode.val()) > 0){ |
| 83 | //If user click on existing row which has value |
| 84 | this.payment_val = flt(this.selected_mode.val()); |
| 85 | } |
| 86 | this.selected_mode.select() |
| 87 | this.bind_amount_change_event(); |
| 88 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 89 | |
| 90 | bind_keyboard_event: function(){ |
| 91 | var me = this; |
| 92 | this.payment_val = ''; |
| 93 | this.bind_payment_mode_keys_event(); |
| 94 | this.bind_keyboard_keys_event(); |
| 95 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 96 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 97 | bind_payment_mode_keys_event: function(){ |
| 98 | var me = this; |
| 99 | $(this.$body).find('.pos-payment-row').click(function(){ |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 100 | if(me.frm.doc.outstanding_amount > 0){ |
| 101 | me.idx = $(this).attr("idx"); |
| 102 | me.set_outstanding_amount() |
| 103 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 104 | }) |
| 105 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 106 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 107 | highlight_selected_row: function(){ |
| 108 | var me = this; |
| 109 | selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx})); |
| 110 | $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode') |
| 111 | selected_row.addClass('selected-payment-mode') |
| 112 | $(this.$body).find('.amount').attr('disabled', true); |
| 113 | this.selected_mode.attr('disabled', false); |
| 114 | }, |
| 115 | |
| 116 | bind_keyboard_keys_event: function(){ |
| 117 | var me = this; |
| 118 | $(this.$body).find('.pos-keyboard-key').click(function(){ |
| 119 | me.payment_val += $(this).text(); |
| 120 | me.selected_mode.val(format_number(me.payment_val, 2)) |
| 121 | me.idx = me.selected_mode.attr("idx") |
| 122 | me.update_paid_amount() |
| 123 | }) |
| 124 | |
| 125 | $(this.$body).find('.delete-btn').click(function(){ |
| 126 | me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1); |
| 127 | me.selected_mode.val(format_number(me.payment_val, 2)); |
| 128 | me.idx = me.selected_mode.attr("idx") |
| 129 | me.update_paid_amount(); |
| 130 | }) |
| 131 | |
| 132 | }, |
| 133 | |
| 134 | bind_amount_change_event: function(){ |
| 135 | var me = this; |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 136 | this.selected_mode.change(function(){ |
Rohit Waghchaure | 53bea82 | 2016-07-06 16:09:26 +0530 | [diff] [blame] | 137 | me.payment_val = flt($(this).val()) || 0.0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 138 | me.selected_mode.val(format_number(me.payment_val, 2)) |
| 139 | me.idx = me.selected_mode.attr("idx") |
| 140 | me.update_paid_amount() |
| 141 | }) |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 142 | |
| 143 | this.selected_mode.click(function(){ |
| 144 | me.selected_mode.select(); |
| 145 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 146 | }, |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 147 | |
| 148 | clear_amount: function(){ |
| 149 | var me = this; |
| 150 | $(this.$body).find('.clr').click(function(e){ |
| 151 | e.stopPropagation(); |
| 152 | me.idx = $(this).attr("idx"); |
| 153 | me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx})); |
| 154 | me.payment_val = 0.0; |
| 155 | me.selected_mode.val(0.0); |
Rohit Waghchaure | a88dec8 | 2016-07-30 16:49:48 +0530 | [diff] [blame] | 156 | me.highlight_selected_row(); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 157 | me.update_paid_amount(); |
| 158 | }) |
| 159 | }, |
| 160 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 161 | update_paid_amount: function(){ |
| 162 | var me = this; |
| 163 | $.each(this.frm.doc.payments, function(index, data){ |
| 164 | if(cint(me.idx) == cint(data.idx)){ |
| 165 | data.amount = flt(me.selected_mode.val(), 2) |
| 166 | } |
| 167 | }) |
Rohit Waghchaure | 61b4a43 | 2016-07-20 11:21:51 +0530 | [diff] [blame] | 168 | this.calculate_outstanding_amount(false); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 169 | this.show_amounts(); |
| 170 | }, |
| 171 | |
| 172 | show_amounts: function(){ |
| 173 | var me = this; |
| 174 | $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency)); |
| 175 | $(this.$body).find('.change_amount').text(format_currency(this.frm.doc.change_amount, this.frm.doc.currency)) |
| 176 | $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, this.frm.doc.currency)) |
| 177 | this.update_invoice(); |
| 178 | } |
| 179 | }) |