<?php
namespace App\Controller\Tresorerie;
use Mpdf\Mpdf;
use App\Entity\Cab;
use App\Entity\Det;
use App\Entity\Periode;
use App\Entity\PStatut;
use App\Entity\PDossier;
use App\Entity\Tbulletin;
use App\Entity\PBordereau;
use App\Entity\NaturePiece;
use App\Entity\CabBordereau;
use App\Entity\UaTechniqueDet;
use App\Controller\ApiController;
use App\Entity\JournalBulletinLg;
use App\Entity\JournalPaieDossier;
use App\Service\CalculPaieService;
use App\Entity\SyntheseRemunarationCab;
use App\Entity\SyntheseRemunarationDet;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/tresorerie/bordereau')]
class BordereauController extends AbstractController
{
private $em;
private $client;
private $api;
private $calculPaieService;
private $requestStack;
public function __construct(ManagerRegistry $doctrine, RequestStack $requestStack, CalculPaieService $calculPaieService, ApiController $api, HttpClientInterface $client)
{
$this->em = $doctrine->getManager();
$this->api = $api;
$this->client = $client;
$this->calculPaieService = $calculPaieService;
$this->requestStack = $requestStack;
}
#[Route('/', name: 'app_tresorerie_bordereau')]
public function index(Request $request): Response
{
// dd('amine');
$operations = $this->api->check($this->getUser(), 'app_tresorerie_bordereau', $this->em, $request);
if (!is_array($operations)) {
return $this->redirectToRoute('app_site');
} elseif (count($operations) == 0) {
return $this->render('includes/404.html.twig');
}
return $this->render('tresorerie/bordereau/index.html.twig', [
'operations' => $operations,
'periode' => $this->calculPaieService->getPeriode()
]);
}
#[Route('/app_tresorerie_bordereau_list/{periode}', name: 'app_tresorerie_bordereau_list', options: ['expose' => true])]
public function app_tresorerie_bordereau_list(Request $request, $periode): Response
{
$date = new \DateTime($periode);
$periode = $this->calculPaieService->getPeriode($date->format('mY'));
$draw = $request->query->get('draw');
$start = $request->query->get('start') ?? 0;
$length = $request->query->get('length') ?? 10;
$dossier = $request->getSession()->get('dossier');
$search = $request->query->all('search')["value"];
$orderColumnIndex = $request->query->all('order')[0]['column'];
$orderColumn = $request->query->all("columns")[$orderColumnIndex]['name'];
$orderDir = $request->query->all('order')[0]['dir'] ?? 'asc';
$queryBuilder = $this->em->createQueryBuilder()
->select('b.id, d.designation as dossier_designation,paiementEntity.designation as paiement, deviseEntity.designation as devise, p.abreviation as periode, b.code, b.created, b.type, natureContract.designation, s.designation as statut , s.icon')
->from(PBordereau::class, 'b')
->innerJoin('b.statut', 's')
->innerJoin('b.periode', 'p')
->innerJoin('b.dossier', 'd')
->innerJoin('b.devise', 'deviseEntity')
->innerJoin('b.paiement', 'paiementEntity')
->leftJoin('b.natureContract', 'natureContract')
->where('b.active = 1')
->andWhere('b.periode = :periode')
->setParameter('periode', $periode);
if (!empty($search)) {
$queryBuilder->andWhere('(paiementEntity.designation LIKE :search OR deviseEntity.designation LIKE :search OR b.id LIKE :search OR p.abreviation LIKE :search OR d.designation LIKE :search OR b.code LIKE :search OR b.type LIKE :search OR natureContract.designation LIKE :search OR s.designation LIKE :search)')
->setParameter('search', "%$search%");
}
if (!empty($orderColumn)) {
$queryBuilder->orderBy("$orderColumn", $orderDir);
}
if ($dossier->getId() != 47) { // DRH
$queryBuilder->andWhere('d.id = :dossier')
->setParameter('dossier', $dossier);
}
$filteredRecords = count($queryBuilder->getQuery()->getResult());
// Paginate results
$queryBuilder->setFirstResult($start)
->setMaxResults($length)
->orderBy('b.id', 'DESC');
$results = $queryBuilder->getQuery()->getResult();
foreach ($results as $key => $b) {
// dd('amine');
// if($contract['id'] == 1301) {
// dd($this->em->getRepository(PArretTravailLg::class)->getNombreJoursArret($contract['id'], $periode));
// }
$bordereau = $this->em->getRepository(PBordereau::class)->find($b['id']);
$results[$key]['DT_RowId'] = $b['id'];
$results[$key]['montant'] = $this->api->getBordereauPrixTotal($bordereau);
}
$totalRecords = $this->em->createQueryBuilder()
->select('COUNT(b.id)')
->from(PBordereau::class, 'b')
->innerJoin('b.dossier', 'd')
->where('b.active = 1')
->andWhere('b.periode = :periode')
->setParameter('periode', $periode);
if ($dossier->getId() != 47) { // DRH
$totalRecords->andWhere('d.id = :dossier')
->setParameter('dossier', $dossier);
}
$totalRecords = $totalRecords->getQuery()
->getSingleScalarResult();
return new JsonResponse([
'draw' => $draw,
'recordsTotal' => $totalRecords,
'recordsFiltered' => $filteredRecords,
'data' => $results,
]);
}
#[Route('/app_tresorerie_bordereau_valider', name: 'app_tresorerie_bordereau_valider', options: ['expose' => true])]
public function app_tresorerie_bordereau_valider(Request $request): Response
{
$bordereauIds = json_decode($request->get('bordereauIds'));
// dd($bordereauIds);
foreach ($bordereauIds as $key => $bordereauId) {
$bordereau = $this->em->getRepository(PBordereau::class)->find($bordereauId);
if ($bordereau->getStatut()->getId() == 2) { // pret
if ($bordereau->getType() == 'operationregularisation') {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
} else {
$charges = $this->em->getRepository(JournalBulletinLg::class)->getSumMontant($bordereau);
if ($charges) {
if (number_format($charges['montant'], 2) != 0) {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(5)
);
} else {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
}
}
}
}
}
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_valider_periode', name: 'app_tresorerie_bordereau_valider_periode', options: ['expose' => true])]
public function app_tresorerie_bordereau_valider_periode(Request $request): Response
{
$periode = $request->get('periode');
$bordereauxProbeleme = $this->em->getRepository(PBordereau::class)->findBy(['periode' => $periode, 'active' => 1, 'statut' => $this->em->getRepository(PStatut::class)->find(5)]);
if(count($bordereauxProbeleme) > 0) {
return new JsonResponse('Vous aves des bordereaux à probleme !', 500, [], JSON_UNESCAPED_UNICODE);
}
$date = new \DateTime($periode);
$periode = $this->calculPaieService->getPeriode($date->format('mY'));
if($periode->IsValider()) {
return new JsonResponse('Periode est valider !', 500, [], JSON_UNESCAPED_UNICODE);
}
$statut = $this->em->getRepository(PStatut::class)->find(2); // pret
$bordereaux = $this->em->getRepository(PBordereau::class)->findBy(['periode' => $periode, 'active' => true, 'statut' => $statut]);
foreach ($bordereaux as $key => $bordereau) {
if ($bordereau->getType() == 'operationregularisation') {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
} else {
$charges = $this->em->getRepository(JournalBulletinLg::class)->getSumMontant($bordereau);
if($charges) {
if(number_format($charges['montant'], 2) != 0) {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(5)
);
} else {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
}
}
}
}
$periode->setIsValider(true);
$this->generateSynthese($periode);
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_periode_check/{periode}', name: 'app_tresorerie_bordereau_periode_check', options: ['expose' => true])]
public function app_tresorerie_bordereau_periode_check($periode): Response
{
$date = new \DateTime($periode);
$periode = $this->calculPaieService->getPeriode($date->format('mY'));
return new JsonResponse(['valider' => $periode->isValider(),'integrer' => $periode->isIntegrer()]);
}
#[Route('/app_tresorerie_bordereau_journal', name: 'app_tresorerie_bordereau_journal', options: ['expose' => true])]
public function app_tresorerie_bordereau_journal(Request $request): Response
{
$bordereauIds = json_decode($request->get('bordereauIds'));
$array = [];
foreach ($bordereauIds as $key => $bordereauId) {
$bordereau = $this->em->getRepository(PBordereau::class)->find($bordereauId);
if ($bordereau->getStatut()->getId() == 1) { // en cours
array_push($array, $bordereau->getId());
}
}
$request->request->add(['bordoreauIds' => json_encode($array)]);
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_devalider', name: 'app_tresorerie_bordereau_devalider', options: ['expose' => true])]
public function app_tresorerie_bordereau_devalider(Request $request): Response
{
$bordereauIds = json_decode($request->get('bordereauIds'));
// dd($bordereauIds);
foreach ($bordereauIds as $key => $bordereauId) {
$bordereau = $this->em->getRepository(PBordereau::class)->find($bordereauId);
if ($bordereau->getStatut()->getId() == 3) { // valider
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(2)
);
}
}
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_desactiver', name: 'app_tresorerie_bordereau_desactiver', options: ['expose' => true])]
public function app_tresorerie_bordereau_desactiver(Request $request): Response
{
$motif = $request->get('motif');
$bordereauIds = json_decode($request->get('bordereauIdsDesactiver'));
// dd($bordereauIds);
foreach ($bordereauIds as $key => $bordereauId) {
$bordereau = $this->em->getRepository(PBordereau::class)->find($bordereauId);
if (in_array($bordereau->getStatut()->getId(), [1, 2])) {
foreach ($bordereau->getActiveBulletins() as $key => $bulletin) {
foreach ($bulletin->getBulletinLgs() as $key => $bulletinLg) {
$bulletinLg->setActive(false);
}
foreach ($bulletin->getJournalBulletinLgs() as $key => $JournalBulletinLg) {
$JournalBulletinLg->setActive(false);
}
$bulletin->setMotifAnnulation($motif);
$bulletin->setUserAnnulation($this->getUser());
$bulletin->setActive(false);
}
$bordereau->setMotifAnnulation($motif);
$bordereau->setUserAnnulation($this->getUser());
$bordereau->setActive(false);
}
}
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_verification', name: 'app_tresorerie_bordereau_verification', options: ['expose' => true])]
public function app_tresorerie_bordereau_verification(Request $request): Response
{
$bordereauIds = json_decode($request->get('uniqueBordereaux'));
foreach ($bordereauIds as $key => $bordereauId) {
$bordereau = $this->em->getRepository(PBordereau::class)->find($bordereauId);
$charge = $this->em->getRepository(JournalBulletinLg::class)->getSumMontant($bordereau);
if (number_format($charge, 2) == 0) {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(2)
);
} else {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(5)
);
}
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(2)
);
}
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_integre/{isExtraction}', name: 'app_tresorerie_bordereau_integre', options: ['expose' => true])]
public function app_tresorerie_bordereau_integre(Request $request, $isExtraction): Response
{
$checkIfDuplicatedRib = "select count(*) , contact_id_id from lrib where active = 1
group by contact_id_id
having count(*) > 1";
$stmt = $this->em->getConnection()->prepare($checkIfDuplicatedRib);
$newstmt = $stmt->executeQuery();
$result = $newstmt->fetchAll();
if (count($result) > 0) {
return new JsonResponse('Vous avez des RIBS en double, contacter l\'administrateur!', 500);
}
$bordereaux = $this->em->getRepository(PBordereau::class)->findBy([
'statut' => $this->em->getRepository(PStatut::class)->find(3),
'active' => true,
'type' => ['paie', 'honoraire', 'indeminite', 'rémunérationavance', 'stc' , 'paiementautrui']
]);
$bordereauxRegularisation = $this->em->getRepository(PBordereau::class)->findBy([
'statut' => $this->em->getRepository(PStatut::class)->find(3),
'active' => true,
'type' => ['operationregularisation']
]);
// $bordereauxStcs = $this->em->getRepository(PBordereau::class)->findBy([
// 'statut' => $this->em->getRepository(PStatut::class)->find(3),
// 'active' => true,
// 'type' => ['stc']
// ]);
if (count($bordereaux) == 0 && count($bordereauxRegularisation) == 0) {
return new JsonResponse('Rien à intégrer', 500);
}
if (count($result) > 0) {
return new JsonResponse('Vous avez des ribs en double, veuillez contacter l\'administrateur !', 500);
}
$groupements = $this->em->getRepository(PDossier::class)->getGroupement();
$dossierRh = $this->em->getRepository(PDossier::class)->find(47);
// dd($bordereauxIndiminites);
$cabs = $this->generateCabs($groupements, $bordereaux, $bordereauxRegularisation, $dossierRh);
// dd($cabs);
if (count($cabs) == 0) {
return new JsonResponse('Rien à intégrer', 500);
}
if ($isExtraction == 1) {
$cabs = $this->toArray($cabs);
return new JsonResponse($cabs);
}
// check info personnels
foreach ($cabs as $key => $cab) {
if ($cab['devise'] != "MAD") {
foreach ($cab['details'] as $key => $detail) {
if (array_key_exists('personnels', $detail)) {
foreach ($detail['personnels'] as $key => $personnel) {
if (
$personnel['swift'] == "" or is_null($personnel['swift']) or
$personnel['banque'] == "" or is_null($personnel['banque']) or
$personnel['adresse'] == "" or is_null($personnel['adresse']) or
$personnel['rib'] == "" or is_null($personnel['rib'])
) {
return new JsonResponse('Vous avez un manque des donnés banquaire dans le contrat (' . $personnel['observation'] . ')', 500);
}
}
}
}
} else {
foreach ($cab['details'] as $key => $detail) {
if ($cab['paiement'] == "Virement" and array_key_exists('personnels', $detail)) {
foreach ($detail['personnels'] as $key => $personnel) {
if (
$personnel['rib'] == "" or is_null($personnel['rib'])
) {
return new JsonResponse('Vous avez un manque de RIB dans le contrat (' . $personnel['observation'] . ')', 500);
}
}
}
}
}
}
// end check
$this->insertCabsAndMakeChanges($cabs, $bordereaux, $bordereauxRegularisation);
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
#[Route('/app_tresorerie_bordereau_integre_periode/{isExtraction}/{periode}', name: 'app_tresorerie_bordereau_integre_periode', options: ['expose' => true])]
public function app_tresorerie_bordereau_integre_periode(Request $request, $isExtraction, $periode): Response
{
$periode = $request->get('periode');
$date = new \DateTime($periode);
$periode = $this->calculPaieService->getPeriode($date->format('mY'));
if($periode->isIntegrer()) {
return new JsonResponse('Periode est déja integrer!', 500);
}
$checkIfDuplicatedRib = "select count(*) , contact_id_id from lrib where active = 1
group by contact_id_id
having count(*) > 1";
$stmt = $this->em->getConnection()->prepare($checkIfDuplicatedRib);
$newstmt = $stmt->executeQuery();
$result = $newstmt->fetchAll();
if (count($result) > 0) {
return new JsonResponse('Vous avez des RIBS en double, contacter l\'administrateur!', 500);
}
$bordereaux = $this->em->getRepository(PBordereau::class)->findBy([
'statut' => $this->em->getRepository(PStatut::class)->find(3),
'active' => true,
'periode' => $periode,
'type' => ['paie', 'honoraire', 'indeminite', 'rémunérationavance', 'stc', 'paiementautrui']
]);
$bordereauxRegularisation = $this->em->getRepository(PBordereau::class)->findBy([
'statut' => $this->em->getRepository(PStatut::class)->find(3),
'active' => true,
'periode' => $periode,
'type' => ['operationregularisation']
]);
if (count($bordereaux) == 0 && count($bordereauxRegularisation) == 0 ) {
return new JsonResponse('Rien à intégrer', 500);
}
if (count($result) > 0) {
return new JsonResponse('Vous avez des ribs en double, veuillez contacter l\'administrateur !', 500);
}
$groupements = $this->em->getRepository(PDossier::class)->getGroupement();
$dossierRh = $this->em->getRepository(PDossier::class)->find(47);
// dd($bordereauxIndiminites);
$cabs = $this->generateCabs($groupements, $bordereaux, $bordereauxRegularisation, $dossierRh);
if (count($cabs) == 0) {
return new JsonResponse('Rien à intégrer', 500);
}
if ($isExtraction == 1) {
$cabs = $this->toArray($cabs);
return new JsonResponse($cabs);
}
// check info personnels
foreach ($cabs as $key => $cab) {
if ($cab['devise'] != "MAD") {
foreach ($cab['details'] as $key => $detail) {
if (array_key_exists('personnels', $detail)) {
foreach ($detail['personnels'] as $key => $personnel) {
if (
$personnel['swift'] == "" or is_null($personnel['swift']) or
$personnel['banque'] == "" or is_null($personnel['banque']) or
$personnel['adresse'] == "" or is_null($personnel['adresse']) or
$personnel['rib'] == "" or is_null($personnel['rib'])
) {
return new JsonResponse('Vous avez un manque des donnés banquaire dans le contrat (' . $personnel['observation'] . ')', 500);
}
}
}
}
} else {
foreach ($cab['details'] as $key => $detail) {
if ($cab['paiement'] == "Virement" and array_key_exists('personnels', $detail)) {
foreach ($detail['personnels'] as $key => $personnel) {
if (
$personnel['rib'] == "" or is_null($personnel['rib'])
) {
return new JsonResponse('Vous avez un manque de RIB dans le contrat (' . $personnel['observation'] . ')', 500);
}
}
}
}
}
}
// end check
$this->insertCabsAndMakeChanges($cabs, $bordereaux, $bordereauxRegularisation );
$periode->setIsIntegrer(true);
$this->em->flush();
return new JsonResponse("Bien enregistrer");
}
public function generateCabs($groupements, $bordereaux, $bordereauxRegularisation, $dossierRh) {
$cabs = [];
// dd($bordereauxRegularisation);
foreach ($groupements as $key => $groupement) {
$journals = $this->em->getRepository(JournalBulletinLg::class)->getCab($bordereaux, $groupement['groupement'], $dossierRh);
foreach ($journals as $key => $journal) {
array_push($cabs, $journal);
}
if (count($bordereauxRegularisation) > 0) {
$journalsOperation = $this->em->getRepository(JournalBulletinLg::class)->getCabOperation($bordereauxRegularisation, $groupement['groupement'], $dossierRh);
foreach ($journalsOperation as $key => $journal) {
array_push($cabs, $journal);
}
}
if ($groupement['groupement'] == 'FCZ') {
$journalRegularisations = $this->em->getRepository(JournalBulletinLg::class)->getCabRegularisations($bordereaux, $groupement['groupement'], $dossierRh);
foreach ($journalRegularisations as $key => $journal) {
array_push($cabs, $journal);
}
}
}
return $cabs;
}
public function insertCabsAndMakeChanges($cabs, $bordereaux, $bordereauxRegularisation) {
$date = new \DateTime('now');
$hashKey = $this->generateRandomPassword();
$date = $date->format('d') . '_' . $date->format('m') . '_' . $date->format('Y') . '_' . $date->format('H') . '_' . $date->format('i') . '_' . $date->format('s') . '(' . $hashKey . ')';
foreach ($cabs as $key => $cab) {
$uniqueId = $cab['groupement'] . '_' . $cab['observation_operation'] . '_' . $date;
$cabEntity = $this->insertIntoCabs($cab, $uniqueId);
foreach ($bordereaux as $key => $bordereau) {
if ((($bordereau->getDossier()->getGroupement() == $cab['groupement'] && !array_key_exists('type_piece', $cab)) or ($bordereau->getDossier()->getDossierUgouvId() == $cab['id_site'] && array_key_exists('type_piece', $cab) && $cab['type_piece'] == 'regul')) &&
$bordereau->getPeriode()->getId() == $cab['periode_id'] &&
$bordereau->getPaiement()->getDesignation() == $cab['paiement'] &&
$bordereau->getDevise()->getDesignation() == $cab['devise'] &&
($bordereau->getType() == $cab['type_bordereau'] or $bordereau->getType() . '_regularisation' == $cab['type_bordereau']) &&
$bordereau->getNatureContract()->getId() == $cab['natureContract_id']
) {
$cabbordereau = new CabBordereau();
$cabbordereau->setCab($cabEntity);
$cabbordereau->setBordereau($bordereau);
$this->em->persist($cabbordereau);
}
}
foreach ($bordereauxRegularisation as $key => $bordereau) {
// if ((($bordereau->getDossier()->getGroupement() == $cab['groupement'] && !array_key_exists('type_piece', $cab)) or ($bordereau->getDossier()->getDossierUgouvId() == $cab['id_site'] && array_key_exists('type_piece', $cab) && $cab['type_piece'] == 'regul')) &&
// $bordereau->getPeriode()->getId() == $cab['periode_id'] &&
// $bordereau->getPaiement()->getDesignation() == $cab['paiement'] &&
// $bordereau->getDevise()->getDesignation() == $cab['devise'] &&
// ($bordereau->getType() == $cab['type_bordereau'] or $bordereau->getType() . '_regularisation' == $cab['type_bordereau']) &&
// $bordereau->getNatureContract()->getId() == $cab['natureContract_id']
// ) {
// $cabbordereau = new CabBordereau();
// $cabbordereau->setCab($cabEntity);
// $cabbordereau->setBordereau($bordereau);
// $this->em->persist($cabbordereau);
// }
if($bordereau->getId() == $cab['bordereau_id']) {
$cabbordereau = new CabBordereau();
$cabbordereau->setCab($cabEntity);
$cabbordereau->setBordereau($bordereau);
$this->em->persist($cabbordereau);
}
}
}
foreach ($bordereaux as $key => $bordereau) {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(4)
);
$bordereau->setDateIntegration(new \DateTime());
$bordereau->setUserIntegration($this->getUser());
$bordereau->setUniqueId($date);
}
foreach ($bordereauxRegularisation as $key => $bordereau) {
$bordereau->setStatut(
$this->em->getRepository(PStatut::class)->find(4)
);
$bordereau->setDateIntegration(new \DateTime());
$bordereau->setUserIntegration($this->getUser());
$bordereau->setUniqueId($date);
}
}
public function insertIntoCabs($data, $uniqueId)
{
$montantInitial = 0;
$naturePiece = $this->em->getRepository(NaturePiece::class)->find($data['naturePiece']);
// $cab = $this->em->getRepository(Cab::class)->findOneBy(['naturePiece' => $naturePiece, 'designiationPiece' => 'FACTURE '.$data['type_bordereau'], 'descriptionPiece' => $data['type_bordereau'], 'autreInformation' => $data['observation_cab'], 'idSite' => $data['id_site']]);
// if($cab) {
// foreach ($cab->getDets() as $key => $det) {
// foreach ($det->getDets() as $key => $detTechnique) {
// $this->em->remove($detTechnique);
// }
// $this->em->remove($det);
// }
// } else {
$cab = new Cab();
$cab->setTypePiece(4);
$cab->setAutreInformation($data['observation_cab']);
if (array_key_exists('type_piece', $data) && $data['type_piece'] == 'regul') {
$cab->setTypePaiement('virement');
} else {
$cab->setTypePaiement(strtolower($data['paiement']));
}
$cab->setIdSite($data['id_site']);
$cab->setDescriptionSite($data['id_site']);
$cab->setDescriptionPiece($data['type_bordereau']);
if (array_key_exists('type_piece', $data) && $data['type_piece'] == 'regul') {
$cab->setDevise('mad');
} else {
$cab->setDevise($data['devise']);
}
$cab->setDesigniationPiece('FACTURE ' . $data['type_bordereau']);
// $cab->setDescriptionGrptOps($data['type_bordereau']);
$cab->setDateOperation(new \DateTime('now'));
$cab->setDateCreationPiece(new \DateTime('now'));
$cab->setDateInterf(new \DateTime('now'));
$cab->setUniqueId($uniqueId);
$cab->setNaturePiece($naturePiece);
if ($data['partenaireId'] == null) {
$cab->setIdTierPiece($data['partenaireNatureContractId']);
} else {
$cab->setIdTierPiece($data['partenaireId']);
}
$this->em->persist($cab);
// }
foreach ($data['details'] as $key => $detail) {
$det = new Det();
$det->setCab($cab);
$det->setRassembleurId($detail['article']);
$det->setDescriptionRassembleur($detail['codeComptable'] . '_' . $detail['rubrique_designation'] . ' : ' . $detail['observation_operation'] . '(' . $detail['rubrique_code'] . ')');
$det->setMontantInitialRassembleur($detail['montant']);
$det->setMontantMad($detail['montant_mad']);
$det->setQuantiteRassembleur($detail['qte']);
$det->setCompteComptable($detail['codeComptable']);
if (array_key_exists('sens', $detail)) {
$det->setSens($detail['sens']);
}
if (array_key_exists('qte', $detail) && $detail['qte'] == "1") {
$montantInitial += (float)$detail['montant'];
}
$this->em->persist($det);
if (array_key_exists('personnels', $detail)) {
foreach ($detail['personnels'] as $key => $personnel) {
$detTechnique = new UaTechniqueDet();
$detTechnique->setDet($det);
$detTechnique->setQuantite(1);
$detTechnique->setSwift($personnel['swift']);
$detTechnique->setBanque($personnel['banque']);
$detTechnique->setAdresse($personnel['adresse']);
$detTechnique->setPrixUnitaire($personnel['montant']);
$detTechnique->setObservation($personnel['observation']);
if ($data['paiement'] == "Virement") {
$detTechnique->setArticle($personnel['nom'] . ' ' . $personnel['prenom'] . '/' . $personnel['rib']);
} else {
$detTechnique->setArticle($personnel['nom'] . ' ' . $personnel['prenom']);
}
$this->em->persist($detTechnique);
}
}
}
$cab->setMontantInitial(round($montantInitial, 2));
return $cab;
}
public function toArray($cabs)
{
$array = [];
foreach ($cabs as $key => $data) {
foreach ($data['details'] as $key => $detail) {
if (array_key_exists('personnels', $detail) && count($detail['personnels']) > 0) {
foreach ($detail['personnels'] as $key => $personnel) {
array_push($array, [
'observation_cab' => $data['observation_cab'],
'observation_det' => $detail['observation_det'],
'id_site' => $data['id_site'],
'type_bordereau' => $data['type_bordereau'],
'devise' => $data['devise'],
'type_bordereau' => $data['type_bordereau'],
'tier_piece' => $data['partenaireId'] == null ? $data['partenaireNatureContractId'] : $data['partenaireId'],
'article' => $detail['article'],
'desicription_rassembleur' => $detail['codeComptable'] . '_' . $detail['rubrique_designation'] . ' : ' . $detail['observation_operation'] . '(' . $detail['rubrique_code'] . ')',
'montant_mad' => $detail['montant_mad'],
'qte' => $detail['qte'],
'codeComptable' => $detail['codeComptable'],
'swift' => $personnel['swift'],
'banque' => $personnel['banque'],
'adresse' => $personnel['adresse'],
'montant' => $personnel['montant'],
'observation' => $personnel['observation'],
'article' => $personnel['nom'] . ' ' . $personnel['prenom'] . '/' . $personnel['rib']
]);
}
} else {
array_push($array, [
'observation_cab' => $data['observation_cab'],
'observation_det' => $detail['observation_det'],
'id_site' => $data['id_site'],
'type_bordereau' => $data['type_bordereau'],
'devise' => $data['devise'],
'type_bordereau' => $data['type_bordereau'],
'tier_piece' => $data['partenaireId'] == null ? $data['partenaireNatureContractId'] : $data['partenaireId'],
'article' => $detail['article'],
'desicription_rassembleur' => $detail['codeComptable'] . '_' . $detail['rubrique_designation'] . ' : ' . $detail['observation_operation'] . '(' . $detail['rubrique_code'] . ')',
'montant_mad' => $detail['montant_mad'],
'qte' => $detail['qte'],
'codeComptable' => $detail['codeComptable'],
'swift' => '',
'banque' => '',
'adresse' => '',
'montant' => '',
'observation' => '',
'article' => ''
]);
}
}
}
return $array;
}
#[Route('/app_tresorerie_bordereau_imprimer/{bordereau}', name: 'app_tresorerie_bordereau_imprimer', options: ['expose' => true])]
public function app_tresorerie_bordereau_imprimer(PBordereau $bordereau): Response
{
$html = $this->render("tresorerie/bordereau/pdf/bordereau.html.twig", [
'bordereau' => $bordereau,
'total' => $this->api->getBordereauPrixTotal($bordereau)
])->getContent();
$mpdf = new Mpdf([
'mode' => 'utf-8',
'margin_left' => '5',
'margin_right' => '5',
]);
$mpdf->SetTitle($bordereau->getType());
$mpdf->WriteHTML($html);
$mpdf->Output($bordereau->getType() . "_" . $bordereau->getCode() . ".pdf", "I");
}
#[Route('/app_tresorerie_bordereau_detail/{code}', name: 'app_tresorerie_bordereau_detail', options: ['expose' => true])]
public function app_tresorerie_bordereau_detail($code): Response
{
$bordereau = $this->em->getRepository(PBordereau::class)->findOneBy(['code' => $code]);
$html = $this->renderView('tresorerie/bordereau/includes/bordereau_detail.html.twig', ['bordereau' => $bordereau]);
return new JsonResponse($html);
}
public function montantBrute($deniedRubrique, Tbulletin $bulletin)
{
// dd($bulletinLg);
$bulletinLg = $this->em->createQueryBuilder('b')
->select('CASE WHEN devise.id = 1 THEN bulletinLgs.montant ELSE bulletinLgs.montantDevise END as montant')
->from(Tbulletin::class, 'b')
->innerJoin('b.bulletinLgs', 'bulletinLgs')
->innerJoin('bulletinLgs.rubrique', 'rubrique')
->innerJoin('b.bordereau', 'bordereau')
->innerJoin('bordereau.devise', 'devise')
->andWhere('rubrique.id NOT IN (:deniedRubrique)') // 5 is net a paye
->andWhere('b.id = :bulletin')
->andWhere('bulletinLgs.active = 1')
->setParameter('bulletin', $bulletin)
->setParameter('deniedRubrique', $deniedRubrique)
->getQuery()
->getOneOrNullResult();
if ($bulletinLg) {
return new Response($bulletinLg['montant']);
}
return new Response(0);
}
public function montant($rubrique, Tbulletin $bulletin)
{
// dd($rubrique);
$bulletinLg = $this->em->createQueryBuilder('b')
->select('CASE WHEN devise.id = 1 THEN bulletinLgs.montant ELSE bulletinLgs.montantDevise END as montant')
->from(Tbulletin::class, 'b')
->innerJoin('b.bulletinLgs', 'bulletinLgs')
->innerJoin('bulletinLgs.rubrique', 'rubrique')
->innerJoin('b.bordereau', 'bordereau')
->innerJoin('bordereau.devise', 'devise')
->andWhere('rubrique.id IN (:rubrique)') // 5 is net a paye
->andWhere('b.id = :bulletin')
->andWhere('bulletinLgs.active = 1')
->setParameter('bulletin', $bulletin->getId())
->setParameter('rubrique', $rubrique)
->getQuery()
->getOneOrNullResult();
// dd($bulletin);
if ($bulletinLg) {
return new Response($bulletinLg['montant']);
}
return new Response(0);
}
public function montantOperationRegularisation(Tbulletin $bulletin)
{
// dd($rubrique);
$bulletinLg = $this->em->createQueryBuilder('b')
->select('b.id , sum(bulletinLgs.montant) as montant')
->from(Tbulletin::class, 'b')
->innerJoin('b.bulletinLgs', 'bulletinLgs')
->innerJoin('bulletinLgs.rubrique', 'rubrique')
->innerJoin('b.bordereau', 'bordereau')
->andWhere('b.id = :bulletin')
->andWhere('bulletinLgs.active = 1')
->setParameter('bulletin', $bulletin->getId())
->groupBy('b.id')
->getQuery()
->getOneOrNullResult();
if ($bulletinLg) {
return new Response($bulletinLg['montant']);
}
return new Response(0);
}
public function montantMad($rubrique, Tbulletin $bulletin)
{
// dd($rubrique);
$bulletinLg = $this->em->createQueryBuilder('b')
->select('bulletinLgs.montant')
->from(Tbulletin::class, 'b')
->innerJoin('b.bulletinLgs', 'bulletinLgs')
->innerJoin('bulletinLgs.rubrique', 'rubrique')
->innerJoin('b.bordereau', 'bordereau')
->innerJoin('bordereau.devise', 'devise')
->andWhere('rubrique.id IN (:rubrique)') // 5 is net a paye
->andWhere('b.id = :bulletin')
->andWhere('bulletinLgs.active = 1')
->setParameter('bulletin', $bulletin->getId())
->setParameter('rubrique', $rubrique)
->getQuery()
->getOneOrNullResult();
// dd($bulletin);
if ($bulletinLg) {
return new Response($bulletinLg['montant']);
}
return new Response(0);
}
function generateRandomPassword($length = 8)
{
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = '';
// Generate a random password using the specified length
for ($i = 0; $i < $length; $i++) {
$password .= $characters[random_int(0, strlen($characters) - 1)];
}
return $password;
}
#[Route('/app_tresorerie_bordereau_synthese/{periodeEntity}', name: 'app_tresorerie_bordereau_synthese', options: ['expose' => true])]
function generateSynthese(Periode $periodeEntity)
{
$periode = $periodeEntity->getId();
$request = "SELECT pdossier.groupement, tbulletin.id as bulletin, 'SalaireBruteImposable' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.imposable = 1 and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'SalaireBrute' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and ((prubrique.id in (1, 4, 6, 7,8,9) or prubrique.fixe = 1) or prubrique.indeminite = 1 or prubrique.honoraire = 1) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'TotalPrimesNonImposable' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and (prubrique.type = 'prime') and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'TotalPrelevement' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and (prubrique.type = 'prelevement') and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'CnssPs' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.id in (50, 47, 53) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'CimrPs' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.id in (48) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'CnssPp' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.id in (51, 52, 54, 55, 56, 58) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'CimrPp' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.id in (57) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'Ir' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and prubrique.id in (43) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'NetFinal' as element, sum(tbulletin_lg.montant) as montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and (prubrique.id in (5, 68)) and pbordereau.active = 1
group by tbulletin.code
UNION
SELECT pdossier.groupement, tbulletin.id as bulletin, 'NetTheorique' as element, contract_net_theorique.montant
FROM tbulletin_lg
INNER JOIN tbulletin on tbulletin.id = tbulletin_lg.bulletin_id
INNER JOIN pbordereau on pbordereau.id = tbulletin.bordereau_id
INNER JOIN contract_net_theorique on contract_net_theorique.bulletin_id = tbulletin.id
INNER JOIN prubrique on prubrique.id = tbulletin_lg.rubrique_id
INNER JOIN pdossier on tbulletin.dossier_id = pdossier.id
where tbulletin.periode_id = $periode and tbulletin.active = 1 and tbulletin_lg.active = 1 and (prubrique.id in (5, 68)) and pbordereau.active = 1
group by tbulletin.code
"
;
$connection = $this->em->getConnection();
$stmt = $connection->prepare($request);
$newstmt = $stmt->executeQuery();
$result = $newstmt->fetchAll();
usort($result, function($a, $b) {
return $a['bulletin'] - $b['bulletin'];
});
// dd($result);
$cabs = $this->geenrateCabsSynthese($periodeEntity);
// dd($cabs['FCZ'], $result[0]);
$id = 0;
// dd('amine');
foreach ($result as $key => $row) {
if($row['bulletin'] != $id) {
$id = $row['bulletin'];
$bulletin = $this->em->getRepository(Tbulletin::class)->find($row['bulletin']);
$syntheseDet = $this->em->getRepository(SyntheseRemunarationDet::class)->findOneBy(['bulletin' => $bulletin]);
if(!$syntheseDet) {
$syntheseDet = new SyntheseRemunarationDet();
$syntheseDet->setBulletin($bulletin);
$syntheseDet->setCab($cabs[$row['groupement']]);
$this->em->persist($syntheseDet);
}
}
$setter = 'set'.$row['element'];
$getter = 'get'.$row['element'];
$syntheseDet->$setter($row['montant']);
$cabs[$row['groupement']]->$setter($cabs[$row['groupement']]->$getter() + $row['montant']);
}
$this->em->flush();
$request = $this->requestStack->getCurrentRequest();
if ($request->isMethod('GET')) {
return new JsonResponse('ok');
} else {
return 'ok';
}
}
function geenrateCabsSynthese($periodeEntity)
{
$syntheseCabFcz = $this->em->getRepository(SyntheseRemunarationCab::class)->findOneBy(['periode' => $periodeEntity, 'groupement' => 'FCZ']);
if($syntheseCabFcz) {
$syntheseCabFcz->setSalaireBrute(0);
$syntheseCabFcz->setSalaireBruteImposable(0);
$syntheseCabFcz->setCnssPp(0);
$syntheseCabFcz->setCnssPs(0);
$syntheseCabFcz->setCimrPp(0);
$syntheseCabFcz->setCimrPs(0);
$syntheseCabFcz->setIr(0);
$syntheseCabFcz->setTotalPrelevement(0);
$syntheseCabFcz->setTotalPrimesNonImposable(0);
$syntheseCabFcz->setNetFinal(0);
$syntheseCabFcz->setNetTheorique(0);
} else {
$syntheseCabFcz = new SyntheseRemunarationCab();
$syntheseCabFcz->setPeriode($periodeEntity);
$syntheseCabFcz->setGroupement('FCZ');
$this->em->persist($syntheseCabFcz);
}
$syntheseCabRga = $this->em->getRepository(SyntheseRemunarationCab::class)->findOneBy(['periode' => $periodeEntity, 'groupement' => 'RGA']);
if($syntheseCabRga) {
$syntheseCabRga->setSalaireBrute(0);
$syntheseCabRga->setSalaireBruteImposable(0);
$syntheseCabRga->setCnssPp(0);
$syntheseCabRga->setCnssPs(0);
$syntheseCabRga->setCimrPp(0);
$syntheseCabRga->setCimrPs(0);
$syntheseCabRga->setIr(0);
$syntheseCabRga->setTotalPrelevement(0);
$syntheseCabRga->setTotalPrimesNonImposable(0);
$syntheseCabRga->setNetFinal(0);
$syntheseCabRga->setNetTheorique(0);
} else {
$syntheseCabRga = new SyntheseRemunarationCab();
$syntheseCabRga->setPeriode($periodeEntity);
$syntheseCabRga->setGroupement('RGA');
$this->em->persist($syntheseCabRga);
}
$syntheseCabSss = $this->em->getRepository(SyntheseRemunarationCab::class)->findOneBy(['periode' => $periodeEntity, 'groupement' => 'SSS']);
if($syntheseCabSss) {
$syntheseCabSss->setSalaireBrute(0);
$syntheseCabSss->setSalaireBruteImposable(0);
$syntheseCabSss->setCnssPp(0);
$syntheseCabSss->setCnssPs(0);
$syntheseCabSss->setCimrPp(0);
$syntheseCabSss->setCimrPs(0);
$syntheseCabSss->setIr(0);
$syntheseCabSss->setTotalPrelevement(0);
$syntheseCabSss->setTotalPrimesNonImposable(0);
$syntheseCabSss->setNetFinal(0);
$syntheseCabSss->setNetTheorique(0);
} else {
$syntheseCabSss = new SyntheseRemunarationCab();
$syntheseCabSss->setPeriode($periodeEntity);
$syntheseCabSss->setGroupement('SSS');
$this->em->persist($syntheseCabSss);
}
$syntheseCabSst = $this->em->getRepository(SyntheseRemunarationCab::class)->findOneBy(['periode' => $periodeEntity, 'groupement' => 'SST']);
if($syntheseCabSst) {
$syntheseCabSst->setSalaireBrute(0);
$syntheseCabSst->setSalaireBruteImposable(0);
$syntheseCabSst->setCnssPp(0);
$syntheseCabSst->setCnssPs(0);
$syntheseCabSst->setCimrPp(0);
$syntheseCabSst->setCimrPs(0);
$syntheseCabSst->setIr(0);
$syntheseCabSst->setTotalPrelevement(0);
$syntheseCabSst->setTotalPrimesNonImposable(0);
$syntheseCabSst->setNetFinal(0);
$syntheseCabSst->setNetTheorique(0);
} else {
$syntheseCabSst = new SyntheseRemunarationCab();
$syntheseCabSst->setPeriode($periodeEntity);
$syntheseCabSst->setGroupement('SST');
$this->em->persist($syntheseCabSst);
}
return [
'SST' => $syntheseCabSst,
'SSS' => $syntheseCabSss,
'FCZ' => $syntheseCabFcz,
'RGA' => $syntheseCabRga,
];
}
}