<?php
namespace App\Controller\Paie;
use App\Entity\PMessengerStatut;
use App\Entity\Users;
use App\Message\GenerateFileMessage;
use App\Message\PaieMessage;
use Mpdf\Mpdf;
use App\Entity\PPiece;
use App\Entity\PDevise;
use App\Entity\Periode;
use App\Entity\PStatut;
use App\Entity\VMatrix;
use App\Entity\PDossier;
use App\Entity\Pemploye;
use App\Entity\Probleme;
use App\Entity\LContract;
use App\Entity\PPaiement;
use App\Entity\Prubrique;
use App\Entity\Tbulletin;
use App\Entity\PBordereau;
use App\Entity\PBaremeCimr;
use App\Entity\TbulletinLg;
use App\Entity\LelementFixe;
use App\Entity\PbaremeBrute;
use App\Entity\PPrelevementLg;
use App\Entity\PArretTravailLg;
use App\Entity\HonoraireDetFixe;
use App\Entity\LElementEcheance;
use App\Controller\ApiController;
use App\Service\CalculPaieService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/paie/bulletin')]
class BulletinController extends AbstractController
{
private $em;
private $calculPaieService;
private $api;
private $bus;
public function __construct(ManagerRegistry $doctrine, CalculPaieService $calculPaieService, ApiController $api,MessageBusInterface $bus)
{
$this->em = $doctrine->getManager();
$this->api = $api;
$this->calculPaieService = $calculPaieService;
$this->bus = $bus;
}
#[Route('/', name: 'app_paie_bulletin')]
public function index(Request $request): Response
{
$operations = $this->api->check($this->getUser(), 'app_paie_bulletin', $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('paie/bulletin/index.html.twig', [
'operations' => $operations,
]);
}
#[Route('/app_bulletin_employe_list/{periode}', name: 'app_bulletin_employe_list', options: ['expose' => true])]
public function app_bulletin_employe_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;
$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';
$dossier = $request->getSession()->get('dossier');
$queryBuilder = $this->em->createQueryBuilder()
->select('b.code as bulletin, b.id as bulletin_id, contract.id as id, contract.code as contract_code, p.nom as nom, p.matricule as matricule , p.prenom')
->from(LContract::class, 'contract')
->innerJoin('contract.employe', 'p')
->innerJoin('contract.bulletins', 'b')
->innerJoin('b.piece', 'piece')
// ->Where('contract.active = 1')
->Where('b.dossier = :dossier')
->andWhere('b.dossier = :dossier')
->andWhere('b.periode = :periode')
->andWhere('piece.id = 1')
->andWhere('b.active = 1')
->setParameter('dossier', $dossier)
->setParameter('periode', $periode);
if (!empty($search)) {
$queryBuilder->andWhere('(contract.code LIKE :search OR b.code LIKE :search OR p.matricule LIKE :search OR p.nom LIKE :search OR p.prenom LIKE :search)')
->setParameter('search', "%$search%");
}
if (!empty($orderColumn)) {
$queryBuilder->orderBy("$orderColumn", $orderDir);
}
$filteredRecords = count($queryBuilder->getQuery()->getResult());
// Paginate results
$queryBuilder->setFirstResult($start)
->setMaxResults($length);
$results = $queryBuilder->getQuery()->getResult();
// dd($results);
foreach ($results as $key => $contract) {
// dd('amine');
// if($contract['id'] == 1301) {
// dd($this->em->getRepository(PArretTravailLg::class)->getNombreJoursArret($contract['id'], $periode));
// }
$results[$key]['DT_RowId'] = $contract['id'];
$results[$key]['nombreJourTravails'] = 26 - $this->em->getRepository(PArretTravailLg::class)->getNombreJoursArret($contract['id'], $periode);
$montant = $this->em->getRepository(Tbulletin::class)->getNetAPaye($contract['bulletin_id']);
$results[$key]['salaire'] = '<p style="text-align: right !important; margin:0 !important">' . number_format($montant, 2, ',', ' ') . '</p>';;
$results[$key]['problemes'] = $this->em->getRepository(Probleme::class)->checkIfTheresProblemes($periode, $contract['id']);
}
// dd($results);
$totalRecords = $this->em->createQueryBuilder()
->select('COUNT(d.id)')
->from(LContract::class, 'd')
->innerJoin('d.bulletins', 'bulletins')
->innerJoin('bulletins.piece', 'piece')
->Where('bulletins.dossier = :dossier')
->andWhere('bulletins.active = 1')
->andWhere('piece.id = 1')
->andWhere('bulletins.periode = :periode')
->setParameter('dossier', $dossier)
->setParameter('periode', $periode)
->getQuery()
->getSingleScalarResult();
return new JsonResponse([
'draw' => $draw,
'recordsTotal' => $totalRecords,
'recordsFiltered' => $filteredRecords,
'data' => $results,
]);
}
#[Route('/app_bulletin_employe_calcul', name: 'app_bulletin_employe_calcul', options: ['expose' => true])]
public function app_bulletin_employe_calcul(Request $request): Response
{
// dd($request);
$dossier = $request->getSession()->get('dossier');
$dossier = $this->em->getRepository(PDossier::class)->find($dossier);
// dd($dossier);
$periode = $this->calculPaieService->getPeriode();
if($periode->IsValider()) {
return new JsonResponse('Periode est valider !', 500, [], JSON_UNESCAPED_UNICODE);
}
$bordoreauIds = [];
// dd(count($dossier->getActiveContracts()));
foreach ($dossier->getActiveContracts() as $contract) {
// dump($contract->getBareme()->getBaremeType()->getId());
if (!$contract->getBareme()) {
continue;
}
$natureContract = $contract->getPnatureContract();
if ($contract->getBareme()->getBaremeType()->getId() == 1) { // standard
if ($contract->getDateAnciennete() == null) {
return new JsonResponse('Date anciennete introuvable !', 500, [], JSON_UNESCAPED_UNICODE);
}
$piece = $this->em->getRepository(PPiece::class)->find(4); // paie
// get nature contract
$bordoreau = $this->em->getRepository(PBordereau::class)->findOneBy(['dossier' => $dossier, 'active' => true, 'periode' => $periode, 'piece' => $piece, 'natureContract' => $natureContract]);
if (!$bordoreau) {
$bordoreau = new PBordereau;
$bordoreau->setPiece($piece);
$bordoreau->setPeriode($periode);
$bordoreau->setNatureContract($natureContract);
$bordoreau->setDossier($dossier);
$bordoreau->setType('paie');
$bordoreau->setPaiement(
$this->em->getRepository(PPaiement::class)->find(1)
);
$bordoreau->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$bordoreau->setDevise(
$this->em->getRepository(PDevise::class)->find(1)
);
$bordoreau->setUserCreated($this->getUser());
$this->em->persist($bordoreau);
$this->em->flush();// flush because the next contract will search for the same bordreau if i dont flush he will not find it
} else {
$bordoreau->setStatut($this->em->getRepository(PStatut::class)->find(1));
}
if (in_array($bordoreau->getStatut()->getId(), [3, 4])) { // valider , integrer
return new JsonResponse('Vous ne pouvez pas modifier un bordereau déja ' . $bordoreau->getStatut()->getDesignation() . ' !', 500);
}
// dd($natureContract->getType());
$this->generateBulletin($bordoreau, $dossier, $contract, $periode);
array_push($bordoreauIds, $bordoreau->getId());
} elseif ($contract->getBareme()->getBaremeType()->getId() == 2) { // detail
$honoraireDetFixe = $this->em->getRepository(HonoraireDetFixe::class)->findOneBy(['contract' => $contract, 'active' => true]);
if (!$honoraireDetFixe) {
continue;
}
$piece = $this->em->getRepository(PPiece::class)->find(5);
$devise = $this->em->getRepository(PDevise::class)->find(1);
$paiement = $this->em->getRepository(PPaiement::class)->find(1);
$bordoreau = $this->em->getRepository(PBordereau::class)->findOneBy([
'dossier' => $dossier,
'active' => true,
'periode' => $periode,
'piece' => $piece,
'devise' => $devise,
'natureContract' => $natureContract,
'paiement' => $paiement,
'observation' => 'Honoraire det fixe (' . $periode->getCode() . ')'
]);
if (!$bordoreau) {
$bordoreau = new PBordereau;
$bordoreau->setPiece($this->em->getRepository(PPiece::class)->find(5));
$bordoreau->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$bordoreau->setPeriode($periode);
$bordoreau->setType('honoraire');
$bordoreau->setPaiement($paiement);
$bordoreau->setNatureContract($natureContract);
$bordoreau->setUserCreated($this->getUser());
$bordoreau->setDossier($dossier);
$bordoreau->setDevise($devise);
$bordoreau->setObservation('Honoraire det fixe (' . $periode->getCode() . ')');
$this->em->persist($bordoreau);
$this->em->flush();// flush because the next contract will search for the same bordreau if i dont flush he will not find it
} else {
$bordoreau->setStatut($this->em->getRepository(PStatut::class)->find(1));
}
if (in_array($bordoreau->getStatut()->getId(), [3, 4])) { // valider , integrer
return new JsonResponse('Vous ne pouvez pas modifier un bordereau déja ' . $bordoreau->getStatut()->getDesignation() . ' !', 500);
}
$this->generateBulletinHonoraire($bordoreau, $dossier, $contract, $periode, $honoraireDetFixe);
$bordoreauIds[] = $bordoreau->getId();
}
$this->em->flush();
}
$bordoreauIds = array_unique($bordoreauIds);
if (count($bordoreauIds) > 0) {
$request->request->add(['bordoreauIds' => json_encode($bordoreauIds)]);
}
return new JsonResponse('Bien enregistrer !', 200);
}
#[Route('/get_dossiers', name: 'get_dossiers', options: ['expose' => true])]
public function get_dossiers(): JsonResponse
{
$dossiers = $this->em->getRepository(PDossier::class)->findBy(['active' => true]);
$array = [];
foreach ($dossiers as $key => $dossier) {
$array[] = $dossier->getId();
}
return new JsonResponse($array);
}
#[Route('/app_bulletin_employe_calcul_all/{dossier}', name: 'app_bulletin_employe_calcul_all', options: ['expose' => true])]
public function app_bulletin_employe_calcul_all(PDossier $dossier, Request $request): Response
{
// dd($request);
// $dossier = $request->getSession()->get('dossier');
// $dossier = $this->em->getRepository(PDossier::class)->find($dossier);
$periode = $this->calculPaieService->getPeriode();
if($periode->IsValider()) {
return new JsonResponse('Periode est valider !', 500, [], JSON_UNESCAPED_UNICODE);
}
$bordoreauIds = [];
foreach ($dossier->getActiveContracts() as $contract) {
if (!$contract->getBareme()) {
continue;
}
$natureContract = $contract->getPnatureContract();
if ($contract->getBareme()->getBaremeType()->getId() == 1) { // standard
if ($contract->getDateAnciennete() == null) {
return new JsonResponse('Date anciennete introuvable !', 500, [], JSON_UNESCAPED_UNICODE);
}
$piece = $this->em->getRepository(PPiece::class)->find(4); // paie
$bordoreau = $this->em->getRepository(PBordereau::class)->findOneBy(['dossier' => $dossier, 'periode' => $periode, 'active' => true, 'piece' => $piece, 'natureContract' => $natureContract]);
if (!$bordoreau) {
$bordoreau = new PBordereau;
$bordoreau->setPiece($piece);
$bordoreau->setPeriode($periode);
$bordoreau->setNatureContract($natureContract);
$bordoreau->setDossier($dossier);
// if($natureContract->getType() == "stagiaires") {
// $bordoreau->setType('honoraire');
// } else {
$bordoreau->setType('paie');
// }
$bordoreau->setPaiement(
$this->em->getRepository(PPaiement::class)->find(1)
);
$bordoreau->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$bordoreau->setDevise(
$this->em->getRepository(PDevise::class)->find(1)
);
$bordoreau->setUserCreated($this->getUser());
$this->em->persist($bordoreau);
$this->em->flush(); // flush because the next contract will search for the same bordreau if i dont flush he will not find it
} else {
$bordoreau->setStatut($this->em->getRepository(PStatut::class)->find(1));
}
if (in_array($bordoreau->getStatut()->getId(), [3, 4])) { // valider , integrer
return new JsonResponse('Vous ne pouvez pas modifier un bordereau déja ' . $bordoreau->getStatut()->getDesignation() . ' !', 500);
}
$this->generateBulletin($bordoreau, $dossier, $contract, $periode);
array_push($bordoreauIds, $bordoreau->getId());
} elseif ($contract->getBareme()->getBaremeType()->getId() == 2) { // detail
$honoraireDetFixe = $this->em->getRepository(HonoraireDetFixe::class)->findOneBy(['contract' => $contract, 'active' => true]);
$piece = $this->em->getRepository(PPiece::class)->find(5);
if (!$honoraireDetFixe) {
continue;
}
$devise = $this->em->getRepository(PDevise::class)->find(1);
$paiement = $this->em->getRepository(PPaiement::class)->find(1);
$bordoreau = $this->em->getRepository(PBordereau::class)->findOneBy([
'dossier' => $dossier,
'active' => true,
'periode' => $periode,
'piece' => $piece,
'devise' => $devise,
'natureContract' => $natureContract,
'paiement' => $paiement,
'observation' => 'Honoraire det fixe (' . $periode->getCode() . ')'
]);
if (!$bordoreau) {
$bordoreau = new PBordereau;
$bordoreau->setPiece($piece);
$bordoreau->setStatut($this->em->getRepository(PStatut::class)->find(1));
$bordoreau->setPeriode($periode);
$bordoreau->setType('honoraire');
$bordoreau->setPaiement($paiement);
$bordoreau->setDevise($devise);
$bordoreau->setNatureContract($natureContract);
$bordoreau->setUserCreated($this->getUser());
$bordoreau->setDossier($dossier);
$bordoreau->setObservation('Honoraire det fixe (' . $periode->getCode() . ')');
$this->em->persist($bordoreau);
$this->em->flush();// flush because the next contract will search for the same bordreau if i dont flush he will not find it
} else {
$bordoreau->setStatut($this->em->getRepository(PStatut::class)->find(1));
}
if (in_array($bordoreau->getStatut()->getId(), [3, 4])) { // valider , integrer
return new JsonResponse('Vous ne pouvez pas modifier un bordereau déja ' . $bordoreau->getStatut()->getDesignation() . ' !', 500);
}
$this->generateBulletinHonoraire($bordoreau, $dossier, $contract, $periode, $honoraireDetFixe);
array_push($bordoreauIds, $bordoreau->getId());
}
$this->em->flush();
}
$bordoreauIds = array_unique($bordoreauIds);
if (count($bordoreauIds) > 0) {
$request->request->add(['bordoreauIds' => json_encode($bordoreauIds)]);
}
return new JsonResponse('Bien enregistrer !', 200);
}
#[Route('/app_bulletin_employe_problemes/{contract}', name: 'app_bulletin_employe_problemes', options: ['expose' => true])]
public function app_bulletin_employe_problemes(LContract $contract): Response
{
// dd($request);
$periode = $this->calculPaieService->getPeriode();
$problemes = $this->em->getRepository(Probleme::class)->findBy(['periode' => $periode, 'contract' => $contract, 'resoudre' => false]);
$html = $this->renderView('paie/bulletin/includes/probleme.html.twig', ['problemes' => $problemes, 'contract' => $contract]);
return new JsonResponse($html);
}
#[Route('/app_bulletin_employe_probleme_solves', name: 'app_bulletin_employe_probleme_solves', options: ['expose' => true])]
public function app_bulletin_employe_probleme_solves(Request $request): Response
{
$problemes = json_decode($request->get('problemes'));
foreach ($problemes as $key => $probleme) {
$date = new \DateTime($probleme->periode);
$probleme = $this->em->getRepository(Probleme::class)->find($probleme->id);
$periode = $this->calculPaieService->getPeriode($date->format('mY'));
if ($probleme->getPrelevementLg()) {
$prelevementLg = new PPrelevementLg();
$prelevementLg->setPrelevement($probleme->getPrelevementLg()->getPrelevement());
$prelevementLg->setMontant($probleme->getPrelevementLg()->getMontant());
$prelevementLg->setPeriode($periode);
$this->em->persist($prelevementLg);
$probleme->getPrelevementLg()->setActive(false);
} else if ($probleme->getElementEcheance()) {
$elementEcheance = new LElementEcheance();
$elementEcheance->setContract($probleme->getElementEcheance()->getContract());
$elementEcheance->setPeriode($periode);
$elementEcheance->setRubrique($probleme->getElementEcheance()->getRubrique());
$elementEcheance->setDateEcheance($date);
$elementEcheance->setCreated(new \DateTime('now'));
$elementEcheance->setSens($probleme->getElementEcheance()->getSens());
$elementEcheance->setMontant($probleme->getElementEcheance()->getMontant());
$elementEcheance->setMontant($probleme->getElementEcheance()->getMontant());
$this->em->persist($elementEcheance);
$probleme->getElementEcheance()->setActive(false);
}
$probleme->setResoudre(true);
}
$this->em->flush();
return new JsonResponse("Bien Enregistrer");
}
#[Route('/app_bulletin_employe_bulletin_details/{code}', name: 'app_bulletin_employe_bulletin_details', options: ['expose' => true])]
public function app_bulletin_employe_bulletin_details($code): Response
{
// dd($request);
$bulletin = $this->em->getRepository(Tbulletin::class)->findOneBy(['code' => $code]);
$html = $this->renderView('paie/bulletin/includes/bulletin_details.html.twig', ['bulletin' => $bulletin, 'bulletinLgs' => $bulletin->getActiveBulletinLgs()]);
return new JsonResponse($html);
}
#[Route('/app_bulletin_employe_disable', name: 'app_bulletin_employe_disable', options: ['expose' => true])]
public function app_bulletin_employe_disable(Request $request)
{
// dd($request);
$motif = $request->get('motif');
$bulletinIds = json_decode($request->get('bulletinIds'));
foreach ($bulletinIds as $key => $bulletinId) {
$bulletin = $this->em->getRepository(Tbulletin::class)->find($bulletinId);
if (in_array($bulletin->getBordereau()->getStatut()->getId(), [3, 4])) { // valider , integrer
return new JsonResponse('Vous ne pouvez pas supprimer. bordereau déja ' . $bulletin->getBordereau()->getStatut()->getDesignation() . ' !', 500);
}
$bulletin->setMotifAnnulation($motif);
$bulletin->setUserAnnulation($this->getUser());
$bulletin->setActive(false);
foreach ($bulletin->getBulletinLgs() as $key => $bulletinLg) {
$bulletinLg->setActive(false);
}
foreach ($bulletin->getJournalBulletinLgs() as $key => $JournalBulletinLg) {
$JournalBulletinLg->setActive(false);
}
if($bulletin->getBordereau()->getType() == 'stc') {
$bulletin->getContract()->getEmploye()->setStc(false);
}
}
$this->em->flush();
return new JsonResponse("Bien Enregistrer");
}
#[Route('/print/{bulletin}', name: 'app_bulletin_employe_bulletin_print', options: ['expose' => true])]
public function app_bulletin_employe_bulletin_print(Tbulletin $bulletin): Response
{
// dd($bulletin->getContract()->getPnatureContract()->getType());
if ($bulletin->getContract()->getPnatureContract()->getType()->getId() == 1) {
$brute = [1, 2, 4, 6, 7, 8, 9, 10, 11, 16, 60, 69, 76,93,94];
} else {
$brute = [65];
}
$bulletinLgBrute = $this->getPbulletinsLgQueryBuilder($brute, $bulletin);
// dd($bulletin->getContract()->getEmploye()->getPrenom());
$bulletinLgPrets = $this->em->createQueryBuilder('b')
->select('bulletinLgs')
->from(TbulletinLg::class, 'bulletinLgs')
->innerJoin('bulletinLgs.bulletin', 'b')
->innerJoin('bulletinLgs.rubrique', 'rubrique')
->innerJoin('b.bordereau', 'bordereau')
->innerJoin('bordereau.devise', 'devise') // 5 is net a paye
->andWhere('rubrique.prets = 1')
->andWhere('b.id = :bulletin')
->andWhere('bulletinLgs.active = 1')
->andWhere('bulletinLgs.montant != 0')
->setParameter('bulletin', $bulletin->getId())
->getQuery()
->getResult();
$bulletinLgCotisationsPS = $this->getPbulletinsLgQueryBuilder([50, 47, 53, 48], $bulletin);
// $bulletinLgCotisationsPS = $this->em->createQueryBuilder()
// ->select('TbulletinLg')
// ->from(TbulletinLg::class, 'TbulletinLg')
// ->innerJoin('TbulletinLg.bulletin', 'bulletin')
// ->innerJoin('TbulletinLg.rubrique', 'rubrique')
// ->andWhere('rubrique.id in (50, 47, 53, 48)')
// ->andWhere('TbulletinLg.active = 1')
// ->andWhere('bulletin.id = :bulletin')
// ->andWhere('TbulletinLg.montant != 0')
// ->setParameter('bulletin', $bulletin)
// ->getQuery()
// ->getResult()
// ;
$bulletinLgIR = $this->em->createQueryBuilder()
->select('TbulletinLg')
->from(TbulletinLg::class, 'TbulletinLg')
->innerJoin('TbulletinLg.bulletin', 'bulletin')
->innerJoin('TbulletinLg.rubrique', 'rubrique')
->andWhere('rubrique.id = 43')
->andWhere('TbulletinLg.active = 1')
->andWhere('bulletin.id = :bulletin')
->andWhere('TbulletinLg.montant != 0')
->setParameter('bulletin', $bulletin)
->getQuery()
->getOneOrNullResult();
// dd($bulletinLgIR);
$montantNetPaye = $this->em->createQueryBuilder()
->select('sum(TbulletinLg.montant) as montant')
->from(TbulletinLg::class, 'TbulletinLg')
->innerJoin('TbulletinLg.bulletin', 'bulletin')
->innerJoin('TbulletinLg.rubrique', 'rubrique')
->andWhere('rubrique.id in(5,68)')
->andWhere('TbulletinLg.active = 1')
->andWhere('bulletin.id = :bulletin')
->setParameter('bulletin', $bulletin)
->getQuery()
->getOneOrNullResult();
// dd($bulletinLgNetPaye);
$montantImposable = $this->em->createQueryBuilder()
->select('sum(TbulletinLg.montant) as montant')
->from(TbulletinLg::class, 'TbulletinLg')
->innerJoin('TbulletinLg.bulletin', 'bulletin')
->innerJoin('TbulletinLg.rubrique', 'rubrique')
->andWhere('rubrique.imposable = 1')
->andWhere('TbulletinLg.active = 1')
->andWhere('bulletin.id = :bulletin')
->setParameter('bulletin', $bulletin)
->getQuery()
->getOneOrNullResult();
// dd($bulletinLgCotisationsPS);
$html = $this->render("paie/bulletin/pdf/bulletin.html.twig", [
'bulletin' => $bulletin,
'bulletinLgPrets' => $bulletinLgPrets,
'bulletinLgCotisationsPS' => $bulletinLgCotisationsPS,
'bulletinLgBrute' => $bulletinLgBrute,
'bulletinLgIR' => $bulletinLgIR,
'montantNetPaye' => $montantNetPaye,
'montantImposable' => $montantImposable,
])->getContent();
$mpdf = new Mpdf([
'mode' => 'c',
'margin_left' => '5',
'margin_right' => '5',
]);
$mpdf->SetTitle('Bulletin');
$mpdf->WriteHTML($html);
$mpdf->Output($bulletin->getContract()->getEmploye()->getNom() . "_" . $bulletin->getContract()->getEmploye()->getPrenom() . ".pdf", "I");
die;
}
public function getPbulletinsLgQueryBuilder($rubrique, $bulletin)
{
$bulletinLgs = $this->em->createQueryBuilder()
->select('TbulletinLg')
->from(TbulletinLg::class, 'TbulletinLg')
->innerJoin('TbulletinLg.bulletin', 'bulletin')
->innerJoin('TbulletinLg.rubrique', 'rubrique')
->andWhere('rubrique.id in (:rubrique)')
->andWhere('TbulletinLg.active = 1')
->andWhere('bulletin.id = :bulletin')
->andWhere('TbulletinLg.montant != 0')
->setParameter('bulletin', $bulletin)
->setParameter('rubrique', $rubrique)
->addOrderBy('rubrique.code', 'ASC')
->getQuery()
->getResult();
// $results = $bulletinLgs->getQuery()->getResult();
$montant = 0;
$resultArray = [];
foreach ($bulletinLgs as $key => $bulletinLg) {
/** @var TbulletinLg $bulletinLg */
// dd();
$base = $taux = 0;
switch ($bulletinLg->getRubrique()->getId()) {
case 1:
$base = 26 - $this->em->getRepository(PArretTravailLg::class)->getNombreJoursArret($bulletinLg->getBulletin()->getContract()->getId(), $bulletinLg->getBulletin()->getPeriode()->getId());
$taux = $this->em->getRepository(PbaremeBrute::class)->findOneBy(['bareme' => $bulletinLg->getBulletin()->getContract()->getBareme()->getId(), 'rubrique' => 1])->getTauxPs();
$montant = $bulletinLg->getMontant();
break;
case 2:
$base = $montant;
$taux = $this->em->getRepository(VMatrix::class)->getNombreAnneeAnciennte($bulletinLg->getBulletin()->getContract()->getDateAnciennete())->getTaux();
break;
case 48:
// $taux = $this->em->getRepository(PBaremeCimr::class)->findOneBy(['rubrique' => $bulletinLg->getRubrique()->getId(), 'bareme' => $bulletinLg->getBulletin()->getContract()->getBareme()->getId()])->getTaux();
$taux = $bulletinLg->getBulletin()->getTauxCotisCimr();
case 47:
case 50:
case 53:
$taux = $this->em->getRepository(VMatrix::class)->findOneBy(['rubrique' => $bulletinLg->getRubrique()->getId()])->getTaux();
break;
default:
$base = $bulletinLg->getMontant();
$taux = 1;
break;
}
$resultArray[$key] = [
'bulletinLg' => $bulletinLg,
'base' => $base,
'taux' => $taux,
];
}
return $resultArray;
}
public function generateBulletin($bordoreau, $dossier, $contract, $periode)
{
$nombreJourTravails = 26 - $this->em->getRepository(PArretTravailLg::class)->getNombreJoursArret($contract->getId(), $periode);
$natureContract = $contract->getPnatureContract();;
if ($contract->getBareme()->getBaremeType()->getId() == 1) {
if ($natureContract->getType()->getId() == 1) {
$this->calculPaieService->permanentPaie($contract, $dossier, $nombreJourTravails, $periode, $bordoreau);
} else if (in_array($natureContract->getType()->getId(), [2, 6])) {
$this->calculPaieService->vacatairePaie($contract, $dossier, $nombreJourTravails, $periode, $bordoreau);
} else if (in_array($natureContract->getType()->getId(), [4, 5, 7])) {
$this->calculPaieService->stagairePaie($contract, $dossier, $nombreJourTravails, $periode, $bordoreau);
}
}
}
public function generateBulletinHonoraire($bordoreau, $dossier, $contract, $periode, $honoraireDetFixe)
{
$returnueIR = $this->em->getRepository(Prubrique::class)->find(43);
$divers = $this->em->getRepository(Prubrique::class)->find(68);
$honoraireRubrique = $this->em->getRepository(Prubrique::class)->find(65);
$dossier = $contract->getDossier();
$piece = $this->em->getRepository(PPiece::class)->find(2);
$bulletin = $this->em->getRepository(Tbulletin::class)->findOneBy(['contract' => $contract, 'bordereau' => $bordoreau, 'dossier' => $dossier, 'piece' => $piece, 'active' => true, 'periode' => $periode]);
if ($bulletin) {
foreach ($bulletin->getBulletinLgs() as $bulletinLg) {
$bulletinLg->setActive(false);
}
} else {
$bulletin = new Tbulletin();
$bulletin->setDossier($dossier);
$bulletin->setPPiece(
$this->em->getRepository(PPiece::class)->find(2)
);
$bulletin->setBordereau($bordoreau);
$bulletin->setObservation('Honoraire det fixe (' . $periode->getCode() . ')');
$bulletin->setContract($contract);
$bulletin->setPeriode($periode);
$this->em->persist($bulletin);
}
$bulletinDet = new TbulletinLg();
$bulletinDet->setBulletin($bulletin);
$bulletinDet->setRubrique($honoraireRubrique);
$bulletinDet->setSens(1);
$bulletinDet->setMontant($honoraireDetFixe->getBrute());
$this->em->persist($bulletinDet);
$bulletinDetRetunueIr = new TbulletinLg();
$bulletinDetRetunueIr->setBulletin($bulletin);
$bulletinDetRetunueIr->setRubrique($returnueIR);
$bulletinDetRetunueIr->setSens(-1);
$bulletinDetRetunueIr->setMontant($honoraireDetFixe->getIr());
$this->em->persist($bulletinDetRetunueIr);
$netAPaye = $honoraireDetFixe->getMontant();
$prelevementLgs = $this->em->getRepository(PPrelevementLg::class)->getPrelevementByPeriodeAndContract($contract, $periode);
foreach ($prelevementLgs as $prelevementLg) {
if ($prelevementLg->getMontant() > $netAPaye) {
$prelevementLg->setActive(false);
$prelevementLg->setValider(false);
$prelevementLg->setMotif('Solde insuffisant!');
$prelevementLg->setBulletin(null);
$lastPrelevementDet = $this->em->getRepository(PPrelevementLg::class)->findBy(['prelevement' => $prelevementLg->getPrelevement()], ['id' => 'desc'])[0];
$newPeriode = $lastPrelevementDet->getPeriode()->getDate()->modify('+1 month');
$newPeriode = $this->calculPaieService->getPeriode($newPeriode->format('mY'));
$prelevementDet = new PPrelevementLg();
$prelevementDet->setPeriode($newPeriode);
$prelevementDet->setMontant($prelevementLg->getMontant());
$prelevementDet->setPrelevement($prelevementLg->getPrelevement());
$this->em->persist($prelevementDet);
} else {
$bulletinDet = new TbulletinLg();
$bulletinDet->setMontant($prelevementLg->getMontant());
$bulletinDet->setSens(-1);
$bulletinDet->setRubrique($prelevementLg->getPrelevement()->getRubrique());
$bulletinDet->setBulletin($bulletin);
$this->em->persist($bulletinDet);
$prelevementLg->setBulletin($bulletin);
$prelevementLg->setValider(true);
$netAPaye -= round($prelevementLg->getMontant(), 2);
}
}
$bulletinDetDivers = new TbulletinLg();
$bulletinDetDivers->setBulletin($bulletin);
$bulletinDetDivers->setRubrique($divers);
$bulletinDetDivers->setSens(1);
$bulletinDetDivers->setMontant($netAPaye);
$this->em->persist($bulletinDetDivers);
}
public function totalMontant($rubrique, Tbulletin $bulletin)
{
// dd('h');
$bulletinLgCumul = $this->em->createQueryBuilder()
->select('sum(TbulletinLg.montant) as montant')
->from(TbulletinLg::class, 'TbulletinLg')
->innerJoin('TbulletinLg.bulletin', 'bulletin')
->innerJoin('TbulletinLg.rubrique', 'rubrique')
->innerJoin('bulletin.contract', 'contract')
->innerJoin('bulletin.periode', 'periode')
->innerJoin('bulletin.bordereau', 'bordereau')
->andWhere('rubrique.id in (:rubrique)')
->andWhere('TbulletinLg.active = 1')
->andWhere('contract.id = :contract')
->andWhere('bordereau.type = :bordereauType ')
// ->andWhere('DATE_FORMAT(periode.date, "%Y") = :year')
->setParameter('contract', $bulletin->getContract()->getId())
->setParameter('rubrique', $rubrique)
// ->setParameter('year', $bulletin->getPeriode()->getDate()->format('Y'))
->setParameter('bordereauType', 'paie')
->getQuery()
->getOneOrNullResult();
if ($bulletinLgCumul['montant'] != null) {
return new Response($bulletinLgCumul['montant']);
}
return new Response(0);
}
#[Route('/app_bulletin_employe_calcul_messenger', name: 'app_bulletin_employe_calcul_messenger', options: ['expose' => true])]
public function app_bulletin_employe_calcul_messenger(Request $request): Response
{
$dossier = $request->get('dossier');
$user = $this->em->getRepository(Users::class)->find($this->getUser());
$messageStatut = new PMessengerStatut();
$messageStatut->setStatut('en cours');
if($dossier == 'all') {
$messageStatut->setClasse('Generation de paie pour tout les dossiers');
} else {
$messageStatut->setClasse('Generation de paie pour dossier: '.$request->getSession()->get('dossier')->getAbreviation());
}
$messageStatut->setUser($this->getUser());
$this->em->persist($messageStatut);
$this->em->flush();
$this->bus->dispatch(new PaieMessage([
'dossier' => $dossier === 'all' ? 'all' : $request->getSession()->get('dossier')->getId(),
'user' => $user->getId(),
'messageStatut' => $messageStatut->getId(),
]));
return new JsonResponse('Votre demande a été envoyée, veuillez consulter la page de demande', 200);
}
}