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