1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the vseth-musikzimmer-pay project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Controller\Administration; |
13
|
|
|
|
14
|
|
|
use App\Controller\Administration\Base\BaseController; |
15
|
|
|
use App\Entity\PaymentRemainder; |
16
|
|
|
use App\Entity\Reservation; |
17
|
|
|
use App\Entity\User; |
18
|
|
|
use App\Enum\PaymentRemainderStatusType; |
19
|
|
|
use App\Enum\RoomType; |
20
|
|
|
use App\Enum\UserCategoryType; |
21
|
|
|
use App\Model\Breadcrumb; |
22
|
|
|
use App\Service\Interfaces\UserPaymentServiceInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
25
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
26
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Route("/payment_remainder") |
30
|
|
|
*/ |
31
|
|
|
class PaymentRemainderController extends BaseController |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @Route("/new", name="administration_payment_remainder_new") |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
* |
38
|
|
|
* @return Response |
39
|
|
|
*/ |
40
|
|
|
public function newAction(Request $request, TranslatorInterface $translator) |
41
|
|
|
{ |
42
|
|
|
$paymentRemainder = new PaymentRemainder(); |
43
|
|
|
$paymentRemainder->setName($translator->trans('default.name', [], 'entity_payment_remainder')); |
44
|
|
|
$paymentRemainder->setSubject($translator->trans('default.subject', [], 'entity_payment_remainder')); |
45
|
|
|
$paymentRemainder->setBody($translator->trans('default.body', ['support_email' => $this->getParameter('REPLY_EMAIL')], 'entity_payment_remainder')); |
46
|
|
|
|
47
|
|
|
$paymentRemainder->setFee(0); |
48
|
|
|
$paymentRemainder->setDueAt((new \DateTime('today'))->add(new \DateInterval('P1M1D'))); |
49
|
|
|
|
50
|
|
|
//process form |
51
|
|
|
$saved = false; |
52
|
|
|
$myForm = $this->handleCreateForm( |
53
|
|
|
$request, |
54
|
|
|
$paymentRemainder, |
55
|
|
|
function () use ($paymentRemainder, $translator, &$saved) { |
56
|
|
|
if (!$this->ensureValidPaymentRemainder($paymentRemainder, $translator)) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$saved = true; |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
); |
65
|
|
|
if ($myForm instanceof Response) { |
66
|
|
|
return $myForm; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($saved) { |
70
|
|
|
return $this->redirectToRoute('administration'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->render('administration/payment_remainder/new.html.twig', ['form' => $myForm->createView()]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Route("/{paymentRemainder}/edit", name="administration_payment_remainder_edit") |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function editAction(Request $request, PaymentRemainder $paymentRemainder, TranslatorInterface $translator) |
82
|
|
|
{ |
83
|
|
|
//process form |
84
|
|
|
$myForm = $this->handleUpdateForm($request, $paymentRemainder, function () use ($paymentRemainder, $translator) { |
85
|
|
|
return $this->ensureValidPaymentRemainder($paymentRemainder, $translator); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
if ($myForm instanceof Response) { |
89
|
|
|
return $myForm; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->render('administration/payment_remainder/edit.html.twig', ['form' => $myForm->createView()]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @Route("/send_test", name="administration_payment_remainder_send_test") |
97
|
|
|
* |
98
|
|
|
* @return Response |
99
|
|
|
*/ |
100
|
|
|
public function sendTestAction(TranslatorInterface $translator, UserPaymentServiceInterface $userPaymentService) |
101
|
|
|
{ |
102
|
|
|
$replyEmail = $this->getParameter('REPLY_EMAIL'); |
103
|
|
|
$testUser = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $replyEmail]); |
104
|
|
|
if ($testUser === null) { |
105
|
|
|
$testUser = $this->createTestUser($replyEmail); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// close or remove active invoice |
109
|
|
|
if ($testUser->getInvoiceId() !== null && $testUser->getPaymentRemainderStatus() === PaymentRemainderStatusType::PAYMENT_STARTED) { |
110
|
|
|
$userPaymentService->closeInvoice($testUser); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// reset user |
114
|
|
|
$testUser->clearPaymentInfo(); |
115
|
|
|
$testUser->setPaymentRemainderStatus(PaymentRemainderStatusType::NONE); |
116
|
|
|
$this->fastSave($testUser); |
117
|
|
|
|
118
|
|
|
// send mail |
119
|
|
|
$userPaymentService->sendPaymentRemainder($testUser); |
120
|
|
|
|
121
|
|
|
$success = $translator->trans('send_test.successful', ['test_email' => $replyEmail], 'administration_payment_remainder'); |
122
|
|
|
$this->displaySuccess($success); |
123
|
|
|
|
124
|
|
|
return $this->redirectToRoute('administration'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function createTestUser(string $email): User |
128
|
|
|
{ |
129
|
|
|
$testUser = new User(); |
130
|
|
|
$testUser->setGivenName('first name (test)'); |
131
|
|
|
$testUser->setFamilyName('last name (test)'); |
132
|
|
|
$testUser->setPhone('phone (test)'); |
133
|
|
|
$testUser->setEmail($email); |
134
|
|
|
$testUser->setAddress("steet (test)\ncity (test)"); |
135
|
|
|
$testUser->setCategory(UserCategoryType::STUDENT); |
136
|
|
|
$testUser->setLastPayedPeriodicFeeEnd(null); |
137
|
|
|
$testUser->setDiscount(0); |
138
|
|
|
$testUser->setAmountOwed(27); |
139
|
|
|
$testUser->generateAuthenticationCode(); |
140
|
|
|
|
141
|
|
|
$testReservation = new Reservation(); |
142
|
|
|
$testReservation->setStart(new \DateTime('01.01.2020 03:00')); |
143
|
|
|
$testReservation->setEnd(new \DateTime('01.01.2020 04:00')); |
144
|
|
|
$testReservation->setCreatedAt(new \DateTime('01.01.2020 00:00')); |
145
|
|
|
$testReservation->setModifiedAt(new \DateTime('01.01.2020 00:00')); |
146
|
|
|
$testReservation->setUser($testUser); |
147
|
|
|
$testReservation->setRoom(RoomType::UNAUTHORIZED); |
148
|
|
|
|
149
|
|
|
$testReservation2 = new Reservation(); |
150
|
|
|
$testReservation2->setStart(new \DateTime('02.01.2020 03:00')); |
151
|
|
|
$testReservation2->setEnd(new \DateTime('02.01.2020 04:00')); |
152
|
|
|
$testReservation2->setCreatedAt(new \DateTime('02.01.2020 00:00')); |
153
|
|
|
$testReservation2->setModifiedAt(new \DateTime('02.01.2020 00:00')); |
154
|
|
|
$testReservation2->setUser($testUser); |
155
|
|
|
$testReservation2->setRoom(RoomType::UNAUTHORIZED); |
156
|
|
|
|
157
|
|
|
$this->fastSave($testUser, $testReservation, $testReservation2); |
158
|
|
|
|
159
|
|
|
return $testUser; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @Route("/send", name="administration_payment_remainder_send") |
164
|
|
|
* |
165
|
|
|
* @return Response |
166
|
|
|
*/ |
167
|
|
|
public function sendAction(TranslatorInterface $translator, UserPaymentServiceInterface $userPaymentService) |
168
|
|
|
{ |
169
|
|
|
$paymentRemainder = $this->getDoctrine()->getRepository(PaymentRemainder::class)->findActive(); |
170
|
|
|
if ($paymentRemainder->isSentToAll()) { |
171
|
|
|
return $this->redirectToRoute('administration'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$notPayedUsers = $this->getDoctrine()->getRepository(User::class)->findByNotPayed(); |
175
|
|
|
foreach ($notPayedUsers as $notPayedUser) { |
176
|
|
|
if ($notPayedUser->getPaymentRemainder() === $paymentRemainder) { |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// close active invoice |
181
|
|
|
if ($notPayedUser->getInvoiceId() !== null) { |
182
|
|
|
$userPaymentService->closeInvoice($notPayedUser); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// do not send mail to admins/service |
186
|
|
|
if ($notPayedUser->getCategory() === UserCategoryType::ADMIN || $notPayedUser->getCategory() === UserCategoryType::SERVICE) { |
187
|
|
|
continue; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// send mail |
191
|
|
|
$userPaymentService->sendPaymentRemainder($notPayedUser); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$paymentRemainder->setSentToAll(true); |
195
|
|
|
$this->fastSave($paymentRemainder); |
196
|
|
|
|
197
|
|
|
$success = $translator->trans('send.successful', [], 'administration_payment_remainder'); |
198
|
|
|
$this->displaySuccess($success); |
199
|
|
|
|
200
|
|
|
return $this->redirectToRoute('administration'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
private function ensureValidPaymentRemainder(PaymentRemainder $paymentRemainder, TranslatorInterface $translator) |
207
|
|
|
{ |
208
|
|
|
if (mb_strrpos($paymentRemainder->getBody(), '(url)') === false) { |
209
|
|
|
$error = $translator->trans('new.error.missing_url', [], 'administration_payment_remainder'); |
210
|
|
|
$this->displayError($error); |
211
|
|
|
|
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* get the breadcrumbs leading to this controller. |
220
|
|
|
* |
221
|
|
|
* @return Breadcrumb[] |
222
|
|
|
*/ |
223
|
|
|
protected function getIndexBreadcrumbs() |
224
|
|
|
{ |
225
|
|
|
return array_merge(parent::getIndexBreadcrumbs(), [ |
226
|
|
|
new Breadcrumb( |
227
|
|
|
$this->generateUrl('administration'), |
228
|
|
|
$this->getTranslator()->trans('index.title', [], 'administration') |
229
|
|
|
), |
230
|
|
|
]); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|