blob: ddf87068097b7567d647b2987ddb2f58cce635d8 [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 });
Deepesh Gargc26de282020-11-02 19:57:27 +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();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +053017 },
18
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 });
Deepesh Garg36d47f92021-05-21 11:16:26 +053023 },
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053024
Deepesh Gargfd380d32021-05-13 14:04:51 +053025 set_payment_primary_action: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053026 var me = this;
Deepesh Gargc26de282020-11-02 19:57:27 +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 })
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053038 },
39
Deepesh Gargfd380d32021-05-13 14:04:51 +053040 make_keyboard: function() {
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();
Deepesh Garge2f83ff2021-05-16 17:46:07 +053045 this.bind_keyboard_event();
46 this.clear_amount();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +053047 },
48
Deepesh Gargfd380d32021-05-13 14:04:51 +053049 make_multimode_payment: function() {
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);
59 },
60
Deepesh Gargfd380d32021-05-13 14:04:51 +053061 show_payment_details: function() {
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 }
84 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +053085
Deepesh Gargfd380d32021-05-13 14:04:51 +053086 set_outstanding_amount: function() {
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();
101 },
Deepesh Gargc26de282020-11-02 19:57:27 +0530102
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530103 bind_keyboard_event() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530104 this.payment_val = '';
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530105 this.bind_form_control_event();
106 this.bind_numeric_keys_event();
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530107 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530108
Deepesh Gargfd380d32021-05-13 14:04:51 +0530109 bind_form_control_event: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530110 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530111 $(this.$body).find('.pos-payment-row').click(function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530112 me.idx = $(this).attr("idx");
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530113 me.set_outstanding_amount();
Deepesh Gargfd380d32021-05-13 14:04:51 +0530114 });
Deepesh Gargc26de282020-11-02 19:57:27 +0530115
Deepesh Gargfd380d32021-05-13 14:04:51 +0530116 $(this.$body).find('.form-control').click(function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530117 me.idx = $(this).attr("idx");
118 me.set_outstanding_amount();
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530119 me.update_paid_amount(true);
Deepesh Gargfd380d32021-05-13 14:04:51 +0530120 });
Deepesh Gargc26de282020-11-02 19:57:27 +0530121
Deepesh Gargfd380d32021-05-13 14:04:51 +0530122 $(this.$body).find('.write_off_amount').change(function() {
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530123 me.write_off_amount(flt($(this).val()), precision("write_off_amount"));
Deepesh Gargfd380d32021-05-13 14:04:51 +0530124 });
Deepesh Gargc26de282020-11-02 19:57:27 +0530125
Deepesh Gargfd380d32021-05-13 14:04:51 +0530126 $(this.$body).find('.change_amount').change(function() {
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530127 me.change_amount(flt($(this).val()), precision("change_amount"));
Deepesh Gargfd380d32021-05-13 14:04:51 +0530128 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530129 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530130
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530131 highlight_selected_row() {
Deepesh Gargfd380d32021-05-13 14:04:51 +0530132 var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']", {'idx': this.idx}));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530133 $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode');
134 selected_row.addClass('selected-payment-mode');
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530135 $(this.$body).find('.amount').attr('disabled', true);
136 this.selected_mode.attr('disabled', false);
137 },
Deepesh Gargc26de282020-11-02 19:57:27 +0530138
Deepesh Gargfd380d32021-05-13 14:04:51 +0530139 bind_numeric_keys_event: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530140 var me = this;
141 $(this.$body).find('.pos-keyboard-key').click(function(){
142 me.payment_val += $(this).text();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530143 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
144 me.idx = me.selected_mode.attr("idx");
145 me.update_paid_amount();
146 });
Deepesh Gargc26de282020-11-02 19:57:27 +0530147
Deepesh Gargfd380d32021-05-13 14:04:51 +0530148 $(this.$body).find('.delete-btn').click(function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530149 me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530150 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530151 me.idx = me.selected_mode.attr("idx");
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530152 me.update_paid_amount();
153 })
154
155 },
Deepesh Gargc26de282020-11-02 19:57:27 +0530156
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530157 bind_amount_change_event() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530158 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530159 this.selected_mode.change(function() {
Rohit Waghchaure53bea822016-07-06 16:09:26 +0530160 me.payment_val = flt($(this).val()) || 0.0;
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530161 me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
162 me.idx = me.selected_mode.attr("idx");
163 me.update_payment_amount();
164 });
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530165 },
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530166
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530167 clear_amount: function() {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530168 var me = this;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530169 $(this.$body).find('.clr').click(function(e) {
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530170 e.stopPropagation();
171 me.idx = $(this).attr("idx");
172 me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
173 me.payment_val = 0.0;
174 me.selected_mode.val(0.0);
Rohit Waghchaurea88dec82016-07-30 16:49:48 +0530175 me.highlight_selected_row();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530176 me.update_payment_amount();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530177 });
Rohit Waghchaure9fe40d52016-06-13 21:37:10 +0530178 },
179
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530180 write_off_amount(write_off_amount) {
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530181 this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount"));
182 this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
183 precision("base_write_off_amount"));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530184 this.calculate_outstanding_amount(false);
185 this.show_amounts();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530186 },
187
188 change_amount: function(change_amount) {
189 var me = this;
190
Rohit Waghchaure713e2b72016-08-19 15:11:36 +0530191 this.frm.doc.change_amount = flt(change_amount, precision("change_amount"));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530192 this.calculate_write_off_amount();
193 this.show_amounts();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530194 },
195
Rohit Waghchaureea6d7e92016-08-22 19:49:17 +0530196 update_paid_amount: function(update_write_off) {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530197 var me = this;
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530198 if (in_list(['change_amount', 'write_off_amount'], this.idx)) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530199 var value = me.selected_mode.val();
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530200 if (me.idx == 'change_amount') {
201 me.change_amount(value);
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530202 } else {
Rohit Waghchaured39f5332016-08-31 02:09:15 +0530203 if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) {
Rohit Waghchaurea57bf5e2016-08-30 00:41:33 +0530204 value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx));
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530205 }
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530206 me.write_off_amount(value);
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530207 }
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530208 } else {
209 this.update_payment_amount();
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530210 }
211 },
212
Deepesh Gargfd380d32021-05-13 14:04:51 +0530213 update_payment_amount: function() {
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530214 var me = this;
215
Deepesh Gargfd380d32021-05-13 14:04:51 +0530216 $.each(this.frm.doc.payments, function(index, data) {
Deepesh Gargb07f7d12021-05-20 17:08:57 +0530217 if (cint(me.idx) == cint(data.idx)) {
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530218 data.amount = flt(me.selected_mode.val(), 2);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530219 }
220 })
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530221
Rohit Waghchaure61b4a432016-07-20 11:21:51 +0530222 this.calculate_outstanding_amount(false);
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530223 this.show_amounts();
224 },
Rohit Waghchaurebaef2622016-08-05 15:41:36 +0530225
Deepesh Gargfd380d32021-05-13 14:04:51 +0530226 show_amounts: function() {
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530227 var me = this;
Rohit Waghchaure609e2b42016-08-31 02:04:37 +0530228 $(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 +0530229 $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
Deepesh Garge2f83ff2021-05-16 17:46:07 +0530230 $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency));
231 $(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 +0530232 this.update_invoice();
233 }
234})