Generate stock on button press
This commit is contained in:
parent
d935a40bcb
commit
7bfb239ec4
4 changed files with 85 additions and 26 deletions
|
@ -6,7 +6,7 @@ ob_start();
|
||||||
<div style="margin: auto">
|
<div style="margin: auto">
|
||||||
<div style="display: flex" class="my-5">
|
<div style="display: flex" class="my-5">
|
||||||
<a href="stock.php" style="margin: auto">
|
<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>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -18,6 +18,11 @@ ob_start();
|
||||||
<button class="btn btn-primary btn-lg">Créer/Éditer des catégories</button>
|
<button class="btn btn-primary btn-lg">Créer/Éditer des catégories</button>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
|
|
||||||
|
@ -26,5 +31,12 @@ 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");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,22 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
$rest_json = file_get_contents("php://input");
|
$relativePath = "../";
|
||||||
$_POST = json_decode($rest_json, true);
|
require_once $relativePath.'classes/postHandler.php';
|
||||||
|
|
||||||
|
$handler = new PostHandler(null, null);
|
||||||
|
|
||||||
//var_dump($_POST);
|
echo json_encode($handler->write_json());
|
||||||
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
|
|
44
assets/js/index.js
Normal file
44
assets/js/index.js
Normal 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',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
|
@ -13,6 +13,7 @@ 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 $responseArray = array(
|
private $responseArray = array(
|
||||||
"status" => 0,
|
"status" => 0,
|
||||||
|
@ -59,6 +60,21 @@ 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->dao->get_articles(),
|
||||||
|
);
|
||||||
|
fwrite($fp, json_encode($array));
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
$this->responseArray["data"] = $result;
|
||||||
|
return $this->responseArray;
|
||||||
|
}
|
||||||
|
|
||||||
private function save_image()
|
private function save_image()
|
||||||
{
|
{
|
||||||
$success = true;
|
$success = true;
|
||||||
|
@ -83,7 +99,8 @@ class PostHandler
|
||||||
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";
|
||||||
|
@ -153,7 +170,8 @@ class PostHandler
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateStock() {
|
function updateStock()
|
||||||
|
{
|
||||||
$result = 0;
|
$result = 0;
|
||||||
foreach ($this->data as $row) {
|
foreach ($this->data as $row) {
|
||||||
$value = $row["value"];
|
$value = $row["value"];
|
||||||
|
|
Loading…
Reference in a new issue