Glue code between kpsul and the new js
This commit is contained in:
parent
c98bc38c83
commit
d537fce257
3 changed files with 52 additions and 109 deletions
2
kfet/static/kfet/js/kpsul.min.js
vendored
2
kfet/static/kfet/js/kpsul.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,3 +1,32 @@
|
|||
class KPsul {
|
||||
constructor() {
|
||||
this.account_manager = new AccountManager(this);
|
||||
this.checkout_manager = new CheckoutManager(this);
|
||||
this._$article_select = $("#article_autocomplete")
|
||||
}
|
||||
|
||||
focus(to_focus) {
|
||||
switch (to_focus) {
|
||||
case "account":
|
||||
this.account_manager.focus()
|
||||
break;
|
||||
case "articles":
|
||||
this._$article_select.focus()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// En attendant de tout convertir : on duplique les fonctions de kpsul.html
|
||||
// en méthodes de l'objet KPsul
|
||||
updateBasketAmount() {
|
||||
window.updateBasketAmount();
|
||||
}
|
||||
|
||||
updateBasketRel() {
|
||||
window.updateBasketRel();
|
||||
}
|
||||
}
|
||||
|
||||
$(function () {
|
||||
self.account_manager = new AccountManager();
|
||||
kpsul = new KPsul()
|
||||
})
|
|
@ -216,90 +216,7 @@ $(document).ready(function() {
|
|||
|
||||
// Initializing
|
||||
var checkout_container = $('#checkout');
|
||||
var checkoutInput = $('#id_checkout_select');
|
||||
var checkout_data = {}
|
||||
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' : '' ,
|
||||
}
|
||||
var last_statement_container = $('#last_statement');
|
||||
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>';
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Display data
|
||||
function displayCheckoutData() {
|
||||
var at_formated = '';
|
||||
if (checkout_data['last_statement_at']) {
|
||||
last_statement_container.html(last_statement_html_default);
|
||||
var at = moment.tz(checkout_data['last_statement_at'], 'UTC');
|
||||
at_formated = at.tz('Europe/Paris').format('DD/MM/YY à HH:mm');
|
||||
} else {
|
||||
last_statement_container.html('');
|
||||
}
|
||||
for (var elem in checkout_data) {
|
||||
$('#checkout-'+elem).text(checkout_data[elem]);
|
||||
}
|
||||
$('#checkout-last_statement_at').text(at_formated);
|
||||
var buttons = '';
|
||||
if (checkout_data['id'] !== 0) {
|
||||
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>';
|
||||
}
|
||||
checkout_container.find('.buttons').html(buttons);
|
||||
}
|
||||
|
||||
// Clear data
|
||||
function resetCheckout() {
|
||||
checkout_data = checkout_data_default;
|
||||
$('#id_checkout').val(0);
|
||||
checkoutInput.find('option:first').prop('selected', true);
|
||||
displayCheckoutData();
|
||||
}
|
||||
|
||||
// Store data
|
||||
function storeCheckoutData(data) {
|
||||
checkout_data = $.extend({}, checkout_data_default, data);
|
||||
$('#id_checkout').val(checkout_data['id']);
|
||||
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) })
|
||||
.fail(function() { resetCheckout() });
|
||||
}
|
||||
|
||||
// Event listener
|
||||
checkoutInput.on('change', function() {
|
||||
retrieveCheckoutData(checkoutInput.val());
|
||||
if (account_manager.data['trigramme']) {
|
||||
articleSelect.focus().select();
|
||||
} else {
|
||||
triInput.focus().select();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// -----
|
||||
// Auth
|
||||
// -----
|
||||
|
@ -629,11 +546,11 @@ $(document).ready(function() {
|
|||
var amount_euro = - article_data[3] * nb ;
|
||||
if (settings['addcost_for']
|
||||
&& settings['addcost_amount']
|
||||
&& account_manager.data['trigramme'] != settings['addcost_for']
|
||||
&& kpsul.account_manager.data['trigramme'] != settings['addcost_for']
|
||||
&& article_data[5])
|
||||
amount_euro -= settings['addcost_amount'] * nb;
|
||||
var reduc_divisor = 1;
|
||||
if (account_manager.data['is_cof'] && article_data[6])
|
||||
if (kpsul.account_manager.data['is_cof'] && article_data[6])
|
||||
reduc_divisor = 1 + settings['subvention_cof'] / 100;
|
||||
return (amount_euro / reduc_divisor).toFixed(2);
|
||||
}
|
||||
|
@ -656,7 +573,7 @@ $(document).ready(function() {
|
|||
.attr('data-opeindex', index)
|
||||
.find('.number').text('('+nb+'/'+article_data[4]+')').end()
|
||||
.find('.name').text(article_data[0]).end()
|
||||
.find('.amount').text(amountToUKF(amount_euro, account_manager.data['is_cof'], false));
|
||||
.find('.amount').text(amountToUKF(amount_euro, kpsul.account_manager.data['is_cof'], false));
|
||||
basket_container.prepend(article_basket_html);
|
||||
if (is_low_stock(id, nb))
|
||||
article_basket_html.find('.lowstock')
|
||||
|
@ -682,7 +599,7 @@ $(document).ready(function() {
|
|||
.attr('data-opeindex', index)
|
||||
.find('.number').text(amount+"€").end()
|
||||
.find('.name').text('Charge').end()
|
||||
.find('.amount').text(amountToUKF(amount, account_manager.data['is_cof'], false));
|
||||
.find('.amount').text(amountToUKF(amount, kpsul.account_manager.data['is_cof'], false));
|
||||
basket_container.prepend(deposit_basket_html);
|
||||
updateBasketRel();
|
||||
}
|
||||
|
@ -695,7 +612,7 @@ $(document).ready(function() {
|
|||
.attr('data-opeindex', index)
|
||||
.find('.number').text(amount+"€").end()
|
||||
.find('.name').text('Édition').end()
|
||||
.find('.amount').text(amountToUKF(amount, account_manager.data['is_cof'], false));
|
||||
.find('.amount').text(amountToUKF(amount, kpsul.account_manager.data['is_cof'], false));
|
||||
basket_container.prepend(deposit_basket_html);
|
||||
updateBasketRel();
|
||||
}
|
||||
|
@ -708,7 +625,7 @@ $(document).ready(function() {
|
|||
.attr('data-opeindex', index)
|
||||
.find('.number').text(amount+"€").end()
|
||||
.find('.name').text('Retrait').end()
|
||||
.find('.amount').text(amountToUKF(amount, account_manager.data['is_cof'], false));
|
||||
.find('.amount').text(amountToUKF(amount, kpsul.account_manager.data['is_cof'], false));
|
||||
basket_container.prepend(withdraw_basket_html);
|
||||
updateBasketRel();
|
||||
}
|
||||
|
@ -764,7 +681,7 @@ $(document).ready(function() {
|
|||
var amount = $(this).find('#id_form-'+opeindex+'-amount');
|
||||
if (!deleted && type == "purchase")
|
||||
amount.val(amountEuroPurchase(article_id, article_nb));
|
||||
basket_container.find('[data-opeindex='+opeindex+'] .amount').text(amountToUKF(amount.val(), account_manager.data['is_cof'], false));
|
||||
basket_container.find('[data-opeindex='+opeindex+'] .amount').text(amountToUKF(amount.val(), kpsul.account_manager.data['is_cof'], false));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -772,7 +689,7 @@ $(document).ready(function() {
|
|||
|
||||
function updateBasketRel() {
|
||||
var basketrel_html = '';
|
||||
if (account_manager.data['trigramme'] == 'LIQ' && !isBasketEmpty()) {
|
||||
if (kpsul.account_manager.data['trigramme'] == 'LIQ' && !isBasketEmpty()) {
|
||||
var amount = - getAmountBasket();
|
||||
basketrel_html += '<div>Total: '+amount.toFixed(2)+' €</div>';
|
||||
if (amount < 5)
|
||||
|
@ -781,11 +698,11 @@ $(document).ready(function() {
|
|||
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_manager.data['trigramme'] != '' && !isBasketEmpty()) {
|
||||
} else if (kpsul.account_manager.data['trigramme'] != '' && !isBasketEmpty()) {
|
||||
var amount = getAmountBasket();
|
||||
var amountUKF = amountToUKF(amount, account_manager.data['is_cof'], false);
|
||||
var newBalance = account_manager.data['balance'] + amount;
|
||||
var newBalanceUKF = amountToUKF(newBalance, account_manager.data['is_cof'], true);
|
||||
var amountUKF = amountToUKF(amount, kpsul.account_manager.data['is_cof'], false);
|
||||
var newBalance = kpsul.account_manager.data['balance'] + amount;
|
||||
var newBalanceUKF = amountToUKF(newBalance, kpsul.account_manager.data['is_cof'], true);
|
||||
basketrel_html += '<div>Total: '+amountUKF+'</div>';
|
||||
basketrel_html += '<div>Nouveau solde: '+newBalanceUKF+'</div>';
|
||||
if (newBalance < 0)
|
||||
|
@ -806,7 +723,7 @@ $(document).ready(function() {
|
|||
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_manager.data['is_cof']);
|
||||
var amountUKF_after = amountToUKF(amountEuro_after, kpsul.account_manager.data['is_cof']);
|
||||
|
||||
if (type == 'purchase') {
|
||||
if (nb_after == 0) {
|
||||
|
@ -1028,7 +945,7 @@ $(document).ready(function() {
|
|||
|
||||
function updatePreviousOp() {
|
||||
var previousop_html = '';
|
||||
var trigramme = account_manager.data['trigramme'];
|
||||
var trigramme = kpsul.account_manager.data['trigramme'];
|
||||
previousop_html += '<div class="trigramme">Trigramme : '+trigramme+'</div>';
|
||||
previousop_html += basketrel_container.html();
|
||||
previousop_container.html(previousop_html);
|
||||
|
@ -1135,12 +1052,9 @@ $(document).ready(function() {
|
|||
khistory.add_history_group(data['groups'][i]);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
kpsul.checkout_manager.ws_update(data["checkouts"]);
|
||||
|
||||
for (var i=0; i<data['articles'].length; i++) {
|
||||
var article = data['articles'][i];
|
||||
var article_line = articles_container.find('#data-article-'+article.id);
|
||||
|
@ -1170,7 +1084,7 @@ $(document).ready(function() {
|
|||
// Reset functions
|
||||
|
||||
function coolReset(give_tri_focus=true) {
|
||||
account_manager.reset(true);
|
||||
kpsul.account_manager.reset(true);
|
||||
resetBasket();
|
||||
resetComment();
|
||||
resetSelectable();
|
||||
|
@ -1180,7 +1094,7 @@ $(document).ready(function() {
|
|||
|
||||
function hardReset(give_tri_focus=true) {
|
||||
coolReset(give_tri_focus);
|
||||
checkoutInput.trigger('change');
|
||||
kpsul.checkout_manager.update();
|
||||
resetArticles();
|
||||
resetPreviousOp();
|
||||
khistory.reset();
|
||||
|
@ -1225,7 +1139,7 @@ $(document).ready(function() {
|
|||
case 113:
|
||||
if (e.shiftKey) {
|
||||
// Shift+F2 - Account reset
|
||||
account_manager.reset(true);
|
||||
kpsul.account_manager.reset(true);
|
||||
triInput.focus();
|
||||
} else {
|
||||
// F2 - Basket reset
|
||||
|
|
Loading…
Reference in a new issue