1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace App\Controller;
use App\Entity\Adherent;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use App\Entity\GlobalParameter;
use App\Entity\Payment;
use App\Entity\Prestataire;
use App\Entity\User;
use App\Enum\MoyenEnum;
use App\Form\Type\CotiserFormType;
use App\Form\Type\DonAdherentFormType;
use App\Form\Type\DonPrestataireFormType;
use App\Form\Type\UserInfosFormType;
use App\Utils\CotisationUtils;
use App\Utils\OperationUtils;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatorInterface;
class UserController extends AbstractController
{
private $em;
private $translator;
private $security;
private $operationUtils;
private $cotisationUtils;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, Security $security, OperationUtils $operationUtils, CotisationUtils $cotisationUtils)
{
$this->em = $em;
$this->translator = $translator;
$this->security = $security;
$this->operationUtils = $operationUtils;
$this->cotisationUtils = $cotisationUtils;
}
/**
* @Route("/cotiser", name="cotiser", defaults={"type": "null"})
* @Route("/cotiser/next", name="cotisernextyear", defaults={"type": "nextyear"})
* @IsGranted("ROLE_USER")
*/
public function cotiserAction($type, Request $request)
{
$options = [];
$payment_type = '';
if ($this->security->isGranted('ROLE_ADHERENT')) {
$options['data_class'] = CotisationAdherent::class;
$options['don_class'] = DonAdherentFormType::class;
$payment_type = Payment::TYPE_COTISATION_ADHERENT;
} elseif ($this->security->isGranted('ROLE_PRESTATAIRE')) {
$options['data_class'] = CotisationPrestataire::class;
$options['don_class'] = DonPrestataireFormType::class;
$payment_type = Payment::TYPE_COTISATION_PRESTA;
}
$form = $this->createForm(CotiserFormType::class, null, $options);
$form->handleRequest($request);
if ('nextyear' === $type) {
$nextYear = new \DateTime('+1 year');
$kohYear = (string) $nextYear->format('Y');
} else {
$now = new \DateTime();
$kohYear = (string) $now->format('Y');
}
if ($form->isSubmitted()) {
$cotisation = $form->getData();
if ($form->isValid()) {
//Manage don if null => delete it !
if ($cotisation->getDon() && 0 == $cotisation->getDon()->getMontant()) {
$cotisation->setDon(null);
}
if ('nextyear' === $type) {
// Cotiser pour l'année suivante
$nextYear = new \DateTime('+1 year');
$startDate = $this->cotisationUtils->isCotisationValid($this->getUser());
$cotisation->getCotisationInfos()->setAnnee((string) $nextYear->format('Y'));
$cotisation->getCotisationInfos()->setDebut($startDate);
$endDate = new \DateTime(date('Y-m-d H:i:s', strtotime('+1 year', strtotime($startDate->format('Y-m-d H:i:s')))));
$cotisation->getCotisationInfos()->setFin($endDate);
}
if (MoyenEnum::MOYEN_EMLC == $cotisation->getMoyen()) {
try {
$cotisation->setRecu(true);
$this->operationUtils->executeOperations($cotisation);
$this->addFlash(
'success',
// "Cotisation pour l'année {$cotisation->getCotisationInfos()->getAnnee()} bien reçue. Merci !"
$this->translator->trans('Cotisation bien reçue. Merci !')
);
} catch (\Exception $e) {
$this->addFlash(
'error',
$this->translator->trans('Problème avec la cotisation !') . ' ' . $e->getMessage()
);
}
return $this->redirectToRoute('index');
} elseif (MoyenEnum::MOYEN_HELLOASSO == $cotisation->getMoyen()) {
if ($this->security->isGranted('ROLE_ADHERENT')) {
$url = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::HELLOASSO_URL_COTISATION_ADHERENT);
} else {
$url = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::HELLOASSO_URL_COTISATION_PRESTATAIRE);
}
return $this->redirect($url);
} elseif (MoyenEnum::MOYEN_CB == $cotisation->getMoyen()) {
// Redirect to payment page
return $this->forward('App\Controller\FluxController::preparePaymentAction', [
'form' => $form,
'type' => $payment_type,
]);
}
} else {
$this->addFlash(
'error',
$this->translator->trans('Problème avec la cotisation !') . ' ' . $form->getErrors()
);
}
}
return $this->render('@kohinos/cotiser.html.twig', [
'form' => $form->createView(),
'koh_year' => $kohYear,
]);
}
/**
* @Route("/userinfos", name="user_infos")
* @IsGranted("ROLE_USER")
*/
public function userInfosAction(Request $request)
{
$form = $this->createForm(UserInfosFormType::class, $this->getUser());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($form->getData());
$this->em->flush();
$this->addFlash(
'success',
$this->translator->trans("Infos de l'utilisateur modifiée !")
);
$referer = $request->headers->get('referer');
if ($referer && !$request->isXmlHttpRequest()) {
return $this->redirect($referer);
} elseif (!$request->isXmlHttpRequest()) {
return new Response('', Response::HTTP_BAD_REQUEST);
}
}
return $this->redirectToRoute('index');
// return $this->render('@kohinos/presta/infos.html.twig', array(
// 'form' => $form->createView()
// ));
}
/**
* @Route("/account/user", name="myaccount")
* @IsGranted("ROLE_USER")
*/
public function myaccountAction(Request $request)
{
return $this->render('@kohinos/common/myaccount.html.twig', [
]);
}
/**
* @Route("/user/achatMonnaieAConfirmer", name="achatMonnaieAConfirmer")
* @IsGranted({"ROLE_ADHERENT", "ROLE_PRESTATAIRE"})
*/
public function achatMonnaieAConfirmerAction(Request $request)
{
$obj = $this->operationUtils->getCurrentAccountable($this->security->getUser());
if ($obj instanceof Prestataire) {
return $this->redirectToRoute('achatMonnaieAConfirmerPrestataire');
} elseif ($obj instanceof Adherent) {
return $this->redirectToRoute('achatMonnaieAConfirmerAdherent');
}
return $this->redirectToRoute('index');
}
/**
* @Route("/user/listachatMonnaieAConfirmer", name="listachatMonnaieAConfirmer")
* @IsGranted({"ROLE_ADHERENT", "ROLE_PRESTATAIRE"})
*/
public function listachatMonnaieAConfirmerAction(Request $request)
{
$obj = $this->operationUtils->getCurrentAccountable($this->security->getUser());
if ($obj instanceof Prestataire) {
return $this->redirectToRoute('listachatMonnaieAConfirmerPrestataire');
} elseif ($obj instanceof Adherent) {
return $this->redirectToRoute('listachatMonnaieAConfirmerAdherent');
}
return $this->redirectToRoute('index');
}
}