nk20/static/js/consos.js

130 lines
4.3 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");
}
$(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-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
autoCompleteNote("note", "alias_matched", "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 TemplateTransaction)
* @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-13 00:34:20 +00:00
if (b[6] === template_id) {
2020-03-12 15:53:35 +00:00
b[1] += 1;
button = b;
}
});
if (button == null)
buttons.push([dest, 1, amount, type, category_id, category_name, template_id, template_name]);
2020-03-12 15:53:35 +00:00
2020-03-13 08:26:39 +00:00
if ($("#double_conso").is(":checked")) {
2020-03-13 00:34:20 +00:00
let html = "";
buttons.forEach(function(button) {
html += li("conso_button_" + button[6], button[7]
+ "<span class=\"badge badge-dark badge-pill\">" + button[1] + "</span>");
});
$("#consos_list").html(html);
}
else if (notes_display.length > 0)
2020-03-12 15:53:35 +00:00
consumeAll();
2020-03-13 00:34:20 +00:00
else {
let html = "";
buttons.forEach(function(button) {
html += li("conso_button_" + button[6], button[7]
+ "<span class=\"badge badge-dark badge-pill\">" + button[1] + "</span>");
});
$("#note_list").html(html);
}
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) {
consume(note_display[1], button[0], button[1] * note_display[3], button[2],
button[7] + " (" + button[5] + ")", button[3], button[4], button[6]);
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)
* @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 TemplateTransaction)
* @param category The category id of the button (type: int)
* @param template The button id (type: int)
*/
function consume(source, dest, quantity, amount, reason, type, category, template) {
$.post("/api/note/transaction/transaction/",
{
"csrfmiddlewaretoken": CSRF_TOKEN,
"quantity": quantity,
"amount": amount,
"reason": reason,
"valid": true,
"polymorphic_ctype": type,
"resourcetype": "TemplateTransaction",
"source": source,
"destination": dest,
"category": category,
"template": template
}, function() {
notes_display.length = 0;
notes.length = 0;
buttons.length = 0;
2020-03-12 15:53:35 +00:00
$("#note_list").html("");
$("#alias_matched").html("");
2020-03-13 00:34:20 +00:00
$("#consos_list").html("");
2020-03-12 15:53:35 +00:00
displayNote(null, "");
refreshHistory();
refreshBalance();
});
}