site-proximo/assets/js/stock.js

121 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-02-15 19:01:27 +01:00
let listContainer = $("#dataList");
let displayedItems = [];
let fetchedData = {};
2020-02-26 11:32:21 +01:00
let currentMode = "buy";
2020-02-15 19:01:27 +01:00
2020-02-26 11:32:21 +01:00
let sellClass = 'btn-danger';
2020-02-15 19:01:27 +01:00
let buyClass = 'btn-success';
$(document).ready(async function () {
2020-02-26 11:32:21 +01:00
await generateList();
$('#buyButton').addClass(buyClass);
$('#saveButton').addClass(buyClass);
2020-02-15 19:01:27 +01:00
$('#buyButton').on('click', function () {
if (!$(this).hasClass(buyClass)) {
$(this).addClass(buyClass);
2020-02-26 11:32:21 +01:00
$('#sellButton').removeClass(sellClass);
$('#saveButton').removeClass(sellClass).addClass(buyClass);
2020-02-15 19:01:27 +01:00
currentMode = "buy";
}
});
$('#sellButton').on('click', function () {
2020-02-26 11:32:21 +01:00
if (!$(this).hasClass(sellClass)) {
$(this).addClass(sellClass);
2020-02-15 19:01:27 +01:00
$('#buyButton').removeClass(buyClass);
2020-02-26 11:32:21 +01:00
$('#saveButton').removeClass(buyClass).addClass(sellClass);
2020-02-15 19:01:27 +01:00
currentMode = "sell";
}
});
$('#saveButton').on('click', function () {
let values = getValues();
let message = 'Voulez vous vraimer ajouter ces articles au stock ?<br><ul>';
let color = 'green';
if (currentMode === "sell"){
message = 'Voulez vous vraiment supprimer ces articles du stock ?<br><ul>';
color = 'red';
}
for (let i = 0; i < values.length; i++) {
if (values[i]['value'] > 0){
message += '<li>' + fetchedData['articles'][i]['name'] + ' : ' + values[i]['value'] + '</li>'
}
}
message += '</ul>';
$.confirm({
title: 'Confirmer',
content: message,
type: color,
buttons: {
formSubmit: {
text: 'Confirmer',
2020-02-26 11:32:21 +01:00
action: async function () {
await AjaxManager.saveStockChange(getFormattedValues(values), currentMode === "sell");
await generateList();
2020-02-15 19:01:27 +01:00
}
},
cancel: {
text: 'Annuler',
}
}
});
});
});
function getListItem(item) {
2020-02-26 11:32:21 +01:00
const inputId = 'stockDiffInput' + item['id'];
const labelId = 'stockDiffLabel' + item['id'];
2020-02-15 19:01:27 +01:00
return (
'<div class="grid-item">' +
'<div class="grid-container">' +
2020-02-26 11:32:21 +01:00
'<div class="img-container">' +
2020-02-15 19:01:27 +01:00
'<img class="list-image" src="../uploaded_images/' + item['id'] + '.jpg"/>' +
2020-02-26 11:32:21 +01:00
'</div>' +
'<h5>' + item['name'] + '</h5>' +
'<p>Quantité : <strong>' + item['quantity'] + '</strong></p>' +
'<label for="' + inputId + '" id="' + labelId + '">À Ajouter :</label>' +
'<input id="' + inputId + '" placeholder="Nombre" type="number" value="0" class="form-control" onchange="onStockDiffInputChange(this)"/>' +
2020-02-15 19:01:27 +01:00
'</div>' +
'</div>');
}
2020-02-26 11:32:21 +01:00
function onStockDiffInputChange(input) {
if ($(input).val() < 0 || isNaN(parseInt($(input).val()))) {
$(input).val(0);
}
}
2020-02-15 19:01:27 +01:00
2020-02-26 11:32:21 +01:00
async function generateList() {
fetchedData = await AjaxManager.getAll();
displayedItems = [];
listContainer.html('');
2020-02-15 19:01:27 +01:00
for (let i = 0; i < fetchedData['articles'].length; i++) {
let listItem = getListItem(fetchedData['articles'][i]);
displayedItems.push($(listItem));
listContainer.append(displayedItems[i]);
}
}
function getValues() {
let values = [];
for (let i = 0; i < displayedItems.length; i++) {
let value = displayedItems[i].find('input').val();
let id = fetchedData['articles'][i]['id'];
values.push({id: id, value: value});
}
return values;
}
function getFormattedValues(values) {
let newValues = [];
for (let i = 0; i < values.length; i++) {
if (values[i]['value'] > 0)
newValues.push(values[i]);
}
return newValues;
}