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