Completed
Pull Request — master (#45)
by Laurent
04:03
created

InventoryController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 21
c 2
b 1
f 1
lcom 1
cbo 10
dl 0
loc 340
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 14 1
A showAction() 0 15 1
A createCreateForm() 0 13 1
B createAction() 0 32 4
A editAction() 0 17 1
A updateAction() 0 21 2
A validAction() 0 17 1
B closeAction() 0 36 5
A deleteAction() 0 12 2
A printAction() 0 23 1
B prepareDataAction() 0 26 1
A getArray() 0 19 1
1
<?php
2
/**
3
 * InventoryController controller des inventaires.
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 Symfony\Component\HttpFoundation\Response;
19
use AppBundle\Controller\AbstractController;
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\Inventory;
25
use AppBundle\Form\Type\InventoryType;
26
use AppBundle\Entity\InventoryArticles;
27
use AppBundle\Form\Type\InventoryEditType;
28
use AppBundle\Form\Type\InventoryValidType;
29
30
/**
31
 * Inventory controller.
32
 *
33
 * @category Controller
34
 *
35
 * @Route("/inventory")
36
 */
37
class InventoryController extends AbstractController
38
{
39
    /**
40
     * Lists all Inventory entities.
41
     *
42
     * @Route("/", name="inventory")
43
     * @Method("GET")
44
     * @Template()
45
     */
46
    public function indexAction(Request $request)
47
    {
48
        $em = $this->getDoctrine()->getManager();
49
        $qb = $em->getRepository('AppBundle:Inventory')->getInventory();
50
        
51
        $createForm = $this->createCreateForm('inventory_create');
52
53
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 5);
54
55
        return array(
56
            'paginator' => $paginator,
57
            'create_form' => $createForm->createView(),
58
        );
59
    }
60
61
    /**
62
     * Finds and displays a Inventory entity.
63
     *
64
     * @Route("/{id}/show", name="inventory_show", requirements={"id"="\d+"})
65
     * @Method("GET")
66
     * @Template()
67
     */
68
    public function showAction(Inventory $inventory)
69
    {
70
        $em = $this->getDoctrine()->getManager();
71
        $inventoryArticles = $em
72
            ->getRepository('AppBundle:InventoryArticles')
73
            ->getArticlesFromInventory($inventory);
74
75
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
76
        
77
        return array(
78
            'inventory' => $inventory,
79
            'inventoryArticles' => $inventoryArticles,
80
            'delete_form' => $deleteForm->createView(),
81
        );
82
    }
83
84
    /**
85
     * Create Create form.
86
     *
87
     * @param string $route Route of action form
88
     * @return \Symfony\Component\Form\Form
89
     */
90
    protected function createCreateForm($route)
91
    {
92
        $inventory = new Inventory();
93
        return $this->createForm(
94
            new InventoryType(),
95
            $inventory,
96
            array(
97
                'attr'   => array('id' => 'create'),
98
                'action' => $this->generateUrl($route),
99
                'method' => 'PUT'
100
            )
101
        );
102
    }
103
104
    /**
105
     * Creates a new Inventory entity.
106
     *
107
     * @Route("/create", name="inventory_create")
108
     * @Method("PUT")
109
     */
110
    public function createAction(Request $request)
111
    {
112
        $em = $this->getDoctrine()->getManager();
113
        $articles = $em->getRepository('AppBundle:Article')->getResultArticles();
114
        $settings = $em->getRepository('AppBundle:Settings')->find(1);
115
116
        $inventory = new Inventory();
117
        $form = $this->createCreateForm('inventory_create');
118
        if ($form->handleRequest($request)->isValid()) {
119
            $em->persist($inventory);
120
121
            // Enregistrement du premier inventaire
122
            if (empty($settings->getFirstInventory)) {
123
                $settings->setFirstInventory($inventory->getDate());
124
                $em->persist($settings);
125
            }
126
            // Enregistrement des articles dans l'inventaire
127
            foreach ($articles as $article) {
128
                $inventoryArticles = new InventoryArticles();
129
                $inventoryArticles->setArticle($article);
130
                $inventoryArticles->setInventory($inventory);
131
                $inventoryArticles->setQuantity($article->getQuantity());
132
                $inventoryArticles->setRealstock(0);
133
                $inventoryArticles->setUnitStorage($article->getUnitStorage());
134
                $inventoryArticles->setPrice($article->getPrice());
135
                $em->persist($inventoryArticles);
136
            }
137
            $em->flush();
138
139
            return $this->redirectToRoute('inventory_print_prepare', array('id' => $inventory->getId()));
140
        }
141
    }
142
143
    /**
144
     * Displays a form to edit an existing Inventory entity.
145
     *
146
     * @Route("/{id}/edit", name="inventory_edit", requirements={"id"="\d+"})
147
     * @Method("GET")
148
     * @Template()
149
     */
150
    public function editAction(Inventory $inventory)
151
    {
152
        $editForm = $this->createForm(new InventoryEditType(), $inventory, array(
153
            'action' => $this->generateUrl(
154
                'inventory_update',
155
                array('id' => $inventory->getId())
156
            ),
157
            'method' => 'PUT',
158
        ));
159
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
160
161
        return array(
162
            'inventory' => $inventory,
163
            'edit_form'   => $editForm->createView(),
164
            'delete_form' => $deleteForm->createView(),
165
        );
166
    }
167
168
    /**
169
     * Edits an existing Inventory entity.
170
     *
171
     * @Route("/{id}/update", name="inventory_update", requirements={"id"="\d+"})
172
     * @Method("PUT")
173
     * @Template("AppBundle:Inventory:edit.html.twig")
174
     */
175
    public function updateAction(Inventory $inventory, Request $request)
176
    {
177
        $editForm = $this->createForm(new InventoryEditType(), $inventory, array(
178
            'action' => $this->generateUrl('inventory_update', array('id' => $inventory->getId())),
179
            'method' => 'PUT',
180
        ));
181
        if ($editForm->handleRequest($request)->isValid()) {
182
            $inventory->setStatus('2');
183
            $this->getDoctrine()->getManager()->flush();
184
185
            return $this->redirectToRoute('inventory_edit', array('id' => $inventory->getId()));
186
        }
187
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
188
189
190
        return array(
191
            'inventory' => $inventory,
192
            'edit_form'   => $editForm->createView(),
193
            'delete_form' => $deleteForm->createView(),
194
        );
195
    }
196
197
    /**
198
     * Displays a form to valid an existing Inventory entity.
199
     *
200
     * @Route("/{id}/valid", name="inventory_valid", requirements={"id"="\d+"})
201
     * @Method("GET")
202
     * @Template()
203
     */
204
    public function validAction(Inventory $inventory)
205
    {
206
        $validForm = $this->createForm(new InventoryValidType(), $inventory, array(
207
            'action' => $this->generateUrl(
208
                'inventory_close',
209
                array('id' => $inventory->getId())
210
            ),
211
            'method' => 'PUT',
212
        ));
213
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
214
215
        return array(
216
            'inventory' => $inventory,
217
            'valid_form'   => $validForm->createView(),
218
            'delete_form' => $deleteForm->createView(),
219
        );
220
    }
221
222
    /**
223
     * Close an existing Inventory entity.
224
     *
225
     * @Route("/{id}/close", name="inventory_close", requirements={"id"="\d+"})
226
     * @Method("PUT")
227
     * @Template("AppBundle:Inventory:valid.html.twig")
228
     */
229
    public function closeAction(Inventory $inventory, Request $request)
230
    {
231
        $em = $this->getDoctrine()->getManager();
232
        $articles = $em->getRepository('AppBundle:Article')->getResultArticles();
233
        
234
        $validForm = $this->createForm(new InventoryValidType(), $inventory, array(
235
            'action' => $this->generateUrl(
236
                'inventory_close',
237
                array('id' => $inventory->getId())
238
            ),
239
            'method' => 'PUT',
240
        ));
241
        if ($validForm->handleRequest($request)->isValid()) {
242
            $inventory->setStatus(3);
243
            $em->persist($inventory);
244
            foreach ($inventory->getArticles() as $line) {
245
                foreach ($articles as $article) {
246
                    if ($article->getId() === $line->getArticle()->getId()) {
247
                        $article->setQuantity($line->getRealstock());
248
                        $em->persist($article);
249
                    }
250
                }
251
            }
252
            $em->flush();
253
254
            return $this->redirectToRoute('inventory');
255
        }
256
257
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
258
259
        return array(
260
            'inventory' => $inventory,
261
            'valid_form'   => $validForm->createView(),
262
            'delete_form' => $deleteForm->createView(),
263
        );
264
    }
265
266
    /**
267
     * Deletes a Inventory entity.
268
     *
269
     * @Route("/{id}/delete", name="inventory_delete", requirements={"id"="\d+"})
270
     * @Method("DELETE")
271
     */
272
    public function deleteAction(Inventory $inventory, Request $request)
273
    {
274
        $form = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
275
        if ($form->handleRequest($request)->isValid()) {
276
            $em = $this->getDoctrine()->getManager();
277
            $inventory->setStatus(0);
278
            $em->persist($inventory);
279
            $em->flush();
280
        }
281
282
        return $this->redirectToRoute('inventory');
283
    }
284
285
    /**
286
     * Print the current inventory.<br />Creating a `PDF` file for viewing on paper
287
     *
288
     * @Route("/{id}/print/", name="inventory_print", requirements={"id"="\d+"})
289
     * @Method("GET")
290
     * @Template()
291
     */
292
    public function printAction(Inventory $inventory)
293
    {
294
        $file = $inventory->getDate()->format('Ymd') . '-inventory.pdf';
295
        // Créer et enregistrer le fichier PDF à imprimer
296
        $html = $this->renderView(
297
            'AppBundle:Inventory:print.pdf.twig',
298
            array(
299
                'articles' => $inventory->getArticles(),
300
                'inventory' => $inventory
301
            )
302
        );
303
        return new Response(
304
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
305
                $html,
306
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
307
            ),
308
            200,
309
            array(
310
                'Content-Type' => 'application/pdf',
311
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
312
            )
313
        );
314
    }
315
316
    /**
317
     * Print the preparation of inventory.<br />Creating a `PDF` file for viewing on paper
318
     *
319
     * @Route("/{id}/print/prepare", name="inventory_print_prepare", requirements={"id"="\d+"})
320
     * @Method("GET")
321
     * @Template()
322
     */
323
    public function prepareDataAction(Inventory $inventory)
324
    {
325
        $em = $this->getDoctrine()->getManager();
326
        $articles = $em->getRepository('AppBundle:Article')->getArticles()->getQuery()->getResult();
327
        $zoneStorages = $em->getRepository('AppBundle:Zonestorage')->findAll();
328
        
329
        $html = $this->renderView(
330
            'AppBundle:Inventory:list.pdf.twig',
331
            array(
332
                    'articles' => $articles,
333
                    'zonestorage' => $zoneStorages,
334
                    'daydate' => $inventory->getDate(),
335
            )
336
        );
337
        return new Response(
338
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
339
                $html,
340
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
341
            ),
342
            200,
343
            array(
344
                'Content-Type' => 'application/pdf',
345
                'Content-Disposition' => 'attachment; filename="prepare.pdf"'
346
            )
347
        );
348
    }
349
350
    /**
351
     * Array of file (`pdf`) layout.
352
     *
353
     * @param string $date File date
354
     * @param string $title Tile title
355
     * @return array
356
     */
357
    private function getArray($date, $title)
358
    {
359
        $array = array(
360
            'margin-top' => 15,
361
            'header-spacing' => 5,
362
            'header-font-size' => 8,
363
            'header-left' => 'G.L.S.R.',
364
            'header-center' => $title,
365
            'header-right' => $date,
366
            'header-line' => true,
367
            'margin-bottom' => 15,
368
            'footer-spacing' => 5,
369
            'footer-font-size' => 8,
370
            'footer-left' => 'GLSR &copy 2014 and beyond.',
371
            'footer-right' => 'Page [page]/[toPage]',
372
            'footer-line' => true,
373
        );
374
        return $array;
375
    }
376
}
377