blob: 0638b84ffa8390b7a194366ce82a34ad827f1f1f [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)
60 })
61 }else{
62 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
63 }
64 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053065
66 set_outstanding_amount: function(){
67 this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx}));
68 this.highlight_selected_row()
69 this.payment_val = 0.0
70 if(this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0){
Rohit Waghchaure88514a22016-07-19 20:04:44 +053071 //When user first time click on row
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053072 this.payment_val = flt(this.frm.doc.outstanding_amount)
73 this.selected_mode.val(format_number(this.payment_val, 2));
74 this.update_paid_amount()
75 }else if(flt(this.selected_mode.val()) > 0){
76 //If user click on existing row which has value
77 this.payment_val = flt(this.selected_mode.val());
78 }
79 this.selected_mode.select()
80 this.bind_amount_change_event();
81 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053082
83 bind_keyboard_event: function(){
84 var me = this;
85 this.payment_val = '';
86 this.bind_payment_mode_keys_event();
87 this.bind_keyboard_keys_event();
88 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053089
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053090 bind_payment_mode_keys_event: function(){
91 var me = this;
92 $(this.$body).find('.pos-payment-row').click(function(){
93 me.idx = $(this).attr("idx");
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053094 me.set_outstanding_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053095 })
96 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053097
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053098 highlight_selected_row: function(){
99 var me = this;
100 selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx}));
101 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode')
102 selected_row.addClass('selected-payment-mode')
103 $(this.$body).find('.amount').attr('disabled', true);
104 this.selected_mode.attr('disabled', false);
105 },
106
107 bind_keyboard_keys_event: function(){
108 var me = this;
109 $(this.$body).find('.pos-keyboard-key').click(function(){
110 me.payment_val += $(this).text();
111 me.selected_mode.val(format_number(me.payment_val, 2))
112 me.idx = me.selected_mode.attr("idx")
113 me.update_paid_amount()
114 })
115
116 $(this.$body).find('.delete-btn').click(function(){
117 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
118 me.selected_mode.val(format_number(me.payment_val, 2));
119 me.idx = me.selected_mode.attr("idx")
120 me.update_paid_amount();
121 })
122
123 },
124
125 bind_amount_change_event: function(){
126 var me = this;
127 me.selected_mode.change(function(){
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530128 me.payment_val = flt($(this).val()) || 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 me.selected_mode.val(format_number(me.payment_val, 2))
130 me.idx = me.selected_mode.attr("idx")
131 me.update_paid_amount()
132 })
133 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530134
135 clear_amount: function(){
136 var me = this;
137 $(this.$body).find('.clr').click(function(e){
138 e.stopPropagation();
139 me.idx = $(this).attr("idx");
140 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
141 me.payment_val = 0.0;
142 me.selected_mode.val(0.0);
143 me.update_paid_amount();
144 })
145 },
146
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530147 update_paid_amount: function(){
148 var me = this;
149 $.each(this.frm.doc.payments, function(index, data){
150 if(cint(me.idx) == cint(data.idx)){
151 data.amount = flt(me.selected_mode.val(), 2)
152 }
153 })
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530154 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530155 this.show_amounts();
156 },
157
158 show_amounts: function(){
159 var me = this;
160 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
161 $(this.$body).find('.change_amount').text(format_currency(this.frm.doc.change_amount, this.frm.doc.currency))
162 $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, this.frm.doc.currency))
163 this.update_invoice();
164 }
165})