<?php
namespace App\Controller;
use App\Entity\Projects;
use App\Entity\ProjectFacturation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Translation\TranslatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Service\ServiceTeaminfo;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Filesystem\Filesystem;
class SuiviFacturationController extends Controller
{
private $ServiceTeaminfo;
private $entityManager;
public function __construct(ServiceTeaminfo $ServiceTeaminfo,EntityManagerInterface $entityManager){
$this->ServiceTeaminfo = $ServiceTeaminfo;
$this->entityManager = $entityManager;
}
public function index($id, Request $request, TokenStorageInterface $tokenStorage, TranslatorInterface $translator )
{
$projets = $this->getDoctrine()->getRepository(Projects::class)->findOneBy(["id"=>$id]);
$id_client = $projets->getClient()->getId();
$checkClientAccess = $this->ServiceTeaminfo->checkClientAccess($id_client);
if ( $checkClientAccess == "home" ) return $this->redirectToRoute('home');
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["project"=>$projets]);
return $this->render('projects/suivi_facturation/index.html.twig', [
'add_js_files' =>"suivi_facturation_js",
'projet_facturation' =>$projet_facturation,
'projets' =>$projets,
'titre_page' =>"Suivi de facturation",
'uniqid' => uniqid(),
]);
}
public function add_edit_facturation_projet_ajax(Request $request)
{
$dateReglement = $request->get("DateReglement");
$dateFacturation = $request->get("DateFacturation");
$montantRegle = 0;
if( $request->get("MontantRegle") != "" )$montantRegle = $request->get("MontantRegle");
$montantFacture = $request->get("MontantFacture");
$designation = $request->get("Designation");
$id = $request->get("id");
$id_projet = $request->get("id_projet");
if($id != null and $id != ""){
$edit = true;
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
}else{
$edit = false;
$projet_facturation = new ProjectFacturation();
$projet = $this->getDoctrine()->getRepository(Projects::class)->findOneBy(["id"=>$id_projet]);
$projet_facturation->setProject($projet);
}
$projet_facturation->setDesignation($designation);
$date_dateFacturation = new \DateTime(str_replace("/","-",$dateFacturation));
$date_dateFacturation->setTimezone(new \DateTimeZone("Europe/Paris"));
$date_dateFacturation->format("Y-m-d");
$projet_facturation->setDateFacturation($date_dateFacturation);
$projet_facturation->setMontantFacture($montantFacture);
$projet_facturation->setMontantRegle($montantRegle);
if($dateReglement){
$date_dateReglement = new \DateTime(str_replace("/","-",$dateReglement));
$date_dateReglement->setTimezone(new \DateTimeZone("Europe/Paris"));
$date_dateReglement->format("Y-m-d");
$projet_facturation->setDateReglement($date_dateReglement);
}
$this->entityManager->persist($projet_facturation);
$this->entityManager->flush();
$newId = $projet_facturation->getId();
$response = new Response(json_encode(array($newId,$edit)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
public function deleted_facturation_projet_ajax(Request $request)
{
$ids = $request->get("ids");
$type = $request->get("type");
$img_templat = $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', array(
'input_document_template' => "input_document_template",
));
$status = array('status' => "success","img_templat" => $img_templat);
$response = new JsonResponse($status);
if($ids != ""){
if($type != "item"){
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$ids]);
$projet_facturation->setDocumentPath(null);
$this->entityManager->flush();
}else{
foreach($ids as $id){
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
$this->entityManager->remove($projet_facturation);
$this->entityManager->flush();
}
$response = new Response("deleted");
}
}
return $response;
}
public function upload_facturation_projet_ajax(Request $request)
{
if($request->get("id") == "")
$id = "";
else
$id = $request->get("id");
$files = $request->files->get('file');
$num = $request->get("num");
$type_file = $request->get("type_file");
$uniqid = $request->get("uniqid");
$data_file = $request->get("data_file");
$validite = $request->get("validite");
if($files){
$originalFileName = $files->getClientOriginalName();
$extension_file = $this->ServiceTeaminfo->get_extension_file($originalFileName);
$type_img = $this->ServiceTeaminfo->getIconFile($extension_file);
}
if($validite == "false"){
$data = $this->ServiceTeaminfo->decode_img_base64($data_file);
$extension_file = $this->ServiceTeaminfo->get_extension_file($files->getClientOriginalName());
$response = $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', [
'id' =>$id,
'path'=>$data_file,
'upload'=>true,
'extension'=>$type_img,
"file_name"=>$uniqid."_".$num,
"extension_file"=>$extension_file,
"upload_not_save"=>"true",
]);
}
if( $id != "" and $id!= null){
if($request->get("data_save") == "false" ){
$extension_file = $type_file;
if( $type_file == "pdf")
$data = $this->ServiceTeaminfo->decode_PDF_base64($data_file);
else
$data = $this->ServiceTeaminfo->decode_img_base64($data_file,$extension_file);
$files = null;
}else{
$extension_file = null;
$data = null;
$files = $files;
}
$path = $this->ServiceTeaminfo->upload_image($data,$files,false,false,[true,false],$extension_file);
$file_name = $this->ServiceTeaminfo->get_name_file($path);
$extension_file = $this->ServiceTeaminfo->get_extension_file($path);
if($request->get("data_save") != "false" and $validite != "false"){
$response = $this->renderView('projects/suivi_facturation/icone_image_template.html.twig', [
'id' =>$id,
'path'=>$path,
'upload'=>true,
'extension'=>$type_img,
"file_name"=>$file_name,
"extension_file"=>$extension_file,
]);
}else{
$response = 200;
}
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findOneBy(["id"=>$id]);
$projet_facturation->setDocumentPath($path);
$this->entityManager->persist($projet_facturation);
$this->entityManager->flush();
}
return new Response($response);
}
public function exportFacture(Request $request)
{
if($request->get("ids")!=[]){
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["id"=> $request->get("ids")]);
}
else{
$projet_facturation = $this->getDoctrine()->getRepository(ProjectFacturation::class)->findBy(["project"=> $request->get("id_projet")]);
}
$file_name = uniqid("tmp/Facture_export_").".csv";
$handle = fopen($file_name, 'w');
fwrite($handle, chr(0xEF) . chr(0xBB) . chr(0xBF));
fputcsv($handle, [
"Désignation",
"Document",
"Date de facture",
"Montant facturé",
"Montant réglé ",
"Date de réglement",
], ';');
foreach ($projet_facturation as $project) {
fputcsv($handle, [
$project->getDesignation(),
($project->getDocumentPath() !== null) ? $request->getSchemeAndHttpHost() . $project->getDocumentPath() : '',
$project->getDateFacturation()->format('Y-m-d'),
$project->getMontantFacture()." €",
$project->getMontantRegle()." €",
$project->getDateReglement()->format('Y-m-d'),
], ';');
}
fclose($handle);
$filePath = "/".$file_name;
return new JsonResponse(["path" => $filePath]);
}
}