Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

InventoryController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 320
Duplicated Lines 7.81 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 9
dl 25
loc 320
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 9 9 1
A showAction() 0 17 2
A createAction() 0 22 2
A editAction() 0 6 1
A updateAction() 0 14 2
A validAction() 0 11 1
A closeAction() 0 25 2
A deleteAction() 0 6 1
A printAction() 0 19 1
A prepareDataAction() 0 14 1
A getHtml() 0 20 2
A saveInventoryArticles() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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