<?php
namespace App\Controller;
use App\Entity\Document;
use App\Entity\EntryModel;
use App\Entity\EntryModelLine;
use App\Entity\EntrySet;
use App\Entity\EntryType;
use App\Entity\Journal;
use App\Services\EwinficExport;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\Form\Type\DateRangeType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EntrySetAdminController extends CRUDController
{
private $em;
private $ewinficExport;
public function __construct(
EntityManagerInterface $em,
EwinficExport $ewinficExport
)
{
$this->em = $em;
$this->ewinficExport = $ewinficExport;
}
public function updateSaleDocumentAction(Request $request)
{
$entrySetRepository = $this->em->getRepository(EntrySet::class);
$entries = $this->em->getRepository(EntrySet::class)->findSaleEntrySets();
$documentRepository = $this->em->getRepository(Document::class);
$entryTypeRepository = $this->em->getRepository(EntryType::class);
/** @var EntryType $saleEntryType */
$saleEntryType = $entryTypeRepository->findOneBy(['code' => 'VE']);
$updated = 0;
/** @var EntrySet $entry */
foreach ($entries as $entry) {
$description = $entry->getDescription();
$number = explode(' - ', $description);
/** @var Document $document */
$document = $documentRepository->findOneBy(['number' => $number]);
if ($document) {
$existingEntryWithDocument = $entrySetRepository->findOneBy(['document' => $document]);
if ($existingEntryWithDocument)
continue;
$entry->setDocument($document);
$entry->setDocumentDate($document->getCreatedAt());
$entry->setEntryType($saleEntryType);
$this->em->persist($entry);
$this->em->flush();
$updated++;
}
}
$this->addFlash('success', $updated . ' entries updated');
return new RedirectResponse($this->generateUrl('admin_app_entryset_list'));
}
function exportXMLAction(Request $request)
{
$form = $this->createFormBuilder()
->add('date', DateRangeType::class)
->add('journal', ChoiceType::class, [
'choices' => [
'AC' => 'AC',
'BQ' => 'BQ',
'VE' => 'VE'
]
])
->add('submit', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$date = $form->getData()['date'];
$journal = $form->getData()['journal'];
return $this->ewinficExport->export($date['start'], $date['end'],$journal);
}
return $this->render('PriceMonitor/Export/xml_export.html.twig', ['form' => $form->createView()]);
}
function exportCSVAction(Request $request)
{
$entries = $this->em->getRepository(EntrySet::class)->findEntriesByDate();
$rows = [];
$rows[] = implode(',', [
'DATE SAISIE',
'DATE MISE A JOUR SAISIE',
'ID',
'JOURNAL',
'DATE',
'NUMERO DE COMPTE',
'LIBELLE DE L\'ECRITURE',
'MONTANT DEBIT',
'MONTANT CREDIT',
'NUMERO DE PIECE',
'MONTANT ORIGINAL',
'DEVISE ORIGINALE',
'URL PIECE',
'ID RECONCILIATION'
]);
/** @var EntrySet $entry */
foreach ($entries as $entry) {
$lines = $entry->getJournalEntries();
if (!empty($lines)) {
if (!empty($entry->getUploadedDocument())) {
$documentNumber = $entry->getUploadedDocument()->getId();
$provider = $this->get('sonata.media.provider.file');
$format = $provider->getFormatName($entry->getUploadedDocument(), 'reference');
$documentUrl = $request->getSchemeAndHttpHost() . $provider->generatePublicUrl($entry->getUploadedDocument(), $format);
} elseif (!empty($entry->getDocument())) {
$documentNumber = $entry->getDocument()->getNumber();
$documentUrl = $request->getSchemeAndHttpHost() . $this->generateUrl(
'download_invoice_public_url',
['number' => $entry->getDocument()->getNumber(),
'id' => $entry->getDocument()->getId(),
]);
} else {
$documentNumber = "";
$documentUrl = "";
}
/** @var Journal $line */
foreach ($lines as $line) {
$accountNumber = $line->getAccount()->getNumber();
if ('41100000' === $accountNumber) {
if (!empty($entry->getDocument())) {
$customerId = $entry->getDocument()->getShopOrder()->getCustomer()->getId();
$accountNumber = '411' . str_pad((string)$customerId, 5, "0", STR_PAD_RIGHT);
}
}
if (!empty($line->getReconciliation())) {
$reconciliationId = $line->getReconciliation()->getId();
} else {
$reconciliationId = "";
}
$entryType = !empty($entry->getEntryType()) ? $entry->getEntryType()->getName() : '';
$data = [
$entry->getCreatedAt()->format('Y-m-d'),
$entry->getUpdatedAt()->format('Y-m-d'),
$entry->getId(),
$entryType,
$line->getDate()->format('d/m/Y'),
$accountNumber,
preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $line->getDescription()),
$line->getType() === 'D' ? $line->getAmountEUR() : "",
$line->getType() === 'C' ? $line->getAmountEUR() : "",
$entry->getDocumentNumber(),
$line->getAmount(),
$line->getCurrency(),
$documentUrl,
$reconciliationId
];
$rows[] = implode(',', $data);
}
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
$time = (new \DateTime("now", new \DateTimeZone("Europe/Paris")))->format('Y-m-d H:i:s');
$response->headers->set('Content-Disposition', 'filename="export' . $time . '.csv"');
return $response;
}
public function createModelAction($id)
{
/** @var EntrySet $object */
$object = $this->admin->getSubject();
if (!$object) {
throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
}
$entryModel = new EntryModel();
$entryModel->setName($object->getDescription());
$entryModel->setEntryType($object->getEntryType());
/** @var Journal $item */
foreach ($object->getJournalEntries() as $item) {
$entryModelLine = new EntryModelLine();
$entryModelLine->setCurrency($item->getCurrency());
$entryModelLine->setType($item->getType());
$entryModelLine->setAccount($item->getAccount());
$entryModelLine->setAmount($item->getAmount());
$entryModel->addItem($entryModelLine);
}
$this->em->persist($entryModel);
$this->em->flush();
return new RedirectResponse($this->generateUrl('admin_app_entrymodel_edit', ['id' => $entryModel->getId()]));
}
}