blob: 77209e093a8408c7c307318115e1680c65a246c5 [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();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053016 this.select_text()
17 },
18
19 select_text: function(){
20 var me = this;
21 $(this.$body).find('.form-control').click(function(){
22 $(this).select();
23 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053024 },
25
26 set_payment_primary_action: function(){
27 var me = this;
28
29 this.dialog.set_primary_action(__("Submit"), function() {
Rohit Waghchauree0934d12016-05-11 15:04:57 +053030 me.dialog.hide()
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053031 me.submit_invoice()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053032 })
33 },
34
35 make_keyboard: function(){
36 var me = this;
37 $(this.$body).empty();
38 $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc))
39 this.show_payment_details();
40 this.bind_keyboard_event()
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053041 this.clear_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053042 },
43
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053044 make_multimode_payment: function(){
45 var me = this;
46
47 if(this.frm.doc.change_amount > 0){
48 me.payment_val = me.doc.outstanding_amount
49 }
50
51 this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments");
52 this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value();
53 this.payments.amount = flt(this.payment_val);
54 },
55
56 show_payment_details: function(){
57 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +053058 var multimode_payments = $(this.$body).find('.multimode-payments').empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053059 if(this.frm.doc.payments.length){
60 $.each(this.frm.doc.payments, function(index, data){
61 $(frappe.render_template('payment_details', {
62 mode_of_payment: data.mode_of_payment,
63 amount: data.amount,
64 idx: data.idx,
65 currency: me.frm.doc.currency,
66 type: data.type
67 })).appendTo(multimode_payments)
Rohit Waghchaurea88dec82016-07-30 16:49:48 +053068
69 if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) {
70 me.idx = data.idx;
71 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
72 me.highlight_selected_row();
73 me.bind_amount_change_event();
74 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053075 })
76 }else{
77 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
78 }
79 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053080
81 set_outstanding_amount: function(){
82 this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx}));
83 this.highlight_selected_row()
84 this.payment_val = 0.0
85 if(this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0){
Rohit Waghchaure88514a22016-07-19 20:04:44 +053086 //When user first time click on row
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +053087 this.payment_val = flt(this.frm.doc.outstanding_amount / this.frm.doc.conversion_rate, precision("outstanding_amount"))
Rohit Waghchaure609e2b42016-08-31 02:04:37 +053088 this.selected_mode.val(format_currency(this.payment_val, this.frm.doc.currency));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053089 this.update_payment_amount()
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053090 }else if(flt(this.selected_mode.val()) > 0){
91 //If user click on existing row which has value
92 this.payment_val = flt(this.selected_mode.val());
93 }
94 this.selected_mode.select()
95 this.bind_amount_change_event();
96 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053097
98 bind_keyboard_event: function(){
99 var me = this;
100 this.payment_val = '';
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530101 this.bind_form_control_event();
102 this.bind_numeric_keys_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530103 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530104
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530105 bind_form_control_event: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530106 var me = this;
107 $(this.$body).find('.pos-payment-row').click(function(){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530108 me.idx = $(this).attr("idx");
109 me.set_outstanding_amount()
110 })
111
112 $(this.$body).find('.form-control').click(function(){
113 me.idx = $(this).attr("idx");
114 me.set_outstanding_amount();
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530115 me.update_paid_amount(true);
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530116 })
117
118 $(this.$body).find('.write_off_amount').change(function(){
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530119 me.write_off_amount(flt($(this).val()), precision("write_off_amount"));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530120 })
121
122 $(this.$body).find('.change_amount').change(function(){
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530123 me.change_amount(flt($(this).val()), precision("change_amount"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530124 })
125 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530126
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530127 highlight_selected_row: function(){
128 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530129 var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx}));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530130 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode')
131 selected_row.addClass('selected-payment-mode')
132 $(this.$body).find('.amount').attr('disabled', true);
133 this.selected_mode.attr('disabled', false);
134 },
135
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530136 bind_numeric_keys_event: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530137 var me = this;
138 $(this.$body).find('.pos-keyboard-key').click(function(){
139 me.payment_val += $(this).text();
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530140 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530141 me.idx = me.selected_mode.attr("idx")
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530142 me.update_paid_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530143 })
144
145 $(this.$body).find('.delete-btn').click(function(){
146 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530147 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530148 me.idx = me.selected_mode.attr("idx")
149 me.update_paid_amount();
150 })
151
152 },
153
154 bind_amount_change_event: function(){
155 var me = this;
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530156 this.selected_mode.change(function(){
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530157 me.payment_val = flt($(this).val()) || 0.0;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530158 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530159 me.idx = me.selected_mode.attr("idx")
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530160 me.update_payment_amount()
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530161 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530162 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530163
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530164 clear_amount: function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530165 var me = this;
166 $(this.$body).find('.clr').click(function(e){
167 e.stopPropagation();
168 me.idx = $(this).attr("idx");
169 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
170 me.payment_val = 0.0;
171 me.selected_mode.val(0.0);
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530172 me.highlight_selected_row();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530173 me.update_payment_amount();
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530174 })
175 },
176
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530177 write_off_amount: function(write_off_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530178 var me = this;
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530179
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530180 this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount"));
181 this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
182 precision("base_write_off_amount"));
183 this.calculate_outstanding_amount(false)
184 this.show_amounts()
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530185 },
186
187 change_amount: function(change_amount) {
188 var me = this;
189
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530190 this.frm.doc.change_amount = flt(change_amount, precision("change_amount"));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530191 this.calculate_write_off_amount()
192 this.show_amounts()
193 },
194
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530195 update_paid_amount: function(update_write_off) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530196 var me = this;
197 if(in_list(['change_amount', 'write_off_amount'], this.idx)){
Faris Ansariab74ca72017-05-30 12:54:42 +0530198 var value = me.selected_mode.val();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530199 if(me.idx == 'change_amount'){
200 me.change_amount(value)
201 } else{
Rohit Waghchaured39f5332016-08-31 02:09:15 +0530202 if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) {
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530203 value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530204 }
205 me.write_off_amount(value)
206 }
207 }else{
208 this.update_payment_amount()
209 }
210 },
211
212 update_payment_amount: function(){
213 var me = this;
214
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530215 $.each(this.frm.doc.payments, function(index, data){
216 if(cint(me.idx) == cint(data.idx)){
217 data.amount = flt(me.selected_mode.val(), 2)
218 }
219 })
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530220
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530221 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530222 this.show_amounts();
223 },
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530224
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530225 show_amounts: function(){
226 var me = this;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530227 $(this.$body).find(".write_off_amount").val(format_currency(this.frm.doc.write_off_amount, this.frm.doc.currency));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530228 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530229 $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency))
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530230 $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, frappe.get_doc(":Company", this.frm.doc.company).default_currency))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530231 this.update_invoice();
232 }
233})