2020-03-17 17:47:16 +00:00
|
|
|
sources = [];
|
|
|
|
sources_notes_display = [];
|
|
|
|
dests = [];
|
|
|
|
dests_notes_display = [];
|
|
|
|
|
|
|
|
function refreshHistory() {
|
|
|
|
$("#history").load("/note/transfer/ #history");
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
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("");
|
|
|
|
$("#last_name").val("");
|
|
|
|
$("#first_name").val("");
|
|
|
|
$("#bank").val("");
|
2020-03-19 13:25:43 +00:00
|
|
|
$("#user_note").val("");
|
|
|
|
$("#profile_pic").attr("src", "/media/pic/default.png");
|
2020-03-17 17:47:16 +00:00
|
|
|
refreshBalance();
|
|
|
|
refreshHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
autoCompleteNote("source_note", "source_alias_matched", "source_note_list", sources, sources_notes_display,
|
|
|
|
"source_alias", "source_note", "user_note", "profile_pic");
|
|
|
|
autoCompleteNote("dest_note", "dest_alias_matched", "dest_note_list", dests, dests_notes_display,
|
|
|
|
"dest_alias", "dest_note", "user_note", "profile_pic", function() {
|
2020-03-19 13:25:43 +00:00
|
|
|
if ($("#type_credit").is(":checked") || $("#type_debit").is(":checked")) {
|
|
|
|
let last = dests_notes_display[dests_notes_display.length - 1];
|
|
|
|
dests_notes_display.length = 0;
|
|
|
|
dests_notes_display.push(last);
|
|
|
|
|
|
|
|
last.quantity = 1;
|
|
|
|
|
2020-03-24 00:34:47 +00:00
|
|
|
if (!last.note.user) {
|
|
|
|
$.getJSON("/api/note/note/" + last.note.id + "/?format=json", function(note) {
|
|
|
|
last.note.user = note.user;
|
|
|
|
$.getJSON("/api/user/" + last.note.user + "/", function(user) {
|
|
|
|
$("#last_name").val(user.last_name);
|
|
|
|
$("#first_name").val(user.first_name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$.getJSON("/api/user/" + last.note.user + "/", function(user) {
|
|
|
|
$("#last_name").val(user.last_name);
|
|
|
|
$("#first_name").val(user.first_name);
|
|
|
|
});
|
|
|
|
}
|
2020-03-19 13:25:43 +00:00
|
|
|
}
|
2020-03-17 17:47:16 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure we begin in gift mode. Removing these lines may cause problems when reloading.
|
2020-04-06 05:06:52 +00:00
|
|
|
let type_gift = $("#type_gift"); // Default mode
|
|
|
|
type_gift.removeAttr('checked');
|
2020-03-17 17:47:16 +00:00
|
|
|
$("#type_transfer").removeAttr('checked');
|
|
|
|
$("#type_credit").removeAttr('checked');
|
|
|
|
$("#type_debit").removeAttr('checked');
|
2020-04-06 05:06:52 +00:00
|
|
|
$("label[for='type_gift']").attr('class', 'btn btn-sm btn-outline-primary');
|
2020-03-17 17:47:16 +00:00
|
|
|
$("label[for='type_transfer']").attr('class', 'btn btn-sm btn-outline-primary');
|
|
|
|
$("label[for='type_credit']").attr('class', 'btn btn-sm btn-outline-primary');
|
|
|
|
$("label[for='type_debit']").attr('class', 'btn btn-sm btn-outline-primary');
|
2020-04-06 05:06:52 +00:00
|
|
|
|
|
|
|
if (location.hash)
|
|
|
|
$("#type_" + location.hash.substr(1)).click();
|
|
|
|
else
|
|
|
|
type_gift.click();
|
|
|
|
location.hash = "";
|
2020-03-17 17:47:16 +00:00
|
|
|
});
|
|
|
|
|
2020-04-06 05:06:52 +00:00
|
|
|
$("#btn_transfer").click(function() {
|
2020-03-17 17:47:16 +00:00
|
|
|
if ($("#type_gift").is(':checked')) {
|
|
|
|
dests_notes_display.forEach(function (dest) {
|
|
|
|
$.post("/api/note/transaction/transaction/",
|
|
|
|
{
|
|
|
|
"csrfmiddlewaretoken": CSRF_TOKEN,
|
|
|
|
"quantity": dest.quantity,
|
|
|
|
"amount": 100 * $("#amount").val(),
|
|
|
|
"reason": $("#reason").val(),
|
|
|
|
"valid": true,
|
|
|
|
"polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE,
|
|
|
|
"resourcetype": "Transaction",
|
|
|
|
"source": user_id,
|
2020-03-26 13:45:48 +00:00
|
|
|
"destination": dest.id,
|
|
|
|
"destination_alias": dest.name
|
2020-03-25 14:27:38 +00:00
|
|
|
}).done(function () {
|
2020-03-17 17:47:16 +00:00
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note "
|
|
|
|
+ " vers la note " + dest.name + " a été fait avec succès !", "success");
|
|
|
|
|
|
|
|
reset();
|
2020-03-25 14:27:38 +00:00
|
|
|
}).fail(function () {
|
|
|
|
$.post("/api/note/transaction/transaction/",
|
|
|
|
{
|
|
|
|
"csrfmiddlewaretoken": CSRF_TOKEN,
|
|
|
|
"quantity": dest.quantity,
|
|
|
|
"amount": 100 * $("#amount").val(),
|
|
|
|
"reason": $("#reason").val(),
|
|
|
|
"valid": false,
|
|
|
|
"invalidity_reason": "Solde insuffisant",
|
|
|
|
"polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE,
|
|
|
|
"resourcetype": "Transaction",
|
|
|
|
"source": user_id,
|
2020-03-26 13:45:48 +00:00
|
|
|
"destination": dest.id,
|
|
|
|
"destination_alias": dest.name
|
2020-03-25 14:27:38 +00:00
|
|
|
}).done(function () {
|
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note "
|
|
|
|
+ " vers la note " + dest.name + " a échoué : Solde insuffisant", "danger");
|
2020-03-17 17:47:16 +00:00
|
|
|
|
2020-03-25 14:27:38 +00:00
|
|
|
reset();
|
|
|
|
}).fail(function (err) {
|
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note "
|
|
|
|
+ " vers la note " + dest.name + " a échoué : " + err.responseText, "danger");
|
|
|
|
|
|
|
|
reset();
|
|
|
|
});
|
2020-03-17 17:47:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if ($("#type_transfer").is(':checked')) {
|
|
|
|
sources_notes_display.forEach(function (source) {
|
|
|
|
dests_notes_display.forEach(function (dest) {
|
|
|
|
$.post("/api/note/transaction/transaction/",
|
|
|
|
{
|
|
|
|
"csrfmiddlewaretoken": CSRF_TOKEN,
|
|
|
|
"quantity": source.quantity * dest.quantity,
|
|
|
|
"amount": 100 * $("#amount").val(),
|
|
|
|
"reason": $("#reason").val(),
|
|
|
|
"valid": true,
|
|
|
|
"polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE,
|
|
|
|
"resourcetype": "Transaction",
|
|
|
|
"source": source.id,
|
2020-03-26 13:45:48 +00:00
|
|
|
"source_alias": source.name,
|
|
|
|
"destination": dest.id,
|
|
|
|
"destination_alias": dest.name
|
2020-03-25 14:27:38 +00:00
|
|
|
}).done(function () {
|
2020-03-17 17:47:16 +00:00
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name
|
|
|
|
+ " vers la note " + dest.name + " a été fait avec succès !", "success");
|
|
|
|
|
|
|
|
reset();
|
|
|
|
}).fail(function (err) {
|
2020-03-25 14:27:38 +00:00
|
|
|
$.post("/api/note/transaction/transaction/",
|
|
|
|
{
|
|
|
|
"csrfmiddlewaretoken": CSRF_TOKEN,
|
|
|
|
"quantity": source.quantity * dest.quantity,
|
|
|
|
"amount": 100 * $("#amount").val(),
|
|
|
|
"reason": $("#reason").val(),
|
|
|
|
"valid": false,
|
|
|
|
"invalidity_reason": "Solde insuffisant",
|
|
|
|
"polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE,
|
|
|
|
"resourcetype": "Transaction",
|
|
|
|
"source": source.id,
|
2020-03-26 13:45:48 +00:00
|
|
|
"source_alias": source.name,
|
|
|
|
"destination": dest.id,
|
|
|
|
"destination_alias": dest.name
|
2020-03-25 14:27:38 +00:00
|
|
|
}).done(function () {
|
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name
|
|
|
|
+ " vers la note " + dest.name + " a échoué : Solde insuffisant", "danger");
|
2020-03-17 17:47:16 +00:00
|
|
|
|
2020-03-25 14:27:38 +00:00
|
|
|
reset();
|
|
|
|
}).fail(function (err) {
|
|
|
|
addMsg("Le transfert de "
|
|
|
|
+ pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name
|
|
|
|
+ " vers la note " + dest.name + " a échoué : " + err.responseText, "danger");
|
|
|
|
|
|
|
|
reset();
|
|
|
|
});
|
2020-03-17 17:47:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if ($("#type_credit").is(':checked') || $("#type_debit").is(':checked')) {
|
|
|
|
let special_note = $("#credit_type").val();
|
|
|
|
let user_note = dests_notes_display[0].id;
|
|
|
|
let given_reason = $("#reason").val();
|
|
|
|
let source, dest, reason;
|
|
|
|
if ($("#type_credit").is(':checked')) {
|
|
|
|
source = special_note;
|
|
|
|
dest = user_note;
|
|
|
|
reason = "Crédit " + $("#credit_type option:selected").text().toLowerCase();
|
|
|
|
if (given_reason.length > 0)
|
|
|
|
reason += " (" + given_reason + ")";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
source = user_note;
|
|
|
|
dest = special_note;
|
|
|
|
reason = "Retrait " + $("#credit_type option:selected").text().toLowerCase();
|
|
|
|
if (given_reason.length > 0)
|
|
|
|
reason += " (" + given_reason + ")";
|
|
|
|
}
|
|
|
|
$.post("/api/note/transaction/transaction/",
|
|
|
|
{
|
|
|
|
"csrfmiddlewaretoken": CSRF_TOKEN,
|
|
|
|
"quantity": 1,
|
|
|
|
"amount": 100 * $("#amount").val(),
|
|
|
|
"reason": reason,
|
|
|
|
"valid": true,
|
|
|
|
"polymorphic_ctype": SPECIAL_TRANSFER_POLYMORPHIC_CTYPE,
|
|
|
|
"resourcetype": "SpecialTransaction",
|
|
|
|
"source": source,
|
2020-03-26 13:45:48 +00:00
|
|
|
"source_alias": source.name,
|
2020-03-17 17:47:16 +00:00
|
|
|
"destination": dest,
|
2020-03-26 13:45:48 +00:00
|
|
|
"destination_alias": dest.name,
|
2020-03-17 17:47:16 +00:00
|
|
|
"last_name": $("#last_name").val(),
|
|
|
|
"first_name": $("#first_name").val(),
|
|
|
|
"bank": $("#bank").val()
|
2020-03-25 14:27:38 +00:00
|
|
|
}).done(function () {
|
2020-03-17 17:47:16 +00:00
|
|
|
addMsg("Le crédit/retrait a bien été effectué !", "success");
|
|
|
|
reset();
|
|
|
|
}).fail(function (err) {
|
2020-03-25 14:27:38 +00:00
|
|
|
addMsg("Le crédit/retrait a échoué : " + err.responseText, "danger");
|
2020-03-17 17:47:16 +00:00
|
|
|
reset();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|