Completed
Push — master ( 3f9c93...d3d201 )
by Laurent
04:34
created

InventoryController::closeAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 33
rs 8.439
cc 5
eloc 21
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
     * @param \Symfony\Component\HttpFoundation\Request $request Paginate request
47
     * @return array
48
     */
49
    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...
50
    {
51
        $etm = $this->getDoctrine()->getManager();
52
        $qbd = $etm->getRepository('AppBundle:Inventory')->getInventory();
53
        
54
        $createForm = $this->createCreateForm('inventory_create');
55
56
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), 5);
57
58
        return array(
59
            'paginator' => $paginator,
60
            'create_form' => $createForm->createView(),
61
        );
62
    }
63
64
    /**
65
     * Finds and displays a Inventory entity.
66
     *
67
     * @Route("/{id}/show", name="inventory_show", requirements={"id"="\d+"})
68
     * @Method("GET")
69
     * @Template()
70
     *
71
     * @param \AppBundle\Entity\Inventory $inventory Inventory item to display
72
     * @return array
73
     */
74
    public function showAction(Inventory $inventory)
75
    {
76
        $etm = $this->getDoctrine()->getManager();
77
        $inventoryArticles = $etm
78
            ->getRepository('AppBundle:InventoryArticles')
79
            ->getArticlesFromInventory($inventory);
80
81
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
82
        
83
        return array(
84
            'inventory' => $inventory,
85
            'inventoryArticles' => $inventoryArticles,
86
            'delete_form' => $deleteForm->createView(),
87
        );
88
    }
89
90
    /**
91
     * Create Create form.
92
     *
93
     * @param string $route Route of action form
94
     * @return \Symfony\Component\Form\Form
95
     */
96
    protected function createCreateForm($route)
97
    {
98
        $inventory = new Inventory();
99
        return $this->createForm(
100
            new InventoryType(),
101
            $inventory,
102
            array('attr' => array('id' => 'create'), 'action' => $this->generateUrl($route), 'method' => 'PUT',)
103
        );
104
    }
105
106
    /**
107
     * Creates a new Inventory entity.
108
     *
109
     * @Route("/create", name="inventory_create")
110
     * @Method("PUT")
111
     *
112
     * @param \Symfony\Component\HttpFoundation\Request $request
113
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
114
     */
115
    public function createAction(Request $request)
116
    {
117
        $etm = $this->getDoctrine()->getManager();
118
        $articles = $etm->getRepository('AppBundle:Article')->getResultArticles();
119
        $settings = $etm->getRepository('AppBundle:Settings')->find(1);
120
121
        $inventory = new Inventory();
122
        $form = $this->createCreateForm('inventory_create');
123
        if ($form->handleRequest($request)->isValid()) {
124
            $etm->persist($inventory);
125
126
            // Saving the first inventory
127
            if (empty($settings->getFirstInventory)) {
128
                $settings->setFirstInventory($inventory->getDate());
129
                $etm->persist($settings);
130
            }
131
            // Saving of articles in the inventory
132
            foreach ($articles as $article) {
133
                $inventoryArticles = new InventoryArticles();
134
                $inventoryArticles->setArticle($article);
135
                $inventoryArticles->setInventory($inventory);
136
                $inventoryArticles->setQuantity($article->getQuantity());
137
                $inventoryArticles->setRealstock(0);
138
                $inventoryArticles->setUnitStorage($article->getUnitStorage());
139
                $inventoryArticles->setPrice($article->getPrice());
140
                $etm->persist($inventoryArticles);
141
            }
142
            $etm->flush();
143
144
            return $this->redirectToRoute('inventory_print_prepare', array('id' => $inventory->getId()));
145
        }
146
    }
147
148
    /**
149
     * Displays a form to edit an existing Inventory entity.
150
     *
151
     * @Route("/{id}/edit", name="inventory_edit", requirements={"id"="\d+"})
152
     * @Method("GET")
153
     * @Template()
154
     *
155
     * @param \AppBundle\Entity\Inventory $inventory Inventory item to edit
156
     * @return array
157
     */
158
    public function editAction(Inventory $inventory)
159
    {
160
        $editForm = $this->createForm(new InventoryEditType(), $inventory, array(
161
            'action' => $this->generateUrl('inventory_update', array('id' => $inventory->getId())),
162
            'method' => 'PUT',
163
        ));
164
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
165
166
        return array(
167
            'inventory' => $inventory,
168
            'edit_form'   => $editForm->createView(),
169
            'delete_form' => $deleteForm->createView(),
170
        );
171
    }
172
173
    /**
174
     * Edits an existing Inventory entity.
175
     *
176
     * @Route("/{id}/update", name="inventory_update", requirements={"id"="\d+"})
177
     * @Method("PUT")
178
     * @Template("AppBundle:Inventory:edit.html.twig")
179
     *
180
     * @param \AppBundle\Entity\Inventory               $inventory Inventory item to update
181
     * @param \Symfony\Component\HttpFoundation\Request $request   Form request
182
     * @return array
183
     */
184 View Code Duplication
    public function updateAction(Inventory $inventory, Request $request)
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...
185
    {
186
        $editForm = $this->createForm(new InventoryEditType(), $inventory, array(
187
            'action' => $this->generateUrl('inventory_update', array('id' => $inventory->getId())),
188
            'method' => 'PUT',
189
        ));
190
        if ($editForm->handleRequest($request)->isValid()) {
191
            $inventory->setStatus('2');
192
            $this->getDoctrine()->getManager()->flush();
193
194
            return $this->redirectToRoute('inventory_edit', array('id' => $inventory->getId()));
195
        }
196
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
197
198
199
        return array(
200
            'inventory' => $inventory,
201
            'edit_form'   => $editForm->createView(),
202
            'delete_form' => $deleteForm->createView(),
203
        );
204
    }
205
206
    /**
207
     * Displays a form to valid an existing Inventory entity.
208
     *
209
     * @Route("/{id}/valid", name="inventory_valid", requirements={"id"="\d+"})
210
     * @Method("GET")
211
     * @Template()
212
     *
213
     * @param \AppBundle\Entity\Inventory $inventory Inventory item to validate
214
     * @return array
215
     */
216
    public function validAction(Inventory $inventory)
217
    {
218
        $validForm = $this->createForm(new InventoryValidType(), $inventory, array(
219
            'action' => $this->generateUrl('inventory_close', array('id' => $inventory->getId())),
220
            'method' => 'PUT',
221
        ));
222
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
223
224
        return array(
225
            'inventory' => $inventory,
226
            'valid_form'   => $validForm->createView(),
227
            'delete_form' => $deleteForm->createView(),
228
        );
229
    }
230
231
    /**
232
     * Close an existing Inventory entity.
233
     *
234
     * @Route("/{id}/close", name="inventory_close", requirements={"id"="\d+"})
235
     * @Method("PUT")
236
     * @Template("AppBundle:Inventory:valid.html.twig")
237
     *
238
     * @param \AppBundle\Entity\Inventory               $inventory Inventory item to close
239
     * @param \Symfony\Component\HttpFoundation\Request $request   Form request
240
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
241
     */
242
    public function closeAction(Inventory $inventory, Request $request)
243
    {
244
        $etm = $this->getDoctrine()->getManager();
245
        $articles = $etm->getRepository('AppBundle:Article')->getResultArticles();
246
        
247
        $validForm = $this->createForm(new InventoryValidType(), $inventory, array(
248
            'action' => $this->generateUrl('inventory_close', array('id' => $inventory->getId())),
249
            'method' => 'PUT',
250
        ));
251
        if ($validForm->handleRequest($request)->isValid()) {
252
            $inventory->setStatus(3);
253
            $etm->persist($inventory);
254
            foreach ($inventory->getArticles() as $line) {
255
                foreach ($articles as $article) {
256
                    if ($article->getId() === $line->getArticle()->getId()) {
257
                        $article->setQuantity($line->getRealstock());
258
                        $etm->persist($article);
259
                    }
260
                }
261
            }
262
            $etm->flush();
263
264
            return $this->redirectToRoute('inventory');
265
        }
266
267
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
268
269
        return array(
270
            'inventory' => $inventory,
271
            'valid_form'   => $validForm->createView(),
272
            'delete_form' => $deleteForm->createView(),
273
        );
274
    }
275
276
    /**
277
     * Deletes a Inventory entity.
278
     *
279
     * @Route("/{id}/delete", name="inventory_delete", requirements={"id"="\d+"})
280
     * @Method("DELETE")
281
     *
282
     * @param \AppBundle\Entity\Inventory               $inventory Inventory item to delete
283
     * @param \Symfony\Component\HttpFoundation\Request $request   Form request
284
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
285
     */
286
    public function deleteAction(Inventory $inventory, Request $request)
287
    {
288
        $form = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
289
        if ($form->handleRequest($request)->isValid()) {
290
            $etm = $this->getDoctrine()->getManager();
291
            $inventory->setStatus(0);
292
            $etm->persist($inventory);
293
            $etm->flush();
294
        }
295
296
        return $this->redirectToRoute('inventory');
297
    }
298
299
    /**
300
     * Print the current inventory.<br />Creating a `PDF` file for viewing on paper
301
     *
302
     * @Route("/{id}/print/", name="inventory_print", requirements={"id"="\d+"})
303
     * @Method("GET")
304
     * @Template()
305
     *
306
     * @param \AppBundle\Entity\Inventory $inventory Inventory item to print
307
     * @return \Symfony\Component\HttpFoundation\Response
308
     */
309
    public function printAction(Inventory $inventory)
310
    {
311
        $file = $inventory->getDate()->format('Ymd') . '-inventory.pdf';
312
        // Create and save the PDF file to print
313
        $html = $this->renderView(
314
            'AppBundle:Inventory:print.pdf.twig',
315
            array('articles' => $inventory->getArticles(), 'inventory' => $inventory,)
316
        );
317
        return new Response(
318
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
319
                $html,
320
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
321
            ),
322
            200,
323
            array(
324
                'Content-Type' => 'application/pdf',
325
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
326
            )
327
        );
328
    }
329
330
    /**
331
     * Print the preparation of inventory.<br />Creating a `PDF` file for viewing on paper
332
     *
333
     * @Route("/{id}/print/prepare", name="inventory_print_prepare", requirements={"id"="\d+"})
334
     * @Method("GET")
335
     * @Template()
336
     *
337
     * @param \AppBundle\Entity\Inventory $inventory
338
     * @return \Symfony\Component\HttpFoundation\Response
339
     */
340
    public function prepareDataAction(Inventory $inventory)
341
    {
342
        $etm = $this->getDoctrine()->getManager();
343
        $articles = $etm->getRepository('AppBundle:Article')->getArticles()->getQuery()->getResult();
344
        $zoneStorages = $etm->getRepository('AppBundle:Zonestorage')->findAll();
345
        
346
        $html = $this->renderView(
347
            'AppBundle:Inventory:list.pdf.twig',
348
            array('articles' => $articles, 'zonestorage' => $zoneStorages, 'daydate' => $inventory->getDate(),)
349
        );
350
        return new Response(
351
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
352
                $html,
353
                $this->getArray((string) $inventory->getDate()->format('d/m/Y'), '- Inventaire -')
354
            ),
355
            200,
356
            array(
357
                'Content-Type' => 'application/pdf',
358
                'Content-Disposition' => 'attachment; filename="prepare.pdf"'
359
            )
360
        );
361
    }
362
}
363