src/App/Controller/Api/LivraisonController.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Controller\AbstractController;
  4. use App\Entity\Livraison;
  5. use App\Entity\Facture;
  6. use App\Model\Enum\AccessControl;
  7. use App\Traits\Autowired\Manager\LivraisonManagerTrait;
  8. use GollumSF\RestBundle\Annotation\Serialize;
  9. use GollumSF\RestBundle\Annotation\Unserialize;
  10. use GollumSF\RestBundle\Annotation\Validate;
  11. use GollumSF\RestDocBundle\Annotation\ApiDescribe;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route("/api/livraisons")
  17.  * @ApiDescribe(Livraison::class)
  18.  */
  19. class LivraisonController extends AbstractController {
  20.     use LivraisonManagerTrait;
  21.     /**
  22.      * @Route("", methods="GET", requirements={"livraison"="\d+"})
  23.      * @IsGranted(AccessControl::API_GET_LIVRAISON, subject="current_user")
  24.      * @Serialize(groups="livraison_getc")
  25.      * @ApiDescribe(
  26.      *     request={
  27.      *         "parameters"={
  28.      *             "filters"={
  29.      *                 "in"="query",
  30.      *                 "required"=false,
  31.      *                 "type"="string",
  32.      *                 "example"="{""boisType"":[],""boisQuality"":[],""boisSize"":[],""marquage"":[],""volumeInner"":{""min"":0,""max"":0},""volumeOutter"":{""min"":0,""max"":0},""coef"":{""min"":0,""max"":0}}"
  33.      *             }
  34.      *         }
  35.      *     }
  36.      * )
  37.      */
  38.     public function list() {
  39.         return $this->livraisonManager->apiFindBy();
  40.     }
  41.     
  42.     /**
  43.      * @Route("/{id}", methods="GET", requirements={"id"="\d+"})
  44.      * @IsGranted(AccessControl::API_GET_LIVRAISON, subject="current_user")
  45.      * @Serialize(groups="livraison_get")
  46.      */
  47.     public function find(Livraison $livraison) {
  48.         return $livraison;
  49.     }
  50.     /**
  51.      * @Route("", methods="POST")
  52.      * @IsGranted(AccessControl::API_POST_LIVRAISON, subject="current_user")
  53.      * @Unserialize("livraison", groups="livraison_post", save=false)
  54.      * @Validate({ "livraison_post" })
  55.      * @Serialize(groups="livraison_get")
  56.      *
  57.      */
  58.     public function post(Livraison $livraison) {
  59.         /*if ($livraison->getLivraison()->getPurchase()) {
  60.             throw new AccessDeniedHttpException();
  61.         }*/
  62.         return $this->livraisonManager->update($livraison);
  63.     }
  64.     /**
  65.      * @Route("/{id}", methods="PUT", requirements={"id"="\d+"})
  66.      * @IsGranted(AccessControl::API_PUT_LIVRAISON, subject="current_user")
  67.      * @Unserialize("livraison", groups="livraison_put", save=false)
  68.      * @Validate({ "livraison_put" })
  69.      * @Serialize(groups="livraison_get")
  70.      *
  71.      */
  72.     public function put(Livraison $livraison) {
  73.         /*if ($livraison->getLivraison()->getPurchase()) {
  74.             throw new AccessDeniedHttpException();
  75.         }*/
  76.         return $this->livraisonManager->update($livraison);
  77.     }
  78.     /**
  79.      * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
  80.      * @IsGranted(AccessControl::API_DELETE_LIVRAISON, subject="current_user")
  81.      * @Serialize(groups="livraison_get")
  82.      *
  83.      */
  84.     public function delete(Livraison $livraison) {
  85.         /*if ($livraison->getLivraison()->getPurchase()) {
  86.             throw new AccessDeniedHttpException();
  87.         }*/
  88.         return $this->livraisonManager->delete($livraison);
  89.     }
  90.     /**
  91.      * @Route("/synthese/{datemin}/{datemax}/{chauffeur}/{scierie}/{bl}/{facture}/{boisType}/{boisQuality}/{boisSize}/{marquage}", methods="GET")
  92.      * @IsGranted(AccessControl::API_GET_LIVRAISON, subject="current_user")
  93.      * @Serialize(groups="livraison_getc")
  94.      */
  95.     public function Synthese(String $dateminString $datemaxString $chauffeurString $scierie
  96.     String $blString $facture
  97.     String $boisTypeString $boisQualityString $boisSizeString $marquage) {
  98.         return $this->livraisonManager->getSynthese($datemin$datemax$chauffeur$scierie$bl$facture$boisType$boisQuality$boisSize$marquage);
  99.     }
  100.     
  101.     /**
  102.      * @Route("/{id}/putFacture/{id_facture}", methods="PUT", requirements={"id"="\d+"})
  103.      * @IsGranted(AccessControl::API_PUT_LIVRAISON, subject="current_user")
  104.      * @Serialize(groups="livraison_get")
  105.      *
  106.      */
  107.     public function putFacture(Livraison $livraisonstring $id_facture) {
  108.         $host $this->getParameter('database_host');
  109.         $port $this->getParameter('database_port');
  110.         $user $this->getParameter('database_user');
  111.         $pass $this->getParameter('database_password');
  112.         $db   $this->getParameter('database_name');
  113.         $charset 'utf8mb4';
  114.         $dsn "mysql:host=$host;dbname=$db;charset=$charset;port=$port";
  115.         $query "UPDATE livraison SET facture_id=" $id_facture " WHERE id=" $livraison->getId();
  116.         $pdo = new \PDO($dsn$user$pass);
  117.         $stmt $pdo->prepare($query);
  118.         $stmt->execute();
  119.         $query "SELECT numfacture from facture WHERE id=" $id_facture;
  120.         $stmt $pdo->prepare($query);
  121.         $stmt->execute();
  122.         $numfacture $stmt->fetchColumn();
  123.         if ($numfacture !== false)
  124.         {
  125.             $query "UPDATE cubage_livraison SET numfacture='" $numfacture "' WHERE livraison_id=" $livraison->getId();
  126.             $stmt $pdo->prepare($query);
  127.             $stmt->execute();
  128.             $query "UPDATE livraison SET numfacture='" $numfacture "' WHERE id=" $livraison->getId();
  129.             $stmt $pdo->prepare($query);
  130.             $stmt->execute();
  131.         }
  132.         return $livraison;
  133.     }
  134.     /**
  135.      * @Route("/resetFacture/{id}", methods="PUT", requirements={"id"="\d+"})
  136.      * @IsGranted(AccessControl::API_PUT_LIVRAISON, subject="current_user")
  137.      * @Serialize(groups="livraison_get")
  138.      *
  139.      */
  140.     public function resetFacture(Livraison $livraison) {
  141.         $host $this->getParameter('database_host');
  142.         $port $this->getParameter('database_port');
  143.         $user $this->getParameter('database_user');
  144.         $pass $this->getParameter('database_password');
  145.         $db   $this->getParameter('database_name');
  146.         $charset 'utf8mb4';
  147.         $dsn "mysql:host=$host;dbname=$db;charset=$charset;port=$port";
  148.         $query "UPDATE livraison SET facture_id=null WHERE id=" $livraison->getId();
  149.         $pdo = new \PDO($dsn$user$pass);
  150.         $stmt $pdo->prepare($query);
  151.         $stmt->execute();
  152.         return $livraison;
  153.     }
  154.     /**
  155.      * @Route("/repartir_prix_camion/{id}/{prix}/{prixTransport}", methods="PUT", requirements={"id"="\d+"})
  156.      * @IsGranted(AccessControl::API_PUT_LIVRAISON, subject="current_user")
  157.      * @Serialize(groups="livraison_get")
  158.      *
  159.      */
  160.     public function RepartirPrixCamion(Livraison $livraisonString $prixString $prixTransport) {
  161.         return $this->livraisonManager->RepartirPrixCamion($livraison$prix$prixTransport);
  162.     }
  163.     /**
  164.      * @Route("/fetch_for_facture/{id}", methods="GET")
  165.      * @IsGranted(AccessControl::API_GET_FACTURE, subject="current_user")
  166.      * @Serialize(groups="facture_get")
  167.      *
  168.      */
  169.     public function fetch_for_facture(String $id) {
  170.         $host $this->getParameter('database_host');
  171.         $port $this->getParameter('database_port');
  172.         $user $this->getParameter('database_user');
  173.         $pass $this->getParameter('database_password');
  174.         $db   $this->getParameter('database_name');
  175.         $charset 'utf8mb4';
  176.         $dsn "mysql:host=$host;dbname=$db;charset=$charset;port=$port";
  177.         $query "SELECT l.id, l.date, l.bl, t.name as scierie_name, c.first_name as chauffeur_prenom, c.last_name as chauffeur_nom 
  178.         FROM livraison AS l  
  179.         LEFT OUTER JOIN chauffeur AS c ON l.chauffeur_id = c.id 
  180.         LEFT OUTER JOIN tiers AS t ON l.scierie_id = t.id";
  181.         $queryWhere '';
  182.         if (isset($_REQUEST['filter'])) {
  183.             $queryWhere .= " WHERE ";
  184.             if (gettype(json_decode($_REQUEST['filter'], true)[0]) === 'string') {
  185.                 $filter json_decode($_REQUEST['filter']);
  186.                 $queryWhere .= $this->createQuery($filter);
  187.             } else {
  188.                 foreach (json_decode($_REQUEST['filter'], true) as $filter) {
  189.                     if (gettype($filter[0]) === 'array') {
  190.                         foreach ($filter as $item) {
  191.                             $queryWhere .= $this->createQuery($item);
  192.                         }
  193.                     } else {
  194.                         $queryWhere .= $this->createQuery($filter);
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.         if ($queryWhere == '') {
  200.             $queryWhere ' WHERE l.facture_id = ' $id;
  201.         } else {
  202.             $queryWhere .= ' AND l.facture_id = ' $id;
  203.         }
  204.         $query .= $queryWhere;
  205.         if (isset($_REQUEST['sort'])) {
  206.             foreach (json_decode($_REQUEST['sort'], true) as $sort) {
  207.                 if (isset($sort['selector']))
  208.                 {
  209.                     $selector $sort['selector'];
  210.                     $order $sort['desc'] === true 'DESC' 'ASC';
  211.                     if ($selector == "chauffeur_name")
  212.                     {
  213.                         $query .= " ORDER BY c.first_name " $order;
  214.                     }
  215.                     else
  216.                     {
  217.                         $query .= " ORDER BY " $selector ' ' $order;
  218.                     }
  219.                 }
  220.             }
  221.         } else {
  222.             $query .= " ORDER BY l.id DESC";
  223.         }
  224.         $offset $_REQUEST['skip'];
  225.         $limit $_REQUEST['take'];
  226.         $query .= " LIMIT " $limit " OFFSET " $offset;
  227.         $result = [];
  228.         $pdo = new \PDO($dsn$user$pass);
  229.         $stmt $pdo->prepare($query);
  230.         $stmt->execute();
  231.         $data $stmt->fetchAll();
  232.         foreach($data as $row) {
  233.             // On va chercher les cubages associĆ©s a la livraison
  234.             $query "SELECT c.id as id, `bois_type_id`, `bois_quality_id`, `bois_size_id`, `marquage_id`, `volume_inner`,  `volume_stere`, volume_approximatif as volumeApproximatif,
  235.             bt.name as bois_type, bq.name as bois_quality, bs.name as bois_size, bs.grume, ma.color as marquage_color,
  236.             ma.name as marquage,
  237.             ch.id as chantier_id, ch.name as chantier_name, ch.client as chantier_client, ch.commune as chantier_commune
  238.             FROM `cubage_livraison` as c 
  239.             LEFT JOIN bois_type as bt ON bt.id=c.bois_type_id
  240.             LEFT JOIN bois_quality as bq ON bq.id=c.bois_quality_id
  241.             LEFT JOIN bois_size as bs ON bs.id=c.bois_size_id
  242.             LEFT JOIN marquage as ma ON ma.id=c.marquage_id
  243.             LEFT JOIN chantier AS ch ON c.chantier_id = ch.id 
  244.             WHERE c.livraison_id = " $row["id"];
  245.             $stmt $pdo->prepare($query);
  246.             $stmt->execute();
  247.             $datarestant $stmt->fetchAll();
  248.             foreach($datarestant as $rowcubage) {
  249.                 $rowcubage["livraison_id"] = $row["id"];
  250.                 $rowcubage["date"] = $row["date"];
  251.                 $rowcubage["bl"] = $row["bl"];
  252.                 $rowcubage["scierie_name"] = $row["scierie_name"];
  253.                 $rowcubage["chauffeur_name"] = $row["chauffeur_prenom"] . " " $row["chauffeur_nom"];
  254.                 $rowcubage["from_plateforme"] = 0;
  255.                 $result[] = $rowcubage;
  256.             }
  257.             // On va chercher les cubages PLATEFORME associĆ©s a la livraison
  258.             $query "SELECT c.id as id, `bois_type_id`, `bois_quality_id`, `bois_size_id`, '' as marquage_id, `volume_inner`,  `volume_stere`, volume_approximatif as volumeApproximatif,
  259.             bt.name as bois_type, bq.name as bois_quality, bs.name as bois_size, bs.grume, '' as marquage_color,
  260.             '' as marquage,
  261.             0 as chantier_id, plat.name as chantier_name, '' as chantier_client, cas.name as chantier_commune
  262.             FROM `cubage_livraison_plateforme` as c 
  263.             LEFT JOIN bois_type as bt ON bt.id=c.bois_type_id
  264.             LEFT JOIN bois_quality as bq ON bq.id=c.bois_quality_id
  265.             LEFT JOIN bois_size as bs ON bs.id=c.bois_size_id
  266.             LEFT JOIN plateforme as plat ON plat.id=c.plateforme_id
  267.             LEFT JOIN plateforme_case as cas ON cas.id=c.case_id
  268.             WHERE c.livraison_id = " $row["id"];
  269.             $stmt $pdo->prepare($query);
  270.             $stmt->execute();
  271.             $datarestant $stmt->fetchAll();
  272.             foreach($datarestant as $rowcubage) {
  273.                 $rowcubage["livraison_id"] = $row["id"];
  274.                 $rowcubage["date"] = $row["date"];
  275.                 $rowcubage["bl"] = $row["bl"];
  276.                 $rowcubage["scierie_name"] = $row["scierie_name"];
  277.                 $rowcubage["chauffeur_name"] = $row["chauffeur_prenom"] . " " $row["chauffeur_nom"];
  278.                 $rowcubage["from_plateforme"] = 1;
  279.                 $result[] = $rowcubage;
  280.             }
  281.             
  282.         }
  283.         $total null;
  284.         if ($total != null) { $data = array('data' => $result'totalCount' => (float)$total); }
  285.         else { $data = array('data' => $result); }
  286.         return $data;
  287.     }
  288.     private function createQuery($param): string {
  289.         $newQuery "";
  290.         if ($param == 'and') {
  291.             return $newQuery .= " AND ";
  292.         } else if ($param == 'or') {
  293.             return $newQuery .= " OR ";
  294.         } else {
  295.             $selector $param[0];
  296.             $text $param[2];
  297.             $operator $param[1];
  298.             if ($selector == "chauffeur_name") { $selector "first_name"; }
  299.             if ($selector == "chantier_name") { $selector "ch.name"; }
  300.             $newQuery .= $selector;
  301.             if ($operator === "contains") { $operator 'LIKE'$text '%' $text '%'; }
  302.             else if ($operator === "notcontains") { $operator 'NOT LIKE'; }
  303.             else if ($operator === "startswith") { $operator 'LIKE'$text $text '%'; }
  304.             else if ($operator === "endswith") { $operator 'LIKE'$text =  '%' $text; }
  305.             else if (strpos($selector'date') || strpos($selector'Date')) { $text date_format(date_create($text), 'Y-d-m H:i:s'); }
  306.             return $newQuery .= " " $operator " '" $text "'";
  307.         }
  308.     }
  309. }