blob: 7437f2a648d40636af7501a6898a58c774177420 [file] [log] [blame]
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2// License: GNU General Public License v3. See license.txt
3
4erpnext.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 Waghchaure6087fe12016-04-09 14:31:09 +053014 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 Waghchauree0934d12016-05-11 15:04:57 +053022 me.dialog.hide()
23 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053024 })
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 Waghchaure9fe40d52016-06-13 21:37:10 +053033 this.clear_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053034 },
35
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053036 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 Waghchaurea88dec82016-07-30 16:49:48 +053060
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 Waghchaure6087fe12016-04-09 14:31:09 +053067 })
68 }else{
69 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
70 }
71 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053072
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 Waghchaure88514a22016-07-19 20:04:44 +053078 //When user first time click on row
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053079 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 Waghchaure6087fe12016-04-09 14:31:09 +053089
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 Waghchaure9fe40d52016-06-13 21:37:10 +053096
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053097 bind_payment_mode_keys_event: function(){
98 var me = this;
99 $(this.$body).find('.pos-payment-row').click(function(){
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530100 if(me.frm.doc.outstanding_amount > 0){
101 me.idx = $(this).attr("idx");
102 me.set_outstanding_amount()
103 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 })
105 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530106
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530107 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 Waghchaurea88dec82016-07-30 16:49:48 +0530136 this.selected_mode.change(function(){
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530137 me.payment_val = flt($(this).val()) || 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530138 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 Waghchaurea88dec82016-07-30 16:49:48 +0530142
143 this.selected_mode.click(function(){
144 me.selected_mode.select();
145 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530146 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530147
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 Waghchaurea88dec82016-07-30 16:49:48 +0530156 me.highlight_selected_row();
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530157 me.update_paid_amount();
158 })
159 },
160
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530161 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 Waghchaure61b4a432016-07-20 11:21:51 +0530168 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530169 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})