blob: 9464f50f67b6bef32a5f64d9fb689d8ee27f7e12 [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;
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 Waghchauree0934d12016-05-11 15:04:57 +053023 me.dialog.hide()
24 me.write_off_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053025 })
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 Waghchaure9fe40d52016-06-13 21:37:10 +053034 this.clear_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053035 },
36
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053037 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 Waghchaure9fe40d52016-06-13 21:37:10 +053061
62 if (data.type == 'Cash' && me.frm.doc.outstanding_amount > 0) {
63 me.idx = data.idx;
64 me.set_outstanding_amount();
65 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 })
67 }else{
68 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
69 }
70 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053071
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){
Rohit Waghchaure88514a22016-07-19 20:04:44 +053077 //When user first time click on row
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053078 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 Waghchaure6087fe12016-04-09 14:31:09 +053088
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 Waghchaure9fe40d52016-06-13 21:37:10 +053095
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053096 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 Waghchaure9fe40d52016-06-13 21:37:10 +0530100 me.set_outstanding_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530101 })
102 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530103
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 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(){
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530134 me.payment_val = flt($(this).val()) || 0.0;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530135 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 Waghchaure9fe40d52016-06-13 21:37:10 +0530140
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 Waghchaure6087fe12016-04-09 14:31:09 +0530153 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})