Thanasoft-H2F/gestion/lib/Db/OrderBdd.php
2025-02-24 17:40:20 +03:00

578 lines
22 KiB
PHP

<?php
namespace OCA\Gestion\Db;
use OCA\Gestion\Constants\OrderStatusConstant;
use OCA\Gestion\Constants\OrderTypeConstant;
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;
}
private function updateOrderNumber($orderId,$datetime){
$lastOrderNumber = $this->getLastOrderNumber();
$currentOrderNumber = $lastOrderNumber + 1;
$currentOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($datetime,$currentOrderNumber);
$sql = "UPDATE ".$this->orderTablePrefix."orders as orders
SET orders.order_number = ?,orders.order_full_number = ?
WHERE orders.id = ?
";
$this->execSQLNoData($sql,[$currentOrderNumber,$currentOrderFullNumber,$orderId]);
}
private function updateOrderDate($orderId,$orderDate){
$sql = "UPDATE ".$this->orderTablePrefix."orders as orders
SET orders.order_date = ?
WHERE orders.id = ?
";
$this->execSQLNoData($sql,[$orderDate,$orderId]);
}
private function getProductPriceByThanatoId($thanatoId,$productId){
$sql = "SELECT *
FROM ".$this->orderTablePrefix ."thanato_product_discount as thanato_product_discount
WHERE thanato_product_discount.fk_thanato_id = ? AND
thanato_product_discount.fk_product_id = ?;
";
$thanatoProductDiscount = $this->execSQLNoJsonReturn(
$sql,
[$thanatoId,$productId]);
if(!empty($thanatoProductDiscount)){
return $thanatoProductDiscount[0]['ht_price'];
}
return null;
}
private function getLastOrderProductId(){
$sql = "SELECT max(order_product.id) as MAX_ORDER_PRODUCT_ID FROM ".$this->orderTablePrefix."order_product as order_product;";
$result = $this->execSQLNoJsonReturn($sql,[]);
if(!empty($result)){
return $result[0]['MAX_ORDER_PRODUCT_ID'];
}
return 0;
}
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'];
}
public function updateOrderStatus($orderId,$statusKey){
$sql= "UPDATE ".$this->orderTablePrefix."orders as orders
SET orders.fk_order_status_key = ?
WHERE orders.id = ?";
$this->execSQLNoData($sql,[$statusKey,$orderId]);
}
public function getOrderItemsReferenceByOrderId($orderId){
$sql = "SELECT
order_item.id as order_item_id,
order_product.reference as order_product_reference
FROM ".$this->orderTablePrefix."order_item as order_item
LEFT JOIN ".$this->orderTablePrefix."order_product as order_product on order_item.fk_order_item_id = order_product.id
WHERE order_item.fk_order_id = ?
GROUP BY order_product.reference;";
$itemList = $this->execSQLNoJsonReturn(
$sql,
[$orderId]);
$itemReferences = [];
foreach($itemList as &$item){
if($item['order_product_reference'] != null){
$itemReferences[] = $item['order_product_reference'];
}
}
return $itemReferences;
}
public function getOrderItemsByOrderId($orderId){
$sql = "SELECT
order_item.id as order_item_id,
order_item.quantity as order_item_quantity,
order_item.fk_order_id as fk_order_id,
order_product.id as order_product_id,
order_product.reference as order_product_reference,
order_product.label as order_product_label,
order_product.ht_amount as order_product_ht_amount
FROM ".$this->orderTablePrefix."order_item as order_item
LEFT JOIN ".$this->orderTablePrefix."order_product as order_product on order_item.fk_order_item_id = order_product.id
WHERE order_item.fk_order_id = ?;";
$itemList = $this->execSQLNoJsonReturn(
$sql,
[$orderId]);
$finalItemList = [];
foreach($itemList as &$item){
if($item['order_product_id'] != null){
$finalItemList[] = $item;
}
}
return $finalItemList;
}
public function getOrderProductsByOrderId($orderId){
$sql = "SELECT
order_product.id as order_product_id,
order_product.quantity as order_product_quantity,
produit.id as produit_id,
produit.reference as produit_reference,
produit.description as produit_description,
produit.prix_unitaire as produit_ht_price,
orders.fk_thanato_id as order_thanato_id
FROM ".$this->orderTablePrefix."order_product as order_product
LEFT JOIN ".$this->orderTablePrefix."produit as produit on order_product.fk_product_id = produit.id
LEFT JOIN ".$this->orderTablePrefix."orders as orders on order_product.fk_order_id = orders.id
WHERE order_product.fk_order_id = ?;";
$produitsList = $this->execSQLNoJsonReturn(
$sql,
[$orderId]);
$finalProduitList = [];
foreach($produitsList as &$produit){
if($produit['produit_id'] != null){
$htPrice = $produit['produit_ht_price'];
$orderThanatoId = $produit["order_thanato_id"];
if($orderThanatoId != null){
$productPrice = $this->getProductPriceByThanatoId($orderThanatoId,$produit['id']);
$htPrice = $productPrice ?? $htPrice;
$produit['produit_ht_price'] = $htPrice * $produit['order_product_quantity'];
}
$finalProduitList[] = $produit;
}
}
return $finalProduitList;
}
public function getOrdersWithDetails(){
$sql = "SELECT
orders.id,
orders.order_date,
orders.order_number,
orders.order_full_number,
orders.order_comment,
orders.fk_order_type_key,
orders.fk_devis_id,
devis.id_defunt as fk_defunt_id,
devis.id_lieu as fk_lieu_id,
devis.id_client as fk_client_id,
devis.id_thanato as fk_thanato_id,
orders.fk_order_status_key,
orders.fk_provider_id,
thanato.id as thanato_id,
thanato.nom as thanato_nom,
thanato.prenom as thanato_prenom,
client.id as client_id,
client.nom as client_nom,
client.prenom as client_prenom,
client.entreprise as client_entreprise,
defunt.id as defunt_id,
defunt.nom as defunt_nom,
lieu.id as lieu_id,
lieu.adresse as lieu_adresse,
lieu.nom as lieu_nom,
order_status.status_label as order_status_label,
provider.provider_name as provider_name,
provider.provider_last_name as provider_last_name,
provider.provider_company_name as provider_company_name
FROM "
.$this->orderTablePrefix."orders as orders
LEFT JOIN ".$this->orderTablePrefix."devis as devis on orders.fk_devis_id = devis.id
LEFT JOIN ".$this->orderTablePrefix."thanato as thanato on devis.id_thanato = thanato.id
LEFT JOIN ".$this->orderTablePrefix."client as client on devis.id_client = client.id
LEFT JOIN ".$this->orderTablePrefix."defunt as defunt on devis.id_defunt = defunt.id
LEFT JOIN ".$this->orderTablePrefix."lieu as lieu on devis.id_lieu = lieu.id
LEFT JOIN ".$this->orderTablePrefix."provider as provider on orders.fk_provider_id = provider.id
LEFT JOIN ".$this->orderTablePrefix."order_status as order_status on orders.fk_order_status_key = order_status.status_key
ORDER BY orders.id DESC;
"
;
return $this->execSQLNoJsonReturn($sql,[]);
}
public function createDefaultOrder($idNextCloud){
$currentDatetime = new Datetime();
$currentDate = $currentDatetime->format('Y-m-d');
$lastOrderNumber = $this->getLastOrderNumber();
$currentOrderNumber = $lastOrderNumber + 1;
$currentOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($currentDatetime,$currentOrderNumber);
$sql = "INSERT INTO `".$this->orderTablePrefix."orders` (
`order_date`,
`order_number`,
`order_full_number`,
`fk_order_status_key`,
`id_nextcloud`,
`fk_order_type_key`
)
VALUES (?,?,?,?,?,?);";
$this->execSQLNoData($sql, array(
$currentDate,
$currentOrderNumber,
$currentOrderFullNumber,
OrderStatusConstant::NEW_KEY,
$idNextCloud,
OrderTypeConstant::ORDER_TYPE_PURCHASE
)
);
}
public function updateOrderDateAndSetNewOrderNumber($orderId,$date){
$this->updateOrderDate($orderId,$date);
$datetime = new Datetime($date);
$this->updateOrderNumber($orderId,$datetime);
}
public function getThanatoProductDiscountList(){
$sql = "SELECT
thanato_product_discount.id,
thanato_product_discount.fk_thanato_id,
thanato_product_discount.fk_product_id,
thanato.nom as thanato_nom,
thanato.prenom as thanato_prenom,
produit.reference as produit_reference,
produit.description as produit_description,
produit.prix_unitaire as produit_ht_price
FROM ".$this->orderTablePrefix."thanato_product_discount as thanato_product_discount
LEFT JOIN ".$this->orderTablePrefix."thanato as thanato on thanato_product_discount.fk_thanato_id = thanato.id
LEFT JOIN ".$this->orderTablePrefix."produit as produit on thanato_product_discount.fk_product_id = produit.id
ORDER BY thanato_product_discount.id DESC
";
return $this->execSQLNoJsonReturn($sql,[]);
}
public function createDefaultThanatoProductFee(){
$sql = "INSERT INTO `".$this->orderTablePrefix."thanato_product_discount` (`fk_thanato_id`,`fk_product_id`,`ht_price`) VALUES (0,0,0);";
$this->execSQLNoData($sql, []);
}
public function getOrderCount(){
$count = 0;
$sql = "SELECT COUNT(orders.id) as order_count
FROM ".$this->orderTablePrefix."orders as orders;";
$result = $this->execSQLNoJsonReturn($sql,[]);
if(!empty($result)){
$count = $result[0]["order_count"];
}
return $count;
}
public function getThanatoProductFeeCount(){
$count = 0;
$sql = "SELECT COUNT(thanato_product_discount.id) as thanato_product_fee_count
FROM ".$this->orderTablePrefix."thanato_product_discount as thanato_product_discount;";
$result = $this->execSQLNoJsonReturn($sql,[]);
if(!empty($result)){
$count = $result[0]["thanato_product_fee_count"];
}
return $count;
}
public function getOrderByIdWithDetails($orderId){
$sql = "SELECT
orders.id,
orders.order_date,
orders.order_number,
orders.order_full_number,
orders.order_comment,
orders.fk_order_status_key,
orders.fk_order_type_key,
orders.fk_devis_id,
thanato.id as thanato_id,
thanato.nom as thanato_nom,
thanato.prenom as thanato_prenom,
client.id as client_id,
client.nom as client_nom,
client.prenom as client_prenom,
client.entreprise as client_entreprise,
client.adresse as client_adresse,
client.mail as client_mail,
client.telephone as client_phone,
client.legal_one as client_legal_one,
defunt.id as defunt_id,
defunt.nom as defunt_nom,
defunt.sexe as defunt_sexe,
lieu.id as lieu_id,
lieu.adresse as lieu_adresse,
lieu.nom as lieu_nom,
order_status.status_label as order_status_label,
provider.provider_name,
provider.provider_last_name,
provider.provider_company_name,
provider.provider_siret_number,
provider.provider_phone,
provider.provider_email,
provider.provider_address,
provider.provider_city,
devis.date as devis_date
FROM "
.$this->orderTablePrefix."orders as orders
LEFT JOIN ".$this->orderTablePrefix."devis as devis on orders.fk_devis_id = devis.id
LEFT JOIN ".$this->orderTablePrefix."thanato as thanato on devis.id_thanato = thanato.id
LEFT JOIN ".$this->orderTablePrefix."client as client on devis.id_client = client.id
LEFT JOIN ".$this->orderTablePrefix."defunt as defunt on devis.id_defunt = defunt.id
LEFT JOIN ".$this->orderTablePrefix."lieu as lieu on devis.id_lieu = lieu.id
LEFT JOIN ".$this->orderTablePrefix."order_status as order_status on orders.fk_order_status_key = order_status.status_key
LEFT JOIN ".$this->orderTablePrefix."provider as provider on orders.fk_provider_id = provider.id
WHERE orders.id = ?
LIMIT 1;
"
;
$result = $this->execSQLNoJsonReturn($sql,[$orderId]);
if(!empty($result)){
return $result[0];
}
return null;
}
public function getOrderTotalAmount($orderId,$tva){
$totalHt = 0;
$totalTtc = 0;
$totalTvaAmount = 0;
$orderItems = $this->getOrderItemsByOrderId($orderId);
foreach($orderItems as $orderItem){
$totalHt += $orderItem["order_product_ht_amount"] * $orderItem["order_item_quantity"];
$totalTvaAmount += $orderItem["order_product_ht_amount"] * $tva / 100;
$totalTtc += ($totalHt * ($tva + 100)) / 100;
}
return [
"totalHt" => $totalHt,
"totalTtc" => $totalTtc,
"totalTvaAmount" => $totalTvaAmount,
"tva" => $tva
];
}
public function createOrderFromDevisIdAndDate(int $devisId,Datetime $devisDate,string $idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){
$lastOrderNumber = $this->getLastOrderNumber();
$currentOrderNumber = $lastOrderNumber + 1;
$currentOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($devisDate,$currentOrderNumber);
$orderDate = $devisDate->format("Y-m-d");
$sql = "INSERT INTO `".$this->orderTablePrefix."orders` (
`order_date`,
`order_number`,
`order_full_number`,
`fk_order_status_key`,
`id_nextcloud`,
`fk_devis_id`,
`fk_order_type_key`
)
VALUES (?,?,?,?,?,?,?);";
$this->execSQLNoData($sql, array(
$orderDate,
$currentOrderNumber,
$currentOrderFullNumber,
OrderStatusConstant::ORDERED_KEY,
$idNextCloud,
$devisId,
OrderTypeConstant::ORDER_TYPE_DEVIS
)
);
return true;
}
public function getOrderProducts(){
$sql = "SELECT * FROM ".$this->orderTablePrefix."order_product as order_product;";
return $this->execSQL($sql,[]);
}
public function createDefaultOrderProduct($idNextCloud){
$sql = "INSERT INTO `".$this->orderTablePrefix."order_product` (`reference`,`label`,`ht_amount`,`id_nextcloud`)
VALUES ('','',0,?);";
$this->execSQLNoData($sql, [$idNextCloud]);
}
public function getOrderProductCount(){
$count = 0;
$sql = "SELECT COUNT(order_product.id) as order_product_count
FROM ".$this->orderTablePrefix."order_product as order_product;";
$result = $this->execSQLNoJsonReturn($sql,[]);
if(!empty($result)){
$count = $result[0]["order_product_count"];
}
return $count;
}
private function addOrderItem($orderId,$orderProductId,$quantity){
$sql = "INSERT INTO `".$this->orderTablePrefix."order_item` (`fk_order_id`,`fk_order_item_id`,`quantity`) VALUES (?,?,?);";
$this->execSQLNoData($sql, array($orderId,$orderProductId,$quantity));
return true;
}
public function addDefaultOrderItem($orderId){
$lastOrderProductId = $this->getLastOrderProductId();
$defaultQuantity = 1;
$this->addOrderItem($orderId,$lastOrderProductId,$defaultQuantity);
return true;
}
public function getProviderOrdersByMonthAndYear($providerId,$month,$year){
$sql =
"SELECT
orders.id,
orders.order_date,
orders.order_number,
orders.order_full_number,
orders.fk_order_status_key,
orders.fk_order_type_key,
orders.fk_provider_id
FROM ".$this->orderTablePrefix."orders as orders
WHERE
orders.fk_provider_id = ? AND
MONTH(orders.order_date) = ? AND
YEAR(orders.order_date) = ? AND
orders.fk_order_type_key = ? AND
orders.fk_order_status_key != ?
ORDER BY orders.order_date ASC;";
$orders = $this->execSQLNoJsonReturn($sql,[ $providerId,$month,$year,OrderTypeConstant::ORDER_TYPE_PURCHASE,OrderStatusConstant::CANCELED_KEY]);
return $orders;
}
public function getItemCountByOrderIdListAndOrderProductId($orderIdsList,$orderProductId){
if(empty($orderIdsList)){
return 0;
}
$sqlConditionsPlaceholder = implode(',', array_fill(0, count($orderIdsList), '?'));
$sql = "SELECT
SUM(order_item.quantity) as total_quantity
FROM ".$this->orderTablePrefix ."order_item as order_item
WHERE order_item.fk_order_id IN ($sqlConditionsPlaceholder) AND
order_item.fk_order_item_id = ?;";
$produitList = $this->execSQLNoJsonReturn(
$sql,
array_merge($orderIdsList,array($orderProductId)));
if(!empty($produitList)){
return $produitList[0]['total_quantity'];
}
return 0;
}
public function getOrderByDevisId($devisId){
$sql = "SELECT * FROM ".$this->orderTablePrefix."orders as orders
WHERE orders.fk_devis_id = ? LIMIT 1";
$result = $this->execSQLNoJsonReturn($sql,[$devisId]);
if(!empty($result)){
return $result[0];
}
return null;
}
}