src/Controller/SuiviFacturationController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Projects;
  4. use App\Entity\ProjectFacturation;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use Symfony\Component\Translation\TranslatorInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use App\Service\ServiceTeaminfo;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\StreamedResponse;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. class SuiviFacturationController extends Controller
  18. {    
  19.     private $ServiceTeaminfo;
  20.     private $entityManager;
  21.     
  22.     
  23.     public function __construct(ServiceTeaminfo $ServiceTeaminfo,EntityManagerInterface $entityManager){
  24.         $this->ServiceTeaminfo $ServiceTeaminfo;
  25.         $this->entityManager $entityManager;
  26.     }
  27.     
  28.     
  29.     
  30.     
  31.     public function index($idRequest $requestTokenStorageInterface $tokenStorageTranslatorInterface $translator )
  32.     {        
  33.         $projets $this->getDoctrine()->getRepository(Projects::class)->findOneBy(["id"=>$id]);
  34.         $id_client $projets->getClient()->getId();
  35.         $checkClientAccess $this->ServiceTeaminfo->checkClientAccess($id_client);
  36.         
  37.         if ( $checkClientAccess == "home" )         return $this->redirectToRoute('home');
  38.         
  39.         $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["project"=>$projets]);
  40.         
  41.         return $this->render('projects/suivi_facturation/index.html.twig', [
  42.             'add_js_files' =>"suivi_facturation_js",
  43.             'projet_facturation' =>$projet_facturation,
  44.             'projets' =>$projets,
  45.             'titre_page' =>"Suivi de facturation",
  46.             'uniqid' => uniqid(),
  47.         ]);
  48.     }
  49.     public function add_edit_facturation_projet_ajax(Request $request)
  50.     {
  51.         $dateReglement $request->get("DateReglement");
  52.         $dateFacturation $request->get("DateFacturation");
  53.         
  54.         $montantRegle 0;
  55.         if( $request->get("MontantRegle") != "" )$montantRegle $request->get("MontantRegle");
  56.         
  57.         $montantFacture $request->get("MontantFacture");
  58.         $designation $request->get("Designation");
  59.         $id $request->get("id");
  60.         $id_projet $request->get("id_projet");
  61.         
  62.         if($id != null and $id != ""){
  63.             $edit true;
  64.             $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
  65.         }else{
  66.             $edit false;
  67.             $projet_facturation = new ProjectFacturation();
  68.             $projet $this->getDoctrine()->getRepository(Projects::class)->findOneBy(["id"=>$id_projet]);
  69.             $projet_facturation->setProject($projet);
  70.         }
  71.         
  72.         $projet_facturation->setDesignation($designation);
  73.         
  74.         $date_dateFacturation = new \DateTime(str_replace("/","-",$dateFacturation));
  75.         $date_dateFacturation->setTimezone(new \DateTimeZone("Europe/Paris"));
  76.         $date_dateFacturation->format("Y-m-d");
  77.         $projet_facturation->setDateFacturation($date_dateFacturation);
  78.         
  79.         $projet_facturation->setMontantFacture($montantFacture);
  80.         $projet_facturation->setMontantRegle($montantRegle);
  81.         
  82.         if($dateReglement){
  83.             $date_dateReglement = new \DateTime(str_replace("/","-",$dateReglement));
  84.             $date_dateReglement->setTimezone(new \DateTimeZone("Europe/Paris"));
  85.             $date_dateReglement->format("Y-m-d");
  86.             $projet_facturation->setDateReglement($date_dateReglement);
  87.         }
  88.         
  89.         $this->entityManager->persist($projet_facturation);
  90.         $this->entityManager->flush();
  91.         $newId $projet_facturation->getId();
  92.         
  93.         $response =  new Response(json_encode(array($newId,$edit)));
  94.         $response->headers->set('Content-Type''application/json');
  95.         return $response;
  96.     }
  97.     
  98.     public function deleted_facturation_projet_ajax(Request $request)
  99.     {
  100.         $ids $request->get("ids");
  101.         $type $request->get("type");
  102.         
  103.         $img_templat $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', array(    
  104.             'input_document_template' => "input_document_template",
  105.         ));
  106.         
  107.         $status = array('status' => "success","img_templat" => $img_templat);
  108.         $response = new JsonResponse($status);
  109.         
  110.         if($ids != ""){
  111.             if($type != "item"){
  112.                 $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$ids]);
  113.                 $projet_facturation->setDocumentPath(null);
  114.                 $this->entityManager->flush();
  115.             }else{
  116.                 foreach($ids as $id){
  117.                     $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
  118.                     $this->entityManager->remove($projet_facturation);
  119.                     $this->entityManager->flush();
  120.                 }
  121.                 
  122.                 $response = new Response("deleted");
  123.             }
  124.         }
  125.         
  126.         return $response;
  127.     }
  128.     
  129.     
  130.     
  131.     
  132.     public function upload_facturation_projet_ajax(Request $request)
  133.     {    
  134.         if($request->get("id") == "")
  135.             $id "";
  136.         else
  137.             $id $request->get("id");
  138.         
  139.         $files $request->files->get('file');
  140.         
  141.     
  142.         $num $request->get("num");
  143.         $type_file $request->get("type_file");
  144.         $uniqid $request->get("uniqid");
  145.         $data_file $request->get("data_file");
  146.         $validite $request->get("validite");
  147.         
  148.         if($files){
  149.             $originalFileName $files->getClientOriginalName();
  150.             $extension_file $this->ServiceTeaminfo->get_extension_file($originalFileName);
  151.             $type_img $this->ServiceTeaminfo->getIconFile($extension_file);
  152.         }
  153.         
  154.         if($validite == "false"){
  155.             $data $this->ServiceTeaminfo->decode_img_base64($data_file);
  156.             $extension_file $this->ServiceTeaminfo->get_extension_file($files->getClientOriginalName());
  157.             $response $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', [
  158.                 'id' =>$id,
  159.                 'path'=>$data_file,
  160.                 'upload'=>true,
  161.                 'extension'=>$type_img,
  162.                 "file_name"=>$uniqid."_".$num,
  163.                 "extension_file"=>$extension_file,
  164.                 "upload_not_save"=>"true",                
  165.             ]);
  166.         }
  167.         
  168.         if( $id != "" and $id!= null){
  169.             if($request->get("data_save") == "false" ){
  170.                 $extension_file $type_file;
  171.                 
  172.                 if( $type_file == "pdf")
  173.                     $data $this->ServiceTeaminfo->decode_PDF_base64($data_file);                    
  174.                 else    
  175.                     $data $this->ServiceTeaminfo->decode_img_base64($data_file,$extension_file);
  176.                 
  177.                 $files null;
  178.             }else{
  179.                 $extension_file null;
  180.                 $data null;
  181.                 $files $files;                
  182.             }
  183.             
  184.             $path $this->ServiceTeaminfo->upload_image($data,$files,false,false,[true,false],$extension_file);
  185.             $file_name $this->ServiceTeaminfo->get_name_file($path);
  186.             $extension_file =  $this->ServiceTeaminfo->get_extension_file($path);
  187.             
  188.             if($request->get("data_save") != "false" and $validite != "false"){
  189.                 $response $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', [
  190.                     'id' =>$id,
  191.                     'path'=>$path,
  192.                     'upload'=>true,
  193.                     'extension'=>$type_img,
  194.                     "file_name"=>$file_name,
  195.                     "extension_file"=>$extension_file,
  196.                 ]);
  197.             }else{
  198.                 $response 200;
  199.             }
  200.             
  201.             $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
  202.             $projet_facturation->setDocumentPath($path);
  203.             $this->entityManager->persist($projet_facturation);
  204.             $this->entityManager->flush();
  205.         }
  206.         
  207.         return new Response($response);
  208.     }
  209.     public function exportFacture(Request $request)
  210.     {
  211.         if($request->get("ids")!=[]){
  212.             $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["id"=>  $request->get("ids")]);
  213.         }
  214.         else{
  215.             $projet_facturation $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["project"=> $request->get("id_projet")]);
  216.           }
  217.         
  218.         $file_name uniqid("tmp/Facture_export_").".csv";
  219.         $handle fopen($file_name'w');
  220.         fwrite($handlechr(0xEF) . chr(0xBB) . chr(0xBF));
  221.         fputcsv($handle, [
  222.             "Désignation",
  223.             "Document",
  224.             "Date de facture",
  225.             "Montant facturé",
  226.             "Montant réglé  ",
  227.             "Date de réglement",
  228.         ], ';');
  229.         
  230.         foreach ($projet_facturation as $project) {
  231.             
  232.             fputcsv($handle, [
  233.                 $project->getDesignation(),
  234.                 ($project->getDocumentPath() !== null) ? $request->getSchemeAndHttpHost() . $project->getDocumentPath() : '',
  235.                 $project->getDateFacturation()->format('Y-m-d'),
  236.                 $project->getMontantFacture()." €",
  237.                 $project->getMontantRegle()." €",
  238.                 $project->getDateReglement()->format('Y-m-d'),
  239.                 
  240.             
  241.             ], ';');
  242.         }
  243.         fclose($handle);
  244.         $filePath "/".$file_name;
  245.         return new JsonResponse(["path" => $filePath]);
  246.     }
  247. }