2016-08-06 22:19:52 +02:00
{% extends 'kfet/base.html' %}
{% load staticfiles %}
{% block extra_head %}
2016-08-18 05:33:42 +02:00
< link rel = "stylesheet" style = "text/css" href = "{% static 'kfet/css/jquery-ui.min.css' %}" >
2016-08-17 00:46:31 +02:00
< link rel = "stylesheet" style = "text/css" href = "{% static 'kfet/css/kpsul_grid.css' %}" >
2017-01-19 12:36:40 -02:00
< script src = "{% static " autocomplete_light / autocomplete . js " % } " type = "text/javascript" > < / script >
2016-08-14 19:59:36 +02:00
< script type = "text/javascript" src = "{% static 'kfet/js/js.cookie.js' %}" > < / script >
< script type = "text/javascript" src = "{% static 'kfet/js/reconnecting-websocket.js' %}" > < / script >
2016-08-18 05:33:42 +02:00
< script type = "text/javascript" src = "{% static 'kfet/js/jquery-ui.min.js' %}" > < / script >
2016-08-17 00:46:31 +02:00
< script type = "text/javascript" src = "{% static 'kfet/js/jquery-confirm.js' %}" > < / script >
2016-09-02 14:50:09 +02:00
< script type = "text/javascript" src = "{% static 'kfet/js/moment.js' %}" > < / script >
< script type = "text/javascript" src = "{% static 'kfet/js/moment-fr.js' %}" > < / script >
< script type = "text/javascript" src = "{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}" > < / script >
2016-08-24 19:52:07 +02:00
< script type = "text/javascript" src = "{% static 'kfet/js/history.js' %}" > < / script >
2016-08-06 22:19:52 +02:00
{% endblock %}
{% block title %}K-Psul{% endblock %}
2016-08-12 10:03:39 +02:00
{% block content-header %}{% endblock %}
2016-09-11 22:55:46 +02:00
{% block help %}
< div class = "row" >
< div class = "col-md-6" >
< div class = "block" >
< h4 > Opérations< / h4 >
< div class = "row" >
< div class = "col-xs-3" > < b > F3< / b > < / div >
< div class = "col-xs-9" > Charge< / div >
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > Shift + F3< / b > < / div >
< div class = "col-xs-9" > Retrait< / div >
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > F8< / b > < / div >
< div class = "col-xs-9" > Edition< / div >
< / div >
< / div >
< / div >
< div class = "col-md-6" >
< div class = "block" >
< h4 > Général< / h4 >
< div class = "row" >
< div class = "col-xs-3" > < b > F1< / b > < / div >
< div class = "col-xs-9" > Reset< / div >
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > F2< / b > < / div >
2016-12-09 00:33:18 -02:00
< div class = "col-xs-9" > Reset panier< / div >
2016-09-11 22:55:46 +02:00
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > Shift + F2< / b > < / div >
2016-12-09 00:33:18 -02:00
< div class = "col-xs-9" > Reset compte< / div >
2016-09-11 22:55:46 +02:00
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > F9< / b > < / div >
< div class = "col-xs-9" > Majoration< / div >
< / div >
< div class = "row" >
< div class = "col-xs-3" > < b > F10< / b > < / div >
< div class = "col-xs-9" > Hard reset< / div >
< / div >
< / div >
< / div >
< / div >
{% endblock %}
2016-08-06 22:19:52 +02:00
{% block content %}
2016-09-10 22:54:23 +02:00
{% include 'kfet/base_messages.html' %}
2016-08-16 03:36:14 +02:00
< div class = "row kpsul_top" >
< div class = "col-sm-8" >
< div class = "row" id = "account" >
< div class = "col-lg-3 col-xs-4" id = "account_form" >
{{ trigramme_form.trigramme }}
< / div >
< div class = "col-lg-9 col-xs-8" id = "account_data" >
< div id = "account-balance" class = "data_line" > < / div >
< div id = "account-name" class = "data_line" > < / div >
< div id = "account-nickname" class = "data_line" > < / div >
< div class = "data_line" >
< span id = "account-is_cof" > < / span >
< span id = "account-departement" > < / span >
< span id = "account-promo" > < / span >
< / div >
< div id = "account-email" class = "data_line" > < / div >
2016-08-19 02:56:45 +02:00
< div class = "buttons" >
< / div >
2016-08-16 03:36:14 +02:00
< / div >
< / div >
< / div >
< div class = "col-sm-4" id = "checkout" >
< div id = "checkout_form" >
{{ checkout_form.checkout }}
< / div >
< div id = "checkout_data" >
< div >
< b > En caisse:< / b > < span id = "checkout-balance" > < / span > €
< / div >
2016-08-21 16:45:25 +02:00
< div id = "last_statement" >
2016-08-16 03:36:14 +02:00
< / div >
2016-08-19 02:56:45 +02:00
< div class = "buttons" >
< / div >
2016-08-16 03:36:14 +02:00
< / div >
< / div >
2016-08-06 22:19:52 +02:00
< / div >
2016-08-16 03:36:14 +02:00
< div class = "row kpsul_middle" >
2016-08-17 00:46:31 +02:00
< div class = "col-sm-8 kpsul_middle_left_col" >
< div class = "kpsul_middle_left" >
< div class = "kpsul_middle_left_top" >
< div id = "special_operations" >
2016-11-30 23:12:58 -02:00
< button role = "button" class = "btn" id = "operation_deposit" > Charge (F3)< / button >
2016-12-02 00:24:49 -02:00
< button role = "button" class = "btn" id = "operation_withdraw" > Retrait (Shift+F3)< / button >
2016-11-30 23:12:58 -02:00
< button role = "button" class = "btn" id = "cool_reset" > RAZ (F1)< / button >
< button role = "button" class = "btn" id = "ask_addcost" > Major. (F9)< / button >
2016-08-17 00:46:31 +02:00
< / div >
< div id = "article_selection" >
2016-09-23 01:44:36 +02:00
< input type = "text" id = "article_autocomplete" autocomplete = "off" >
2016-08-17 00:46:31 +02:00
< input type = "number" id = "article_number" step = "1" min = "1" >
2016-12-13 22:31:52 -02:00
< span type = "stock" id = "article_stock" > < / span >
2016-08-17 00:46:31 +02:00
< input type = "hidden" id = "article_id" value = "" >
< / div >
< div id = "articles_data" >
< table >
< tbody >
< / tbody >
< / table >
< / div >
2016-08-16 03:36:14 +02:00
< / div >
2016-08-17 00:46:31 +02:00
< div class = "row kpsul_middle_left_bottom" >
2016-08-19 01:44:56 +02:00
< div class = "col-sm-6" >
< div id = "basket" >
< table id = "basket_table" >
< / table >
< / div >
2016-08-17 00:46:31 +02:00
< / div >
2016-12-22 01:13:14 -02:00
< div class = "col-sm-3" >
2016-08-19 01:44:56 +02:00
< div id = "basket_rel" >
< / div >
2016-08-17 00:46:31 +02:00
< / div >
2016-12-22 01:13:14 -02:00
< div class = "col-sm-3" >
< div id = "previous_op" >
< / div >
< / div >
2016-08-16 03:36:14 +02:00
< / div >
< / div >
< / div >
2016-08-19 01:08:57 +02:00
< div class = "col-sm-4 kpsul_middle_right_col" >
2016-08-18 18:26:58 +02:00
< div id = "history" >
2016-08-17 00:46:31 +02:00
< / div >
2016-08-16 03:36:14 +02:00
< / div >
2016-08-06 22:19:52 +02:00
< / div >
2016-08-17 11:44:58 +02:00
2016-08-24 19:52:07 +02:00
< div id = "history2" >
< / div >
2016-08-23 15:43:16 +02:00
< form id = "operationgroup_form" style = "display:none;" > {{ operationgroup_form }}< / form >
< form id = "operation_formset" style = "display:none;" > {{ operation_formset }}< / form >
2016-08-06 22:19:52 +02:00
2016-08-17 11:44:58 +02:00
< div style = "display:none;" id = "operation_empty_html" data-opeindex = "__prefix__" >
{{ operation_formset.empty_form }}
< / div >
2016-08-14 19:59:36 +02:00
2016-08-24 23:34:14 +02:00
{% csrf_token %}
2016-08-06 22:19:52 +02:00
< script type = "text/javascript" >
$(document).ready(function() {
// -----
// General
// -----
2016-08-31 00:54:16 +02:00
// Lock to avoid multiple requests
lock = 0;
2016-08-22 16:08:21 +02:00
// Retrieve settings
2016-08-24 19:52:07 +02:00
settings = {}
2016-08-22 16:08:21 +02:00
function resetSettings() {
2017-05-14 23:50:09 -03:00
return $.ajax({
2016-08-22 16:08:21 +02:00
dataType: "json",
url : "{% url 'kfet.kpsul.get_settings' %}",
method : "POST",
})
.done(function(data) {
settings['addcost_for'] = data['addcost_for'];
settings['addcost_amount'] = parseFloat(data['addcost_amount']);
settings['subvention_cof'] = parseFloat(data['subvention_cof']);
});
}
2016-08-06 22:19:52 +02:00
// -----
// Account data management
// -----
// Initializing
2016-08-19 02:56:45 +02:00
var account_container = $('#account');
2016-08-26 15:30:40 +02:00
var triInput = $('#id_trigramme');
2016-09-03 19:09:38 +02:00
var account_data = {};
2016-08-12 10:03:39 +02:00
var account_data_default = {
2016-08-19 02:56:45 +02:00
'id' : 0,
2016-08-12 10:03:39 +02:00
'name' : '',
'email': '',
2016-08-18 05:33:42 +02:00
'is_cof' : '',
2016-08-12 10:03:39 +02:00
'promo' : '',
'balance': '',
2016-08-16 03:36:14 +02:00
'trigramme' : '',
2016-08-13 10:07:35 +02:00
'is_frozen' : false,
2016-08-12 10:03:39 +02:00
'departement': '',
'nickname' : '',
2016-08-26 15:30:40 +02:00
};
2016-08-06 22:19:52 +02:00
// Display data
function displayAccountData() {
2016-08-18 05:33:42 +02:00
var balance = account_data['trigramme'] != 'LIQ' ? account_data['balance'] : '';
if (balance != '')
2017-02-12 19:05:41 -02:00
balance = amountToUKF(account_data['balance'], account_data['is_cof'], true);
2016-09-03 17:51:12 +02:00
var is_cof = account_data['trigramme'] ? account_data['is_cof'] : '';
if (is_cof !== '')
is_cof = is_cof ? '< b > COF< / b > ' : '< b > Non-COF< / b > ';
2016-08-16 03:36:14 +02:00
for (var elem in account_data) {
2016-08-18 05:33:42 +02:00
if (elem == 'balance') {
$('#account-balance').text(balance);
2016-09-03 17:51:12 +02:00
} else if (elem == 'is_cof') {
$('#account-is_cof').html(is_cof);
2016-08-18 05:33:42 +02:00
} else {
$('#account-'+elem).text(account_data[elem]);
}
2016-08-16 03:36:14 +02:00
}
if (account_data['is_frozen']) {
$('#account').attr('data-balance', 'frozen');
} else if (account_data['balance'] >= 5 || account_data['trigramme'] == 'LIQ') {
$('#account').attr('data-balance', 'ok');
2016-08-19 01:08:57 +02:00
} else if (account_data['balance'] == '') {
$('#account').attr('data-balance', '');
2016-08-16 03:36:14 +02:00
} else if (account_data['balance'] >= 0) {
$('#account').attr('data-balance', 'low');
} else {
$('#account').attr('data-balance', 'neg');
2016-08-06 22:19:52 +02:00
}
2016-08-19 02:56:45 +02:00
var buttons = '';
if (account_data['id'] != 0) {
2016-09-05 13:11:02 +02:00
var url_base = '{% url 'kfet.account.read' 'LIQ' %}';
url_base = url_base.substr(0, url_base.length - 3);
2016-12-09 01:05:30 -02:00
trigramme = encodeURIComponent(account_data['trigramme']) ;
buttons += '< a href = "'+ url_base + trigramme +'" class = "btn btn-primary" target = "_blank" title = "Modifier" > < span class = "glyphicon glyphicon-cog" > < / span > < / a > ';
2016-08-19 02:56:45 +02:00
}
2016-09-03 15:21:26 +02:00
if (account_data['id'] == 0) {
var trigramme = triInput.val().toUpperCase();
if (isValidTrigramme(trigramme)) {
2016-12-09 00:43:23 -02:00
var url_base = '{% url 'kfet.account.create' %}'
2016-12-09 01:05:30 -02:00
trigramme = encodeURIComponent(trigramme);
2016-09-06 17:37:18 +02:00
buttons += '< a href = "'+url_base+'?trigramme='+trigramme+'" class = "btn btn-primary" target = "_blank" title = "Créer" > < span class = "glyphicon glyphicon-plus" > < / span > < / a > ';
2016-12-09 00:43:23 -02:00
} else {
var url_base = '{% url 'kfet.account' %}'
2017-01-19 12:36:40 -02:00
buttons += '< button class = "btn btn-primary search" title = "Rechercher" > < span class = "glyphicon glyphicon-search" > < / span > < / button > ';
2016-09-03 15:21:26 +02:00
}
}
2016-08-19 02:56:45 +02:00
account_container.find('.buttons').html(buttons);
2016-08-06 22:19:52 +02:00
}
2017-01-19 12:36:40 -02:00
// Search for an account
function searchAccount() {
2017-01-19 22:53:58 -02:00
var content = '< input type = "text" name = "q" id = "search_autocomplete" spellcheck = "false" autofocus > < div id = "account_results" > < / div > ' ;
2017-01-19 12:36:40 -02:00
$.dialog({
title: 'Recherche de compte',
content: content,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
2017-03-20 19:08:48 -03:00
2017-01-19 12:36:40 -02:00
onOpen: function() {
var that=this ;
$('input#search_autocomplete').yourlabsAutocomplete({
2017-01-19 22:53:58 -02:00
url: '{% url "kfet.account.search.autocomplete" %}',
2017-01-19 12:36:40 -02:00
minimumCharacters: 2,
id: 'search_autocomplete',
choiceSelector: '.choice',
placeholder: "Chercher un utilisateur K-Fêt",
2017-01-19 22:53:58 -02:00
box: $("#account_results"),
2017-03-20 19:08:48 -03:00
fixPosition: function() {},
2017-01-19 12:36:40 -02:00
});
$('input#search_autocomplete').bind(
'selectChoice',
function(e, choice, autocomplete) {
autocomplete.hide() ;
triInput.val(choice.find('.trigramme').text()) ;
triInput.trigger('input') ;
that.close() ;
});
}
});
}
account_container.on('click', '.search', function () {
searchAccount() ;
}) ;
2017-01-19 22:54:11 -02:00
account_container.on('keydown', function(e) {
if (e.which == 70 & & e.ctrlKey) {
// Ctrl + F : universal search shortcut
searchAccount() ;
e.preventDefault() ;
}
});
2016-08-06 22:19:52 +02:00
// Clear data
function resetAccountData() {
2016-08-12 10:03:39 +02:00
account_data = account_data_default;
2016-08-06 22:19:52 +02:00
$('#id_on_acc').val(0);
displayAccountData();
}
2016-08-18 05:33:42 +02:00
function resetAccount() {
triInput.val('');
2016-09-03 19:09:38 +02:00
resetAccountData();
2016-08-18 05:33:42 +02:00
}
2016-08-06 22:19:52 +02:00
// Store data
function storeAccountData(data) {
2016-08-16 03:36:14 +02:00
account_data = $.extend({}, account_data_default, data);
2016-08-18 16:38:44 +02:00
account_data['balance'] = parseFloat(account_data['balance']);
2016-08-19 03:07:21 +02:00
$('#id_on_acc').val(account_data['id']);
2016-08-06 22:19:52 +02:00
displayAccountData();
}
// Retrieve via ajax
function retrieveAccountData(tri) {
$.ajax({
dataType: "json",
2016-08-26 15:30:40 +02:00
url : "{% url 'kfet.account.read.json' %}",
2016-08-06 22:19:52 +02:00
method : "POST",
data : { trigramme: tri },
})
2016-08-17 00:46:31 +02:00
.done(function(data) {
storeAccountData(data);
2016-08-18 16:38:44 +02:00
articleSelect.focus();
updateBasketAmount();
updateBasketRel();
2016-08-17 00:46:31 +02:00
})
.fail(function() { resetAccountData() });
2016-08-06 22:19:52 +02:00
}
// Event listener
triInput.on('input', function() {
2016-08-16 03:36:14 +02:00
var tri = triInput.val().toUpperCase();
2016-08-06 22:19:52 +02:00
// Checking if tri is valid to avoid sending requests
2016-08-26 15:30:40 +02:00
if (isValidTrigramme(tri)) {
2016-08-06 22:19:52 +02:00
retrieveAccountData(tri);
} else {
resetAccountData();
}
});
2016-08-31 01:03:19 +02:00
triInput.on('keydown', function(e) {
if (e.keyCode == 40) {
// Arrow Down - Shorcut to LIQ
triInput.val('LIQ');
triInput.trigger('input');
}
});
2016-08-06 22:19:52 +02:00
// -----
// Checkout data management
// -----
// Initializing
2016-08-19 02:56:45 +02:00
var checkout_container = $('#checkout');
2016-08-06 22:19:52 +02:00
var checkoutInput = $('#id_checkout_select');
var checkout_data = {}
2016-08-12 10:03:39 +02:00
var checkout_data_default = {
'id' : 0,
'name': '',
'balance' : '',
'valid_from': '',
'valid_to' : '',
'last_statement_balance': '',
'last_statement_at' : '',
'last_statement_by_trigramme' : '',
'last_statement_by_first_name': '',
'last_statement_by_last_name' : '' ,
}
2016-08-21 16:45:25 +02:00
var last_statement_container = $('#last_statement');
2016-08-23 03:39:33 +02:00
var last_statement_html_default = '< b > Dernier relevé: < / b > < br > < span id = "checkout-last_statement_balance" > < / span > € le < span id = "checkout-last_statement_at" > < / span > par < span id = "checkout-last_statement_by_trigramme" > < / span > ';
2016-08-21 16:45:25 +02:00
2017-04-06 16:45:44 +02:00
// If only one checkout is available, select it
var checkout_choices = checkoutInput.find("option[value!='']");
if (checkout_choices.length == 1) {
$(checkout_choices[0]).prop("selected", true);
}
2016-08-06 22:19:52 +02:00
// Display data
function displayCheckoutData() {
2016-08-23 03:39:33 +02:00
var at_formated = '';
2016-08-21 16:45:25 +02:00
if (checkout_data['last_statement_at']) {
last_statement_container.html(last_statement_html_default);
2016-08-23 04:35:09 +02:00
var at = moment.tz(checkout_data['last_statement_at'], 'UTC');
at_formated = at.tz('Europe/Paris').format('DD/MM/YY à HH:mm');
2016-08-21 16:45:25 +02:00
} else {
last_statement_container.html('');
}
2016-08-16 03:36:14 +02:00
for (var elem in checkout_data) {
$('#checkout-'+elem).text(checkout_data[elem]);
2016-08-06 22:19:52 +02:00
}
2016-08-23 03:39:33 +02:00
$('#checkout-last_statement_at').text(at_formated);
2016-08-19 02:56:45 +02:00
var buttons = '';
if (checkout_data['id'] !== 0) {
2016-09-05 19:50:16 +02:00
var url_base = "{% url 'kfet.checkoutstatement.create' 1 %}";
url_base = url_base.substr(0,url_base.length - 16);
buttons += '< a class = "btn btn-primary" href = "'+url_base+checkout_data['id']+'/statements/add" title = "Effectuer un relevé" target = "_blank" > < span class = "glyphicon glyphicon-euro" > < / span > < / a > ';
buttons += '< a class = "btn btn-primary" href = "'+url_base+checkout_data['id']+'" title = "Modifier" target = "_blank" > < span class = "glyphicon glyphicon-cog" > < / span > < / a > ';
2016-08-19 02:56:45 +02:00
}
checkout_container.find('.buttons').html(buttons);
2016-08-06 22:19:52 +02:00
}
// Clear data
2016-08-18 05:33:42 +02:00
function resetCheckout() {
2016-08-12 10:03:39 +02:00
checkout_data = checkout_data_default;
2016-08-06 22:19:52 +02:00
$('#id_checkout').val(0);
2016-08-17 00:46:31 +02:00
checkoutInput.find('option:first').prop('selected', true);
2016-08-06 22:19:52 +02:00
displayCheckoutData();
}
// Store data
function storeCheckoutData(data) {
2016-08-16 03:36:14 +02:00
checkout_data = $.extend({}, checkout_data_default, data);
2016-08-12 10:03:39 +02:00
$('#id_checkout').val(checkout_data['id']);
2016-08-06 22:19:52 +02:00
displayCheckoutData();
}
// Retrieve data
function retrieveCheckoutData(id) {
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.checkout_data' %}",
method : "POST",
data : { 'pk': id },
})
.done(function(data) { storeCheckoutData(data) })
2016-08-18 05:33:42 +02:00
.fail(function() { resetCheckout() });
2016-08-06 22:19:52 +02:00
}
// Event listener
checkoutInput.on('change', function() {
retrieveCheckoutData(checkoutInput.val());
2016-09-04 17:07:44 +02:00
if (account_data['trigramme']) {
articleSelect.focus().select();
} else {
triInput.focus().select();
}
2016-08-06 22:19:52 +02:00
});
2016-08-19 06:20:37 +02:00
// -----
// Auth
// -----
2016-08-23 15:43:16 +02:00
function askComment(callback) {
var comment = $('#id_comment').val();
$.confirm({
title: 'Commentaire requis',
content: '< input type = "text" name = "comment_opegroup" value = "'+comment+'" autofocus > ',
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
confirm: function() {
$('#id_comment').val(this.$content.find('input').val());
callback();
},
onOpen: function() {
var that = this;
2016-09-02 14:17:11 +02:00
this.$content.find('input').on('keydown', function(e) {
2016-08-19 06:20:37 +02:00
if (e.keyCode == 13)
that.$confirmButton.click();
});
},
onClose: function() { this._lastFocused = articleSelect; }
});
}
2016-08-23 15:43:16 +02:00
function resetComment() {
$('#id_comment').val('');
}
2016-08-20 17:55:46 +02:00
// -----
// Errors ajax
// -----
2016-08-26 15:38:44 +02:00
2016-08-20 17:55:46 +02:00
function displayErrors(html) {
$.alert({
title: 'Erreurs',
content: html,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
});
}
2016-08-06 22:19:52 +02:00
// -----
// Perform operations
// -----
var performButton = $('#perform_operations');
var operationGroup = $('#operationgroup_form');
var operations = $('#operation_formset');
2016-08-19 06:20:37 +02:00
function performOperations(password = '') {
2016-08-31 00:54:16 +02:00
if (lock == 1)
return false;
lock = 1;
2016-08-09 11:02:26 +02:00
var data = operationGroup.serialize() + '& ' + operations.serialize();
2016-08-06 22:19:52 +02:00
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.perform_operations' %}",
method : "POST",
data : data,
2016-08-19 06:20:37 +02:00
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
2016-08-06 22:19:52 +02:00
})
.done(function(data) {
2016-12-22 01:13:14 -02:00
updatePreviousOp();
2016-08-19 06:20:37 +02:00
coolReset();
2016-08-31 00:54:16 +02:00
lock = 0;
2016-08-06 22:19:52 +02:00
})
2016-08-14 19:59:36 +02:00
.fail(function($xhr) {
2016-08-06 22:19:52 +02:00
var data = $xhr.responseJSON;
2016-08-19 06:20:37 +02:00
switch ($xhr.status) {
case 403:
2016-08-26 15:30:40 +02:00
requestAuth(data, performOperations, articleSelect);
2016-08-19 06:20:37 +02:00
break;
2016-08-20 17:55:46 +02:00
case 400:
2016-08-23 15:43:16 +02:00
if ('need_comment' in data['errors']) {
askComment(performOperations);
} else {
displayErrors(getErrorsHtml(data));
}
2016-08-20 17:55:46 +02:00
break;
2016-08-19 06:20:37 +02:00
}
2016-08-31 00:54:16 +02:00
lock = 0;
2016-08-06 22:19:52 +02:00
});
}
// Event listeners
performButton.on('click', function() {
performOperations();
});
2016-08-09 11:02:26 +02:00
2016-08-14 19:59:36 +02:00
// -----
2016-08-09 11:02:26 +02:00
// Cancel operations
2016-08-14 19:59:36 +02:00
// -----
2016-08-09 11:02:26 +02:00
var cancelButton = $('#cancel_operations');
var cancelForm = $('#cancel_form');
2016-08-19 06:20:37 +02:00
function cancelOperations(opes_array, password = '') {
2016-08-31 00:54:16 +02:00
if (lock == 1)
return false
lock = 1;
2016-08-18 18:26:58 +02:00
var data = { 'operations' : opes_array }
2016-08-09 11:02:26 +02:00
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.cancel_operations' %}",
method : "POST",
data : data,
2016-08-19 06:20:37 +02:00
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
2016-08-09 11:02:26 +02:00
})
.done(function(data) {
2016-08-19 06:20:37 +02:00
coolReset();
2016-08-31 00:54:16 +02:00
lock = 0;
2016-08-09 11:02:26 +02:00
})
2016-08-14 20:27:13 +02:00
.fail(function($xhr) {
2016-08-09 11:02:26 +02:00
var data = $xhr.responseJSON;
2016-08-19 06:20:37 +02:00
switch ($xhr.status) {
case 403:
requestAuth(data, function(password) {
cancelOperations(opes_array, password);
2016-08-26 15:30:40 +02:00
}, triInput);
2016-08-19 06:20:37 +02:00
break;
2016-08-20 17:56:38 +02:00
case 400:
displayErrors(getErrorsHtml(data));
break;
2016-08-19 06:20:37 +02:00
}
2016-08-31 00:54:16 +02:00
lock = 0;
2016-08-09 11:02:26 +02:00
});
}
// Event listeners
cancelButton.on('click', function() {
cancelOperations();
});
2016-08-14 19:59:36 +02:00
// -----
2016-08-14 23:37:05 +02:00
// Articles data
2016-08-14 19:59:36 +02:00
// -----
var articles_container = $('#articles_data tbody');
2016-08-17 00:46:31 +02:00
var article_category_default_html = '< tr class = "category" > < td colspan = "3" > < / td > < / tr > ';
var article_default_html = '< tr class = "article" > < td class = "name" > < / td > < td class = "price" > < / td > < td class = "stock" > < / td > < / tr > ';
2016-08-14 19:59:36 +02:00
function addArticle(article) {
2016-08-14 23:37:05 +02:00
var article_html = $(article_default_html);
2016-09-06 16:33:04 +02:00
article_html.attr('id', 'data-article-'+article['id']);
article_html.addClass('data-category-'+article['category_id']);
2016-08-14 19:59:36 +02:00
for (var elem in article) {
2016-08-14 23:37:05 +02:00
article_html.find('.'+elem).text(article[elem])
2016-08-14 19:59:36 +02:00
}
2016-12-13 22:31:52 -02:00
if (-5 < = article['stock'] & & article['stock'] < = 5) {
article_html.addClass('low-stock');
}
2017-02-13 10:51:24 -02:00
article_html.find('.price').text(amountToUKF(article['price'], false, false)+' UKF');
2016-08-17 00:46:31 +02:00
var category_html = articles_container
2016-09-06 16:33:04 +02:00
.find('#data-category-'+article['category_id']);
2016-08-17 00:46:31 +02:00
if (category_html.length == 0) {
category_html = $(article_category_default_html);
2016-09-06 16:33:04 +02:00
category_html.attr('id', 'data-category-'+article['category_id']);
2016-08-17 00:46:31 +02:00
category_html.find('td').text(article['category__name']);
var added = false;
articles_container.find('.category').each(function() {
if (article['category__name'].toLowerCase() < $(this).text().toLowerCase()) {
$(this).before(category_html);
added = true;
return false;
}
});
if (!added) articles_container.append(category_html);
}
2016-09-06 16:38:48 +02:00
var $after = articles_container.find('#data-category-'+article['category_id']);
2016-08-17 00:46:31 +02:00
articles_container
2016-09-06 16:33:04 +02:00
.find('.article.data-category-'+article['category_id']).each(function() {
2016-09-06 16:38:48 +02:00
if (article['name'].toLowerCase < $('.name', this).text().toLowerCase())
2016-08-17 00:46:31 +02:00
return false;
2016-09-06 16:38:48 +02:00
$after = $(this);
2016-08-17 00:46:31 +02:00
});
2016-09-06 16:38:48 +02:00
$after.after(article_html);
2016-08-16 03:36:14 +02:00
// Pour l'autocomplétion
2017-03-17 19:17:36 +01:00
articlesList.push([article['name'],article['id'],article['category_id'],article['price'], article['stock'],article['category__has_addcost']]);
2016-08-14 19:59:36 +02:00
}
2016-08-18 05:33:42 +02:00
function getArticles() {
2016-08-14 19:59:36 +02:00
$.ajax({
dataType: "json",
2016-08-14 23:37:05 +02:00
url : "{% url 'kfet.kpsul.articles_data' %}",
2016-08-14 19:59:36 +02:00
method : "GET",
})
.done(function(data) {
2016-08-14 23:37:05 +02:00
for (var i=0; i< data [ ' articles ' ] . length ; i + + ) {
addArticle(data['articles'][i]);
}
2016-08-14 19:59:36 +02:00
});
}
2016-08-18 05:33:42 +02:00
function resetArticles() {
articles_container.find('tr').remove();
articlesList = [];
}
2016-08-16 03:36:14 +02:00
// -----
// Article selection
// -----
var articleSelect = $('#article_autocomplete');
var articleId = $('#article_id');
var articleNb = $('#article_number');
2016-12-13 22:31:52 -02:00
var articleStock = $('#article_stock');
2016-12-13 01:11:35 -02:00
// 8:Backspace|9:Tab|13:Enter|38-40:Arrows|46:DEL|112-117:F1-6|119-123:F8-F12
var normalKeys = /^(8|9|13|37|38|39|40|46|112|113|114|115|116|117|119|120|121|122|123)$/;
2016-08-16 03:36:14 +02:00
var articlesList = [];
function deleteNonMatching(array, str) {
var dup = [];
var lower_str = str.toLowerCase();
for (var i=0; i< array.length ; i + + ) {
if (((array[i][0]).toLowerCase()).indexOf(lower_str) === 0)
dup.push(array[i])
}
return dup;
}
function callbackForPrefix(elt) {
return elt[0].toLowerCase();
}
function sharedPrefix(array) {
var dup = array.map(callbackForPrefix);
dup.sort(); // On trie l'array
// On récupère le préfixe du premier et du dernier élément
var first = dup[0], last = dup[array.length-1],
length = first.length, i = 0;
while (i < length & & first . charAt ( i ) = = = last . charAt ( i ) ) i + + ;
return first.substring(0, i);
}
2016-08-17 00:46:31 +02:00
function displayMatchedArticles(array) {
var categories_to_display = [];
for (var i=0; i< articlesList.length ; i + + ) {
if (array.indexOf(articlesList[i]) > -1) {
2016-09-06 16:33:04 +02:00
articles_container.find('#data-article-'+articlesList[i][1]).show();
2016-08-17 00:46:31 +02:00
if (categories_to_display.indexOf(articlesList[i][2]) == -1)
categories_to_display.push(articlesList[i][2]);
} else {
2016-09-06 16:33:04 +02:00
articles_container.find('#data-article-'+articlesList[i][1]).hide();
2016-08-17 00:46:31 +02:00
}
}
articles_container.find('.category').hide();
for (var i=0; i< categories_to_display.length ; i + + ) {
articles_container
2016-09-06 16:33:04 +02:00
.find('#data-category-'+categories_to_display[i])
2016-08-17 00:46:31 +02:00
.show();
}
}
function updateMatchedArticles(str, commit = true) {
var lower_str = str.toLowerCase();
var articlesMatch = deleteNonMatching(articlesList, lower_str);
if (articlesMatch.length == 1) {
articleId.val(0);
// 1 seul résultat, victoire
if (commit) {
articleId.val(articlesMatch[0][1]);
articleSelect.val(articlesMatch[0][0]);
2016-12-13 22:31:52 -02:00
articleStock.text('/'+articlesMatch[0][4]);
2016-08-17 00:46:31 +02:00
displayMatchedArticles(articlesList);
return true;
}
displayMatchedArticles(articlesMatch);
} else if (articlesMatch.length > 1) {
articleId.val(0);
if (commit)
articleSelect.val(sharedPrefix(articlesMatch));
displayMatchedArticles(articlesMatch);
}
return false;
}
2016-09-22 17:59:51 +02:00
// A utiliser après la sélection d'un article
function goToArticleNb() {
articleNb.val('1');
articleNb.focus().select();
}
2016-09-05 20:35:45 +02:00
articleSelect.on('keydown', function(e) {
2016-08-17 00:46:31 +02:00
var text = articleSelect.val();
2016-08-16 03:36:14 +02:00
// Comportement normal pour ces touches
if (normalKeys.test(e.keyCode) || e.ctrlKey) {
2016-08-19 06:20:37 +02:00
if (text == '' & & e.keyCode == 13)
2016-08-17 11:44:58 +02:00
performOperations();
2016-08-16 03:36:14 +02:00
if (e.keyCode == 8)
2016-08-17 00:46:31 +02:00
updateMatchedArticles(text.substring(0,text.length-1), commit=false);
2016-09-02 14:17:11 +02:00
if (e.charCode == 65 & & e.ctrlKey) {
2016-08-16 03:36:14 +02:00
articleId.val(0);
articleSelect.val('');
}
return true;
2016-09-05 20:35:45 +02:00
}
2016-09-22 17:59:51 +02:00
if (updateMatchedArticles(text+e.key))
goToArticleNb();
2016-08-16 03:36:14 +02:00
return false;
});
2016-09-22 17:59:51 +02:00
function getArticleId($article) {
return $article.attr('id').split('-')[2];
}
function getArticleName($article) {
return $article.find('.name').text();
}
2016-12-14 23:40:23 -02:00
function getArticleStock($article) {
return $article.find('.stock').text();
}
2016-09-22 17:59:51 +02:00
// Sélection des articles à la souris/tactile
articles_container.on('click', '.article', function() {
articleId.val(getArticleId($(this)));
articleSelect.val(getArticleName($(this)));
2016-12-14 23:40:23 -02:00
articleStock.text('/'+getArticleStock($(this)));
2016-09-22 17:59:51 +02:00
displayMatchedArticles(articlesList);
goToArticleNb();
});
2016-08-17 00:46:31 +02:00
function is_nb_ok(nb) {
return /^[0-9]+$/.test(nb) & & nb > 0 & & nb < = 24;
}
2016-09-02 14:17:11 +02:00
articleNb.on('keydown', function(e) {
2016-08-17 00:46:31 +02:00
if (e.keyCode == 13 & & is_nb_ok(articleNb.val()) & & articleId.val() > 0) {
2016-08-18 05:33:42 +02:00
addPurchase(articleId.val(), articleNb.val());
2016-08-17 00:46:31 +02:00
articleSelect.val('');
articleNb.val('');
2016-12-20 16:48:09 -02:00
articleStock.text('');
2016-08-17 00:46:31 +02:00
articleSelect.focus();
displayMatchedArticles(articlesList);
return false;
}
if (normalKeys.test(e.keyCode) || arrowKeys.test(e.keyCode) || e.keyCode == 8 || e.ctrlKey) {
2016-09-02 14:17:11 +02:00
if (e.ctrlKey & & e.charCode == 65)
2016-08-17 00:46:31 +02:00
articleNb.val('');
return true;
}
var nb = articleNb.val()+e.key;
if (is_nb_ok(nb))
return true;
return false;
});
2016-08-18 05:33:42 +02:00
// -----
// Basket
// -----
2016-12-20 16:48:09 -02:00
var item_basket_default_html = '< tr > < td class = "amount" > < / td > < td class = "number" > < / td > < td > < span class = "lowstock glyphicon glyphicon-alert" > < / span > < / td > < td class = "name" > < / td > < / tr > ';
2016-08-18 05:33:42 +02:00
var basket_container = $('#basket table');
var arrowKeys = /^(37|38|39|40)$/;
2016-08-18 16:38:44 +02:00
function amountEuroPurchase(id,nb) {
2016-08-18 05:33:42 +02:00
var i = 0;
while (i< articlesList.length & & id ! = articlesList [ i ] [ 1 ] ) i + + ;
article_data = articlesList[i];
2016-08-22 16:42:03 +02:00
var amount_euro = - article_data[3] * nb ;
2017-03-17 19:17:36 +01:00
if (settings['addcost_for']
& & settings['addcost_amount']
& & account_data['trigramme'] != settings['addcost_for']
2017-04-04 16:57:17 +02:00
& & article_data[5])
2017-03-17 19:17:36 +01:00
amount_euro -= settings['addcost_amount'] * nb;
2016-08-18 05:33:42 +02:00
var reduc_divisor = 1;
if (account_data['is_cof'])
2016-08-22 16:08:21 +02:00
reduc_divisor = 1 + settings['subvention_cof'] / 100;
2017-04-06 00:11:41 +02:00
return (amount_euro / reduc_divisor).toFixed(2);
2016-08-18 16:38:44 +02:00
}
function addPurchase(id, nb) {
2016-12-13 01:13:16 -02:00
var existing = false;
formset_container.find('[data-opeindex]').each(function () {
var opeindex = $(this).attr('data-opeindex');
var article_id = $(this).find('#id_form-'+opeindex+'-article').val();
if (article_id == id) {
existing = true ;
addExistingPurchase(opeindex, nb);
}
});
if (!existing) {
2017-04-06 00:11:41 +02:00
var amount_euro = amountEuroPurchase(id, nb);
2016-12-13 01:13:16 -02:00
var index = addPurchaseToFormset(article_data[1], nb, amount_euro);
article_basket_html = $(item_basket_default_html);
article_basket_html
.attr('data-opeindex', index)
2016-12-20 01:09:22 -02:00
.find('.number').text('('+nb+'/'+article_data[4]+')').end()
2016-12-13 01:13:16 -02:00
.find('.name').text(article_data[0]).end()
2017-02-13 10:51:24 -02:00
.find('.amount').text(amountToUKF(amount_euro, account_data['is_cof']), false);
2017-02-12 11:09:03 -02:00
basket_container.prepend(article_basket_html);
2016-12-13 01:41:59 -02:00
if (is_low_stock(id, nb))
2016-12-20 01:09:22 -02:00
article_basket_html.find('.lowstock')
2016-12-20 16:48:09 -02:00
.show();
2016-12-13 01:13:16 -02:00
updateBasketRel();
}
2016-08-18 05:33:42 +02:00
}
2016-12-13 01:41:59 -02:00
function is_low_stock(id, nb) {
var i = 0 ;
while (i< articlesList.length & & id ! = articlesList [ i ] [ 1 ] ) i + + ;
article_data = articlesList[i];
stock = article_data[4] ;
2016-12-14 23:40:42 -02:00
return (-5 < = stock - nb & & stock - nb < = 5);
2016-12-13 01:41:59 -02:00
}
2017-03-24 20:52:49 -03:00
function addDeposit(amount) {
2016-08-18 05:33:42 +02:00
var deposit_basket_html = $(item_basket_default_html);
2016-08-19 01:08:57 +02:00
var amount = parseFloat(amount).toFixed(2);
2017-03-24 20:52:49 -03:00
var index = addDepositToFormset(amount);
2016-08-18 05:33:42 +02:00
deposit_basket_html
.attr('data-opeindex', index)
2016-08-19 01:08:57 +02:00
.find('.number').text(amount+"€").end()
2017-03-24 20:52:49 -03:00
.find('.name').text('Charge').end()
2017-03-24 21:07:32 -03:00
.find('.amount').text(amountToUKF(amount, account_data['is_cof'], false));
2017-03-24 20:52:49 -03:00
basket_container.prepend(deposit_basket_html);
updateBasketRel();
}
function addEdit(amount) {
2016-08-18 05:33:42 +02:00
var deposit_basket_html = $(item_basket_default_html);
2016-08-19 01:08:57 +02:00
var amount = parseFloat(amount).toFixed(2);
2017-03-24 20:52:49 -03:00
var index = addEditToFormset(amount);
2016-08-18 05:33:42 +02:00
deposit_basket_html
.attr('data-opeindex', index)
2016-08-19 01:08:57 +02:00
.find('.number').text(amount+"€").end()
2017-03-24 20:52:49 -03:00
.find('.name').text('Édition').end()
2017-02-12 19:05:41 -02:00
.find('.amount').text(amountToUKF(amount, account_data['is_cof'], false));
2016-08-18 05:33:42 +02:00
basket_container.prepend(deposit_basket_html);
updateBasketRel();
}
function addWithdraw(amount) {
var withdraw_basket_html = $(item_basket_default_html);
2016-08-19 01:08:57 +02:00
var amount = (- parseFloat(amount)).toFixed(2);
2016-08-18 05:33:42 +02:00
var index = addWithdrawToFormset(amount);
withdraw_basket_html
.attr('data-opeindex', index)
2016-08-19 01:08:57 +02:00
.find('.number').text(amount+"€").end()
2016-08-18 05:33:42 +02:00
.find('.name').text('Retrait').end()
2017-02-12 19:05:41 -02:00
.find('.amount').text(amountToUKF(amount, account_data['is_cof'], false));
2016-08-18 05:33:42 +02:00
basket_container.prepend(withdraw_basket_html);
updateBasketRel();
}
basket_container.selectable({
filter: 'tr',
});
2016-09-02 14:17:11 +02:00
$(document).on('keydown', function (e) {
2016-12-13 01:11:35 -02:00
switch(e.which) {
case 46:
// DEL (Suppr)
basket_container.find('.ui-selected').each(function () {
deleteFromBasket($(this).data('opeindex'));
});
break;
case 38:
// Arrow up
basket_container.find('.ui-selected').each(function () {
addExistingPurchase($(this).data('opeindex'), 1);
});
break;
case 40:
// Arrow down
basket_container.find('.ui-selected').each(function () {
addExistingPurchase($(this).data('opeindex'), -1);
});
break;
2016-08-18 05:33:42 +02:00
}
});
2016-08-18 16:38:44 +02:00
function isBasketEmpty() {
return !basket_container.find('[data-opeindex]').length;
}
2016-08-18 05:33:42 +02:00
function getAmountBasket() {
var total = 0;
formset_container.find('[data-opeindex]').each(function () {
var opeindex = $(this).attr('data-opeindex');
if (!$(this).find('#id_form-'+opeindex+'-DELETE').prop('checked'))
total += parseFloat($(this).find('#id_form-'+opeindex+'-amount').val());
});
return total;
}
2016-08-18 16:38:44 +02:00
function updateBasketAmount() {
formset_container.find('[data-opeindex]').each(function () {
var opeindex = $(this).attr('data-opeindex');
var deleted = $(this).find('#id_form-'+opeindex+'-DELETE').prop('checked');
var type = $(this).find('#id_form-'+opeindex+'-type').val();
var article_id = $(this).find('#id_form-'+opeindex+'-article').val();
var article_nb = $(this).find('#id_form-'+opeindex+'-article_nb').val();
var amount = $(this).find('#id_form-'+opeindex+'-amount');
if (!deleted & & type == "purchase")
amount.val(amountEuroPurchase(article_id, article_nb));
2017-02-12 19:05:41 -02:00
basket_container.find('[data-opeindex='+opeindex+'] .amount').text(amountToUKF(amount.val(), account_data['is_cof'], false));
2016-08-18 16:38:44 +02:00
});
}
2016-08-18 05:33:42 +02:00
var basketrel_container = $('#basket_rel');
function updateBasketRel() {
2016-08-18 16:38:44 +02:00
var basketrel_html = '';
if (account_data['trigramme'] == 'LIQ' & & !isBasketEmpty()) {
var amount = - getAmountBasket();
basketrel_html += '< div > Total: '+amount.toFixed(2)+' €< / div > ';
if (amount < 5 )
basketrel_html += '< div > Sur 5€: '+ (5-amount).toFixed(2) +' €< / div > ';
if (amount < 10 )
basketrel_html += '< div > Sur 10€: '+ (10-amount).toFixed(2) +' €< / div > ';
if (amount < 20 )
basketrel_html += '< div > Sur 20€: '+ (20-amount).toFixed(2) +' €< / div > ';
} else if (account_data['trigramme'] != '' & & !isBasketEmpty()) {
var amount = getAmountBasket();
2017-02-12 19:05:41 -02:00
var amountUKF = amountToUKF(amount, account_data['is_cof'], false);
2016-08-18 16:38:44 +02:00
var newBalance = account_data['balance'] + amount;
2017-02-12 19:05:41 -02:00
var newBalanceUKF = amountToUKF(newBalance, account_data['is_cof'], true);
2016-08-18 16:38:44 +02:00
basketrel_html += '< div > Total: '+amountUKF+'< / div > ';
basketrel_html += '< div > Nouveau solde: '+newBalanceUKF+'< / div > ';
if (newBalance < 0 )
basketrel_html += '< div > Manque: '+ (-newBalance).toFixed(2) +' €< / div > ';
2016-08-18 05:33:42 +02:00
}
basketrel_container.html(basketrel_html);
2016-08-18 16:38:44 +02:00
}
2016-08-18 05:33:42 +02:00
function deleteFromBasket(opeindex) {
basket_container.find('[data-opeindex='+opeindex+']').remove();
deleteFromFormset(opeindex);
2016-08-18 16:38:44 +02:00
updateBasketRel();
2016-08-18 05:33:42 +02:00
}
2016-12-13 00:33:25 -02:00
function addExistingPurchase(opeindex, nb) {
2017-02-12 11:02:54 -02:00
var type = formset_container.find("#id_form-"+opeindex+"-type").val();
var id = formset_container.find("#id_form-"+opeindex+"-article").val();
var nb_before = formset_container.find("#id_form-"+opeindex+"-article_nb").val();
var nb_after = parseInt(nb_before) + parseInt(nb);
var amountEuro_after = amountEuroPurchase(id, nb_after);
var amountUKF_after = amountToUKF(amountEuro_after, account_data['is_cof']);
2016-12-13 00:33:25 -02:00
if (type == 'purchase') {
if (nb_after == 0) {
deleteFromBasket(opeindex);
} else if (nb_after > 0 & & nb_after < = 25) {
2017-02-12 11:02:54 -02:00
if (nb_before > 0) {
var article_html = basket_container.find('[data-opeindex='+opeindex+']');
article_html.find('.amount').text(amountUKF_after).end()
2017-02-12 11:09:03 -02:00
.find('.number').text('('+nb_after+'/'+article_data[4]+')').end() ;
2017-02-12 11:02:54 -02:00
} else {
article_html = $(item_basket_default_html);
article_html
.attr('data-opeindex', opeindex)
.find('.number').text('('+nb_after+'/'+article_data[4]+')').end()
.find('.name').text(article_data[0]).end()
.find('.amount').text(amountUKF_after);
basket_container.prepend(article_basket_html);
}
2016-12-20 01:09:22 -02:00
2017-02-12 11:02:54 -02:00
if (is_low_stock(id, nb_after))
article_html.find('.lowstock')
.show();
else
article_html.find('.lowstock')
.hide();
2016-12-13 00:33:25 -02:00
updateExistingFormset(opeindex, nb_after, amountEuro_after);
updateBasketRel();
}
}
}
2016-08-17 00:46:31 +02:00
function resetBasket() {
basket_container.find('tr').remove();
2016-08-17 11:56:42 +02:00
mngmt_total_forms = 1;
mngmt_total_forms_input.val(1);
formset_container.find('div').remove();
2016-08-18 05:33:42 +02:00
updateBasketRel();
articleId.val(0);
articleSelect.val('');
articleNb.val('');
2016-12-13 22:31:52 -02:00
articleStock.text('');
2016-08-18 05:33:42 +02:00
displayMatchedArticles(articlesList);
2016-08-17 00:46:31 +02:00
}
// -----
2016-08-18 05:33:42 +02:00
// Ask deposit or withdraw
2016-08-17 00:46:31 +02:00
// -----
2017-03-24 20:52:49 -03:00
function askDeposit() {
2016-08-17 00:46:31 +02:00
$.confirm({
2017-03-24 20:52:49 -03:00
title: 'Montant de la charge',
2017-03-25 09:56:36 -03:00
content: '< input type = "number" lang = "en" step = "0.01" min = "0.01" autofocus placeholder = "€" > ',
2016-08-17 00:46:31 +02:00
backgroundDismiss: true,
animation:'top',
closeAnimation:'bottom',
2016-08-18 05:33:42 +02:00
keyboardEnabled: true,
2016-08-17 00:46:31 +02:00
confirm: function() {
var amount = this.$content.find('input').val();
if (!$.isNumeric(amount) || amount < = 0)
return false;
2017-03-24 20:52:49 -03:00
addDeposit(amount);
},
onOpen: function() {
var that = this
this.$content.find('input').on('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
that.$confirmButton.click();
}
});
},
onClose: function() { this._lastFocused = (articleSelect.val() ? articleNb : articleSelect) ; }
});
}
function askEdit() {
$.confirm({
title: "Montant de l'édition",
2017-03-25 09:56:36 -03:00
content: '< input type = "number" lang = "en" step = "0.01" min = "0.01" autofocus placeholder = "€" > ',
2017-03-24 20:52:49 -03:00
backgroundDismiss: true,
animation:'top',
closeAnimation:'bottom',
keyboardEnabled: true,
confirm: function() {
var amount = this.$content.find('input').val();
if (!$.isNumeric(amount))
return false;
addEdit(amount);
2016-08-17 00:46:31 +02:00
},
onOpen: function() {
var that = this
2016-09-02 14:17:11 +02:00
this.$content.find('input').on('keydown', function(e) {
2016-09-03 19:04:49 +02:00
if (e.keyCode == 13) {
e.preventDefault();
2016-08-17 00:46:31 +02:00
that.$confirmButton.click();
2016-09-03 19:04:49 +02:00
}
2016-08-17 00:46:31 +02:00
});
},
2016-11-17 17:44:44 -02:00
onClose: function() { this._lastFocused = (articleSelect.val() ? articleNb : articleSelect) ; }
2016-08-17 00:46:31 +02:00
});
}
function askWithdraw() {
$.confirm({
title: 'Montant du retrait',
2017-03-25 09:56:36 -03:00
content: '< input type = "number" lang = "en" step = "0.01" min = "0.01" autofocus placeholder = "€" > ',
2016-08-17 00:46:31 +02:00
backgroundDismiss: true,
animation:'top',
closeAnimation:'bottom',
2016-08-18 05:33:42 +02:00
keyboardEnabled: true,
2016-08-17 00:46:31 +02:00
confirm: function() {
var amount = this.$content.find('input').val();
if (!$.isNumeric(amount) || amount < = 0)
return false;
addWithdraw(amount);
},
onOpen: function() {
var that = this
2016-09-02 14:17:11 +02:00
this.$content.find('input').on('keydown', function(e) {
2016-09-03 19:04:49 +02:00
if (e.keyCode == 13) {
e.preventDefault();
2016-08-17 00:46:31 +02:00
that.$confirmButton.click();
2016-09-03 19:04:49 +02:00
}
2016-08-17 00:46:31 +02:00
});
},
onClose: function() { this._lastFocused = articleSelect; }
});
}
// Event
var depositButton = $('#operation_deposit');
var withdrawButton = $('#operation_withdraw');
depositButton.on('click', function() { askDeposit(); });
withdrawButton.on('click', function() { askWithdraw(); });
2016-08-17 11:44:58 +02:00
// -----
2016-08-18 05:33:42 +02:00
// Operation formset management
2016-08-17 11:44:58 +02:00
// -----
var operation_empty_html = $('#operation_empty_html')
.removeAttr('id')
.find('label').remove().end()
.find('#id_form-__prefix__-DELETE').css('display','none').end();
$('#id_form-0-DELETE').prop('checked',true);
var formset_container = $('#operation_formset');
var mngmt_total_forms_input = $('#id_form-TOTAL_FORMS');
var mngmt_total_forms = 1;
var prefix_regex = /__prefix__/;
2017-03-24 20:52:49 -03:00
function addOperationToFormset(type, amount, article='', article_nb='') {
2016-08-17 11:44:58 +02:00
var operation_html = operation_empty_html.clone();
var index = mngmt_total_forms;
2016-08-17 11:56:42 +02:00
operation_html.attr('data-opeindex', index);
2016-08-17 11:44:58 +02:00
operation_html
.find('#id_form-__prefix__-type').val(type).end()
2016-08-20 17:55:46 +02:00
.find('#id_form-__prefix__-amount').val((parseFloat(amount)).toFixed(2)).end()
2016-08-17 11:44:58 +02:00
.find('#id_form-__prefix__-article').val(article).end()
2017-03-24 20:52:49 -03:00
.find('#id_form-__prefix__-article_nb').val(article_nb).end();
2016-08-17 11:44:58 +02:00
mngmt_total_forms_input.val(index+1);
mngmt_total_forms++;
operation_html.find(':input').each(function() {
var name = $(this).attr('name').replace(prefix_regex, index);
var id = 'id_' + name;
$(this).attr({'name': name, 'id': id});
});
formset_container.append(operation_html);
return index;
}
2017-03-24 20:52:49 -03:00
function addDepositToFormset(amount) {
return addOperationToFormset('deposit', amount, '', '');
}
function addEditToFormset(amount) {
return addOperationToFormset('edit', amount, '', '');
2016-08-17 11:44:58 +02:00
}
2017-03-24 20:52:49 -03:00
function addWithdrawToFormset(amount) {
return addOperationToFormset('withdraw', amount, '', '');
2016-08-18 05:33:42 +02:00
}
function addPurchaseToFormset(article_id, article_nb, amount=0) {
return addOperationToFormset('purchase', amount, article_id, article_nb);
}
function deleteFromFormset(opeindex) {
2017-02-12 11:02:54 -02:00
updateExistingFormset(opeindex, 0, '0.00');
2016-08-17 11:44:58 +02:00
}
2016-12-13 00:33:25 -02:00
function updateExistingFormset(opeindex, nb, amount) {
formset_container
.find('#id_form-'+opeindex+'-amount').val((parseFloat(amount)).toFixed(2)).end()
2017-02-12 11:02:54 -02:00
.find('#id_form-'+opeindex+'-article_nb').val(nb).end()
.find('#id_form-'+opeindex+'-DELETE').prop('checked', !nb);
2016-12-13 00:33:25 -02:00
}
2016-08-14 19:59:36 +02:00
// -----
// History
// -----
2016-08-24 19:52:07 +02:00
khistory = new KHistory();
2016-08-14 19:59:36 +02:00
function getHistory() {
2016-08-24 02:05:05 +02:00
var data = {
from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'),
};
2016-08-14 19:59:36 +02:00
$.ajax({
dataType: "json",
2016-08-24 02:05:05 +02:00
url : "{% url 'kfet.history.json' %}",
method : "POST",
data : data,
2016-08-14 19:59:36 +02:00
})
.done(function(data) {
2016-08-24 02:05:05 +02:00
for (var i=0; i< data [ ' opegroups ' ] . length ; i + + ) {
2016-08-24 19:52:07 +02:00
khistory.addOpeGroup(data['opegroups'][i]);
2016-08-14 19:59:36 +02:00
}
});
}
2016-12-22 01:13:14 -02:00
var previousop_container = $('#previous_op');
function updatePreviousOp() {
var previousop_html = '';
2017-01-07 02:28:19 +01:00
var trigramme = account_data['trigramme'];
2016-12-22 01:29:40 -02:00
previousop_html += '< div class = "trigramme" > Trigramme : '+trigramme+'< / div > ';
2016-12-22 01:13:14 -02:00
previousop_html += basketrel_container.html();
previousop_container.html(previousop_html);
2016-08-14 19:59:36 +02:00
}
2017-01-07 02:28:19 +01:00
function resetPreviousOp() {
previousop_container.html('');
}
2016-08-22 05:41:31 +02:00
// -----
// Addcost
// -----
2016-08-22 16:08:21 +02:00
var addcost_default_html = '< div id = "addcost" > Majoration vers < span class = "addcost_for" > < / span > de < span class = "addcost_amount" > < / span > €< / div > ';
function displayAddcost() {
checkout_container.find('#addcost').remove();
$('body').css('animation', 'none');
if (settings['addcost_for'] & & settings['addcost_amount']) {
var addcost_html = $(addcost_default_html);
addcost_html
.find('.addcost_for').text(settings['addcost_for']).end()
.find('.addcost_amount').text(settings['addcost_amount'].toFixed(2)).end();
checkout_container.prepend(addcost_html);
$('body').css('animation', 'colorchange 3s infinite');
}
}
2016-08-22 05:41:31 +02:00
function sendAddcost(trigramme, amount, password='') {
var data = {
trigramme: trigramme,
amount: amount,
}
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.update_addcost' %}",
method : "POST",
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
})
.fail(function($xhr) {
var data = $xhr.responseJSON;
switch ($xhr.status) {
case 403:
requestAuth(data, function(password) {
sendAddcost(trigramme, amount, password);
2016-08-26 15:30:40 +02:00
}, triInput);
2016-08-22 05:41:31 +02:00
break;
case 400:
2016-08-22 16:29:12 +02:00
askAddcost(getErrorsHtml(data));
2016-08-22 05:41:31 +02:00
break;
}
});
}
2016-08-22 16:29:12 +02:00
function askAddcost(errors = '') {
2016-08-22 05:41:31 +02:00
$.confirm({
title: 'Majoration',
2016-12-19 19:41:50 +01:00
content: errors + '< input type = "text" placeholder = "Trigramme" autocomplete = "off" name = "trigramme" spellcheck = "false" style = "text-transform:uppercase" autofocus > < input type = "number" lang = "en" step = "0.01" min = "0.01" placeholder = "€" name = "amount" > ',
2016-08-22 05:41:31 +02:00
backgroundDismiss: true,
animation:'top',
closeAnimation:'bottom',
keyboardEnabled: true,
confirm: function() {
var trigramme = this.$content.find('input[name=trigramme]').val().toUpperCase();
var amount = this.$content.find('input[name=amount]').val();
sendAddcost(trigramme, amount);
},
onOpen: function() {
var that = this
2016-09-02 14:17:11 +02:00
this.$content.find('input[name=amount]').on('keydown', function(e) {
2016-09-03 19:04:49 +02:00
if (e.keyCode == 13) {
e.preventDefault();
2016-08-22 05:41:31 +02:00
that.$confirmButton.click();
2016-09-03 19:04:49 +02:00
}
2016-08-22 05:41:31 +02:00
});
},
2016-08-22 16:29:12 +02:00
onClose: function() { this._lastFocused = triInput; }
2016-08-22 05:41:31 +02:00
});
}
2016-08-18 18:26:58 +02:00
// -----
// Cancel from history
// -----
2016-08-24 19:52:07 +02:00
khistory.$container.selectable({
2016-08-20 17:18:41 +02:00
filter: 'div.opegroup, div.ope',
2016-08-18 18:26:58 +02:00
selected: function(e, ui) {
$(ui.selected).each(function() {
2016-08-25 02:32:11 +02:00
if ($(this).hasClass('opegroup')) {
2016-08-24 19:52:07 +02:00
var opegroup = $(this).data('opegroup');
$(this).siblings('.ope').filter(function() {
return $(this).data('opegroup') == opegroup
}).addClass('ui-selected');
2016-08-25 02:32:11 +02:00
}
2016-08-18 18:26:58 +02:00
});
},
});
2016-08-20 21:15:14 +02:00
$(document).on('keydown', function (e) {
2016-08-18 18:26:58 +02:00
if (e.keyCode == 46) {
// DEL (Suppr)
var opes_to_cancel = [];
2016-08-24 19:52:07 +02:00
khistory.$container.find('.ope.ui-selected').each(function () {
opes_to_cancel.push($(this).data('ope'));
2016-08-18 18:26:58 +02:00
});
2016-08-23 15:43:16 +02:00
if (opes_to_cancel.length > 0)
cancelOperations(opes_to_cancel);
2016-08-18 18:26:58 +02:00
}
});
2016-08-14 19:59:36 +02:00
// -----
// Synchronization
// -----
2017-03-31 17:37:39 -03:00
OperationWebSocket.add_handler(function(data) {
2016-08-14 19:59:36 +02:00
for (var i=0; i< data [ ' opegroups ' ] . length ; i + + ) {
if (data['opegroups'][i]['add']) {
2016-08-25 01:52:20 +02:00
khistory.addOpeGroup(data['opegroups'][i]);
2016-08-14 19:59:36 +02:00
} else if (data['opegroups'][i]['cancellation']) {
2016-08-25 01:52:20 +02:00
khistory.cancelOpeGroup(data['opegroups'][i]);
2016-08-14 19:59:36 +02:00
}
}
for (var i=0; i< data [ ' opes ' ] . length ; i + + ) {
if (data['opes'][i]['cancellation']) {
2016-08-25 01:52:20 +02:00
khistory.cancelOpe(data['opes'][i]);
2016-08-14 19:59:36 +02:00
}
}
for (var i=0; i< data [ ' checkouts ' ] . length ; i + + ) {
if (checkout_data['id'] == data['checkouts'][i]['id']) {
checkout_data['balance'] = data['checkouts'][i]['balance'];
displayCheckoutData();
}
}
2016-08-14 23:37:05 +02:00
for (var i=0; i< data [ ' articles ' ] . length ; i + + ) {
2017-03-31 23:46:10 -03:00
var article = data['articles'][i];
var article_line = articles_container.find('#data-article-'+article.id);
2017-03-29 09:43:10 -03:00
if (article.stock < = 5 & & article.stock >= -5) {
2017-03-31 23:46:10 -03:00
article_line.addClass('low-stock');
2017-03-29 09:43:10 -03:00
} else {
2017-03-31 23:46:10 -03:00
article_line.removeClass('low-stock');
2017-03-29 09:43:10 -03:00
}
2017-03-31 23:46:10 -03:00
article_line.find('.stock')
2016-08-14 23:37:05 +02:00
.text(article['stock']);
2017-03-29 09:43:10 -03:00
var i = 0;
while (i < articlesList.length & & articlesList [ i ] [ 1 ] ! = article . id ) i + + ;
articlesList[i][4] = article.stock ;
2016-08-14 23:37:05 +02:00
}
2016-08-22 16:08:21 +02:00
if (data['addcost']) {
settings['addcost_for'] = data['addcost']['for'];
settings['addcost_amount'] = parseFloat(data['addcost']['amount']);
displayAddcost();
}
2017-03-31 12:23:27 -03:00
});
2016-08-14 19:59:36 +02:00
2016-08-17 00:46:31 +02:00
// -----
// General
// -----
// Reset functions
2016-08-17 11:51:44 +02:00
function coolReset(give_tri_focus=true) {
2016-08-18 05:33:42 +02:00
resetAccount();
2016-08-17 00:46:31 +02:00
resetBasket();
2016-08-23 15:43:16 +02:00
resetComment();
2016-08-19 01:08:57 +02:00
resetSelectable();
2016-08-17 11:51:44 +02:00
if (give_tri_focus)
triInput.focus();
2016-08-17 00:46:31 +02:00
}
2016-08-17 11:51:44 +02:00
function hardReset(give_tri_focus=true) {
coolReset(give_tri_focus);
2016-09-05 20:07:08 +02:00
checkoutInput.trigger('change');
2016-08-18 05:33:42 +02:00
resetArticles();
2017-01-07 02:28:19 +01:00
resetPreviousOp();
2016-08-24 19:52:07 +02:00
khistory.reset();
2017-05-14 23:50:09 -03:00
resetSettings().done(function (){
getArticles();
getHistory();
2017-05-15 17:52:49 -03:00
displayAddcost();
2017-05-14 23:50:09 -03:00
});
2016-08-17 00:46:31 +02:00
}
2016-08-19 01:08:57 +02:00
function resetSelectable() {
$('.ui-selected').removeClass('ui-selected');
}
2016-08-17 00:46:31 +02:00
// Shorcuts
2016-09-03 22:43:45 +02:00
$('#cool_reset').click(function() {
coolReset();
});
$('#ask_addcost').click(function() {
askAddcost();
});
2016-08-19 03:07:21 +02:00
$(document).on('keydown', function(e) {
2016-08-17 00:46:31 +02:00
switch (e.keyCode) {
2016-09-11 22:55:46 +02:00
case 27:
// Escape - Hide help
$('.help').hide('fast');
return false;
case 72:
if (e.ctrlKey) {
// Ctrl+H - Display help
2016-09-25 23:35:29 +02:00
e.preventDefault();
2016-09-11 22:55:46 +02:00
$('.help').show('fast');
}
2016-09-25 23:35:29 +02:00
return true;
2016-08-17 00:46:31 +02:00
case 112:
// F1 - Cool reset
coolReset();
return false;
case 113:
2016-08-18 05:33:42 +02:00
if (e.shiftKey) {
2016-12-09 00:32:44 -02:00
// Shift+F2 - Account reset
2016-08-18 05:33:42 +02:00
resetAccount();
triInput.focus();
2016-12-09 00:32:44 -02:00
} else {
// F2 - Basket reset
resetBasket();
articleSelect.focus();
2016-08-18 05:33:42 +02:00
}
2016-08-17 00:46:31 +02:00
return false;
case 114:
2016-08-18 05:33:42 +02:00
if (e.shiftKey) {
// Shift+F3 - Withdraw
askWithdraw();
} else {
// F3 - Withdraw
askDeposit();
}
2016-08-17 00:46:31 +02:00
return false;
2016-08-31 01:36:58 +02:00
case 119:
// F8 - Edition
2017-03-24 20:52:49 -03:00
askEdit();
2016-08-31 01:36:58 +02:00
return false;
2016-08-22 05:41:31 +02:00
case 120:
// F9 - Addcost
askAddcost();
return false;
2016-08-17 00:46:31 +02:00
case 121:
// F10 - Hard reset
hardReset();
return false;
}
});
2016-08-14 19:59:36 +02:00
// -----
// Initiliazing all
// -----
2016-08-17 00:46:31 +02:00
hardReset();
2016-08-14 19:59:36 +02:00
2016-08-06 22:19:52 +02:00
});
< / script >
{% endblock %}