blob: 554566ad950786080ad41bb7621b2443fa0910f3 [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() {
Helkyd3264c132017-11-22 16:06:22 +010030 //Allow no ZERO payment
31 $.each(me.frm.doc.payments, function (index, data) {
32 if (data.amount != 0) {
33 me.dialog.hide();
34 me.submit_invoice();
35 return;
36 }
37 }); })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053038 },
39
40 make_keyboard: function(){
41 var me = this;
42 $(this.$body).empty();
43 $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc))
44 this.show_payment_details();
45 this.bind_keyboard_event()
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053046 this.clear_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053047 },
48
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053049 make_multimode_payment: function(){
50 var me = this;
51
52 if(this.frm.doc.change_amount > 0){
53 me.payment_val = me.doc.outstanding_amount
54 }
55
56 this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments");
57 this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value();
58 this.payments.amount = flt(this.payment_val);
59 },
60
61 show_payment_details: function(){
62 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +053063 var multimode_payments = $(this.$body).find('.multimode-payments').empty();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053064 if(this.frm.doc.payments.length){
65 $.each(this.frm.doc.payments, function(index, data){
66 $(frappe.render_template('payment_details', {
67 mode_of_payment: data.mode_of_payment,
68 amount: data.amount,
69 idx: data.idx,
70 currency: me.frm.doc.currency,
71 type: data.type
72 })).appendTo(multimode_payments)
Rohit Waghchaurea88dec82016-07-30 16:49:48 +053073
74 if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) {
75 me.idx = data.idx;
76 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
77 me.highlight_selected_row();
78 me.bind_amount_change_event();
79 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053080 })
81 }else{
82 $("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
83 }
84 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053085
86 set_outstanding_amount: function(){
87 this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx}));
88 this.highlight_selected_row()
89 this.payment_val = 0.0
90 if(this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0){
Rohit Waghchaure88514a22016-07-19 20:04:44 +053091 //When user first time click on row
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +053092 this.payment_val = flt(this.frm.doc.outstanding_amount / this.frm.doc.conversion_rate, precision("outstanding_amount"))
Rohit Waghchaure609e2b42016-08-31 02:04:37 +053093 this.selected_mode.val(format_currency(this.payment_val, this.frm.doc.currency));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053094 this.update_payment_amount()
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053095 }else if(flt(this.selected_mode.val()) > 0){
96 //If user click on existing row which has value
97 this.payment_val = flt(this.selected_mode.val());
98 }
99 this.selected_mode.select()
100 this.bind_amount_change_event();
101 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530102
103 bind_keyboard_event: function(){
104 var me = this;
105 this.payment_val = '';
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530106 this.bind_form_control_event();
107 this.bind_numeric_keys_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530108 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530109
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530110 bind_form_control_event: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530111 var me = this;
112 $(this.$body).find('.pos-payment-row').click(function(){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530113 me.idx = $(this).attr("idx");
114 me.set_outstanding_amount()
115 })
116
117 $(this.$body).find('.form-control').click(function(){
118 me.idx = $(this).attr("idx");
119 me.set_outstanding_amount();
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530120 me.update_paid_amount(true);
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530121 })
122
123 $(this.$body).find('.write_off_amount').change(function(){
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530124 me.write_off_amount(flt($(this).val()), precision("write_off_amount"));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530125 })
126
127 $(this.$body).find('.change_amount').change(function(){
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530128 me.change_amount(flt($(this).val()), precision("change_amount"));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 })
130 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530131
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530132 highlight_selected_row: function(){
133 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530134 var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']",{'idx': this.idx}));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530135 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode')
136 selected_row.addClass('selected-payment-mode')
137 $(this.$body).find('.amount').attr('disabled', true);
138 this.selected_mode.attr('disabled', false);
139 },
140
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530141 bind_numeric_keys_event: function(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530142 var me = this;
143 $(this.$body).find('.pos-keyboard-key').click(function(){
144 me.payment_val += $(this).text();
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530145 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530146 me.idx = me.selected_mode.attr("idx")
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530147 me.update_paid_amount()
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530148 })
149
150 $(this.$body).find('.delete-btn').click(function(){
151 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530152 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530153 me.idx = me.selected_mode.attr("idx")
154 me.update_paid_amount();
155 })
156
157 },
158
159 bind_amount_change_event: function(){
160 var me = this;
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530161 this.selected_mode.change(function(){
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530162 me.payment_val = flt($(this).val()) || 0.0;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530163 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency))
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530164 me.idx = me.selected_mode.attr("idx")
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530165 me.update_payment_amount()
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530166 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530167 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530168
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530169 clear_amount: function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530170 var me = this;
171 $(this.$body).find('.clr').click(function(e){
172 e.stopPropagation();
173 me.idx = $(this).attr("idx");
174 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
175 me.payment_val = 0.0;
176 me.selected_mode.val(0.0);
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530177 me.highlight_selected_row();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530178 me.update_payment_amount();
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530179 })
180 },
181
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530182 write_off_amount: function(write_off_amount) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530183 var me = this;
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530184
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530185 this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount"));
186 this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
187 precision("base_write_off_amount"));
188 this.calculate_outstanding_amount(false)
189 this.show_amounts()
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530190 },
191
192 change_amount: function(change_amount) {
193 var me = this;
194
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530195 this.frm.doc.change_amount = flt(change_amount, precision("change_amount"));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530196 this.calculate_write_off_amount()
197 this.show_amounts()
198 },
199
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530200 update_paid_amount: function(update_write_off) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530201 var me = this;
202 if(in_list(['change_amount', 'write_off_amount'], this.idx)){
Faris Ansariab74ca72017-05-30 12:54:42 +0530203 var value = me.selected_mode.val();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530204 if(me.idx == 'change_amount'){
205 me.change_amount(value)
206 } else{
Rohit Waghchaured39f5332016-08-31 02:09:15 +0530207 if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) {
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530208 value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530209 }
210 me.write_off_amount(value)
211 }
212 }else{
213 this.update_payment_amount()
214 }
215 },
216
217 update_payment_amount: function(){
218 var me = this;
219
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530220 $.each(this.frm.doc.payments, function(index, data){
221 if(cint(me.idx) == cint(data.idx)){
222 data.amount = flt(me.selected_mode.val(), 2)
223 }
224 })
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530225
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530226 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530227 this.show_amounts();
228 },
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530229
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530230 show_amounts: function(){
231 var me = this;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530232 $(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 +0530233 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530234 $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency))
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530235 $(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 +0530236 this.update_invoice();
237 }
238})