mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	Merge remote-tracking branch 'origin/master' into tresorerie
# Conflicts: # locale/de/LC_MESSAGES/django.po # locale/fr/LC_MESSAGES/django.po # note_kfet/settings/base.py # templates/base.html
This commit is contained in:
		
							
								
								
									
										297
									
								
								static/js/base.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										297
									
								
								static/js/base.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,297 @@
 | 
			
		||||
// Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
 | 
			
		||||
// SPDX-License-Identifier: GPL-3.0-or-later
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Convert balance in cents to a human readable amount
 | 
			
		||||
 * @param value the balance, in cents
 | 
			
		||||
 * @returns {string}
 | 
			
		||||
 */
 | 
			
		||||
function pretty_money(value) {
 | 
			
		||||
    if (value % 100 === 0)
 | 
			
		||||
        return (value < 0 ? "- " : "") + Math.floor(Math.abs(value) / 100) + " €";
 | 
			
		||||
    else
 | 
			
		||||
        return (value < 0 ? "- " : "") + Math.floor(Math.abs(value) / 100) + "."
 | 
			
		||||
            + (Math.abs(value) % 100 < 10 ? "0" : "") + (Math.abs(value) % 100) + " €";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Add a message on the top of the page.
 | 
			
		||||
 * @param msg The message to display
 | 
			
		||||
 * @param alert_type The type of the alert. Choices: info, success, warning, danger
 | 
			
		||||
 */
 | 
			
		||||
function addMsg(msg, alert_type) {
 | 
			
		||||
    let msgDiv = $("#messages");
 | 
			
		||||
    let html = msgDiv.html();
 | 
			
		||||
    html += "<div class=\"alert alert-" + alert_type + " alert-dismissible\">" +
 | 
			
		||||
        "<button class=\"close\" data-dismiss=\"alert\" href=\"#\"><span aria-hidden=\"true\">×</span></button>"
 | 
			
		||||
        + msg + "</div>\n";
 | 
			
		||||
    msgDiv.html(html);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Reload the balance of the user on the right top corner
 | 
			
		||||
 */
 | 
			
		||||
function refreshBalance() {
 | 
			
		||||
    $("#user_balance").load("/ #user_balance");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Query the 20 first matched notes with a given pattern
 | 
			
		||||
 * @param pattern The pattern that is queried
 | 
			
		||||
 * @param fun For each found note with the matched alias `alias`, fun(note, alias) is called.
 | 
			
		||||
 */
 | 
			
		||||
function getMatchedNotes(pattern, fun) {
 | 
			
		||||
    $.getJSON("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club&ordering=normalized_name", fun);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Generate a <li> entry with a given id and text
 | 
			
		||||
 */
 | 
			
		||||
function li(id, text) {
 | 
			
		||||
    return "<li class=\"list-group-item py-1 d-flex justify-content-between align-items-center\"" +
 | 
			
		||||
                " id=\"" + id + "\">" + text + "</li>\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Render note name and picture
 | 
			
		||||
 * @param note The note to render
 | 
			
		||||
 * @param alias The alias to be displayed
 | 
			
		||||
 * @param user_note_field
 | 
			
		||||
 * @param profile_pic_field
 | 
			
		||||
 */
 | 
			
		||||
function displayNote(note, alias, user_note_field=null, profile_pic_field=null) {
 | 
			
		||||
    if (!note.display_image) {
 | 
			
		||||
        note.display_image = 'https://nk20.ynerant.fr/media/pic/default.png';
 | 
			
		||||
        $.getJSON("/api/note/note/" + note.id + "/?format=json", function(new_note) {
 | 
			
		||||
            note.display_image = new_note.display_image.replace("http:", "https:");
 | 
			
		||||
            note.name = new_note.name;
 | 
			
		||||
            note.balance = new_note.balance;
 | 
			
		||||
 | 
			
		||||
            displayNote(note, alias, user_note_field, profile_pic_field);
 | 
			
		||||
        });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let img = note.display_image;
 | 
			
		||||
    if (alias !== note.name)
 | 
			
		||||
        alias += " (aka. " + note.name + ")";
 | 
			
		||||
    if (user_note_field !== null)
 | 
			
		||||
        $("#" + user_note_field).text(alias + (note.balance == null ? "" : (" : " + pretty_money(note.balance))));
 | 
			
		||||
    if (profile_pic_field != null)
 | 
			
		||||
        $("#" + profile_pic_field).attr('src', img);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Remove a note from the emitters.
 | 
			
		||||
 * @param d The note to remove
 | 
			
		||||
 * @param note_prefix The prefix of the identifiers of the <li> blocks of the emitters
 | 
			
		||||
 * @param notes_display An array containing the infos of the buyers: [alias, note id, note object, quantity]
 | 
			
		||||
 * @param note_list_id The div block identifier where the notes of the buyers are displayed
 | 
			
		||||
 * @param user_note_field The identifier of the field that display the note of the hovered note (useful in
 | 
			
		||||
 *                        consumptions, put null if not used)
 | 
			
		||||
 * @param profile_pic_field The identifier of the field that display the profile picture of the hovered note
 | 
			
		||||
 *                          (useful in consumptions, put null if not used)
 | 
			
		||||
 * @returns an anonymous function to be compatible with jQuery events
 | 
			
		||||
 */
 | 
			
		||||
function removeNote(d, note_prefix="note", notes_display, note_list_id, user_note_field=null, profile_pic_field=null) {
 | 
			
		||||
    return (function() {
 | 
			
		||||
        let new_notes_display = [];
 | 
			
		||||
        let html = "";
 | 
			
		||||
        notes_display.forEach(function (disp) {
 | 
			
		||||
            if (disp.quantity > 1 || disp.id !== d.id) {
 | 
			
		||||
                disp.quantity -= disp.id === d.id ? 1 : 0;
 | 
			
		||||
                new_notes_display.push(disp);
 | 
			
		||||
                html += li(note_prefix + "_" + disp.id, disp.name
 | 
			
		||||
                    + "<span class=\"badge badge-dark badge-pill\">" + disp.quantity + "</span>");
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        notes_display.length = 0;
 | 
			
		||||
        new_notes_display.forEach(function(disp) {
 | 
			
		||||
            notes_display.push(disp);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $("#" + note_list_id).html(html);
 | 
			
		||||
        notes_display.forEach(function (disp) {
 | 
			
		||||
            let obj = $("#" + note_prefix + "_" + disp.id);
 | 
			
		||||
            obj.click(removeNote(disp, note_prefix, notes_display, note_list_id, user_note_field, profile_pic_field));
 | 
			
		||||
            obj.hover(function() {
 | 
			
		||||
                if (disp.note)
 | 
			
		||||
                    displayNote(disp.note, disp.name, user_note_field, profile_pic_field);
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Generate an auto-complete field to query a note with its alias
 | 
			
		||||
 * @param field_id The identifier of the text field where the alias is typed
 | 
			
		||||
 * @param alias_matched_id The div block identifier where the matched aliases are displayed
 | 
			
		||||
 * @param note_list_id The div block identifier where the notes of the buyers are displayed
 | 
			
		||||
 * @param notes An array containing the note objects of the buyers
 | 
			
		||||
 * @param notes_display An array containing the infos of the buyers: [alias, note id, note object, quantity]
 | 
			
		||||
 * @param alias_prefix The prefix of the <li> blocks for the matched aliases
 | 
			
		||||
 * @param note_prefix The prefix of the <li> blocks for the notes of the buyers
 | 
			
		||||
 * @param user_note_field The identifier of the field that display the note of the hovered note (useful in
 | 
			
		||||
 *                        consumptions, put null if not used)
 | 
			
		||||
 * @param profile_pic_field The identifier of the field that display the profile picture of the hovered note
 | 
			
		||||
 *                          (useful in consumptions, put null if not used)
 | 
			
		||||
 * @param alias_click Function that is called when an alias is clicked. If this method exists and doesn't return true,
 | 
			
		||||
 *                    the associated note is not displayed.
 | 
			
		||||
 *                    Useful for a consumption if the item is selected before.
 | 
			
		||||
 */
 | 
			
		||||
function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes_display, alias_prefix="alias",
 | 
			
		||||
                          note_prefix="note", user_note_field=null, profile_pic_field=null, alias_click=null) {
 | 
			
		||||
    let field = $("#" + field_id);
 | 
			
		||||
    // When the user clicks on the search field, it is immediately cleared
 | 
			
		||||
    field.click(function() {
 | 
			
		||||
        field.val("");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let old_pattern = null;
 | 
			
		||||
 | 
			
		||||
    // When the user type "Enter", the first alias is clicked
 | 
			
		||||
    field.keypress(function(event) {
 | 
			
		||||
        if (event.originalEvent.charCode === 13)
 | 
			
		||||
            $("#" + alias_matched_id + " li").first().trigger("click");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // When the user type something, the matched aliases are refreshed
 | 
			
		||||
    field.keyup(function(e) {
 | 
			
		||||
        if (e.originalEvent.charCode === 13)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        let pattern = field.val();
 | 
			
		||||
        // If the pattern is not modified, we don't query the API
 | 
			
		||||
        if (pattern === old_pattern || pattern === "")
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        old_pattern = pattern;
 | 
			
		||||
 | 
			
		||||
        // Clear old matched notes
 | 
			
		||||
        notes.length = 0;
 | 
			
		||||
 | 
			
		||||
        let aliases_matched_obj = $("#" + alias_matched_id);
 | 
			
		||||
        let aliases_matched_html = "";
 | 
			
		||||
 | 
			
		||||
        // Get matched notes with the given pattern
 | 
			
		||||
        getMatchedNotes(pattern, function(aliases) {
 | 
			
		||||
            // The response arrived too late, we stop the request
 | 
			
		||||
            if (pattern !== $("#" + field_id).val())
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            aliases.results.forEach(function (alias) {
 | 
			
		||||
                let note = alias.note;
 | 
			
		||||
                note = {
 | 
			
		||||
                    id: note,
 | 
			
		||||
                    name: alias.name,
 | 
			
		||||
                    alias: alias,
 | 
			
		||||
                    balance: null
 | 
			
		||||
                };
 | 
			
		||||
                aliases_matched_html += li(alias_prefix + "_" + alias.id, alias.name);
 | 
			
		||||
                notes.push(note);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Display the list of matched aliases
 | 
			
		||||
            aliases_matched_obj.html(aliases_matched_html);
 | 
			
		||||
 | 
			
		||||
            notes.forEach(function (note) {
 | 
			
		||||
                let alias = note.alias;
 | 
			
		||||
                let alias_obj = $("#" + alias_prefix + "_" + alias.id);
 | 
			
		||||
                // When an alias is hovered, the profile picture and the balance are displayed at the right place
 | 
			
		||||
                alias_obj.hover(function () {
 | 
			
		||||
                    displayNote(note, alias.name, user_note_field, profile_pic_field);
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                // When the user click on an alias, the associated note is added to the emitters
 | 
			
		||||
                alias_obj.click(function () {
 | 
			
		||||
                    field.val("");
 | 
			
		||||
                    old_pattern = "";
 | 
			
		||||
                    // If the note is already an emitter, we increase the quantity
 | 
			
		||||
                    var disp = null;
 | 
			
		||||
                    notes_display.forEach(function (d) {
 | 
			
		||||
                        // We compare the note ids
 | 
			
		||||
                        if (d.id === note.id) {
 | 
			
		||||
                            d.quantity += 1;
 | 
			
		||||
                            disp = d;
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                    // In the other case, we add a new emitter
 | 
			
		||||
                    if (disp == null) {
 | 
			
		||||
                        disp = {
 | 
			
		||||
                            name: alias.name,
 | 
			
		||||
                            id: note.id,
 | 
			
		||||
                            note: note,
 | 
			
		||||
                            quantity: 1
 | 
			
		||||
                        };
 | 
			
		||||
                        notes_display.push(disp);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // If the function alias_click exists, it is called. If it doesn't return true, then the notes are
 | 
			
		||||
                    // note displayed. Useful for a consumption when a button is already clicked
 | 
			
		||||
                    if (alias_click && !alias_click())
 | 
			
		||||
                        return;
 | 
			
		||||
 | 
			
		||||
                    let note_list = $("#" + note_list_id);
 | 
			
		||||
                    let html = "";
 | 
			
		||||
                    notes_display.forEach(function (disp) {
 | 
			
		||||
                        html += li(note_prefix + "_" + disp.id, disp.name
 | 
			
		||||
                            + "<span class=\"badge badge-dark badge-pill\">" + disp.quantity + "</span>");
 | 
			
		||||
                    });
 | 
			
		||||
 | 
			
		||||
                    // Emitters are displayed
 | 
			
		||||
                    note_list.html(html);
 | 
			
		||||
 | 
			
		||||
                    notes_display.forEach(function (disp) {
 | 
			
		||||
                        let line_obj = $("#" + note_prefix + "_" + disp.id);
 | 
			
		||||
                        // Hover an emitter display also the profile picture
 | 
			
		||||
                        line_obj.hover(function () {
 | 
			
		||||
                            displayNote(disp.note, disp.name, user_note_field, profile_pic_field);
 | 
			
		||||
                        });
 | 
			
		||||
 | 
			
		||||
                        // When an emitter is clicked, it is removed
 | 
			
		||||
                        line_obj.click(removeNote(disp, note_prefix, notes_display, note_list_id, user_note_field,
 | 
			
		||||
                            profile_pic_field));
 | 
			
		||||
                    });
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// When a validate button is clicked, we switch the validation status
 | 
			
		||||
function de_validate(id, validated) {
 | 
			
		||||
    $("#validate_" + id).html("<strong style=\"font-size: 16pt;\">⟳ ...</strong>");
 | 
			
		||||
 | 
			
		||||
    // Perform a PATCH request to the API in order to update the transaction
 | 
			
		||||
    // If the user has insuffisent rights, an error message will appear
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        "url": "/api/note/transaction/transaction/" + id + "/",
 | 
			
		||||
        type: "PATCH",
 | 
			
		||||
        dataType: "json",
 | 
			
		||||
        headers: {
 | 
			
		||||
            "X-CSRFTOKEN": CSRF_TOKEN
 | 
			
		||||
        },
 | 
			
		||||
        data: {
 | 
			
		||||
            "resourcetype": "RecurrentTransaction",
 | 
			
		||||
            valid: !validated
 | 
			
		||||
        },
 | 
			
		||||
        success: function () {
 | 
			
		||||
            // Refresh jQuery objects
 | 
			
		||||
            $(".validate").click(de_validate);
 | 
			
		||||
 | 
			
		||||
            refreshBalance();
 | 
			
		||||
            // error if this method doesn't exist. Please define it.
 | 
			
		||||
            refreshHistory();
 | 
			
		||||
        },
 | 
			
		||||
        error: function(err) {
 | 
			
		||||
            addMsg("Une erreur est survenue lors de la validation/dévalidation " +
 | 
			
		||||
                "de cette transaction : " + err.responseText, "danger");
 | 
			
		||||
 | 
			
		||||
            refreshBalance();
 | 
			
		||||
            // error if this method doesn't exist. Please define it.
 | 
			
		||||
            refreshHistory();
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										206
									
								
								static/js/consos.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								static/js/consos.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,206 @@
 | 
			
		||||
// 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");
 | 
			
		||||
    $("#most_used").load("/note/consos/ #most_used");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
$(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");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // Switching in double consumptions mode should update the layout
 | 
			
		||||
    let double_conso_obj = $("#double_conso");
 | 
			
		||||
    double_conso_obj.click(function() {
 | 
			
		||||
        $("#consos_list_div").show();
 | 
			
		||||
        $("#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');
 | 
			
		||||
 | 
			
		||||
        let note_list_obj = $("#note_list");
 | 
			
		||||
        if (buttons.length > 0 && note_list_obj.text().length > 0) {
 | 
			
		||||
            $("#consos_list").html(note_list_obj.html());
 | 
			
		||||
            note_list_obj.html("");
 | 
			
		||||
 | 
			
		||||
            buttons.forEach(function(button) {
 | 
			
		||||
                $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
 | 
			
		||||
                    "consos_list"));
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let single_conso_obj = $("#single_conso");
 | 
			
		||||
    single_conso_obj.click(function() {
 | 
			
		||||
        $("#consos_list_div").hide();
 | 
			
		||||
        $("#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');
 | 
			
		||||
 | 
			
		||||
        let consos_list_obj = $("#consos_list");
 | 
			
		||||
        if (buttons.length > 0) {
 | 
			
		||||
            if (notes_display.length === 0 && consos_list_obj.text().length > 0) {
 | 
			
		||||
                $("#note_list").html(consos_list_obj.html());
 | 
			
		||||
                consos_list_obj.html("");
 | 
			
		||||
                buttons.forEach(function(button) {
 | 
			
		||||
                    $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
 | 
			
		||||
                        "note_list"));
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                buttons.length = 0;
 | 
			
		||||
                consos_list_obj.html("");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // Ensure we begin in single consumption. Removing these lines may cause problems when reloading.
 | 
			
		||||
    single_conso_obj.prop('checked', 'true');
 | 
			
		||||
    double_conso_obj.removeAttr('checked');
 | 
			
		||||
    $("label[for='double_conso']").attr('class', 'btn btn-sm btn-outline-primary');
 | 
			
		||||
 | 
			
		||||
    $("#consos_list_div").hide();
 | 
			
		||||
 | 
			
		||||
    $("#consume_all").click(consumeAll);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
notes = [];
 | 
			
		||||
notes_display = [];
 | 
			
		||||
buttons = [];
 | 
			
		||||
 | 
			
		||||
// When the user searches an alias, we update the auto-completion
 | 
			
		||||
autoCompleteNote("note", "alias_matched", "note_list", notes, notes_display,
 | 
			
		||||
    "alias", "note", "user_note", "profile_pic", function() {
 | 
			
		||||
        if (buttons.length > 0 && $("#single_conso").is(":checked")) {
 | 
			
		||||
            consumeAll();
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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)
 | 
			
		||||
 * @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) {
 | 
			
		||||
        if (b.id === template_id) {
 | 
			
		||||
            b.quantity += 1;
 | 
			
		||||
            button = b;
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let dc_obj = $("#double_conso");
 | 
			
		||||
    if (dc_obj.is(":checked") || notes_display.length === 0) {
 | 
			
		||||
        let list = dc_obj.is(":checked") ? "consos_list" : "note_list";
 | 
			
		||||
        let html = "";
 | 
			
		||||
        buttons.forEach(function(button) {
 | 
			
		||||
            html += li("conso_button_" + button.id, button.name
 | 
			
		||||
                + "<span class=\"badge badge-dark badge-pill\">" + button.quantity + "</span>");
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $("#" + list).html(html);
 | 
			
		||||
 | 
			
		||||
        buttons.forEach(function(button) {
 | 
			
		||||
            $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons, list));
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
        consumeAll();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Reset the page as its initial state.
 | 
			
		||||
 */
 | 
			
		||||
function reset() {
 | 
			
		||||
    notes_display.length = 0;
 | 
			
		||||
    notes.length = 0;
 | 
			
		||||
    buttons.length = 0;
 | 
			
		||||
    $("#note_list").html("");
 | 
			
		||||
    $("#alias_matched").html("");
 | 
			
		||||
    $("#consos_list").html("");
 | 
			
		||||
    $("#user_note").text("");
 | 
			
		||||
    $("#profile_pic").attr("src", "/media/pic/default.png");
 | 
			
		||||
    refreshHistory();
 | 
			
		||||
    refreshBalance();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Apply all transactions: all notes in `notes` buy each item in `buttons`
 | 
			
		||||
 */
 | 
			
		||||
function consumeAll() {
 | 
			
		||||
    notes_display.forEach(function(note_display) {
 | 
			
		||||
        buttons.forEach(function(button) {
 | 
			
		||||
            consume(note_display.id, button.dest, button.quantity * note_display.quantity, button.amount,
 | 
			
		||||
                button.name + " (" + button.category_name + ")", button.type, button.category_id, button.id);
 | 
			
		||||
       });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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 RecurrentTransaction)
 | 
			
		||||
 * @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": "RecurrentTransaction",
 | 
			
		||||
            "source": source,
 | 
			
		||||
            "destination": dest,
 | 
			
		||||
            "category": category,
 | 
			
		||||
            "template": template
 | 
			
		||||
        }, reset).fail(function (e) {
 | 
			
		||||
            reset();
 | 
			
		||||
 | 
			
		||||
            addMsg("Une erreur est survenue lors de la transaction : " + e.responseText, "danger");
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										161
									
								
								static/js/transfer.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								static/js/transfer.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,161 @@
 | 
			
		||||
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("");
 | 
			
		||||
    $("#user_note").val("");
 | 
			
		||||
    $("#profile_pic").attr("src", "/media/pic/default.png");
 | 
			
		||||
    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() {
 | 
			
		||||
            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;
 | 
			
		||||
 | 
			
		||||
                $.getJSON("/api/user/" + last.note.user + "/", function(user) {
 | 
			
		||||
                    $("#last_name").val(user.last_name);
 | 
			
		||||
                    $("#first_name").val(user.first_name);
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return true;
 | 
			
		||||
       });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // Ensure we begin in gift mode. Removing these lines may cause problems when reloading.
 | 
			
		||||
    $("#type_gift").prop('checked', 'true');
 | 
			
		||||
    $("#type_transfer").removeAttr('checked');
 | 
			
		||||
    $("#type_credit").removeAttr('checked');
 | 
			
		||||
    $("#type_debit").removeAttr('checked');
 | 
			
		||||
    $("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');
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
$("#transfer").click(function() {
 | 
			
		||||
    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,
 | 
			
		||||
                    "destination": dest.id
 | 
			
		||||
                }, function () {
 | 
			
		||||
                    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();
 | 
			
		||||
                }).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();
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    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,
 | 
			
		||||
                        "destination": dest.id
 | 
			
		||||
                    }, 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 été fait avec succès !", "success");
 | 
			
		||||
 | 
			
		||||
                        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();
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    } 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,
 | 
			
		||||
                "destination": dest,
 | 
			
		||||
                "last_name": $("#last_name").val(),
 | 
			
		||||
                "first_name": $("#first_name").val(),
 | 
			
		||||
                "bank": $("#bank").val()
 | 
			
		||||
            }, function () {
 | 
			
		||||
                addMsg("Le crédit/retrait a bien été effectué !", "success");
 | 
			
		||||
                reset();
 | 
			
		||||
            }).fail(function (err) {
 | 
			
		||||
                addMsg("Le crédit/transfert a échoué : " + err.responseText, "danger");
 | 
			
		||||
                reset();
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user