122 lines
3.9 KiB
PHP
122 lines
3.9 KiB
PHP
<?php
|
|
namespace OCA\Gestion\Db;
|
|
|
|
use OCA\Gestion\Constants\OrderStatusConstant;
|
|
use OCA\Gestion\Constants\ThanatoTypeConstant;
|
|
use OCA\Gestion\Helpers\OrderHelpers;
|
|
use OCP\IDBConnection;
|
|
use \Datetime;
|
|
use OCA\Gestion\Constants\BddConstant;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class OrderBdd {
|
|
private $orderTablePrefix;
|
|
|
|
private $defaultTablePrefix;
|
|
private IDbConnection $pdo;
|
|
|
|
public function __construct(IDbConnection $db) {
|
|
$this->orderTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX ."gestion_";
|
|
$this->defaultTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX;
|
|
$this->pdo = $db;
|
|
}
|
|
|
|
private function execSQL($sql, $conditions){
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($conditions);
|
|
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
$stmt->closeCursor();
|
|
return json_encode($data);
|
|
}
|
|
|
|
private function execSQLNoData($sql, $conditions){
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($conditions);
|
|
$stmt->closeCursor();
|
|
}
|
|
|
|
private function execSQLNoJsonReturn($sql, $conditions){
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($conditions);
|
|
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
$stmt->closeCursor();
|
|
return $data;
|
|
}
|
|
|
|
private function getLastOrderNumber(){
|
|
$sql = "SELECT max(orders.order_number) as order_last_number FROM "
|
|
.$this->orderTablePrefix."orders as orders";
|
|
|
|
$result = $this->execSQLNoJsonReturn($sql,[]);
|
|
$lastNumber = 0;
|
|
if(!empty($result)){
|
|
$lastNumber = $result[0]["order_last_number"];
|
|
}
|
|
return $lastNumber;
|
|
}
|
|
|
|
private function insertOrderProduct(int $orderId,int $productId){
|
|
$sql = "INSERT INTO ".$this->orderTablePrefix."order_product (fk_order_id, fk_product_id, quantity) VALUES (?,?,1)";
|
|
$this->execSQLNoData($sql, array($orderId, $productId));
|
|
return true;
|
|
}
|
|
|
|
public function getOrderByCalendarUuid($calendarUuid){
|
|
$sql = "SELECT * FROM ".$this->orderTablePrefix."orders as orders
|
|
WHERE orders.fk_calendar_uuid = ? LIMIT 1";
|
|
|
|
$result = $this->execSQLNoJsonReturn($sql,[$calendarUuid]);
|
|
if(!empty($result)){
|
|
return $result[0];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function insertOrderProductsByProductIds(int $orderId,array $productIds){
|
|
if (!empty($productIds)) {
|
|
foreach ($productIds as $productId) {
|
|
$this->insertOrderProduct(
|
|
$orderId,
|
|
$productId
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function insertOrderFromVCalendarPropertyAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$date,$idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){
|
|
$dateTime = new DateTime($date);
|
|
|
|
$lastOrderNumber = $this->getLastOrderNumber();
|
|
$thisOrderNumber = $lastOrderNumber + 1;
|
|
$thisOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($dateTime,$thisOrderNumber);
|
|
$sql = "INSERT INTO `".$this->orderTablePrefix."orders` (
|
|
`order_date`,
|
|
`order_number`,
|
|
`order_full_number`,
|
|
`fk_defunt_id`,
|
|
`fk_lieu_id`,
|
|
`fk_client_id`,
|
|
`fk_thanato_id`,
|
|
`fk_order_status_key`,
|
|
`fk_calendar_uuid`,
|
|
`id_nextcloud`
|
|
)
|
|
VALUES (?,?,?,?,?,?,?,?,?,?);";
|
|
$this->execSQLNoData($sql, array(
|
|
$date,
|
|
$thisOrderNumber,
|
|
$thisOrderFullNumber,
|
|
$defuntId,
|
|
$locationId,
|
|
$clientId,
|
|
$thanatoId,
|
|
OrderStatusConstant::ORDERED_KEY,
|
|
$calendarUuid,
|
|
$idNextCloud
|
|
)
|
|
);
|
|
|
|
$order = $this->getOrderByCalendarUuid($calendarUuid);
|
|
return $order['id'];
|
|
}
|
|
} |