src/Manager/ContactSheetManager.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Entity\ContactSheet\ContactSheet;
  4. use App\Entity\ContactSheet\InterruptionContact;
  5. use App\Entity\ContactSheet\PriorityContact;
  6. use App\Entity\User;
  7. use App\Exception\ContactSheetFileException;
  8. use App\Repository\App\ContactSheet\ContactSheetRepository;
  9. use App\Repository\App\ContactSheet\InterruptionContactRepository;
  10. use App\Repository\App\ContactSheet\PriorityContactRepository;
  11. use App\Repository\Scp\BillingContactRepository;
  12. use App\Repository\Scp\ContactRepository;
  13. use App\Repository\Scp\ContractRepository;
  14. use App\Repository\Scp\UserRepository;
  15. use App\Traits\TranslatorByDomainTrait;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use mikehaertl\pdftk\Pdf;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class ContactSheetManager
  20. {
  21.     private const CONTACT_MAPPING = [
  22.         PriorityContact::FIRST_CONTACT => 1,
  23.         PriorityContact::SECOND_CONTACT => 2,
  24.         PriorityContact::THIRD_CONTACT => 3,
  25.         PriorityContact::FOURTH_CONTACT => 4,
  26.         PriorityContact::CRISIS_CONTACT => 5,
  27.     ];
  28.     use TranslatorByDomainTrait;
  29.     protected $contactSheetRepository;
  30.     protected $contractRepository;
  31.     protected $contractManager;
  32.     protected $billingContactRepository;
  33.     protected $pdftkPath;
  34.     protected $contactSheetEIEELayoutPath;
  35.     protected $contactSheetEUECLayoutPath;
  36.     protected $exportPath;
  37.     protected $objectManager;
  38.     protected $interruptionContactRepository;
  39.     protected $priorityContactRepository;
  40.     public function __construct(
  41.         TranslatorInterface           $translator,
  42.         ContactSheetRepository        $contactSheetRepository,
  43.         ContractRepository            $contractRepository,
  44.         ContractManager               $contractManager,
  45.         BillingContactRepository      $billingContactRepository,
  46.         string                        $contactSheetEIEELayoutPath,
  47.         string                        $contactSheetEUECLayoutPath,
  48.         string                        $pdftkPath,
  49.         string                        $exportPath,
  50.         EntityManagerInterface        $objectManager,
  51.         InterruptionContactRepository $interruptionContactRepository,
  52.         PriorityContactRepository     $priorityContactRepository,
  53.         UserRepository                $userRepository
  54.     )
  55.     {
  56.         $this->contactSheetRepository $contactSheetRepository;
  57.         $this->contactSheetEIEELayoutPath $contactSheetEIEELayoutPath;
  58.         $this->contactSheetEUECLayoutPath $contactSheetEUECLayoutPath;
  59.         $this->contractManager $contractManager;
  60.         $this->contractRepository $contractRepository;
  61.         $this->billingContactRepository $billingContactRepository;
  62.         $this->pdftkPath $pdftkPath;
  63.         $this->exportPath $exportPath;
  64.         $this->translator $translator;
  65.         $this->objectManager $objectManager;
  66.         $this->interruptionContactRepository $interruptionContactRepository;
  67.         $this->priorityContactRepository $priorityContactRepository;
  68.         $this->userRepository $userRepository;
  69.     }
  70.     public function getContactSheet(string $idUser $user): ?ContactSheet
  71.     {
  72.         $customerNumber $user->isMainAccount() ? $user->getCustomerNumber() : $user->getMainAccount()->getCustomerNumber();
  73.         return $this->contactSheetRepository->findOneBy([
  74.             'id' => $id,
  75.             'customerNumber' => $customerNumber,
  76.         ]);
  77.     }
  78.     public function getContactSheetByDeliverationsStations(array $deliveryStationsIdsUser $user): array
  79.     {
  80.         $customerNumber $user->isMainAccount() ? $user->getCustomerNumber() : $user->getMainAccount()->getCustomerNumber();
  81.         return $this->contactSheetRepository->findAllByDeliveryStationIds($deliveryStationsIds$customerNumber);
  82.     }
  83.     public function generateFile(ContactSheet $contactSheetUser $user): ContactSheet
  84.     {
  85.         $donneesCompteClient $this->userRepository->findDonneesCompteClientByCustomerNumber($user->getCustomerNumber());
  86.         $deliveryStation $this->contractRepository->findOneByContractNumberAndDeliveryStation($contactSheet->getContractNumber(), $contactSheet->getDeliveryStationId());
  87.         if (null === $deliveryStation) {
  88.             throw new ContactSheetFileException(sprintf(
  89.                 'Contact sheet %s of customer %s : Missing contract %s or delivery station %s',
  90.                 $contactSheet->getId(),
  91.                 $contactSheet->getCustomerNumber(),
  92.                 $contactSheet->getContractNumber(),
  93.                 $contactSheet->getDeliveryStationId()
  94.             ));
  95.         }
  96.         $pdf = new Pdf($this->getLayout($deliveryStation), [
  97.             'command' => $this->pdftkPath,
  98.         ]);
  99.         $data = [
  100.             'p1_actualise_le' => Date('d-m-Y'),
  101.             'p1_numero_client' => $user->getCustomerNumber(),
  102.             'p1_prenom_nom' => $donneesCompteClient['nom'] . ' ' $donneesCompteClient['prenom'],
  103.             'p2_prenom_nom' => $donneesCompteClient['nom'] . ' ' $donneesCompteClient['prenom'],
  104.             'p1_numero_du_poste' => $contactSheet->getDeliveryStationId(),
  105.             'p1_nom_du_poste' => $deliveryStation $deliveryStation['Reperage'] : 'reperage',
  106.             'p2_ information_complementaires' => $contactSheet->getAdditionalInformation(),
  107.             'p2_numero_poste' => $contactSheet->getDeliveryStationId(),
  108.             'p2_nom_poste' => $deliveryStation $deliveryStation['Reperage'] : 'reperage',
  109.             'p2_numero_client' => $user->getCustomerNumber(),
  110.             'p1_nom_du_contrat' => $deliveryStation['Libelle'],
  111.             'p2_nom_du_contrat' => $deliveryStation['Libelle'],
  112.         ];
  113.         $data array_merge($data$this->getBillingContact());
  114.         $data array_merge($data$this->getOtherContact($contactSheet$user));
  115.         $data array_merge($data$this->getProjectContact($contactSheet$user));
  116.         $data array_merge($data$this->getPriorityContact($contactSheet));
  117.         $data array_merge($data$this->getInterruptionContact($contactSheet));
  118.         $pdf->fillForm($data);
  119.         $customerNumber $user->isMainAccount() ? $user->getCustomerNumber() : $user->getMainAccount()->getCustomerNumber();
  120.         $fileName sprintf('%s-%s.pdf'$customerNumber$contactSheet->getDeliveryStationId());
  121.         $fileName str_replace('/''_'$fileName);
  122.         $path $this->getPathDirectory() . $fileName;
  123.         $pdf->flatten()->saveAs($path);
  124.         if (!empty($pdf->getError())) {
  125.             throw new ContactSheetFileException($pdf->getError());
  126.         }
  127.         $date \DateTimeImmutable::createFromFormat('d-m-Y'$data["p1_actualise_le"]);
  128.         $contactSheet->setUpdatedAt($date);
  129.         $contactSheet->setFileName($fileName);
  130.         $contactSheet->setFileSize(filesize($path));
  131.         return $contactSheet;
  132.     }
  133.     public function getFilePath(ContactSheet $contactSheet): string
  134.     {
  135.         return $this->getPathDirectory() . $contactSheet->getFileName();
  136.     }
  137.     public function deleteFromUser(User $user)
  138.     {
  139.         $this->contactSheetRepository->deleteFromCustomerNumber($user->getCustomerNumber());
  140.         $this->priorityContactRepository->deleteFormUserId($user->getId());
  141.         $this->interruptionContactRepository->deleteFormUserId($user->getId());
  142.     }
  143.     public function isUserUsedInContactSheet(User $user): bool
  144.     {
  145.         return < ($this->interruptionContactRepository->count(['user' => $user]) + $this->priorityContactRepository->count(['user' => $user]));
  146.     }
  147.     protected function getLayout(array $deliveryStation): string
  148.     {
  149.         if (in_array($deliveryStation['TypeFicheContact'], ContactSheetRepository::CONTACT_SHEET_TYPE_EE_EI_GLF)
  150.         ) {
  151.             return $this->contactSheetEIEELayoutPath;
  152.         }
  153.         return $this->contactSheetEUECLayoutPath;
  154.     }
  155.     protected function getProjectContact(ContactSheet $contactSheetUser $user): array
  156.     {
  157.         $principalContact $this->contractManager->getPrincipalContactsByContractNumberAndType(
  158.             $contactSheet->getContractNumber(),
  159.             ContactRepository::TYPE_COMMERCIAL,
  160.             $user
  161.         );
  162.         if (null === $principalContact) {
  163.             return [];
  164.         }
  165.         return [
  166.             'p1_interlocuteur_dedie_prenom' => $principalContact['Prenom'],
  167.             'p1_interlocuteur_dedie_nom' => $principalContact['Nom'],
  168.             'p1_votre_interlocuteur_dedie_fixe' => $principalContact['Telephone'],
  169.             'p1_votre_interlocuteur_dedie_portable' => $principalContact['Portable'],
  170.             'p1_votre_interlocuteur_dedie_email' => $principalContact['Email'],
  171.         ];
  172.     }
  173.     protected function getOtherContact(ContactSheet $contactSheetUser $user): array
  174.     {
  175.         $technicalContact $this->contractManager->getPrincipalContactsByContractNumberAndType(
  176.             $contactSheet->getContractNumber(),
  177.             ContactRepository::TYPE_TECHNICAL,
  178.             $user
  179.         );
  180.         if (null === $technicalContact) {
  181.             return [];
  182.         }
  183.         return [
  184.             'p1_urgence_nom_centre' => $technicalContact['NomCE'],
  185.             'p1_urgence_telephone' => $technicalContact['Telephone'],
  186.             'p1_autres_contacts_responsable_technique_prenom' => $technicalContact['Prenom'],
  187.             'p1_autres_contacts_responsable_technique_nom' => $technicalContact['Nom'],
  188.             'p1_autres_contacts_responsable_technique_tel_fixe' => $technicalContact['Telephone'],
  189.             'p1_autres_contacts_responsable_technique_tel_portable' => $technicalContact['Portable'],
  190.             'p1_autres_contacts_responsable_technique_email' => $technicalContact['Email'],
  191.         ];
  192.     }
  193.     protected function getBillingContact(): array
  194.     {
  195.         $billingContact $this->billingContactRepository->findOneByContactType(BillingContactRepository::TYPE_CONTACT_SHEET);
  196.         if (null === $billingContact) {
  197.             return [];
  198.         }
  199.         return [
  200.             'p1_autres_contacts_facturation_prenom' => $billingContact['Prenom'],
  201.             'p1_autres_contacts_facturation_nom' => $billingContact['Nom'],
  202.             'p1_autres_contacts_facturation_service' => $billingContact['Titre'],
  203.             'p1_autres_contacts_facturation_email' => $billingContact['Email'],
  204.             'p1_autres_contacts_facturation_tel_fixe' => $billingContact['Telephone'],
  205.         ];
  206.     }
  207.     protected function getInterruptionContact(ContactSheet $contactSheet): array
  208.     {
  209.         $data = [];
  210.         $interruptionContacts $contactSheet->getInterruptionContacts();
  211.         $types = [
  212.             InterruptionContact::BEFORE_SCHEDULE_INTERVENTION => 'p2_interruption_programme_preparation_intervention_numero_contact',
  213.             InterruptionContact::D10_BEFORE_SCHEDULE_INTERVENTION => 'p2_interruption_programme_avant_intervention_numero_contact',
  214.             InterruptionContact::AFTER_SCHEDULE_INTERVENTION => 'p2_interruption_programme_remise_en_eau_numero_contact',
  215.             InterruptionContact::DURING_NOT_SCHEDULE_INTERVENTION => 'p2_interruption_non_programme_des_que_possible_numero_contact',
  216.             InterruptionContact::AFTER_NOT_SCHEDULE_INTERVENTION => 'p2_interruption_non_programme_remise_en_eau_numero_contact',
  217.         ];
  218.         foreach ($types as $type => $key) {
  219.             $contacts $interruptionContacts->filter(function (InterruptionContact $contact) use ($type) {
  220.                 return $contact->getType() === $type;
  221.             });
  222.             $contactsNumber $contacts->map(function (InterruptionContact $contact) use ($contactSheet) {
  223.                 $contacts $contactSheet->getPriorityContacts()->filter(function (PriorityContact $priorityContact) use ($contact) {
  224.                     return $priorityContact->getUser()->getId() === $contact->getUser()->getId();
  225.                 });
  226.                 $contactPriority $contacts->count() > $contacts->first()->getPriority() : null;
  227.                 return (null !== $contactPriority) ? static::CONTACT_MAPPING[$contactPriority] : null;
  228.             });
  229.             $data[$key] = 'N° ' implode('-'$contactsNumber->toArray());
  230.         }
  231.         return $data;
  232.     }
  233.     protected function getPriorityContact(ContactSheet $contactSheet): array
  234.     {
  235.         $data = [];
  236.         foreach ($contactSheet->getPriorityContacts() as $priorityContact) {
  237.             $number = static::CONTACT_MAPPING[$priorityContact->getPriority()] ?? null;
  238.             $user $priorityContact->getUser();
  239.             $contactInformation null !== $user $user->getContactInformation() : null;
  240.             if (null === $number || null === $user || null === $contactInformation) {
  241.                 break;
  242.             }
  243.             $priorityContactData = [
  244.                 'p2_interlocuteur_livraison_N' $number '_prenom_et_nom' => $user->getUsername(),
  245.                 'p2_interlocuteur_livraison_N' $number '_fonction' => $contactInformation->getJob(),
  246.                 'p2_interlocuteur_livraison_N' $number '_organisme' => $contactInformation->getOrganization(),
  247.                 'p2_interlocuteur_livraison_N' $number '_Tel_fixe' => $contactInformation->getPhone(),
  248.                 'p2_interlocuteur_livraison_N' $number '_Tel_portable' => $contactInformation->getCellPhone(),
  249.                 'p2_interlocuteur_livraison_N' $number '_email' => $contactInformation->getEmail(),
  250.                 'p2_interlocuteur_livraison_N' $number '_adresse' => $contactInformation->getAddress(),
  251.                 'p2_interlocuteur_livraison_N' $number '_adresse_code_postale' => $contactInformation->getZipCode(),
  252.                 'p2_interlocuteur_livraison_N' $number '_adresse_ville' => $contactInformation->getCity(),
  253.             ];
  254.             $data array_merge($data$priorityContactData);
  255.         }
  256.         return $data;
  257.     }
  258.     protected function getPathDirectory(): string
  259.     {
  260.         return $this->exportPath DIRECTORY_SEPARATOR 'contact_sheet' DIRECTORY_SEPARATOR;
  261.     }
  262. }