blob: f5527b1d04c2ccaf621eafbfbd738d34c4983ad1 [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()
34 },
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 },
65
66 bind_keyboard_event: function(){
67 var me = this;
68 this.payment_val = '';
69 this.bind_payment_mode_keys_event();
70 this.bind_keyboard_keys_event();
71 },
72
73 bind_payment_mode_keys_event: function(){
74 var me = this;
75 $(this.$body).find('.pos-payment-row').click(function(){
76 me.idx = $(this).attr("idx");
77 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
78 me.highlight_selected_row()
79 me.payment_val = 0.0
80 if(me.frm.doc.outstanding_amount > 0 && flt(me.selected_mode.val()) == 0.0){
81 //When user first time click on row
82 me.payment_val = flt(me.frm.doc.outstanding_amount)
83 me.selected_mode.val(format_number(me.payment_val, 2));
84 me.update_paid_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053085 }else if(flt(me.selected_mode.val()) > 0){
86 //If user click on existing row which has value
87 me.payment_val = flt(me.selected_mode.val());
88 }
89 me.selected_mode.select()
Rohit Waghchauree0934d12016-05-11 15:04:57 +053090 me.bind_amount_change_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053091 })
92 },
93
94 highlight_selected_row: function(){
95 var me = this;
96 selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx}));
97 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode')
98 selected_row.addClass('selected-payment-mode')
99 $(this.$body).find('.amount').attr('disabled', true);
100 this.selected_mode.attr('disabled', false);
101 },
102
103 bind_keyboard_keys_event: function(){
104 var me = this;
105 $(this.$body).find('.pos-keyboard-key').click(function(){
106 me.payment_val += $(this).text();
107 me.selected_mode.val(format_number(me.payment_val, 2))
108 me.idx = me.selected_mode.attr("idx")
109 me.update_paid_amount()
110 })
111
112 $(this.$body).find('.delete-btn').click(function(){
113 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
114 me.selected_mode.val(format_number(me.payment_val, 2));
115 me.idx = me.selected_mode.attr("idx")
116 me.update_paid_amount();
117 })
118
119 },
120
121 bind_amount_change_event: function(){
122 var me = this;
123 me.selected_mode.change(function(){
124 me.payment_val = $(this).val() || 0.0;
125 me.selected_mode.val(format_number(me.payment_val, 2))
126 me.idx = me.selected_mode.attr("idx")
127 me.update_paid_amount()
128 })
129 },
130
131 update_paid_amount: function(){
132 var me = this;
133 $.each(this.frm.doc.payments, function(index, data){
134 if(cint(me.idx) == cint(data.idx)){
135 data.amount = flt(me.selected_mode.val(), 2)
136 }
137 })
138 this.calculate_outstanding_amount();
139 this.show_amounts();
140 },
141
142 show_amounts: function(){
143 var me = this;
144 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
145 $(this.$body).find('.change_amount').text(format_currency(this.frm.doc.change_amount, this.frm.doc.currency))
146 $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, this.frm.doc.currency))
147 this.update_invoice();
148 }
149})