Completed
Push — sf2.7 ( 8586ff...27ae11 )
by Laurent
03:38
created

InventoryController::closeAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
rs 8.439
cc 5
eloc 23
nc 5
nop 2
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', array('id' => $inventory->getId())
209
            ),
210
            'method' => 'PUT',
211
        ));
212
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
213
214
        return array(
215
            'inventory' => $inventory,
216
            'valid_form'   => $validForm->createView(),
217
            'delete_form' => $deleteForm->createView(),
218
        );
219
    }
220
221
    /**
222
     * Close an existing Inventory entity.
223
     *
224
     * @Route("/{id}/close", name="inventory_close", requirements={"id"="\d+"})
225
     * @Method("PUT")
226
     * @Template("AppBundle:Inventory:valid.html.twig")
227
     */
228
    public function closeAction(Inventory $inventory, Request $request)
229
    {
230
        $em = $this->getDoctrine()->getManager();
231
        $articles = $em->getRepository('AppBundle:Article')->getResultArticles();
232
        
233
        $validForm = $this->createForm(new InventoryValidType(), $inventory, array(
234
            'action' => $this->generateUrl(
235
                'inventory_close',
236
                array('id' => $inventory->getId())
237
            ),
238
            'method' => 'PUT',
239
        ));
240
        if ($validForm->handleRequest($request)->isValid()) {
241
            $inventory->setStatus(3);
242
            $em->persist($inventory);
243
            foreach ($inventory->getArticles() as $line) {
0 ignored issues
show
Bug introduced by
The expression $inventory->getArticles() of type string is not traversable.
Loading history...
244
                foreach ($articles as $article) {
245
                    if ($article->getId() === $line->getArticle()->getId()) {
246
                        $article->setQuantity($line->getRealstock());
247
                        $em->persist($article);                        
248
                    }
249
                }
250
            }
251
            $em->flush();
252
253
            return $this->redirectToRoute('inventory');
254
        }
255
256
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
257
258
        return array(
259
            'inventory' => $inventory,
260
            'valid_form'   => $validForm->createView(),
261
            'delete_form' => $deleteForm->createView(),
262
        );
263
    }
264
265
    /**
266
     * Deletes a Inventory entity.
267
     *
268
     * @Route("/{id}/delete", name="inventory_delete", requirements={"id"="\d+"})
269
     * @Method("DELETE")
270
     */
271
    public function deleteAction(Inventory $inventory, Request $request)
272
    {
273
        $form = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
274
        if ($form->handleRequest($request)->isValid()) {
275
            $em = $this->getDoctrine()->getManager();
276
            $inventory->setStatus(0);
277
            $em->persist($inventory);
278
            $em->flush();
279
        }
280
281
        return $this->redirectToRoute('inventory');
282
    }
283
284
    /**
285
     * Print the current inventory.<br />Creating a `PDF` file for viewing on paper
286
     *
287
     * @Route("/{id}/print/", name="inventory_print", requirements={"id"="\d+"})
288
     * @Method("GET")
289
     * @Template()
290
     */
291
    public function printAction(Inventory $inventory)
292
    {
293
        $file = $inventory->getDate()->format('Ymd') . '-inventory.pdf';
294
        // Créer et enregistrer le fichier PDF à imprimer
295
        $html = $this->renderView(
296
            'AppBundle:Inventory:print.pdf.twig',
297
            array(
298
                'articles' => $inventory->getArticles(),
299
                'inventory' => $inventory
300
            )
301
        );
302
        return new Response(
303
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
304
                $html,
305
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
306
            ),
307
            200,
308
            array(
309
                'Content-Type' => 'application/pdf',
310
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
311
            )
312
        );
313
    }
314
315
    /**
316
     * Print the preparation of inventory.<br />Creating a `PDF` file for viewing on paper
317
     *
318
     * @Route("/{id}/print/prepare", name="inventory_print_prepare", requirements={"id"="\d+"})
319
     * @Method("GET")
320
     * @Template()
321
     */
322
    public function prepareData(Inventory $inventory)
323
    {
324
        $em = $this->getDoctrine()->getManager();
325
        $articles = $em->getRepository('AppBundle:Article')->getArticles()->getQuery()->getResult();
326
        $zoneStorages = $em->getRepository('AppBundle:Zonestorage')->findAll();
327
        
328
        $html = $this->renderView(
329
            'AppBundle:Inventory:list.pdf.twig',
330
            array(
331
                    'articles' => $articles,
332
                    'zonestorage' => $zoneStorages,
333
                    'daydate' => $inventory->getDate(),
334
            )
335
        );
336
        return new Response(
337
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
338
                $html,
339
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
340
            ),
341
            200,
342
            array(
343
                'Content-Type' => 'application/pdf',
344
                'Content-Disposition' => 'attachment; filename="prepare.pdf"'
345
            )
346
        );
347
    }
348
349
    /**
350
     * Array of file (`pdf`) layout.
351
     *
352
     * @param string $date File date
353
     * @param string $title Tile title
354
     * @return array
355
     */
356
    private function getArray($date, $title) {
357
        $array = array(
358
            'margin-top' => 15,
359
            'header-spacing' => 5,
360
            'header-font-size' => 8,
361
            'header-left' => 'G.L.S.R.',
362
            'header-center' => $title,
363
            'header-right' => $date,
364
            'header-line' => true,
365
            'margin-bottom' => 15,
366
            'footer-spacing' => 5,
367
            'footer-font-size' => 8,
368
            'footer-left' => 'GLSR &copy 2014 and beyond.',
369
            'footer-right' => 'Page [page]/[toPage]',
370
            'footer-line' => true,
371
        );
372
        return $array;
373
    }
374
}
375