Thanasoft-H2F/gestion/lib/Controller/OrderController.php
2025-02-11 15:56:13 +03:00

252 lines
6.2 KiB
PHP

<?php
namespace OCA\Gestion\Controller;
use Exception;
use OCA\Gestion\Service\ConfigurationService;
use OCA\Gestion\Service\NavigationService;
use OCA\Gestion\Service\Order\OrderPdfService;
use OCA\Gestion\Service\Order\OrderService;
use Psr\Log\LoggerInterface;
defined("TAB1") or define("TAB1", "\t");
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Files\IRootFolder;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Gestion\Db\Bdd;
use OCP\IURLGenerator;
use OCP\IConfig;
date_default_timezone_set('Europe/Paris');
class OrderController extends Controller {
private $idNextcloud;
private $myDb;
// private $src_path = "/var/www/html/apps/gestion/img/";
private $src_path = "/var/www/html/custom_apps/gestion/img/";
private const H2F_DEFAULT_ADMIN = "Emmanuelle";
private const DEFAULT_ADMIN = "admin";
private $urlGenerator;
private $mailer;
private $config;
/** @var IRootStorage */
private $storage;
private $user;
private $groups = [];
/** @var OrderService */
private $orderService;
/** @var \OCA\Gestion\Service\NavigationService */
private $navigationService;
private $configurationService;
private $orderPdfService;
private $logger;
/**
* Constructor
*/
public function __construct($AppName,
IRequest $request,
$UserId,
Bdd $myDb,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IMailer $mailer,
Iconfig $config,
IUserSession $userSession,
IGroupManager $groupManager,
OrderService $orderService,
NavigationService $navigationService,
ConfigurationService $configurationService,
LoggerInterface $logger,
OrderPdfService $orderPdfService
){
parent::__construct($AppName, $request);
$this->idNextcloud = $UserId;
$this->myDb = $myDb;
$this->urlGenerator = $urlGenerator;
$this->mailer = $mailer;
$this->config = $config;
$this->orderService = $orderService;
$this->navigationService = $navigationService;
$this->configurationService = $configurationService;
$this->logger = $logger;
$this->orderPdfService = $orderPdfService;
if ($userSession->isLoggedIn()) {
$this->user = $userSession->getUser();
}
if ($this->user != null) {
$groups = $groupManager->getUserGroups($this->user);
$this->groups = [];
foreach ($groups as $group) {
$this->groups[] = $group->getGID();
}
}
try{
$this->storage = $rootFolder->getUserFolder($this->idNextcloud);
}catch(\OC\User\NoUserException $e){
}
}
private function getLogo(){
try {
if(isset($this->storage)){
$file = $this->storage->get('/.gestion/logo.png');
}else{
return "nothing";
}
} catch(\OCP\Files\NotFoundException $e) {
return "nothing";
}
return base64_encode($file->getContent());
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function order() {
return new TemplateResponse('gestion', 'order', array('groups' => $this->groups, 'user' => $this->user, 'path' => $this->idNextcloud, 'url' => $this->navigationService->getNavigationLink()));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getOrdersWithDetails() {
$orders = $this->orderService->getOrdersWithDetailsAsArray();
return json_encode($orders);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function createDefaultOrder() {
try{
$this->orderService->createDefaultOrder($this->idNextcloud);
return true;
}
catch(Exception $e){
return null;
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function updateOrderDate($orderId,$orderDate) {
try{
$this->orderService->updateOrderDate($orderId,$orderDate);
return true;
}
catch(Exception $e){
return null;
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function thanatoProductFee() {
return new TemplateResponse('gestion', 'thanatoProductFee', array('groups' => $this->groups, 'user' => $this->user, 'path' => $this->idNextcloud, 'url' => $this->navigationService->getNavigationLink()));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getThanatoProductsFees() {
$thanatoProductsFees = $this->orderService->getThanatoProductsFees();
return json_encode($thanatoProductsFees);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function createDefaultThanatoProductFee() {
try{
$this->orderService->createDefaultThanatoProductFee();
return true;
}
catch(Exception $e){
return null;
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $orderId
*/
public function orderDetails($orderId) {
$this->logger->debug("logId");
$this->logger->debug("$orderId");
$orderWithDetail = $this->orderService->getOrderByIdWithDetails($orderId);
return new TemplateResponse(
'gestion',
'orderDetails',
array(
'groups' => $this->groups,
'user' => $this->user,
"configuration" => json_decode(json_encode($this->configurationService->getDefaultConfiguration())),
'order'=>json_decode(json_encode($orderWithDetail)),
'path' => $this->idNextcloud,
'url' => $this->navigationService->getNavigationLink(),
'logo' => $this->getLogo()
));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $orderId
*/
public function getOrderProductsById($orderId) {
$orderProducts = $this->orderService->getOrderProductsById($orderId);
return json_encode($orderProducts);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $orderId
*/
public function getOrderTotalAmount($orderId) {
$configuration = $this->configurationService->getDefaultConfiguration();
$orderTotalAmount = $this->orderService->getOrderTotalAmount($orderId,$configuration["tva_default"] ?? 0);
return json_encode($orderTotalAmount);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $orderId
*/
public function exportOrderToPdf($orderId) {
try{
$factureFilenames = $this->orderPdfService->generateOrderPdfByOrderId($orderId,$this->idNextcloud);
return json_encode($factureFilenames);
}
catch(\OCP\Files\NotFoundException $e) { }
}
}