Thanasoft-H2F/gestion/lib/Db/VehicleRepository.php
2025-02-21 13:57:37 +03:00

101 lines
3.0 KiB
PHP

<?php
namespace OCA\Gestion\Db;
use OCP\IDBConnection;
use \Datetime;
use OCA\Gestion\Constants\BddConstant;
class VehicleRepository {
private $gestionTablePrefix;
private $defaultTablePrefix;
private IDbConnection $pdo;
public function __construct(IDbConnection $db) {
$this->gestionTablePrefix = 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;
}
public function getVehicles(){
$sql = "SELECT
vehicle.id,
vehicle.brand,
vehicle.model,
vehicle.immatriculation,
vehicle.purchase_date,
vehicle.fk_vehicle_purchase_type_key,
vehicle.fk_thanato_id,
vehicle_purchase_type.type_label as purchase_type_label,
thanato.nom as thanato_name,
thanato.prenom as thanato_last_name
FROM ".$this->gestionTablePrefix."vehicle as vehicle
LEFT JOIN ".$this->gestionTablePrefix."vehicle_purchase_type as vehicle_purchase_type
ON vehicle.fk_vehicle_purchase_type_key = vehicle_purchase_type.type_key
LEFT JOIN ".$this->gestionTablePrefix."thanato as thanato
ON vehicle.fk_thanato_id = thanato.id
";
return $this->execSQL($sql, []);
}
public function createDefaultVehicle(){
$date = new DateTime();
$date = $date->format("Y-m-d");
$sql = "INSERT INTO `".$this->gestionTablePrefix."vehicle` (
`purchase_date`
)
VALUES (?);";
$this->execSQLNoData($sql, array(
$date
)
);
}
public function getVehiclePurchaseTypes(){
$sql = "SELECT * FROM ".$this->gestionTablePrefix."vehicle_purchase_type";
return $this->execSQL($sql, []);
}
public function getVehicleCount(){
$count = 0;
$sql = "SELECT COUNT(vehicle.id) as vehicle_count
FROM ".$this->gestionTablePrefix."vehicle as vehicle;";
$result = $this->execSQLNoJsonReturn($sql,[]);
if(!empty($result)){
$count = $result[0]["vehicle_count"];
}
return $count;
}
public function getAvailableVehicles(){
$sql = "SELECT * FROM ".$this->gestionTablePrefix."vehicle as vehicle
WHERE vechile.fk_thanato_id IS NULL
";
return $this->execSQL($sql, []);
}
}