blob: 7ac2b7baeada06268cca2b7f56018314ac241014 [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() {
23 frappe.confirm(__("Do you really want to submit the invoice?"), function () {
24 me.write_off_amount();
25 me.dialog.hide();
26 })
27 })
28 },
29
30 make_keyboard: function(){
31 var me = this;
32 $(this.$body).empty();
33 $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc))
34 this.show_payment_details();
35 this.bind_keyboard_event()
36 },
37
38 pay_amount: function(){
39 var me = this;
40 this.make_multimode_payment();
41 this.calculate_outstanding_amount()
42 this.show_payment_details();
43 },
44
45 make_multimode_payment: function(){
46 var me = this;
47
48 if(this.frm.doc.change_amount > 0){
49 me.payment_val = me.doc.outstanding_amount
50 }
51
52 this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments");
53 this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value();
54 this.payments.amount = flt(this.payment_val);
55 },
56
57 show_payment_details: function(){
58 var me = this;
59 multimode_payments = $(this.$body).find('.multimode-payments').empty();
60 if(this.frm.doc.payments.length){
61 $.each(this.frm.doc.payments, function(index, data){
62 $(frappe.render_template('payment_details', {
63 mode_of_payment: data.mode_of_payment,
64 amount: data.amount,
65 idx: data.idx,
66 currency: me.frm.doc.currency,
67 type: data.type
68 })).appendTo(multimode_payments)
69 })
70 }else{
71 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
72 }
73 },
74
75 bind_keyboard_event: function(){
76 var me = this;
77 this.payment_val = '';
78 this.bind_payment_mode_keys_event();
79 this.bind_keyboard_keys_event();
80 },
81
82 bind_payment_mode_keys_event: function(){
83 var me = this;
84 $(this.$body).find('.pos-payment-row').click(function(){
85 me.idx = $(this).attr("idx");
86 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
87 me.highlight_selected_row()
88 me.payment_val = 0.0
89 if(me.frm.doc.outstanding_amount > 0 && flt(me.selected_mode.val()) == 0.0){
90 //When user first time click on row
91 me.payment_val = flt(me.frm.doc.outstanding_amount)
92 me.selected_mode.val(format_number(me.payment_val, 2));
93 me.update_paid_amount()
94 me.bind_amount_change_event();
95 }else if(flt(me.selected_mode.val()) > 0){
96 //If user click on existing row which has value
97 me.payment_val = flt(me.selected_mode.val());
98 }
99 me.selected_mode.select()
100 })
101 },
102
103 highlight_selected_row: function(){
104 var me = this;
105 selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx}));
106 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode')
107 selected_row.addClass('selected-payment-mode')
108 $(this.$body).find('.amount').attr('disabled', true);
109 this.selected_mode.attr('disabled', false);
110 },
111
112 bind_keyboard_keys_event: function(){
113 var me = this;
114 $(this.$body).find('.pos-keyboard-key').click(function(){
115 me.payment_val += $(this).text();
116 me.selected_mode.val(format_number(me.payment_val, 2))
117 me.idx = me.selected_mode.attr("idx")
118 me.update_paid_amount()
119 })
120
121 $(this.$body).find('.delete-btn').click(function(){
122 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
123 me.selected_mode.val(format_number(me.payment_val, 2));
124 me.idx = me.selected_mode.attr("idx")
125 me.update_paid_amount();
126 })
127
128 },
129
130 bind_amount_change_event: function(){
131 var me = this;
132 me.selected_mode.change(function(){
133 me.payment_val = $(this).val() || 0.0;
134 me.selected_mode.val(format_number(me.payment_val, 2))
135 me.idx = me.selected_mode.attr("idx")
136 me.update_paid_amount()
137 })
138 },
139
140 update_paid_amount: function(){
141 var me = this;
142 $.each(this.frm.doc.payments, function(index, data){
143 if(cint(me.idx) == cint(data.idx)){
144 data.amount = flt(me.selected_mode.val(), 2)
145 }
146 })
147 this.calculate_outstanding_amount();
148 this.show_amounts();
149 },
150
151 show_amounts: function(){
152 var me = this;
153 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
154 $(this.$body).find('.change_amount').text(format_currency(this.frm.doc.change_amount, this.frm.doc.currency))
155 $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, this.frm.doc.currency))
156 this.update_invoice();
157 }
158})