2017-09-25 17:16:19 +02:00
|
|
|
/**
|
|
|
|
* CSRF Token
|
|
|
|
*/
|
|
|
|
|
|
|
|
var csrftoken = '';
|
|
|
|
if (typeof Cookies !== 'undefined')
|
|
|
|
csrftoken = Cookies.get('csrftoken');
|
|
|
|
|
|
|
|
// Add CSRF token in header of AJAX requests.
|
|
|
|
|
|
|
|
function csrfSafeMethod(method) {
|
|
|
|
// these HTTP methods do not require CSRF protection
|
|
|
|
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
|
|
|
}
|
|
|
|
|
|
|
|
$.ajaxSetup({
|
|
|
|
beforeSend: function(xhr, settings) {
|
|
|
|
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
|
|
|
|
xhr.setRequestHeader("X-CSRFToken", csrftoken);
|
2016-08-24 19:52:07 +02:00
|
|
|
}
|
2016-08-26 15:30:40 +02:00
|
|
|
}
|
2016-08-20 23:31:30 +02:00
|
|
|
});
|
2016-08-24 19:52:07 +02:00
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
function add_csrf_form($form) {
|
|
|
|
$form.append(
|
|
|
|
$('<input>', {'name': 'csrfmiddlewaretoken', 'value': csrftoken})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-31 17:23:27 +02:00
|
|
|
/*
|
|
|
|
* Generic Websocket class and k-psul ws instanciation
|
|
|
|
*/
|
|
|
|
|
2017-03-31 20:57:15 +02:00
|
|
|
class KfetWebsocket {
|
2017-03-31 17:23:27 +02:00
|
|
|
|
2017-03-31 22:37:39 +02:00
|
|
|
static get defaults() {
|
2017-06-21 07:08:28 +02:00
|
|
|
return {
|
|
|
|
relative_url: '',
|
|
|
|
default_msg: {},
|
|
|
|
handlers: [],
|
|
|
|
base_path: '/ws/k-fet/'
|
|
|
|
};
|
2017-03-31 17:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(data) {
|
2017-03-31 22:37:39 +02:00
|
|
|
$.extend(this, this.constructor.defaults, data);
|
2017-06-21 07:08:28 +02:00
|
|
|
if (window.location.pathname.startsWith('/gestion/'))
|
|
|
|
this.base_path = '/gestion' + this.base_path;
|
2017-03-31 17:23:27 +02:00
|
|
|
}
|
2017-06-12 01:51:10 +02:00
|
|
|
|
2017-03-31 20:10:41 +02:00
|
|
|
get url() {
|
2017-06-21 07:08:28 +02:00
|
|
|
var protocol = window.location.protocol == 'https:' ? 'wss' : 'ws';
|
|
|
|
var host = window.location.host;
|
|
|
|
return protocol + "://" + host + this.base_path + this.relative_url;
|
2017-03-31 20:10:41 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 22:37:39 +02:00
|
|
|
add_handler(handler) {
|
|
|
|
if (!this.socket)
|
|
|
|
this.listen();
|
|
|
|
|
|
|
|
this.handlers.push(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
listen() {
|
2017-03-31 20:10:41 +02:00
|
|
|
var that = this;
|
2017-03-31 20:45:15 +02:00
|
|
|
this.socket = new ReconnectingWebSocket(this.url);
|
2017-03-31 17:23:27 +02:00
|
|
|
|
2017-03-31 20:45:15 +02:00
|
|
|
this.socket.onmessage = function(e) {
|
2017-03-31 17:23:27 +02:00
|
|
|
var data = $.extend({}, that.default_msg, JSON.parse(e.data));
|
2017-03-31 22:37:39 +02:00
|
|
|
for (let handler of that.handlers) {
|
|
|
|
handler(data);
|
|
|
|
}
|
2017-03-31 17:23:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 20:57:15 +02:00
|
|
|
var OperationWebSocket = new KfetWebsocket({
|
2017-06-21 07:08:28 +02:00
|
|
|
'relative_url': 'k-psul/',
|
2017-03-31 17:23:27 +02:00
|
|
|
'default_msg': {'opegroups':[],'opes':[],'checkouts':[],'articles':[]},
|
|
|
|
});
|
|
|
|
|
2016-08-24 19:52:07 +02:00
|
|
|
function dateUTCToParis(date) {
|
|
|
|
return moment.tz(date, 'UTC').tz('Europe/Paris');
|
|
|
|
}
|
|
|
|
|
|
|
|
function amountDisplay(amount, is_cof=false, tri='') {
|
|
|
|
if (tri == 'LIQ')
|
|
|
|
return (- amount).toFixed(2) +'€';
|
|
|
|
return amountToUKF(amount, is_cof);
|
|
|
|
}
|
|
|
|
|
2017-02-12 22:05:41 +01:00
|
|
|
function amountToUKF(amount, is_cof=false, account=false) {
|
|
|
|
var rounding = account ? Math.floor : Math.round ;
|
2016-08-24 19:52:07 +02:00
|
|
|
var coef_cof = is_cof ? 1 + settings['subvention_cof'] / 100 : 1;
|
2017-02-12 22:05:41 +01:00
|
|
|
return rounding(amount * coef_cof * 10);
|
2016-08-24 19:52:07 +02:00
|
|
|
}
|
2016-08-26 15:30:40 +02:00
|
|
|
|
|
|
|
function isValidTrigramme(trigramme) {
|
|
|
|
var pattern = /^[^a-z]{3}$/;
|
|
|
|
return trigramme.match(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getErrorsHtml(data) {
|
|
|
|
var content = '';
|
2017-03-24 19:39:10 +01:00
|
|
|
if (!data)
|
|
|
|
return "L'utilisateur n'est pas dans l'équipe";
|
2016-08-26 15:30:40 +02:00
|
|
|
if ('operation_group' in data['errors']) {
|
|
|
|
content += 'Général';
|
|
|
|
content += '<ul>';
|
|
|
|
if (data['errors']['operation_group'].indexOf('on_acc') != -1)
|
|
|
|
content += '<li>Pas de compte sélectionné</li>';
|
|
|
|
if (data['errors']['operation_group'].indexOf('checkout') != -1)
|
|
|
|
content += '<li>Pas de caisse sélectionnée</li>';
|
|
|
|
content += '</ul>';
|
|
|
|
}
|
|
|
|
if ('missing_perms' in data['errors']) {
|
|
|
|
content += 'Permissions manquantes';
|
|
|
|
content += '<ul>';
|
|
|
|
for (var i=0; i<data['errors']['missing_perms'].length; i++)
|
|
|
|
content += '<li>'+data['errors']['missing_perms'][i]+'</li>';
|
|
|
|
content += '</ul>';
|
|
|
|
}
|
2016-09-03 18:32:12 +02:00
|
|
|
if ('negative' in data['errors']) {
|
2016-09-06 18:50:57 +02:00
|
|
|
if (window.location.pathname.startsWith('/gestion/')) {
|
|
|
|
var url_base = '/gestion/k-fet/accounts/';
|
|
|
|
} else {
|
|
|
|
var url_base = '/k-fet/accounts/';
|
|
|
|
}
|
2016-09-03 18:32:12 +02:00
|
|
|
for (var i=0; i<data['errors']['negative'].length; i++) {
|
2016-09-05 19:50:16 +02:00
|
|
|
content += '<a class="btn btn-primary" href="'+url_base+data['errors']['negative'][i]+'/edit" target="_blank" style="width:100%">Autorisation de négatif requise pour '+data['errors']['negative'][i]+'</a>';
|
2016-09-03 18:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 15:30:40 +02:00
|
|
|
if ('addcost' in data['errors']) {
|
|
|
|
content += '<ul>';
|
|
|
|
if (data['errors']['addcost'].indexOf('__all__') != -1)
|
|
|
|
content += '<li>Compte invalide</li>';
|
|
|
|
if (data['errors']['addcost'].indexOf('amount') != -1)
|
|
|
|
content += '<li>Montant invalide</li>';
|
|
|
|
content += '</ul>';
|
|
|
|
}
|
2017-03-25 14:39:53 +01:00
|
|
|
if ('account' in data['errors']) {
|
|
|
|
content += 'Général';
|
|
|
|
content += '<ul>';
|
|
|
|
content += '<li>Opération invalide sur le compte '+data['errors']['account']+'</li>';
|
|
|
|
content += '</ul>';
|
|
|
|
}
|
2016-08-26 15:30:40 +02:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestAuth(data, callback, focus_next = null) {
|
|
|
|
var content = getErrorsHtml(data);
|
2016-11-18 01:35:33 +01:00
|
|
|
content += '<div class="capslock"><span class="glyphicon glyphicon-lock"></span><input type="password" name="password" autofocus><div>',
|
2016-08-26 15:30:40 +02:00
|
|
|
$.confirm({
|
|
|
|
title: 'Authentification requise',
|
|
|
|
content: content,
|
|
|
|
backgroundDismiss: true,
|
|
|
|
animation:'top',
|
|
|
|
closeAnimation:'bottom',
|
|
|
|
keyboardEnabled: true,
|
|
|
|
confirm: function() {
|
|
|
|
var password = this.$content.find('input').val();
|
|
|
|
callback(password);
|
|
|
|
},
|
|
|
|
onOpen: function() {
|
|
|
|
var that = this;
|
2016-11-18 01:35:33 +01:00
|
|
|
var capslock = -1 ; // 1 -> caps on ; 0 -> caps off ; -1 or 2 -> unknown
|
2016-08-26 15:30:40 +02:00
|
|
|
this.$content.find('input').on('keypress', function(e) {
|
|
|
|
if (e.keyCode == 13)
|
|
|
|
that.$confirmButton.click();
|
2016-11-18 01:35:33 +01:00
|
|
|
|
|
|
|
var s = String.fromCharCode(e.which);
|
|
|
|
if ((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey)|| //caps on, shift off
|
|
|
|
(s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)) { //caps on, shift on
|
|
|
|
capslock = 1 ;
|
|
|
|
} else if ((s.toLowerCase() === s && s.toUpperCase() !== s && !e.shiftKey)|| //caps off, shift off
|
|
|
|
(s.toLowerCase() !== s && s.toUpperCase() === s && e.shiftKey)) { //caps off, shift on
|
|
|
|
capslock = 0 ;
|
|
|
|
}
|
|
|
|
if (capslock == 1)
|
|
|
|
$('.capslock .glyphicon').show() ;
|
|
|
|
else if (capslock == 0)
|
|
|
|
$('.capslock .glyphicon').hide() ;
|
|
|
|
});
|
|
|
|
// Capslock key is not detected by keypress
|
|
|
|
this.$content.find('input').on('keydown', function(e) {
|
|
|
|
if (e.which == 20) {
|
|
|
|
capslock = 1-capslock ;
|
|
|
|
}
|
|
|
|
if (capslock == 1)
|
|
|
|
$('.capslock .glyphicon').show() ;
|
|
|
|
else if (capslock == 0)
|
|
|
|
$('.capslock .glyphicon').hide() ;
|
2016-08-26 15:30:40 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onClose: function() {
|
|
|
|
if (focus_next)
|
|
|
|
this._lastFocused = focus_next;
|
|
|
|
}
|
2016-11-18 01:35:33 +01:00
|
|
|
|
2016-08-26 15:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2017-06-12 01:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup jquery-confirm
|
|
|
|
*/
|
|
|
|
|
|
|
|
jconfirm.defaults = {
|
|
|
|
confirmButton: '<span class="glyphicon glyphicon-ok"></span>',
|
|
|
|
cancelButton: '<span class="glyphicon glyphicon-remove"></span>'
|
|
|
|
};
|
2017-09-25 17:16:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create form node, given an url used as 'action', with csrftoken set.
|
|
|
|
*/
|
|
|
|
function create_form(url) {
|
|
|
|
let $form = $('<form>', {
|
|
|
|
'action': url,
|
|
|
|
'method': 'post',
|
|
|
|
});
|
|
|
|
add_csrf_form($form);
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit a POST request from <a> tag.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* <a href="#" data-url="{target url}" onclick="submit_url(this)">{…}</a>
|
|
|
|
*/
|
|
|
|
function submit_url(el) {
|
|
|
|
let url = $(el).data('url');
|
|
|
|
create_form(url).appendTo($('body')).submit();
|
|
|
|
}
|