1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-26 18:37:12 +00:00

Transfers: switch between transfer and gift

This commit is contained in:
Yohann D'ANELLO 2020-03-13 01:57:32 +01:00
parent bb0fc8c2cc
commit 56a2d482c9

View File

@ -6,8 +6,15 @@ SPDX-License-Identifier: GPL-2.0-or-later
{% load i18n static %} {% load i18n static %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-xl-12">
<button id="switch_mode" class="form-control btn btn-secondary">Passer en mode transfert</button>
</div>
</div>
<div class="row">
<div class="col-md-6" id="emitters_div" style="display: none;">
<div class="card border-success shadow mb-4"> <div class="card border-success shadow mb-4">
<div class="card-header"> <div class="card-header">
<p class="card-text font-weight-bold"> <p class="card-text font-weight-bold">
@ -24,7 +31,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
</div> </div>
</div> </div>
<div class="col-md-6"> <div class="col-md-12" id="dests_div">
<div class="card border-info shadow mb-4"> <div class="card border-info shadow mb-4">
<div class="card-header"> <div class="card-header">
<p class="card-text font-weight-bold"> <p class="card-text font-weight-bold">
@ -70,6 +77,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
var sources_notes_display = []; var sources_notes_display = [];
var dests = []; var dests = [];
var dests_notes_display = []; var dests_notes_display = [];
var transfer_mode = false;
$(document).ready(function() { $(document).ready(function() {
autoCompleteNote("source_note", "source_alias_matched", "source_note_list", sources, sources_notes_display, autoCompleteNote("source_note", "source_alias_matched", "source_note_list", sources, sources_notes_display,
@ -78,7 +86,76 @@ SPDX-License-Identifier: GPL-2.0-or-later
"dest_alias", "dest_note"); "dest_alias", "dest_note");
}); });
$("#switch_mode").click(function () {
transfer_mode ^= true;
if (transfer_mode) {
$("#switch_mode").text("Passer en mode virement");
$("#emitters_div").show();
$("#dests_div").attr('class', 'col-md-6');
}
else {
$("#switch_mode").text("Passer en mode transfert");
$("#emitters_div").hide();
$("#dests_div").attr('class', 'col-md-12');
}
});
$("#transfer").click(function() { $("#transfer").click(function() {
if (sources.length === 0) {
dests_notes_display.forEach(function (dest) {
$.post("/api/note/transaction/transaction/",
{
"csrfmiddlewaretoken": CSRF_TOKEN,
"quantity": dest[3],
"amount": $("#amount").val(),
"reason": $("#reason").val() + " (Don)",
"valid": true,
"polymorphic_ctype": {{ polymorphic_ctype }},
"resourcetype": "Transaction",
"source": {{ user.note.id }},
"destination": dest[1]
}, function () {
let msgDiv = $("#messages");
let html = msgDiv.html();
html += "<div class=\"alert alert-success\">Le transfert de "
+ pretty_money(dest[3] * $("#amount").val()) + " de votre note "
+ " vers la note " + dest[0] + " a été fait avec succès !</div>\n";
msgDiv.html(html);
sources_notes_display.length = 0;
sources.length = 0;
dests_notes_display.length = 0;
dests.length = 0;
$("#source_note_list").html("");
$("#dest_note_list").html("");
$("#source_alias_matched").html("");
$("#dest_alias_matched").html("");
$("#amount").val("");
$("#reason").val("");
refreshBalance();
}).fail(function (err) {
let msgDiv = $("#messages");
let html = msgDiv.html();
html += "<div class=\"alert alert-danger\">Le transfert de "
+ pretty_money(dest[3] * $("#amount").val()) + " de votre note "
+ " vers la note " + dest[0] + " a échoué : " + err.responseText + "</div>\n";
msgDiv.html(html);
sources_notes_display.length = 0;
sources.length = 0;
dests_notes_display.length = 0;
dests.length = 0;
$("#source_note_list").html("");
$("#dest_note_list").html("");
$("#source_alias_matched").html("");
$("#dest_alias_matched").html("");
$("#amount").val("");
$("#reason").val("");
refreshBalance();
});
});
}
else {
sources_notes_display.forEach(function (source) { sources_notes_display.forEach(function (source) {
dests_notes_display.forEach(function (dest) { dests_notes_display.forEach(function (dest) {
$.post("/api/note/transaction/transaction/", $.post("/api/note/transaction/transaction/",
@ -93,25 +170,13 @@ SPDX-License-Identifier: GPL-2.0-or-later
"source": source[1], "source": source[1],
"destination": dest[1] "destination": dest[1]
}, function () { }, function () {
sources_notes_display.length = 0;
sources.length = 0;
dests_notes_display.length = 0;
dests.length = 0;
$("#source_note_list").html("");
$("#dest_note_list").html("");
$("#source_alias_matched").html("");
$("#dest_alias_matched").html("");
$("#amount").val("");
$("#reason").val("");
refreshBalance();
let msgDiv = $("#messages"); let msgDiv = $("#messages");
let html = msgDiv.html(); let html = msgDiv.html();
html += "<div class=\"alert alert-success\">Le transfert de " html += "<div class=\"alert alert-success\">Le transfert de "
+ pretty_money(source[3] * dest[3] * $("#amount").val()) + " de la note " + source[0] + pretty_money(source[3] * dest[3] * $("#amount").val()) + " de la note " + source[0]
+ " vers la note " + dest[0] + " a été fait avec succès !</div>\n"; + " vers la note " + dest[0] + " a été fait avec succès !</div>\n";
msgDiv.html(html); msgDiv.html(html);
}).fail(function (err) {
sources_notes_display.length = 0; sources_notes_display.length = 0;
sources.length = 0; sources.length = 0;
dests_notes_display.length = 0; dests_notes_display.length = 0;
@ -123,16 +188,29 @@ SPDX-License-Identifier: GPL-2.0-or-later
$("#amount").val(""); $("#amount").val("");
$("#reason").val(""); $("#reason").val("");
refreshBalance(); refreshBalance();
}).fail(function (err) {
let msgDiv = $("#messages"); let msgDiv = $("#messages");
let html = msgDiv.html(); let html = msgDiv.html();
html += "<div class=\"alert alert-danger\">Le transfert de " html += "<div class=\"alert alert-danger\">Le transfert de "
+ pretty_money(source[3] * dest[3] * $("#amount").val()) + " de la note " + source[0] + pretty_money(source[3] * dest[3] * $("#amount").val()) + " de la note " + source[0]
+ " vers la note " + dest[0] + " a échoué : " + err.responseText + "</div>\n"; + " vers la note " + dest[0] + " a échoué : " + err.responseText + "</div>\n";
msgDiv.html(html); msgDiv.html(html);
sources_notes_display.length = 0;
sources.length = 0;
dests_notes_display.length = 0;
dests.length = 0;
$("#source_note_list").html("");
$("#dest_note_list").html("");
$("#source_alias_matched").html("");
$("#dest_alias_matched").html("");
$("#amount").val("");
$("#reason").val("");
refreshBalance();
}); });
}); });
}); });
}
}); });
</script> </script>
{% endblock %} {% endblock %}