|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2016: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppBundle\Controller; |
|
22
|
|
|
|
|
23
|
|
|
use AppBundle\Entity\Expense; |
|
24
|
|
|
use AppBundle\Entity\User; |
|
25
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
28
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Route("/desplazamientos") |
|
32
|
|
|
* @Security("is_granted('ROLE_EDUCATIONAL_TUTOR') or is_granted('ROLE_FINANCIAL_MANAGER')") |
|
33
|
|
|
*/ |
|
34
|
|
|
class ExpenseController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @Route("", name="expense_tutor_index", methods={"GET"}) |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function teacherIndexAction() |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
/** @var User $user */ |
|
42
|
|
|
$user = $this->getUser(); |
|
43
|
|
|
if ($this->isGranted('ROLE_FINANCIAL_MANAGER')) { |
|
44
|
|
|
$expenses = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->getEducationalTutorsExpenseSummary(); |
|
45
|
|
|
} elseif ($this->isGranted('ROLE_DEPARTMENT_HEAD')) { |
|
46
|
|
|
$expenses = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->getEducationalTutorsByDepartmentsExpenseSummary($user->getDirects()); |
|
47
|
|
|
} else { |
|
48
|
|
|
return $this->expenseIndexAction($user); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->render('expense/tutor_index.html.twig', [ |
|
52
|
|
|
'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('expense_tutor_index'), |
|
53
|
|
|
'title' => null, |
|
54
|
|
|
'elements' => $expenses |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Route("/{id}", name="expense_index", methods={"GET"}) |
|
60
|
|
|
* @Security("is_granted('ROLE_FINANCIAL_MANAGER') or is_granted('USER_VISIT_TRACK', tutor)") |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function expenseIndexAction(User $tutor) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$workcenters = $this->getDoctrine()->getManager()->getRepository('AppBundle:Expense')->getRelatedExpenses($tutor); |
|
65
|
|
|
|
|
66
|
|
|
$title = (string) $tutor; |
|
67
|
|
|
|
|
68
|
|
|
return $this->render('expense/expense_index.html.twig', [ |
|
69
|
|
|
'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('expense_tutor_index'), |
|
70
|
|
|
'breadcrumb' => [ |
|
71
|
|
|
['fixed' => $title] |
|
72
|
|
|
], |
|
73
|
|
|
'title' => $this->get('translator')->trans('browse.expenses', ['%user%' => $title], 'expense'), |
|
74
|
|
|
'tutor' => $tutor, |
|
75
|
|
|
'elements' => $workcenters, |
|
76
|
|
|
'back_route_name' => ($this->isGranted('ROLE_DEPARTMENT_HEAD') || $this->isGranted('ROLE_FINANCIAL_MANAGER')) ? 'expense_tutor_index' : 'frontpage' |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @Route("/{id}/modificar/{expense}", name="expense_form", methods={"GET", "POST"}) |
|
82
|
|
|
* @Security("is_granted('ROLE_FINANCIAL_MANAGER') or (is_granted('USER_VISIT_TRACK', tutor) and expense.getTeacher() == tutor)") |
|
83
|
|
|
*/ |
|
84
|
|
|
public function expenseFormAction(User $tutor, Expense $expense, Request $request) |
|
85
|
|
|
{ |
|
86
|
|
|
$form = $this->createForm('AppBundle\Form\Type\ExpenseType', $expense, [ |
|
87
|
|
|
'admin' => $this->isGranted('ROLE_FINANCIAL_MANAGER'), |
|
88
|
|
|
'disabled' => $expense->isReviewed() && !$this->isGranted('ROLE_FINANCIAL_MANAGER') |
|
89
|
|
|
]); |
|
90
|
|
|
$form->handleRequest($request); |
|
91
|
|
|
|
|
92
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
93
|
|
|
$this->getDoctrine()->getManager()->persist($expense); |
|
94
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
95
|
|
|
$this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'expense')); |
|
96
|
|
|
return $this->redirectToRoute('expense_index', ['id' => $tutor->getId()]); |
|
97
|
|
|
} |
|
98
|
|
|
$title = $this->get('translator')->trans($expense->getId() ? 'form.view' : 'form.new', [], 'expense'); |
|
99
|
|
|
|
|
100
|
|
|
return $this->render('expense/form_expense.html.twig', [ |
|
101
|
|
|
'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('expense_tutor_index'), |
|
102
|
|
|
'breadcrumb' => [ |
|
103
|
|
|
['fixed' => (string) $tutor, 'path' => 'expense_index', 'options' => ['id' => $tutor->getId()]], |
|
104
|
|
|
['fixed' => $expense->getId() ? $expense->getDate()->format('d/m/Y') : $title] |
|
105
|
|
|
], |
|
106
|
|
|
'title' => $title, |
|
107
|
|
|
'expense' => $expense, |
|
108
|
|
|
'form' => $form->createView(), |
|
109
|
|
|
'tutor' => $tutor |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @Route("/{id}/eliminar/{expense}", name="expense_delete", methods={"GET", "POST"}) |
|
116
|
|
|
* @Security("is_granted('ROLE_FINANCIAL_MANAGER') or (is_granted('USER_VISIT_TRACK', tutor) and expense.getTeacher() == tutor)") |
|
117
|
|
|
*/ |
|
118
|
|
|
public function expenseDeleteAction(User $tutor, Expense $expense, Request $request) |
|
119
|
|
|
{ |
|
120
|
|
|
if ('POST' === $request->getMethod() && $request->request->has('delete')) { |
|
121
|
|
|
|
|
122
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
123
|
|
|
|
|
124
|
|
|
if (!$expense->isPaid() || $this->isGranted('ROLE_FINANCIAL_MANAGER')) { |
|
125
|
|
|
// Eliminar el desplazamiento de la base de datos |
|
126
|
|
|
$em->remove($expense); |
|
127
|
|
|
try { |
|
128
|
|
|
$em->flush(); |
|
129
|
|
|
$this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'expense')); |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
$this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'expense')); |
|
132
|
|
|
} |
|
133
|
|
|
} else { |
|
134
|
|
|
$this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'expense')); |
|
135
|
|
|
} |
|
136
|
|
|
return $this->redirectToRoute('expense_index', ['id' => $tutor->getId()]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$title = $expense->getDate()->format('d/m/Y'); |
|
140
|
|
|
|
|
141
|
|
|
return $this->render('expense/delete_expense.html.twig', [ |
|
142
|
|
|
'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('expense_tutor_index'), |
|
143
|
|
|
'breadcrumb' => [ |
|
144
|
|
|
['fixed' => (string) $tutor, 'path' => 'expense_index', 'options' => ['id' => $tutor->getId()]], |
|
145
|
|
|
['fixed' => $expense->getDate()->format('d/m/Y'), 'path' => 'expense_form', 'options' => ['id' => $tutor->getId(), 'expense' => $expense->getId()]], |
|
146
|
|
|
['fixed' => $this->get('translator')->trans('form.delete', [], 'expense')] |
|
147
|
|
|
], |
|
148
|
|
|
'title' => $title, |
|
149
|
|
|
'tutor' => $tutor, |
|
150
|
|
|
'expense' => $expense |
|
151
|
|
|
]); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @Route("/{id}/registrar", name="expense_form_new", methods={"GET", "POST"}) |
|
156
|
|
|
* @Security("is_granted('ROLE_FINANCIAL_MANAGER') or is_granted('USER_VISIT_TRACK', tutor)") |
|
157
|
|
|
*/ |
|
158
|
|
|
public function expenseNewAction(User $tutor, Request $request) |
|
159
|
|
|
{ |
|
160
|
|
|
$expense = new Expense(); |
|
161
|
|
|
$expense->setTeacher($tutor); |
|
162
|
|
|
$expense->setDate(new \DateTime()); |
|
163
|
|
|
|
|
164
|
|
|
return $this->expenseFormAction($tutor, $expense, $request); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @Route("/{id}/informe", name="expense_report", methods={"GET"}) |
|
169
|
|
|
* @Security("is_granted('ROLE_FINANCIAL_MANAGER') or is_granted('USER_VISIT_TRACK', tutor)") |
|
170
|
|
|
*/ |
|
171
|
|
|
public function downloadExpenseReportAction(User $tutor) |
|
172
|
|
|
{ |
|
173
|
|
|
$translator = $this->get('translator'); |
|
174
|
|
|
|
|
175
|
|
|
$title = $translator->trans('form.expense_report', [], 'expense_report') . ' - ' . (string) $tutor; |
|
176
|
|
|
|
|
177
|
|
|
$mpdf = $this->get('sasedev_mpdf'); |
|
178
|
|
|
$mpdf->init('', 'A4'); |
|
179
|
|
|
|
|
180
|
|
|
$obj = $mpdf->getMpdf(); |
|
181
|
|
|
$obj->SetImportUse(); |
|
182
|
|
|
$obj->SetDocTemplate('pdf/A4_vacio.pdf', true); |
|
183
|
|
|
|
|
184
|
|
|
// si no está verificado cada apunte, agregar la marca de agua de borrador |
|
185
|
|
|
if (!$this->getDoctrine()->getManager()->getRepository('AppBundle:Expense')->areExpensesReviewed($tutor)) { |
|
186
|
|
|
$obj->SetWatermarkText($translator->trans('form.draft', [], 'expense_report'), 0.1); |
|
187
|
|
|
$obj->showWatermarkText = true; |
|
188
|
|
|
$obj->watermark_font = 'DejaVuSansCondensed'; |
|
189
|
|
|
} |
|
190
|
|
|
$mpdf->useTwigTemplate('expense/expense_report.html.twig', [ |
|
191
|
|
|
'tutor' => $tutor, |
|
192
|
|
|
'title' => $title, |
|
193
|
|
|
'expenses' => $this->getDoctrine()->getManager()->getRepository('AppBundle:Expense')->getRelatedExpenses($tutor), |
|
194
|
|
|
'financial_managers' => $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findBy(['financialManager' => true]) |
|
195
|
|
|
]); |
|
196
|
|
|
|
|
197
|
|
|
$title = str_replace(' ', '_', $title); |
|
198
|
|
|
return $mpdf->generateInlineFileResponse($title . '.pdf'); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.