Generate stock on button press

This commit is contained in:
keplyx 2020-02-20 12:20:42 +01:00
parent d935a40bcb
commit 7bfb239ec4
4 changed files with 85 additions and 26 deletions

View file

@ -6,7 +6,7 @@ ob_start();
<div style="margin: auto">
<div style="display: flex" class="my-5">
<a href="stock.php" style="margin: auto">
<button class="btn btn-primary btn-lg">Gestion du stocks</button>
<button class="btn btn-success btn-lg">Gestion du stocks</button>
</a>
</div>
@ -18,6 +18,11 @@ ob_start();
<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>
@ -26,5 +31,12 @@ ob_start();
<?php
$pageContent = ob_get_clean();
$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");
?>

View file

@ -1,22 +1,7 @@
<?php
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);
$relativePath = "../";
require_once $relativePath.'classes/postHandler.php';
$handler = new PostHandler(null, null);
//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);
}
echo json_encode($handler->write_json());

44
assets/js/index.js Normal file
View file

@ -0,0 +1,44 @@
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();
console.log(result);
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

@ -13,6 +13,7 @@ class PostHandler
private $data;
private $dao;
private $uploadBaseDir = '../uploaded_images/';
private $stockFile = "../data/stock-v2.json";
private $responseArray = array(
"status" => 0,
@ -59,16 +60,31 @@ class PostHandler
return $this->responseArray;
}
public function write_json()
{
$result = 0;
$fp = fopen($this->stockFile, "w");
$array = array(
"types" => $this->dao->get_categories(),
"articles" => $this->dao->get_articles(),
);
fwrite($fp, json_encode($array));
fclose($fp);
$this->responseArray["data"] = $result;
return $this->responseArray;
}
private function save_image()
{
$success = true;
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)) {
$this->responseArray["message"] = "Image upload success";
} else {
$this->responseArray["message"] = "Image upload failure: ". $uploadPath;
$this->responseArray["message"] = "Image upload failure: " . $uploadPath;
$this->responseArray["status"] = 1;
$success = false;
}
@ -80,11 +96,12 @@ class PostHandler
if ($success)
return 0;
else
return json_encode($this->filesData)."id: ".$this->data;
return json_encode($this->filesData) . "id: " . $this->data;
}
private function remove_image() {
$uploadPath = $this->uploadBaseDir.$this->data["id"].".jpg";
private function remove_image()
{
$uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
if (file_exists($uploadPath) && unlink($uploadPath)) {
$this->responseArray["message"] = "Success: Deleted image";
} else if (!file_exists($uploadPath)) {
@ -153,7 +170,8 @@ class PostHandler
return $result;
}
function updateStock() {
function updateStock()
{
$result = 0;
foreach ($this->data as $row) {
$value = $row["value"];