GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 513de7...415d8e )
by Luis Ramón
12:46
created

ExpenseController::teacherIndexAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
     * @Route("/{id}/eliminar/{expense}", name="expense_delete", methods={"GET", "POST"})
115
     * @Security("is_granted('ROLE_FINANCIAL_MANAGER') or (is_granted('USER_VISIT_TRACK', tutor) and expense.getTeacher() == tutor)")
116
     */
117
    public function expenseDeleteAction(User $tutor, Expense $expense, Request $request)
118
    {
119
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
120
121
            $em = $this->getDoctrine()->getManager();
122
            
123
            if (!$expense->isPaid() || $this->isGranted('ROLE_FINANCIAL_MANAGER')) {
124
                // Eliminar el desplazamiento de la base de datos
125
                $em->remove($expense);
126
                try {
127
                    $em->flush();
128
                    $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'expense'));
129
                } catch (\Exception $e) {
130
                    $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'expense'));
131
                }
132
            } else {
133
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'expense'));
134
            }
135
            return $this->redirectToRoute('expense_index', ['id' => $tutor->getId()]);
136
        }
137
138
        $title = $expense->getDate()->format('d/m/Y');
139
140
        return $this->render('expense/delete_expense.html.twig', [
141
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('expense_tutor_index'),
142
            'breadcrumb' => [
143
                ['fixed' => (string) $tutor, 'path' => 'expense_index', 'options' => ['id' => $tutor->getId()]],
144
                ['fixed' => $expense->getDate()->format('d/m/Y'), 'path' => 'expense_form', 'options' => ['id' => $tutor->getId(), 'expense' => $expense->getId()]],
145
                ['fixed' => $this->get('translator')->trans('form.delete', [], 'expense')]
146
            ],
147
            'title' => $title,
148
            'tutor' => $tutor,
149
            'expense' => $expense
150
        ]);
151
    }
152
153
    /**
154
     * @Route("/{id}/registrar", name="expense_form_new", methods={"GET", "POST"})
155
     * @Security("is_granted('ROLE_FINANCIAL_MANAGER') or is_granted('USER_VISIT_TRACK', tutor)")
156
     */
157
    public function expenseNewAction(User $tutor, Request $request)
158
    {
159
        $expense = new Expense();
160
        $expense
161
            ->setTeacher($tutor)
162
            ->setDate(new \DateTime())
163
            ->setPaid(false)
164
            ->setReviewed(false);
165
166
        return $this->expenseFormAction($tutor, $expense, $request);
167
    }
168
169
    /**
170
     * @Route("/{id}/operacion", name="expense_operation", methods={"POST"})
171
     * @Security("is_granted('ROLE_FINANCIAL_MANAGER')")
172
     */
173
    public function expenseOperationAction(User $tutor, Request $request)
174
    {
175
        $em = $this->getDoctrine()->getManager();
176
177
        try {
178
            if ($request->request->has('review')) {
179
                $em->getRepository('AppBundle:Expense')->setReviewed($tutor);
180
            }
181
            if ($request->request->has('pay')) {
182
                $em->getRepository('AppBundle:Expense')->setPaid($tutor);
183
            }
184
185
            if ($request->request->has('review')) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
186
            }
187
            $em->flush();
188
            $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'expense'));
189
        } catch (\Exception $e) {
190
            $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'expense'));
191
        }
192
193
        return $this->redirectToRoute('expense_index', ['id' => $tutor->getId()]);
194
    }
195
196
    /**
197
     * @Route("/{id}/informe", name="expense_report", methods={"GET"})
198
     * @Security("is_granted('ROLE_FINANCIAL_MANAGER') or is_granted('USER_VISIT_TRACK', tutor)")
199
     */
200
    public function downloadExpenseReportAction(User $tutor)
201
    {
202
        $translator = $this->get('translator');
203
204
        $title = $translator->trans('form.expense_report', [], 'expense_report') . ' - ' . (string) $tutor;
205
206
        $mpdf = $this->get('sasedev_mpdf');
207
        $mpdf->init('', 'A4');
208
209
        $obj = $mpdf->getMpdf();
210
        $obj->SetImportUse();
211
        $obj->SetDocTemplate('pdf/A4_vacio.pdf', true);
212
213
        // si no está verificado cada apunte, agregar la marca de agua de borrador
214
        if (!$this->getDoctrine()->getManager()->getRepository('AppBundle:Expense')->areExpensesReviewed($tutor)) {
215
            $obj->SetWatermarkText($translator->trans('form.draft', [], 'expense_report'), 0.1);
216
            $obj->showWatermarkText = true;
217
            $obj->watermark_font = 'DejaVuSansCondensed';
218
        }
219
        $mpdf->useTwigTemplate('expense/expense_report.html.twig', [
220
            'tutor' => $tutor,
221
            'title' => $title,
222
            'expenses' => $this->getDoctrine()->getManager()->getRepository('AppBundle:Expense')->getRelatedExpenses($tutor),
223
            'financial_managers' => $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findBy(['financialManager' => true])
224
        ]);
225
226
        $title = str_replace(' ', '_', $title);
227
        return $mpdf->generateInlineFileResponse($title . '.pdf');
228
    }
229
}
230