src/Controller/Account/StatementController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Account;
  3. use App\Controller\BaseController;
  4. use App\RequestManager\Account\AccountRequestManager;
  5. use App\RequestManager\ReportingRequestManager;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use Paynetics\Exception\ApiException;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. class StatementController extends BaseController
  12. {
  13.     /**
  14.      * @required
  15.      */
  16.     public ReportingRequestManager $reportingRequestManager;
  17.     /**
  18.      * @required
  19.      */
  20.     public AccountRequestManager $accountRequestManager;
  21.     /**
  22.      * @throws ApiException
  23.      * @throws GuzzleException
  24.      */
  25.     public function status(Request $requeststring $token): Response
  26.     {
  27.         $status $this->reportingRequestManager->status(
  28.             $token,
  29.             $request->query->get('merchant'),
  30.             $request->query->get('user'),
  31.             $request->query->get('language'),
  32.             $request->query->get('balance'),
  33.             $request->query->get('iban'),
  34.             $request->query->get('sort_code'),
  35.             $request->query->get('account_number'),
  36.             $request->query->get('internal_number')
  37.         );
  38.         $response = new Response(base64_decode($status['file']));
  39.         $disposition $response->headers->makeDisposition(
  40.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  41.             'Account-Status-'.(new \DateTime())->format('Y-m-dTH:i:s').'.pdf'
  42.         );
  43.         // Set the content disposition
  44.         $response->headers->set('Content-Disposition'$disposition);
  45.         // Dispatch request
  46.         return $response;
  47.     }
  48.     public function statement(Request $requeststring $token): Response
  49.     {
  50.         $startDate $request->query->get('startDate', (new \DateTime())->modify('first day of this month')->format('Y-m-d'));
  51.         $selectedDate = new \DateTime($startDate);
  52.         $lastDayOfMonth = (clone $selectedDate)->modify('last day of this month');
  53.         $availableDate = (clone $lastDayOfMonth)->modify('+5 days');
  54.         $today = new \DateTime();
  55.         if ($today $availableDate) {
  56.             $this->addFlash('error''Monthly statement is available only 5 days after month-end. This statement will be available starting '.$availableDate->format('Y-m-d').'.');
  57.             return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  58.         }
  59.         $account $this->accountRequestManager->find($token);
  60.         if ($account && isset($account['created_at']) && $account['created_at']) {
  61.             $createdAt = (new \DateTime($account['created_at']))->format('Y-m-d');
  62.             if ($startDate $createdAt) {
  63.                 $this->addFlash('error''Cannot generate statement for a date older than the account creation date.');
  64.                 return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  65.             }
  66.         }
  67.         $status $this->reportingRequestManager->statement($token,
  68.             $request->query->get('merchant'),
  69.             $request->query->get('user'),
  70.             $request->query->get('balance'),
  71.             $request->query->get('format''pdf'),
  72.             $request->query->get('startDate', (new \DateTime())->modify('first day of this month')->format('Y-m-d H:i:s')),
  73.             $request->query->get('language''en')
  74.         );
  75.         $response = new Response(base64_decode($status['file']));
  76.         $disposition $response->headers->makeDisposition(
  77.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  78.             'Account-Statement-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.$status['file_type']
  79.         );
  80.         // Set the content disposition
  81.         $response->headers->set('Content-Disposition'$disposition);
  82.         // Dispatch request
  83.         return $response;
  84.     }
  85.     /**
  86.      * @throws ApiException
  87.      * @throws GuzzleException
  88.      * @throws \Exception
  89.      */
  90.     public function movements(Request $requeststring $token): Response
  91.     {
  92.         $startDate $request->query->get('startDate');
  93.         $endDate $request->query->get('endDate');
  94.         $today = (new \DateTime())->format('Y-m-d');
  95.         if (($startDate && $startDate $today) || ($endDate && $endDate $today)) {
  96.             $this->addFlash('error''The selected period cannot be in the future.');
  97.             return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  98.         }
  99.         if ($startDate && $endDate) {
  100.             $start = new \DateTime($startDate);
  101.             $end = new \DateTime($endDate);
  102.             $interval $start->diff($end);
  103.             // Check if interval is more than 3 months
  104.             if ($interval->|| $interval->|| (== $interval->&& $interval->0)) {
  105.                 $this->addFlash('error''The selected period length should not exceed 3 months.');
  106.                 return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  107.             }
  108.         }
  109.         try {
  110.             $status $this->reportingRequestManager->movements(
  111.                 $token,
  112.                 $request->query->get('format''excel'),
  113.                 $request->query->get('startDate', (new \DateTime())->format('Y-m-d H:i:s')),
  114.                 $request->query->get('endDate', (new \DateTime())->format('Y-m-d H:i:s')),
  115.                 $request->query->get('balance'),
  116.                 $request->query->get('language''en'),
  117.                 $request->query->get('merchant'),
  118.                 $request->query->get('user'),
  119.             );
  120.             $response = new Response(base64_decode($status['file']));
  121.             $disposition $response->headers->makeDisposition(
  122.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  123.                 'Account-Movement-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.$status['file_type']
  124.             );
  125.             $response->headers->set('Content-Disposition'$disposition);
  126.             return $response;
  127.         } catch (\Exception $e) {
  128.             $this->addFlash('error''An error occurred while generating the movement statement: '.$e->getMessage());
  129.             return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  130.         }
  131.     }
  132.     public function balanceConfirmation(Request $requeststring $token)
  133.     {
  134.         try {
  135.             $account $this->accountRequestManager->find($token);
  136.             $startDate $request->query->get('startDate');
  137.             $today = (new \DateTime())->format('Y-m-d');
  138.             if ($startDate >= $today) {
  139.                 $this->addFlash('error''Balance confirmation can only be generated for previous dates.');
  140.                 return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  141.             }
  142.             if ($account && isset($account['created_at']) && $account['created_at']) {
  143.                 $createdAt = (new \DateTime($account['created_at']))->format('Y-m-d');
  144.                 if ($startDate $createdAt) {
  145.                     $this->addFlash('error''Cannot generate statement for a date older than the account creation date.');
  146.                     return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  147.                 }
  148.             }
  149.             $balance $this->reportingRequestManager->balance(
  150.                 $token,
  151.                 $request->query->get('merchant'),
  152.                 $request->query->get('user'),
  153.                 $request->query->get('format''excel'),
  154.                 $request->query->get('date', (new \DateTime())->format('Y-m-d H:i:s')),
  155.                 $request->query->get('balance'),
  156.                 $request->query->get('language''en'),
  157.             );
  158.             if (!isset($balance['file'])) {
  159.                 $this->addFlash('error''Balance confirmation file not found in response.');
  160.                 return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  161.             }
  162.             $response = new Response(base64_decode($balance['file']));
  163.             $filename 'Balance-Confirmation-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.($balance['file_type'] ?? 'pdf');
  164.             $disposition $response->headers->makeDisposition(
  165.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  166.                 $filename
  167.             );
  168.             $response->headers->set('Content-Disposition'$disposition);
  169.             return $response;
  170.         } catch (\Exception $e) {
  171.             $this->addFlash('error''An error occurred while generating balance confirmation: '.$e->getMessage());
  172.             return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
  173.         }
  174.     }
  175.     /**
  176.      * Generate confirmation statement for a specific transaction token.
  177.      */
  178.     public function transactionConfirmation(Request $requeststring $token): Response
  179.     {
  180.         $result $this->reportingRequestManager->transactionConfirmation(
  181.             $token,
  182.             $request->query->get('merchant'),
  183.             $request->query->get('user'),
  184.             $request->query->get('account'),
  185.             $request->query->get('language''en'),
  186.             $request->query->get('format''pdf'),
  187.         );
  188.         $response = new Response(base64_decode($result['file']));
  189.         $filename 'Transaction-Confirmation-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.($result['file_type'] ?? 'pdf');
  190.         $disposition $response->headers->makeDisposition(
  191.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  192.             $filename
  193.         );
  194.         $response->headers->set('Content-Disposition'$disposition);
  195.         return $response;
  196.     }
  197. }