src/Controller/EntrySetAdminController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Document;
  4. use App\Entity\EntryModel;
  5. use App\Entity\EntryModelLine;
  6. use App\Entity\EntrySet;
  7. use App\Entity\EntryType;
  8. use App\Entity\Journal;
  9. use App\Services\EwinficExport;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Sonata\AdminBundle\Controller\CRUDController;
  12. use Sonata\Form\Type\DateRangeType;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. class EntrySetAdminController extends CRUDController
  20. {
  21.     private $em;
  22.     private $ewinficExport;
  23.     public function __construct(
  24.         EntityManagerInterface $em,
  25.         EwinficExport          $ewinficExport
  26.     )
  27.     {
  28.         $this->em $em;
  29.         $this->ewinficExport $ewinficExport;
  30.     }
  31.     public function updateSaleDocumentAction(Request $request)
  32.     {
  33.         $entrySetRepository $this->em->getRepository(EntrySet::class);
  34.         $entries $this->em->getRepository(EntrySet::class)->findSaleEntrySets();
  35.         $documentRepository $this->em->getRepository(Document::class);
  36.         $entryTypeRepository $this->em->getRepository(EntryType::class);
  37.         /** @var EntryType $saleEntryType */
  38.         $saleEntryType $entryTypeRepository->findOneBy(['code' => 'VE']);
  39.         $updated 0;
  40.         /** @var EntrySet $entry */
  41.         foreach ($entries as $entry) {
  42.             $description $entry->getDescription();
  43.             $number explode(' - '$description);
  44.             /** @var Document $document */
  45.             $document $documentRepository->findOneBy(['number' => $number]);
  46.             if ($document) {
  47.                 $existingEntryWithDocument $entrySetRepository->findOneBy(['document' => $document]);
  48.                 if ($existingEntryWithDocument)
  49.                     continue;
  50.                 $entry->setDocument($document);
  51.                 $entry->setDocumentDate($document->getCreatedAt());
  52.                 $entry->setEntryType($saleEntryType);
  53.                 $this->em->persist($entry);
  54.                 $this->em->flush();
  55.                 $updated++;
  56.             }
  57.         }
  58.         $this->addFlash('success'$updated ' entries updated');
  59.         return new RedirectResponse($this->generateUrl('admin_app_entryset_list'));
  60.     }
  61.     function exportXMLAction(Request $request)
  62.     {
  63.         $form $this->createFormBuilder()
  64.             ->add('date'DateRangeType::class)
  65.             ->add('journal'ChoiceType::class, [
  66.                 'choices' => [
  67.                     'AC' => 'AC',
  68.                     'BQ' => 'BQ',
  69.                     'VE' => 'VE'
  70.                 ]
  71.             ])
  72.             ->add('submit'SubmitType::class)
  73.             ->getForm();
  74.         $form->handleRequest($request);
  75.         if ($form->isSubmitted() && $form->isValid()) {
  76.             $date $form->getData()['date'];
  77.             $journal $form->getData()['journal'];
  78.             return $this->ewinficExport->export($date['start'], $date['end'],$journal);
  79.         }
  80.         return $this->render('PriceMonitor/Export/xml_export.html.twig', ['form' => $form->createView()]);
  81.     }
  82.     function exportCSVAction(Request $request)
  83.     {
  84.         $entries $this->em->getRepository(EntrySet::class)->findEntriesByDate();
  85.         $rows = [];
  86.         $rows[] = implode(',', [
  87.             'DATE SAISIE',
  88.             'DATE MISE A JOUR SAISIE',
  89.             'ID',
  90.             'JOURNAL',
  91.             'DATE',
  92.             'NUMERO DE COMPTE',
  93.             'LIBELLE DE L\'ECRITURE',
  94.             'MONTANT DEBIT',
  95.             'MONTANT CREDIT',
  96.             'NUMERO DE PIECE',
  97.             'MONTANT ORIGINAL',
  98.             'DEVISE ORIGINALE',
  99.             'URL PIECE',
  100.             'ID RECONCILIATION'
  101.         ]);
  102.         /** @var EntrySet $entry */
  103.         foreach ($entries as $entry) {
  104.             $lines $entry->getJournalEntries();
  105.             if (!empty($lines)) {
  106.                 if (!empty($entry->getUploadedDocument())) {
  107.                     $documentNumber $entry->getUploadedDocument()->getId();
  108.                     $provider $this->get('sonata.media.provider.file');
  109.                     $format $provider->getFormatName($entry->getUploadedDocument(), 'reference');
  110.                     $documentUrl $request->getSchemeAndHttpHost() . $provider->generatePublicUrl($entry->getUploadedDocument(), $format);
  111.                 } elseif (!empty($entry->getDocument())) {
  112.                     $documentNumber $entry->getDocument()->getNumber();
  113.                     $documentUrl $request->getSchemeAndHttpHost() . $this->generateUrl(
  114.                             'download_invoice_public_url',
  115.                             ['number' => $entry->getDocument()->getNumber(),
  116.                                 'id' => $entry->getDocument()->getId(),
  117.                             ]);
  118.                 } else {
  119.                     $documentNumber "";
  120.                     $documentUrl "";
  121.                 }
  122.                 /** @var Journal $line */
  123.                 foreach ($lines as $line) {
  124.                     $accountNumber $line->getAccount()->getNumber();
  125.                     if ('41100000' === $accountNumber) {
  126.                         if (!empty($entry->getDocument())) {
  127.                             $customerId $entry->getDocument()->getShopOrder()->getCustomer()->getId();
  128.                             $accountNumber '411' str_pad((string)$customerId5"0"STR_PAD_RIGHT);
  129.                         }
  130.                     }
  131.                     if (!empty($line->getReconciliation())) {
  132.                         $reconciliationId $line->getReconciliation()->getId();
  133.                     } else {
  134.                         $reconciliationId "";
  135.                     }
  136.                     $entryType = !empty($entry->getEntryType()) ? $entry->getEntryType()->getName() : '';
  137.                     $data = [
  138.                         $entry->getCreatedAt()->format('Y-m-d'),
  139.                         $entry->getUpdatedAt()->format('Y-m-d'),
  140.                         $entry->getId(),
  141.                         $entryType,
  142.                         $line->getDate()->format('d/m/Y'),
  143.                         $accountNumber,
  144.                         preg_replace('/[\x00-\x1F\x7F-\xFF]/'''$line->getDescription()),
  145.                         $line->getType() === 'D' $line->getAmountEUR() : "",
  146.                         $line->getType() === 'C' $line->getAmountEUR() : "",
  147.                         $entry->getDocumentNumber(),
  148.                         $line->getAmount(),
  149.                         $line->getCurrency(),
  150.                         $documentUrl,
  151.                         $reconciliationId
  152.                     ];
  153.                     $rows[] = implode(','$data);
  154.                 }
  155.             }
  156.         }
  157.         $content implode("\n"$rows);
  158.         $response = new Response($content);
  159.         $response->headers->set('Content-Type''text/csv');
  160.         $time = (new \DateTime("now", new \DateTimeZone("Europe/Paris")))->format('Y-m-d H:i:s');
  161.         $response->headers->set('Content-Disposition''filename="export' $time '.csv"');
  162.         return $response;
  163.     }
  164.     public function createModelAction($id)
  165.     {
  166.         /** @var EntrySet $object */
  167.         $object $this->admin->getSubject();
  168.         if (!$object) {
  169.             throw new NotFoundHttpException(sprintf('unable to find the object with id: %s'$id));
  170.         }
  171.         $entryModel = new EntryModel();
  172.         $entryModel->setName($object->getDescription());
  173.         $entryModel->setEntryType($object->getEntryType());
  174.         /** @var Journal $item */
  175.         foreach ($object->getJournalEntries() as $item) {
  176.             $entryModelLine = new EntryModelLine();
  177.             $entryModelLine->setCurrency($item->getCurrency());
  178.             $entryModelLine->setType($item->getType());
  179.             $entryModelLine->setAccount($item->getAccount());
  180.             $entryModelLine->setAmount($item->getAmount());
  181.             $entryModel->addItem($entryModelLine);
  182.         }
  183.         $this->em->persist($entryModel);
  184.         $this->em->flush();
  185.         return new RedirectResponse($this->generateUrl('admin_app_entrymodel_edit', ['id' => $entryModel->getId()]));
  186.     }
  187. }