|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Transaction; |
|
6
|
|
|
use App\Service\AlmaApi; |
|
7
|
|
|
use App\Service\AlmaUserData; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* List the Alma fees for a particular user. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ListFeesController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
private $api; |
|
18
|
|
|
private $userData; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(AlmaApi $api, AlmaUserData $userData) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->api = $api; |
|
23
|
|
|
$this->userData = $userData; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @Route("/", name="index") |
|
28
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
29
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function index() |
|
32
|
|
|
{ |
|
33
|
|
|
$user = $this->getUser(); |
|
34
|
|
|
$transactionToNotify = $this->processTransactions(); |
|
35
|
|
|
|
|
36
|
|
|
$totalDue = 0.0; |
|
37
|
|
|
$userFees = $this->userData->listFees($this->api->getUserFees($user->getUsername())); |
|
38
|
|
|
foreach ($userFees as $userFee) { |
|
39
|
|
|
$totalDue += $userFee['balance']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->render('views/index.html.twig', [ |
|
43
|
|
|
'full_name' => $user->getFullName(), |
|
44
|
|
|
'user_fees' => $userFees, |
|
45
|
|
|
'total_Due' => $totalDue, |
|
46
|
|
|
'transaction' => $transactionToNotify |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Remove user's pending transactions and return the latest transaction if it has not been notified. |
|
52
|
|
|
* @return Transaction|null Return null if the latest transaction has been notified or in PENDING status. |
|
53
|
|
|
*/ |
|
54
|
|
|
private function processTransactions() |
|
55
|
|
|
{ |
|
56
|
|
|
$userId = $this->getUser()->getUsername(); |
|
57
|
|
|
$repository = $this->getDoctrine()->getRepository(Transaction::class); |
|
58
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
59
|
|
|
|
|
60
|
|
|
$transactions = $repository->findBy([ |
|
61
|
|
|
'user_id' => $userId, |
|
62
|
|
|
'status' => Transaction::STATUS_PENDING |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($transactions as $transaction) { |
|
66
|
|
|
$entityManager->remove($transaction); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$latestTransaction = $repository->findBy(['user_id' => $userId], ['date' => 'DESC'], 1); |
|
70
|
|
|
if (empty($latestTransaction) or $latestTransaction[0]->getNotified() or ($latestTransaction[0]->getStatus() === Transaction::STATUS_PENDING)) { |
|
71
|
|
|
$entityManager->flush(); |
|
72
|
|
|
return null; |
|
73
|
|
|
} else { |
|
74
|
|
|
$latestTransaction[0]->setNotified(true); |
|
75
|
|
|
$entityManager->persist($latestTransaction[0]); |
|
76
|
|
|
$entityManager->flush(); |
|
77
|
|
|
return $latestTransaction[0]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|