From 59bfdbbfc758b7cc246ccd26dba5d7bb69454a9c Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 1 Aug 2020 12:12:56 +0200 Subject: [PATCH] =?UTF-8?q?:lipstick:=20Warn=20users=20when=20a=20note=20b?= =?UTF-8?q?alance=20is=20bellow=200=20or=20-50=20=E2=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/js/consos.js | 22 ++++++++++---- static/js/transfer.js | 67 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/static/js/consos.js b/static/js/consos.js index 268ddc95..f0fd3458 100644 --- a/static/js/consos.js +++ b/static/js/consos.js @@ -156,7 +156,7 @@ function reset() { function consumeAll() { notes_display.forEach(function(note_display) { buttons.forEach(function(button) { - consume(note_display.note.id, note_display.name, button.dest, button.quantity * note_display.quantity, button.amount, + consume(note_display.note, note_display.name, button.dest, button.quantity * note_display.quantity, button.amount, button.name + " (" + button.category_name + ")", button.type, button.category_id, button.id); }); }); @@ -164,7 +164,7 @@ function consumeAll() { /** * Create a new transaction from a button through the API. - * @param source The note that paid the item (type: int) + * @param source The note that paid the item (type: note) * @param source_alias The alias used for the source (type: str) * @param dest The note that sold the item (type: int) * @param quantity The quantity sold (type: int) @@ -184,13 +184,25 @@ function consume(source, source_alias, dest, quantity, amount, reason, type, cat "valid": true, "polymorphic_ctype": type, "resourcetype": "RecurrentTransaction", - "source": source, + "source": source.id, "source_alias": source_alias, "destination": dest, "category": category, "template": template - }, reset).fail(function (e) { - $.post("/api/note/transaction/transaction/", + }) + .done(function () { + if (!isNaN(source.balance)) { + let newBalance = source.balance - quantity * amount; + if (newBalance <= -5000) + addMsg("Attention, la note émettrice " + source_alias + " passe en négatif sévère.", + "danger", 10000); + else if (newBalance < 0) + addMsg("Attention, la note émettrice " + source_alias + " passe en négatif.", + "warning", 10000); + } + reset(); + }).fail(function (e) { + $.post("/api/note/transaction/transaction/", { "csrfmiddlewaretoken": CSRF_TOKEN, "quantity": quantity, diff --git a/static/js/transfer.js b/static/js/transfer.js index 9f768c3b..5b577066 100644 --- a/static/js/transfer.js +++ b/static/js/transfer.js @@ -14,6 +14,14 @@ function reset(refresh=true) { dests.length = 0; $("#source_note_list").html(""); $("#dest_note_list").html(""); + let source_field = $("#source_note"); + source_field.val(""); + source_field.trigger("keyup"); + source_field.removeClass('is-invalid'); + let dest_field = $("#dest_note"); + dest_field.val(""); + dest_field.trigger("keyup"); + dest_field.removeClass('is-invalid'); let amount_field = $("#amount"); amount_field.val(""); amount_field.removeClass('is-invalid'); @@ -81,6 +89,8 @@ $(document).ready(function() { $("#type_transfer").click(function() { $("#source_me_div").removeClass('d-none'); + $("#source_note").removeClass('is-invalid'); + $("#dest_note").removeClass('is-invalid'); $("#special_transaction_div").addClass('d-none'); source.attr('disabled', false); $("#source_note_list").removeClass('d-none'); @@ -90,6 +100,8 @@ $(document).ready(function() { $("#type_credit").click(function() { $("#source_me_div").addClass('d-none'); + $("#source_note").removeClass('is-invalid'); + $("#dest_note").removeClass('is-invalid'); $("#special_transaction_div").removeClass('d-none'); $("#source_note_list").addClass('d-none'); $("#dest_note_list").removeClass('d-none'); @@ -108,6 +120,8 @@ $(document).ready(function() { $("#type_debit").click(function() { $("#source_me_div").addClass('d-none'); + $("#source_note").removeClass('is-invalid'); + $("#dest_note").removeClass('is-invalid'); $("#special_transaction_div").removeClass('d-none'); $("#source_note_list").removeClass('d-none'); $("#dest_note_list").addClass('d-none'); @@ -210,7 +224,6 @@ $("#btn_transfer").click(function() { let reason = reason_field.val(); if ($("#type_transfer").is(':checked')) { - // We copy the arrays to ensure that transactions are well-processed even if the form is reset [...sources_notes_display].forEach(function (source) { [...dests_notes_display].forEach(function (dest) { @@ -228,12 +241,31 @@ $("#btn_transfer").click(function() { "destination": dest.note.id, "destination_alias": dest.name }).done(function () { + if (!isNaN(source.note.balance)) { + let newBalance = source.note.balance - source.quantity * dest.quantity * amount; + if (newBalance <= -5000) { + addMsg("Le transfert de " + + pretty_money(source.quantity * dest.quantity * amount) + " de la note " + + source.name + " vers la note " + dest.name + " a été fait avec succès, " + + "mais la note émettrice passe en négatif sévère.", "danger", 10000); + reset(); + return; + } + else if (newBalance < 0) { + addMsg("Le transfert de " + + pretty_money(source.quantity * dest.quantity * amount) + " de la note " + + source.name + " vers la note " + dest.name + " a été fait avec succès, " + + "mais la note émettrice passe en négatif.", "warning", 10000); + reset(); + return; + } + } addMsg("Le transfert de " + pretty_money(source.quantity * dest.quantity * amount) + " de la note " + source.name + " vers la note " + dest.name + " a été fait avec succès !", "success", 10000); reset(); - }).fail(function (err) { // do it again but valid = false + }).fail(function () { // do it again but valid = false $.post("/api/note/transaction/transaction/", { "csrfmiddlewaretoken": CSRF_TOKEN, @@ -264,19 +296,29 @@ $("#btn_transfer").click(function() { let special_note = $("#credit_type").val(); let user_note; let given_reason = reason; - let source, dest, reason; + let source_id, dest_id; if ($("#type_credit").is(':checked')) { + if (!dests_notes_display.length) { + $("#dest_note").addClass('is-invalid'); + return; + } + user_note = dests_notes_display[0].note.id; - source = special_note; - dest = user_note; + source_id = special_note; + dest_id = user_note; reason = "Crédit " + $("#credit_type option:selected").text().toLowerCase(); if (given_reason.length > 0) reason += " (" + given_reason + ")"; } else { + if (!sources_notes_display.length) { + $("#source_note").addClass('is-invalid'); + return; + } + user_note = sources_notes_display[0].note.id; - source = user_note; - dest = special_note; + source_id = user_note; + dest_id = special_note; reason = "Retrait " + $("#credit_type option:selected").text().toLowerCase(); if (given_reason.length > 0) reason += " (" + given_reason + ")"; @@ -290,10 +332,10 @@ $("#btn_transfer").click(function() { "valid": true, "polymorphic_ctype": SPECIAL_TRANSFER_POLYMORPHIC_CTYPE, "resourcetype": "SpecialTransaction", - "source": source, - "source_alias": source.name, - "destination": dest, - "destination_alias": dest.name, + "source": source_id, + "source_alias": sources_notes_display[0].name, + "destination": dest_id, + "destination_alias": dests_notes_display[0].name, "last_name": $("#last_name").val(), "first_name": $("#first_name").val(), "bank": $("#bank").val() @@ -301,7 +343,8 @@ $("#btn_transfer").click(function() { addMsg("Le crédit/retrait a bien été effectué !", "success", 10000); reset(); }).fail(function (err) { - addMsg("Le crédit/retrait a échoué : " + JSON.parse(err.responseText)["detail"], "danger", 10000); + addMsg("Le crédit/retrait a échoué : " + JSON.parse(err.responseText)["detail"], + "danger", 10000); }); } });