Completed
Push — master ( 17ea5b...78f03f )
by Laurent
16:28 queued 03:13
created

InventoryController::closeInventoryArticles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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