Compare commits

..

No commits in common. "d9e799a5389c7bfa5dfdecc3f8d7d35aa1aede78" and "0b085a45864affb246246cc245a1eeeeb654d3fd" have entirely different histories.

14 changed files with 71 additions and 365 deletions

View file

@ -1,6 +0,0 @@
<?php
$relativePath = "../";
$pageTitle = "Gestion des articles";
$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
include("template.php");

View file

@ -2,28 +2,13 @@
$relativePath = "../"; $relativePath = "../";
ob_start(); ob_start();
?> ?>
<div class="admin-container" style="display: flex"> <div class="admin-container">
<div style="margin: auto"> <a href="stock.php">
<div style="display: flex" class="my-5"> <button class="btn btn-primary btn-large">Gestion des stocks</button>
<a href="stock.php" style="margin: auto"> </a>
<button class="btn btn-success btn-lg">Gestion du stocks</button> <a href="categories.php">
</a> <button class="btn btn-primary btn-large">Gestion des catégories</button>
</div> </a>
<div class="my-5" style="display: flex">
<a href="articles.php" style="margin: auto" class="mx-1">
<button class="btn btn-primary btn-lg">Créer/Éditer des articles</button>
</a>
<a href="categories.php" style="margin: auto" class="mx-1">
<button class="btn btn-primary btn-lg">Créer/Éditer des catégories</button>
</a>
</div>
<div style="display: flex" class="my-5">
<button id="uploadButton" class="btn btn-warning btn-lg" style="margin: auto">Mettre l'inventaire en ligne
</button>
</div>
</div>
</div> </div>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/> <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
@ -31,12 +16,5 @@ ob_start();
<?php <?php
$pageContent = ob_get_clean(); $pageContent = ob_get_clean();
$pageTitle = "Admin"; $pageTitle = "Admin";
$pageScripts =
"
<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/jquery-confirm-defaults.js\"></script>
<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/index.js\"></script>
";
include($relativePath . "includes/template.php"); include($relativePath . "includes/template.php");
?> ?>

View file

@ -1,7 +1,6 @@
<?php <?php
$relativePath = "../"; $relativePath = "../";
$isStock = true; $pageTitle = "Gestion des articles";
$pageTitle = "Gestion des stocks"; $script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/articles.js\"></script>";
$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
include("template.php"); include("template.php");

View file

@ -1,61 +1,35 @@
<?php <?php
$relativePath = "../"; $relativePath = "../";
if (!isset($isStock))
$isStock = false;
ob_start(); ob_start();
?> ?>
<div class="admin-container"> <div class="admin-container">
<a href="index.php"> <a href="index.php">
<button class="btn btn-primary btn-large"> <button class="btn btn-primary btn-large">Retour</button>
<span class="mdi mdi-chevron-left"></span>
Retour
</button>
</a> </a>
<h1 class="text-center"><?= $pageTitle ?></h1> <h1 class="text-center"><?= $pageTitle ?></h1>
<?php if (!$isStock): ?> <div style="display: flex">
<div style="display: flex"> <button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)">
<button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)"> <i class="mdi mdi-plus"></i>
<i class="mdi mdi-plus"></i> </button>
</button> </div>
</div>
<div id="listContainer">
<ul id="dataList">
</ul> <div id="listContainer">
</div> <ul id="dataList">
<?php else: ?>
<div>
<h3 class="text-center">Mode</h3>
<div class="button-container">
<button id="buyButton" class="btn mr-0">Achat</button>
<button id="sellButton" class="btn ml-0">Vente</button>
</div>
</div>
<div id="dataList">
</div> </ul>
<div style="display: flex"> </div>
<button id="saveButton" style="margin: auto" class="btn btn-lg my-2">Sauvegarder</button>
</div>
<?php endif; ?>
</div> </div>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/libs/materialdesignicons.min.css" <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/libs/materialdesignicons.min.css" media="screen,projection"/>
media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/> <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/categories.css" media="screen,projection"/>
<?php if (!$isStock): ?>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/list.css" media="screen,projection"/>
<?php else: ?>
<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
<?php endif; ?>
<?php <?php
$pageContent = ob_get_clean(); $pageContent = ob_get_clean();

View file

@ -1,7 +1,22 @@
<?php <?php
$relativePath = "../"; $rest_json = file_get_contents("php://input");
require_once $relativePath.'classes/postHandler.php'; $_POST = json_decode($rest_json, true);
$handler = new PostHandler(null, null);
echo json_encode($handler->write_json()); //var_dump($_POST);
$fp = fopen('../data/stock-v2.json', 'w');
$result = fwrite($fp, json_encode($_POST["v2"]));
fclose($fp);
$fp = fopen('../data/stock.json', 'w');
$result = fwrite($fp, json_encode($_POST["v1"]));
fclose($fp);
if ($result) {
echo 'Réussite !';
} else {
echo 'Echec!
'; // Allows to create a newline
var_dump($_POST);
}

View file

@ -33,13 +33,13 @@
margin: 20px 0 20px 0; margin: 20px 0 20px 0;
} }
#listContainer .mdi { .mdi {
font-size: 2rem; font-size: 2rem;
} }
.list-name { .list-name {
font-weight: bold; font-weight: bold;
width: 200px; width: 150px;
height: 50px; height: 50px;
overflow: hidden; overflow: hidden;
} }
@ -61,15 +61,7 @@
text-align: right; text-align: right;
} }
.list-image { .list-image img {
width: 50px; width: 50px;
height: 50px; height: 50px;
display: flex;
}
.list-image img {
width: 45px;
height: 45px;
margin: auto;
} }

View file

@ -1,43 +1,33 @@
#dataList {
display: grid; .name-column {
grid-template-columns: auto auto ; width: 200px;
width: 100%;
} }
.img-container { .quantity-column, .price-column {
display: flex; width: 150px;
width: 100%;
} }
.grid-item { .code-column {
display: flex; width: 200px;
padding: 5px;
} }
.type-column {
.grid-container { width: 200px;
width: 100px; overflow-x: auto;
margin: auto;
display: block;
} }
.grid-container input { .image-column {
width: 150px;
}
.actions-column {
width: 100px; width: 100px;
} }
.grid-container p { #stockTable tr {
text-align: center; border-bottom: 1px solid #dedede;
} }
.grid-container img { .row-img {
width : 100px; height: 50px;
margin: auto;
}
.button-container {
display: flex;
}
.button-container button {
margin: auto;
} }

View file

@ -78,23 +78,6 @@ class AjaxManager {
} else } else
return 1; return 1;
} }
static async saveStockChange(data, isSell) {
let formattedData = {
type: 'stock',
action: isSell ? 'sell' : 'buy',
data: data,
};
let response = await $.ajax({
type: "POST",
url: "json_ajax.php",
data: JSON.stringify(formattedData),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
console.log(response);
return response['data'];
}
} }

View file

@ -1,43 +0,0 @@
async function sendRequest() {
let response = await $.ajax({
type: "POST",
url: "write_json.php",
});
response = JSON.parse(response);
console.log(response);
return response["status"];
}
$('#uploadButton').on('click', function () {
$.confirm({
title: 'Confirmer',
content: "Voulez vous vraiment mettre en ligne le stock actuel du Proximo ? Il sera visible depuis l'application CAMPUS.",
type: "orange",
buttons: {
formSubmit: {
text: 'Confirmer',
btnClass: "btn-warning",
action: async function () {
let result = await sendRequest();
if (result !== 0) {
$.alert({
title: "Erreur",
content: "Une erreur est survenue, merci de réessayer plus tard.",
type: "red",
})
} else {
$.alert({
title: "Succès",
content: "Le stock a bien été mis à jour.",
type: "green",
})
}
}
},
cancel: {
text: 'Annuler',
}
}
});
});

View file

@ -1,7 +1,7 @@
jconfirm.defaults = { jconfirm.defaults = {
title: 'Title', title: 'Title',
titleClass: '', titleClass: '',
type: 'blue', type: 'red',
typeAnimated: true, typeAnimated: true,
draggable: false, draggable: false,
dragWindowGap: 15, dragWindowGap: 15,

View file

@ -138,9 +138,7 @@ class ListManager {
} }
getImagePicker(id, index) { getImagePicker(id, index) {
let imageSrc = undefined; let imageSrc = $(this.displayedData[index].find("img")[0]).attr('src');
if (index !== -1)
imageSrc = $(this.displayedData[index].find("img")[0]).attr('src');
return ( return (
'<img style="width: 50px; height: 50px" src="' + imageSrc + '" id="image_' + id + '"/>' + '<img style="width: 50px; height: 50px" src="' + imageSrc + '" id="image_' + id + '"/>' +
'<div class="custom-file">\n' + '<div class="custom-file">\n' +

View file

@ -1,106 +0,0 @@
let listContainer = $("#dataList");
let displayedItems = [];
let fetchedData = {};
let currentMode = "sell";
let sellCLass = 'btn-danger';
let buyClass = 'btn-success';
$(document).ready(async function () {
fetchedData = await AjaxManager.getAll();
$('#sellButton').addClass(sellCLass);
$('#saveButton').addClass(sellCLass);
generateList();
$('#buyButton').on('click', function () {
if (!$(this).hasClass(buyClass)) {
$(this).addClass(buyClass);
$('#sellButton').removeClass(sellCLass);
$('#saveButton').removeClass(sellCLass).addClass(buyClass);
currentMode = "buy";
}
});
$('#sellButton').on('click', function () {
if (!$(this).hasClass(sellCLass)) {
$(this).addClass(sellCLass);
$('#buyButton').removeClass(buyClass);
$('#saveButton').removeClass(buyClass).addClass(sellCLass);
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',
action: function () {
window.location.reload();
}
},
cancel: {
text: 'Annuler',
}
}
});
AjaxManager.saveStockChange(getFormattedValues(values), currentMode === "sell");
});
});
function getListItem(item) {
return (
'<div class="grid-item">' +
'<div class="grid-container">' +
'<img class="list-image" src="../uploaded_images/' + item['id'] + '.jpg"/>' +
'<p>' + item['name'] + '</p>' +
'<p>Quantité : ' + item['quantity'] + '</p>' +
'<input placeholder="Nombre" type="number" value="0" class="form-control"/>' +
'</div>' +
'</div>');
}
function generateList() {
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;
}

View file

@ -35,19 +35,6 @@ class Dao
return $cursor->fetchAll(PDO::FETCH_ASSOC); return $cursor->fetchAll(PDO::FETCH_ASSOC);
} }
public function get_categories_of_article($articleid)
{
$sql = 'SELECT category_id FROM article_categories WHERE article_id=?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$articleid]);
$result = $cursor->fetchAll(PDO::FETCH_ASSOC);
$final = [];
foreach ($result as $row) {
array_push($final, $row["category_id"]);
}
return $final;
}
public function get_article_categories() public function get_article_categories()
{ {
$sql = 'SELECT * FROM article_categories'; $sql = 'SELECT * FROM article_categories';
@ -150,12 +137,4 @@ class Dao
} else } else
return 0; return 0;
} }
public function update_article_stock($articleid, $diff)
{
$sql = 'UPDATE articles SET quantity=quantity+? WHERE id=?';
$cursor = $this->conn->prepare($sql);
$data = [$diff, $articleid];
return $cursor->execute($data);
}
} }

View file

@ -3,8 +3,8 @@ require_once 'dao.php';
class PostHandler class PostHandler
{ {
private $valid_types = ["article", "category", "article_categories", "image", "stock"]; private $valid_types = ["article", "category", "article_categories", 'image'];
private $valid_actions = ["create", "update", "remove", "get", "buy", "sell"]; private $valid_actions = ["create", "update", "remove", "get"];
private $action; private $action;
private $type; private $type;
@ -13,8 +13,6 @@ class PostHandler
private $data; private $data;
private $dao; private $dao;
private $uploadBaseDir = '../uploaded_images/'; private $uploadBaseDir = '../uploaded_images/';
private $stockFile = "../data/stock-v2.json";
private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
private $responseArray = array( private $responseArray = array(
"status" => 0, "status" => 0,
@ -37,8 +35,6 @@ class PostHandler
$result = -1; $result = -1;
if ($this->type == "image") { if ($this->type == "image") {
$result = $this->save_image(); $result = $this->save_image();
} else if ($this->type == "stock") {
$result = $this->updateStock();
} else if (count($this->data) > 0) { } else if (count($this->data) > 0) {
if ($this->action == "create") if ($this->action == "create")
$result = $this->create(); $result = $this->create();
@ -61,44 +57,16 @@ class PostHandler
return $this->responseArray; return $this->responseArray;
} }
public function write_json()
{
$result = 0;
$fp = fopen($this->stockFile, "w");
$array = array(
"types" => $this->dao->get_categories(),
"articles" => $this->get_articles_json_list(),
);
fwrite($fp, json_encode($array));
fclose($fp);
$this->responseArray["data"] = $result;
return $this->responseArray;
}
public function get_articles_json_list()
{
$articles = $this->dao->get_articles();
$formatted_articles = [];
foreach ($articles as $article) {
$article["type"] = $this->dao->get_categories_of_article($article["id"]);
$article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
array_push($formatted_articles, $article);
}
return $formatted_articles;
}
private function save_image() private function save_image()
{ {
$success = true; $success = true;
if ($this->filesData["image"]["size"] > 0 && $this->data != null) { if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
$uploadPath = $this->uploadBaseDir . $this->data . ".jpg"; $uploadPath = $this->uploadBaseDir.$this->data.".jpg";
if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) { if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
$this->responseArray["message"] = "Image upload success"; $this->responseArray["message"] = "Image upload success";
} else { } else {
$this->responseArray["message"] = "Image upload failure: " . $uploadPath; $this->responseArray["message"] = "Image upload failure: ". $uploadPath;
$this->responseArray["status"] = 1; $this->responseArray["status"] = 1;
$success = false; $success = false;
} }
@ -110,12 +78,11 @@ class PostHandler
if ($success) if ($success)
return 0; return 0;
else else
return json_encode($this->filesData) . "id: " . $this->data; return json_encode($this->filesData)."id: ".$this->data;
} }
private function remove_image() private function remove_image() {
{ $uploadPath = $this->uploadBaseDir.$this->data["id"].".jpg";
$uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
if (file_exists($uploadPath) && unlink($uploadPath)) { if (file_exists($uploadPath) && unlink($uploadPath)) {
$this->responseArray["message"] = "Success: Deleted image"; $this->responseArray["message"] = "Success: Deleted image";
} else if (!file_exists($uploadPath)) { } else if (!file_exists($uploadPath)) {
@ -184,20 +151,6 @@ class PostHandler
return $result; return $result;
} }
function updateStock()
{
$result = 0;
foreach ($this->data as $row) {
$value = $row["value"];
if ($this->action == "sell")
$value = -$value;
$result = $this->dao->update_article_stock($row["id"], $value);
if (!$result)
break;
}
return $result;
}
function setUnknownTypeResponse() function setUnknownTypeResponse()
{ {
$this->responseArray["status"] = 1; $this->responseArray["status"] = 1;