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