Completed
Push — master ( 903aaa...639ca8 )
by Laurent
03:26
created

DeliveriesController::printAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
23
use AppBundle\Entity\Orders;
24
use AppBundle\Form\Type\OrdersEditType;
25
26
/**
27
 * Orders controller.
28
 *
29
 * @Route("/deliveries")
30
 */
31
class DeliveriesController extends AbstractOrdersController
32
{
33
    /**
34
     * Lists all Orders entities.
35
     *
36
     * @Route("/", name="deliveries")
37
     * @Method("GET")
38
     * @Template()
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        $etm = $this->getDoctrine()->getManager();
43
        $item = $this->container->getParameter('knp_paginator.page_range');
44
        $qbd = $etm->getRepository('AppBundle:Orders')->createQueryBuilder('o');
45
        $qbd->where('o.delivdate >= ' . date('Y-m-d'));
46
        $qbd->andWhere('o.status = 1');
47
        
48
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), $item);
49
        return array(
50
            'paginator' => $paginator,
51
        );
52
    }
53
54
    /**
55
     * Finds and displays a Orders entity.
56
     *
57
     * @Route("/{id}/show", name="deliveries_show", requirements={"id"="\d+"})
58
     * @Method("GET")
59
     * @Template()
60
     */
61
    public function showAction(Orders $orders)
62
    {
63
        return array(
64
            'orders' => $orders,
65
        );
66
    }
67
68
    /**
69
     * Displays a form to edit an existing Orders entity.
70
     *
71
     * @Route("/admin/{id}/edit", name="deliveries_edit", requirements={"id"="\d+"})
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function editAction(Orders $orders)
76
    {
77
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
78
            'action' => $this->generateUrl('deliveries_update', array('id' => $orders->getId())),
79
            'method' => 'PUT',
80
        ));
81
82
        return array(
83
            'orders' => $orders,
84
            'edit_form'   => $editForm->createView(),
85
        );
86
    }
87
88
    /**
89
     * Edits an existing Orders entity.
90
     *
91
     * @Route("/admin/{id}/update", name="deliveries_update", requirements={"id"="\d+"})
92
     * @Method("PUT")
93
     * @Template("AppBundle:Deliveries:edit.html.twig")
94
     */
95
    public function updateAction(Orders $orders, Request $request)
96
    {
97
        $etm = $this->getDoctrine()->getManager();
98
99
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
100
            'action' => $this->generateUrl('deliveries_update', array('id' => $orders->getId())),
101
            'method' => 'PUT',
102
        ));
103
        $editForm->handleRequest($request);
104
105
        $return = array(
106
            'orders' => $orders,
107
            'edit_form'   => $editForm->createView(),
108
        );
109
        
110
        if ($editForm->isValid()) {
111
            $orders->setStatus(2);
112
            $etm->persist($orders);
113
            $this->updateArticles($orders, $etm);
114
            $this->addFlash('info', 'gestock.edit.ok');
115
            $etm->flush();
116
117
            $return = $this->redirect($this->generateUrl('_home'));
118
        }
119
120
        return $return;
121
    }
122
123
    /**
124
     * Print the current delivery.<br />Creating a `PDF` file for viewing on paper
125
     *
126
     * @Route("/{id}/print/", name="deliveries_print", requirements={"id"="\d+"})
127
     * @Method("GET")
128
     * @Template()
129
     *
130
     * @param \AppBundle\Entity\Inventory $orders Inventory item to print
131
     * @return \Symfony\Component\HttpFoundation\Response
132
     */
133
    public function printAction(Orders $orders)
134
    {
135
        $return = $this->abstractPrintAction($orders, 'Deliveries');
136
        
137
        return $return;
138
    }
139
140
    /**
141
     * Update Articles.
142
     *
143
     * @param \AppBundle\Entity\Orders   $orders   Articles de la commande à traiter
144
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
145
     */
146
    private function updateArticles(Orders $orders, $etm)
147
    {
148
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($orders->getSupplier()->getId());
149
        foreach ($orders->getArticles() as $line) {
150
            foreach ($articles as $art) {
151
                if ($art->getId() === $line->getArticle()->getId()) {
152
                    $art->setQuantity($line->getQuantity() + $art->getQuantity());
153
                    $etm->persist($art);
154
                }
155
            }
156
        }
157
    }
158
}
159