nk20/static/js/consos.js

221 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-03-12 15:53:35 +00:00
// Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* Refresh the history table on the consumptions page.
*/
function refreshHistory() {
$("#history").load("/note/consos/ #history");
2020-03-14 18:00:20 +00:00
$("#most_used").load("/note/consos/ #most_used");
2020-03-12 15:53:35 +00:00
}
$(document).ready(function() {
// If hash of a category in the URL, then select this category
// else select the first one
if (location.hash) {
$("a[href='" + location.hash + "']").tab("show");
} else {
$("a[data-toggle='tab']").first().tab("show");
}
// When selecting a category, change URL
$(document.body).on("click", "a[data-toggle='tab']", function() {
location.hash = this.getAttribute("href");
});
2020-03-16 08:32:39 +00:00
// Switching in double consumptions mode should update the layout
2020-04-10 18:12:14 +00:00
$("#double_conso").click(function() {
$("#consos_list_div").removeClass('d-none');
2020-03-16 08:32:39 +00:00
$("#infos_div").attr('class', 'col-sm-5 col-xl-6');
$("#note_infos_div").attr('class', 'col-xl-3');
$("#user_select_div").attr('class', 'col-xl-4');
$("#buttons_div").attr('class', 'col-sm-7 col-xl-6');
2020-03-16 12:06:02 +00:00
let note_list_obj = $("#note_list");
if (buttons.length > 0 && note_list_obj.text().length > 0) {
2020-03-16 08:32:39 +00:00
$("#consos_list").html(note_list_obj.html());
note_list_obj.html("");
2020-03-16 12:06:02 +00:00
buttons.forEach(function(button) {
$("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
"consos_list"));
});
2020-03-16 08:32:39 +00:00
}
});
2020-04-10 18:12:14 +00:00
$("#single_conso").click(function() {
$("#consos_list_div").addClass('d-none');
2020-03-16 08:32:39 +00:00
$("#infos_div").attr('class', 'col-sm-5 col-md-4');
$("#note_infos_div").attr('class', 'col-xl-5');
$("#user_select_div").attr('class', 'col-xl-7');
$("#buttons_div").attr('class', 'col-sm-7 col-md-8');
2020-03-16 12:06:02 +00:00
let consos_list_obj = $("#consos_list");
2020-03-16 08:32:39 +00:00
if (buttons.length > 0) {
2020-03-16 12:06:02 +00:00
if (notes_display.length === 0 && consos_list_obj.text().length > 0) {
2020-03-16 08:32:39 +00:00
$("#note_list").html(consos_list_obj.html());
consos_list_obj.html("");
2020-03-16 12:06:02 +00:00
buttons.forEach(function(button) {
$("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
"note_list"));
});
2020-03-16 08:32:39 +00:00
}
else {
buttons.length = 0;
2020-03-16 12:06:02 +00:00
consos_list_obj.html("");
2020-03-16 08:32:39 +00:00
}
}
});
2020-04-10 18:12:14 +00:00
// Ensure we begin in single consumption. Fix issue with TurboLinks and BootstrapJS
$("label[for='double_conso']").removeClass('active');
2020-03-16 12:06:02 +00:00
2020-03-16 08:32:39 +00:00
$("#consume_all").click(consumeAll);
2020-03-12 15:53:35 +00:00
});
2020-03-13 08:26:39 +00:00
notes = [];
notes_display = [];
buttons = [];
2020-03-12 15:53:35 +00:00
// When the user searches an alias, we update the auto-completion
2020-04-10 19:05:06 +00:00
autoCompleteNote("note", "note_list", notes, notes_display,
2020-03-13 00:34:20 +00:00
"alias", "note", "user_note", "profile_pic", function() {
2020-03-13 08:26:39 +00:00
if (buttons.length > 0 && $("#single_conso").is(":checked")) {
2020-03-13 00:34:20 +00:00
consumeAll();
return false;
}
return true;
});
2020-03-12 15:53:35 +00:00
/**
* Add a transaction from a button.
* @param dest Where the money goes
* @param amount The price of the item
* @param type The type of the transaction (content type id for RecurrentTransaction)
2020-03-12 15:53:35 +00:00
* @param category_id The category identifier
* @param category_name The category name
* @param template_id The identifier of the button
* @param template_name The name of the button
*/
function addConso(dest, amount, type, category_id, category_name, template_id, template_name) {
var button = null;
buttons.forEach(function(b) {
2020-03-16 08:32:39 +00:00
if (b.id === template_id) {
b.quantity += 1;
2020-03-12 15:53:35 +00:00
button = b;
}
});
2020-03-16 08:32:39 +00:00
if (button == null) {
button = {
id: template_id,
name: template_name,
dest: dest,
quantity: 1,
amount: amount,
type: type,
category_id: category_id,
category_name: category_name
};
buttons.push(button);
}
2020-03-12 15:53:35 +00:00
2020-03-16 08:32:39 +00:00
let dc_obj = $("#double_conso");
if (dc_obj.is(":checked") || notes_display.length === 0) {
let list = dc_obj.is(":checked") ? "consos_list" : "note_list";
2020-03-13 00:34:20 +00:00
let html = "";
buttons.forEach(function(button) {
2020-03-16 08:32:39 +00:00
html += li("conso_button_" + button.id, button.name
+ "<span class=\"badge badge-dark badge-pill\">" + button.quantity + "</span>");
2020-03-13 00:34:20 +00:00
});
2020-03-16 08:32:39 +00:00
$("#" + list).html(html);
2020-03-13 00:34:20 +00:00
buttons.forEach(function(button) {
2020-03-16 08:32:39 +00:00
$("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons, list));
2020-03-13 00:34:20 +00:00
});
}
2020-03-16 08:32:39 +00:00
else
consumeAll();
2020-03-12 15:53:35 +00:00
}
2020-03-16 11:11:16 +00:00
/**
* Reset the page as its initial state.
*/
function reset() {
notes_display.length = 0;
notes.length = 0;
buttons.length = 0;
$("#note_list").html("");
$("#consos_list").html("");
$("#user_note").text("");
$("#profile_pic").attr("src", "/media/pic/default.png");
2020-03-16 11:11:16 +00:00
refreshHistory();
refreshBalance();
}
2020-03-12 15:53:35 +00:00
/**
* Apply all transactions: all notes in `notes` buy each item in `buttons`
*/
function consumeAll() {
notes_display.forEach(function(note_display) {
2020-03-12 15:53:35 +00:00
buttons.forEach(function(button) {
2020-03-28 20:31:22 +00:00
consume(note_display.note.id, note_display.name, button.dest, button.quantity * note_display.quantity, button.amount,
2020-03-16 08:32:39 +00:00
button.name + " (" + button.category_name + ")", button.type, button.category_id, button.id);
2020-03-12 15:53:35 +00:00
});
});
}
/**
* Create a new transaction from a button through the API.
* @param source The note that paid the item (type: int)
2020-03-26 13:45:48 +00:00
* @param source_alias The alias used for the source (type: str)
2020-03-12 15:53:35 +00:00
* @param dest The note that sold the item (type: int)
* @param quantity The quantity sold (type: int)
* @param amount The price of one item, in cents (type: int)
* @param reason The transaction details (type: str)
* @param type The type of the transaction (content type id for RecurrentTransaction)
2020-03-12 15:53:35 +00:00
* @param category The category id of the button (type: int)
* @param template The button id (type: int)
*/
2020-03-26 13:45:48 +00:00
function consume(source, source_alias, dest, quantity, amount, reason, type, category, template) {
2020-03-12 15:53:35 +00:00
$.post("/api/note/transaction/transaction/",
{
"csrfmiddlewaretoken": CSRF_TOKEN,
"quantity": quantity,
"amount": amount,
"reason": reason,
"valid": true,
"polymorphic_ctype": type,
"resourcetype": "RecurrentTransaction",
2020-03-12 15:53:35 +00:00
"source": source,
2020-03-26 13:45:48 +00:00
"source_alias": source_alias,
2020-03-12 15:53:35 +00:00
"destination": dest,
"category": category,
"template": template
2020-03-16 11:11:16 +00:00
}, reset).fail(function (e) {
$.post("/api/note/transaction/transaction/",
{
"csrfmiddlewaretoken": CSRF_TOKEN,
"quantity": quantity,
"amount": amount,
"reason": reason,
"valid": false,
"invalidity_reason": "Solde insuffisant",
"polymorphic_ctype": type,
"resourcetype": "RecurrentTransaction",
"source": source,
2020-03-26 13:45:48 +00:00
"source_alias": source_alias,
"destination": dest,
"category": category,
"template": template
}).done(function() {
reset();
addMsg("La transaction n'a pas pu être validée pour cause de solde insuffisant.", "danger");
}).fail(function () {
reset();
2020-03-27 13:07:22 +00:00
errMsg(e.responseJSON);
});
2020-03-16 11:11:16 +00:00
});
2020-03-12 15:53:35 +00:00
}