Completed
Push — master ( 4be90b...11f63d )
by Laurent
12:32 queued 08:08
created

DeliveriesController::updateAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
/**
3
 * DeliveriesController controller des livraisons.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractOrdersController;
19
use Symfony\Component\HttpFoundation\Response;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
23
24
use AppBundle\Entity\Orders;
25
use AppBundle\Form\Type\OrdersEditType;
26
27
/**
28
 * Orders controller.
29
 *
30
 * @Route("/deliveries")
31
 */
32
class DeliveriesController extends AbstractOrdersController
33
{
34
    /**
35
     * Lists all Orders entities.
36
     *
37
     * @Route("/", name="deliveries")
38
     * @Method("GET")
39
     * @Template()
40
     */
41 View Code Duplication
    public function indexAction(Request $request)
1 ignored issue
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...
42
    {
43
        $em = $this->getDoctrine()->getManager();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
44
        $item = $this->container->getParameter('knp_paginator.page_range');
45
        $qb = $em->getRepository('AppBundle:Orders')->createQueryBuilder('o');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qb. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
46
        $qb->where('o.delivdate >= ' . date('Y-m-d'));
47
        $qb->andWhere('o.status = 1');
48
        
49
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), $item);
50
        return array(
51
            'paginator' => $paginator,
52
        );
53
    }
54
55
    /**
56
     * Finds and displays a Orders entity.
57
     *
58
     * @Route("/{id}/show", name="deliveries_show", requirements={"id"="\d+"})
59
     * @Method("GET")
60
     * @Template()
61
     */
62
    public function showAction(Orders $orders)
63
    {
64
        return array(
65
            'orders' => $orders,
66
        );
67
    }
68
69
    /**
70
     * Displays a form to edit an existing Orders entity.
71
     *
72
     * @Route("/admin/{id}/edit", name="deliveries_edit", requirements={"id"="\d+"})
73
     * @Method("GET")
74
     * @Template()
75
     */
76
    public function editAction(Orders $orders)
77
    {
78
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
79
            'action' => $this->generateUrl('deliveries_update', array('id' => $orders->getId())),
80
            'method' => 'PUT',
81
        ));
82
83
        return array(
84
            'orders' => $orders,
85
            'edit_form'   => $editForm->createView(),
86
        );
87
    }
88
89
    /**
90
     * Edits an existing Orders entity.
91
     *
92
     * @Route("/admin/{id}/update", name="deliveries_update", requirements={"id"="\d+"})
93
     * @Method("PUT")
94
     * @Template("AppBundle:Deliveries:edit.html.twig")
95
     */
96
    public function updateAction(Orders $orders, Request $request)
97
    {
98
        $etm = $this->getDoctrine()->getManager();
99
100
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
101
            'action' => $this->generateUrl('deliveries_update', array('id' => $orders->getId())),
102
            'method' => 'PUT',
103
        ));
104
        $editForm->handleRequest($request);
105
106
        $return = array(
107
            'orders' => $orders,
108
            'edit_form'   => $editForm->createView(),
109
        );
110
        
111
        if ($editForm->isValid()) {
112
            $orders->setStatus(2);
113
            $etm->persist($orders);
114
            $this->updateArticles($orders, $etm);
115
            $this->addFlash('info', 'gestock.edit.ok');
116
            $etm->flush();
117
118
            $return = $this->redirect($this->generateUrl('_home'));
119
        }
120
121
        return $return;
122
    }
123
124
    /**
125
     * Print the current delivery.<br />Creating a `PDF` file for viewing on paper
126
     *
127
     * @Route("/{id}/print/", name="deliveries_print", requirements={"id"="\d+"})
128
     * @Method("GET")
129
     * @Template()
130
     *
131
     * @param \AppBundle\Entity\Inventory $orders Inventory item to print
132
     * @return \Symfony\Component\HttpFoundation\Response
133
     */
134 View Code Duplication
    public function printAction(Orders $orders)
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...
135
    {
136
        $file = 'delivery-' . $orders->getId() . '.pdf';
137
        $company = $this->getDoctrine()->getManager()->getRepository('AppBundle:Company')->find(1);
138
        // Create and save the PDF file to print
139
        $html = $this->renderView(
140
            'AppBundle:Deliveries:print.pdf.twig',
141
            ['articles' => $orders->getArticles(), 'orders' => $orders, 'company' => $company, ]
142
        );
143
        return new Response(
144
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
145
                $html,
146
                $this->getArray((string)date('d/m/y - H:i:s'), '')
147
            ),
148
            200,
149
            array(
150
                'Content-Type' => 'application/pdf',
151
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
152
            )
153
        );
154
    }
155
156
    /**
157
     * Update Articles.
158
     *
159
     * @param \AppBundle\Entity\Orders   $orders   Articles de la commande à traiter
160
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
161
     */
162
    private function updateArticles(Orders $orders, $etm)
163
    {
164
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($orders->getSupplier()->getId());
165
        foreach ($orders->getArticles() as $line) {
166
            foreach ($articles as $art) {
167
                if ($art->getId() === $line->getArticle()->getId()) {
168
                    $art->setQuantity($line->getQuantity() + $art->getQuantity());
169
                    $etm->persist($art);
170
                }
171
            }
172
        }
173
    }
174
}
175