Completed
Push — master ( 9634ae...e1c0e4 )
by Laurent
19:33 queued 16:12
created

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