<?php
namespace App\Controller\Account;
use App\Controller\BaseController;
use App\RequestManager\Account\AccountRequestManager;
use App\RequestManager\ReportingRequestManager;
use GuzzleHttp\Exception\GuzzleException;
use Paynetics\Exception\ApiException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class StatementController extends BaseController
{
/**
* @required
*/
public ReportingRequestManager $reportingRequestManager;
/**
* @required
*/
public AccountRequestManager $accountRequestManager;
/**
* @throws ApiException
* @throws GuzzleException
*/
public function status(Request $request, string $token): Response
{
$status = $this->reportingRequestManager->status(
$token,
$request->query->get('merchant'),
$request->query->get('user'),
$request->query->get('language'),
$request->query->get('balance'),
$request->query->get('iban'),
$request->query->get('sort_code'),
$request->query->get('account_number'),
$request->query->get('internal_number')
);
$response = new Response(base64_decode($status['file']));
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'Account-Status-'.(new \DateTime())->format('Y-m-dTH:i:s').'.pdf'
);
// Set the content disposition
$response->headers->set('Content-Disposition', $disposition);
// Dispatch request
return $response;
}
public function statement(Request $request, string $token): Response
{
$startDate = $request->query->get('startDate', (new \DateTime())->modify('first day of this month')->format('Y-m-d'));
$selectedDate = new \DateTime($startDate);
$lastDayOfMonth = (clone $selectedDate)->modify('last day of this month');
$availableDate = (clone $lastDayOfMonth)->modify('+5 days');
$today = new \DateTime();
if ($today < $availableDate) {
$this->addFlash('error', 'Monthly statement is available only 5 days after month-end. This statement will be available starting '.$availableDate->format('Y-m-d').'.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
$account = $this->accountRequestManager->find($token);
if ($account && isset($account['created_at']) && $account['created_at']) {
$createdAt = (new \DateTime($account['created_at']))->format('Y-m-d');
if ($startDate < $createdAt) {
$this->addFlash('error', 'Cannot generate statement for a date older than the account creation date.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
}
$status = $this->reportingRequestManager->statement($token,
$request->query->get('merchant'),
$request->query->get('user'),
$request->query->get('balance'),
$request->query->get('format', 'pdf'),
$request->query->get('startDate', (new \DateTime())->modify('first day of this month')->format('Y-m-d H:i:s')),
$request->query->get('language', 'en')
);
$response = new Response(base64_decode($status['file']));
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'Account-Statement-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.$status['file_type']
);
// Set the content disposition
$response->headers->set('Content-Disposition', $disposition);
// Dispatch request
return $response;
}
/**
* @throws ApiException
* @throws GuzzleException
* @throws \Exception
*/
public function movements(Request $request, string $token): Response
{
$startDate = $request->query->get('startDate');
$endDate = $request->query->get('endDate');
$today = (new \DateTime())->format('Y-m-d');
if (($startDate && $startDate > $today) || ($endDate && $endDate > $today)) {
$this->addFlash('error', 'The selected period cannot be in the future.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
if ($startDate && $endDate) {
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$interval = $start->diff($end);
// Check if interval is more than 3 months
if ($interval->m > 3 || $interval->y > 0 || (3 == $interval->m && $interval->d > 0)) {
$this->addFlash('error', 'The selected period length should not exceed 3 months.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
}
try {
$status = $this->reportingRequestManager->movements(
$token,
$request->query->get('format', 'excel'),
$request->query->get('startDate', (new \DateTime())->format('Y-m-d H:i:s')),
$request->query->get('endDate', (new \DateTime())->format('Y-m-d H:i:s')),
$request->query->get('balance'),
$request->query->get('language', 'en'),
$request->query->get('merchant'),
$request->query->get('user'),
);
$response = new Response(base64_decode($status['file']));
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'Account-Movement-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.$status['file_type']
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
} catch (\Exception $e) {
$this->addFlash('error', 'An error occurred while generating the movement statement: '.$e->getMessage());
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
}
public function balanceConfirmation(Request $request, string $token)
{
try {
$account = $this->accountRequestManager->find($token);
$startDate = $request->query->get('startDate');
$today = (new \DateTime())->format('Y-m-d');
if ($startDate >= $today) {
$this->addFlash('error', 'Balance confirmation can only be generated for previous dates.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
if ($account && isset($account['created_at']) && $account['created_at']) {
$createdAt = (new \DateTime($account['created_at']))->format('Y-m-d');
if ($startDate < $createdAt) {
$this->addFlash('error', 'Cannot generate statement for a date older than the account creation date.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
}
$balance = $this->reportingRequestManager->balance(
$token,
$request->query->get('merchant'),
$request->query->get('user'),
$request->query->get('format', 'excel'),
$request->query->get('date', (new \DateTime())->format('Y-m-d H:i:s')),
$request->query->get('balance'),
$request->query->get('language', 'en'),
);
if (!isset($balance['file'])) {
$this->addFlash('error', 'Balance confirmation file not found in response.');
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
$response = new Response(base64_decode($balance['file']));
$filename = 'Balance-Confirmation-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.($balance['file_type'] ?? 'pdf');
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
} catch (\Exception $e) {
$this->addFlash('error', 'An error occurred while generating balance confirmation: '.$e->getMessage());
return $this->redirect($request->headers->get('referer') ?? $this->generateUrl('account_list'));
}
}
/**
* Generate confirmation statement for a specific transaction token.
*/
public function transactionConfirmation(Request $request, string $token): Response
{
$result = $this->reportingRequestManager->transactionConfirmation(
$token,
$request->query->get('merchant'),
$request->query->get('user'),
$request->query->get('account'),
$request->query->get('language', 'en'),
$request->query->get('format', 'pdf'),
);
$response = new Response(base64_decode($result['file']));
$filename = 'Transaction-Confirmation-'.(new \DateTime())->format('Y-m-dTH:i:s').'.'.($result['file_type'] ?? 'pdf');
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}