blob: 4c23669dbbb563db15af02266d7beeb898c0b843 [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
Faris Ansari1fe891b2021-04-23 08:04:00 +05304erpnext.payments = class payments extends erpnext.stock.StockController {
5 make_payment() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +05306 var me = this;
7
8 this.dialog = new frappe.ui.Dialog({
9 title: 'Payment'
10 });
Faris Ansari1fe891b2021-04-23 08:04:00 +053011
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053012 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();
Deepesh Garge2f83ff2021-05-16 17:46:07 +053016 this.select_text();
Faris Ansari1fe891b2021-04-23 08:04:00 +053017 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053018
Deepesh Gargb07f7d12021-05-20 17:08:57 +053019 select_text() {
Deepesh Gargfd380d32021-05-13 14:04:51 +053020 $(this.$body).find('.form-control').click(function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053021 $(this).select();
Deepesh Gargb07f7d12021-05-20 17:08:57 +053022 });
Faris Ansari1fe891b2021-04-23 08:04:00 +053023 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053024
Ankushfd848472021-06-16 11:14:40 +053025 set_payment_primary_action() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053026 var me = this;
Faris Ansari1fe891b2021-04-23 08:04:00 +053027
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053028 this.dialog.set_primary_action(__("Submit"), function() {
Cesard8b19272018-01-29 15:39:52 +010029 // Allow no ZERO payment
Helkyd3264c132017-11-22 16:06:22 +010030 $.each(me.frm.doc.payments, function (index, data) {
31 if (data.amount != 0) {
32 me.dialog.hide();
33 me.submit_invoice();
34 return;
35 }
Cesard8b19272018-01-29 15:39:52 +010036 });
Helkyd6e3f7892017-11-22 16:12:27 +010037 })
Faris Ansari1fe891b2021-04-23 08:04:00 +053038 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053039
Faris Ansari1fe891b2021-04-23 08:04:00 +053040 make_keyboard(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053041 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()
Faris Ansari1fe891b2021-04-23 08:04:00 +053047 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053048
Faris Ansari1fe891b2021-04-23 08:04:00 +053049 make_multimode_payment(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053050 var me = this;
51
Deepesh Garge2f83ff2021-05-16 17:46:07 +053052 if (this.frm.doc.change_amount > 0) {
53 me.payment_val = me.doc.outstanding_amount;
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053054 }
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);
Faris Ansari1fe891b2021-04-23 08:04:00 +053059 }
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053060
Faris Ansari1fe891b2021-04-23 08:04:00 +053061 show_payment_details(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053062 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +053063 var multimode_payments = $(this.$body).find('.multimode-payments').empty();
Deepesh Gargb07f7d12021-05-20 17:08:57 +053064 if (this.frm.doc.payments.length) {
65 $.each(this.frm.doc.payments, function(index, data) {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053066 $(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 }
Faris Ansari1fe891b2021-04-23 08:04:00 +053084 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053085
Faris Ansari1fe891b2021-04-23 08:04:00 +053086 set_outstanding_amount(){
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053087 this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx}));
Deepesh Garg36d47f92021-05-21 11:16:26 +053088 this.highlight_selected_row();
89 this.payment_val = 0.0;
Deepesh Gargb07f7d12021-05-20 17:08:57 +053090 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));
Deepesh Gargb07f7d12021-05-20 17:08:57 +053094 this.update_payment_amount();
95 } else if (flt(this.selected_mode.val()) > 0) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053096 //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();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530101 }
102
103 bind_keyboard_event(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 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();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530108 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530109
Ankushfd848472021-06-16 11:14:40 +0530110 bind_form_control_event() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530111 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530112 $(this.$body).find('.pos-payment-row').click(function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530113 me.idx = $(this).attr("idx");
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530114 me.set_outstanding_amount();
Deepesh Gargfd380d32021-05-13 14:04:51 +0530115 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530116
Deepesh Gargfd380d32021-05-13 14:04:51 +0530117 $(this.$body).find('.form-control').click(function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530118 me.idx = $(this).attr("idx");
119 me.set_outstanding_amount();
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530120 me.update_paid_amount(true);
Deepesh Gargfd380d32021-05-13 14:04:51 +0530121 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530122
Deepesh Gargfd380d32021-05-13 14:04:51 +0530123 $(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"));
Deepesh Gargfd380d32021-05-13 14:04:51 +0530125 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530126
Deepesh Gargfd380d32021-05-13 14:04:51 +0530127 $(this.$body).find('.change_amount').change(function() {
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530128 me.change_amount(flt($(this).val()), precision("change_amount"));
Deepesh Gargfd380d32021-05-13 14:04:51 +0530129 });
Ankushfd848472021-06-16 11:14:40 +0530130 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530131
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530132 highlight_selected_row() {
Deepesh Gargfd380d32021-05-13 14:04:51 +0530133 var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']", {'idx': this.idx}));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530134 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode');
135 selected_row.addClass('selected-payment-mode');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530136 $(this.$body).find('.amount').attr('disabled', true);
137 this.selected_mode.attr('disabled', false);
Ankushfd848472021-06-16 11:14:40 +0530138 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530139
Ankushfd848472021-06-16 11:14:40 +0530140 bind_numeric_keys_event() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530141 var me = this;
142 $(this.$body).find('.pos-keyboard-key').click(function(){
143 me.payment_val += $(this).text();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530144 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
145 me.idx = me.selected_mode.attr("idx");
146 me.update_paid_amount();
147 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530148
Deepesh Gargfd380d32021-05-13 14:04:51 +0530149 $(this.$body).find('.delete-btn').click(function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530150 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530151 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530152 me.idx = me.selected_mode.attr("idx");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530153 me.update_paid_amount();
154 })
155
Faris Ansari1fe891b2021-04-23 08:04:00 +0530156 }
157
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530158 bind_amount_change_event() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530159 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530160 this.selected_mode.change(function() {
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530161 me.payment_val = flt($(this).val()) || 0.0;
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530162 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
163 me.idx = me.selected_mode.attr("idx");
164 me.update_payment_amount();
165 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530166 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530167
Faris Ansari1fe891b2021-04-23 08:04:00 +0530168 clear_amount() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530169 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530170 $(this.$body).find('.clr').click(function(e) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530171 e.stopPropagation();
172 me.idx = $(this).attr("idx");
173 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
174 me.payment_val = 0.0;
175 me.selected_mode.val(0.0);
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530176 me.highlight_selected_row();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530177 me.update_payment_amount();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530178 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530179 }
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530180
Faris Ansari1fe891b2021-04-23 08:04:00 +0530181 write_off_amount(write_off_amount) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530182 this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount"));
183 this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
184 precision("base_write_off_amount"));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530185 this.calculate_outstanding_amount(false);
186 this.show_amounts();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530187 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530188
Faris Ansari1fe891b2021-04-23 08:04:00 +0530189 change_amount(change_amount) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530190 var me = this;
191
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530192 this.frm.doc.change_amount = flt(change_amount, precision("change_amount"));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530193 this.calculate_write_off_amount();
194 this.show_amounts();
Ankushfd848472021-06-16 11:14:40 +0530195 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530196
Faris Ansari1fe891b2021-04-23 08:04:00 +0530197 update_paid_amount(update_write_off) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530198 var me = this;
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530199 if (in_list(['change_amount', 'write_off_amount'], this.idx)) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530200 var value = me.selected_mode.val();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530201 if (me.idx == 'change_amount') {
202 me.change_amount(value);
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530203 } else {
Rohit Waghchaured39f5332016-08-31 02:09:15 +0530204 if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) {
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530205 value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530206 }
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530207 me.write_off_amount(value);
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530208 }
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530209 } else {
210 this.update_payment_amount();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530211 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530212 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530213
Faris Ansari1fe891b2021-04-23 08:04:00 +0530214 update_payment_amount(){
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530215 var me = this;
216
Deepesh Gargfd380d32021-05-13 14:04:51 +0530217 $.each(this.frm.doc.payments, function(index, data) {
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530218 if (cint(me.idx) == cint(data.idx)) {
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530219 data.amount = flt(me.selected_mode.val(), 2);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530220 }
221 })
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530222
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530223 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530224 this.show_amounts();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530225 }
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530226
Faris Ansari1fe891b2021-04-23 08:04:00 +0530227 show_amounts(){
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530228 var me = this;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530229 $(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 +0530230 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530231 $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency));
232 $(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 +0530233 this.update_invoice();
234 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530235}