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

AbstractInventoryController::getLineArticles()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 7
nop 2
1
<?php
2
/**
3
 * AbstractInventoryController Common Methods InventoryController.
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
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace App\Controller\Stocks;
16
17
use App\Controller\AbstractAppController;
18
use App\Entity\Stocks\Inventory;
19
use App\Form\Type\Stocks\InventoryType;
20
use App\Form\Type\Stocks\InventoryEditType;
21
use App\Form\Type\Stocks\InventoryEditZonesType;
22
23
use Doctrine\Common\Persistence\ObjectManager;
24
25
/**
26
 * Abstract controller.
27
 *
28
 * @category Controller
29
 */
30
class AbstractInventoryController extends AbstractAppController
31
{
32
    /**
33
     * Get the Inventory edit type
34
     *
35
     * @param \App\Controller\Stocks\Inventory $inventory Inventaire à éditer
36
     * @return array
37
     */
38
    protected function getInvetoryEditType(Inventory $inventory)
39
    {
40
        $etm = $this->getDoctrine()->getManager();
41
        $zoneStorages = null;
42
        $settings = $etm->getRepository('App:Settings\Settings')->findFirst();
43
44
        if ($settings->getInventoryStyle() == 'zonestorage') {
45
            $zoneStorages = $etm->getRepository('App:Settings\Diverse\ZoneStorage')->findAll();
46
            $typeClass = InventoryEditZonesType::class;
47
        } else {
48
            $typeClass = InventoryEditType::class;
49
        }
50
        $editForm = $this->createForm(
51
            $typeClass,
52
            $inventory,
53
            ['action' => $this->generateUrl('inventory_update', ['id' => $inventory->getId()]),
54
            'method' => 'PUT',]
55
        );
56
        $deleteForm = $this->createDeleteForm($inventory->getId(), 'inventory_delete');
57
58
        $return = ['editForm' => $editForm, 'inventory' => $inventory, 'zoneStorages' => $zoneStorages,
59
            'edit_form'   => $editForm->createView(),
60
            'delete_form' => $deleteForm->createView(),];
61
        return $return;
62
    }
63
64
    /**
65
     * Create Create form.
66
     *
67
     * @param string $route Route of action form
68
     * @return \Symfony\Component\Form\Form
69
     */
70
    protected function createCreateForm($route)
71
    {
72
        $inventory = new Inventory();
73
        return $this->createForm(
74
            InventoryType::class,
75
            $inventory,
76
            ['attr' => ['id' => 'create'], 'action' => $this->generateUrl($route), 'method' => 'PUT',]
77
        );
78
    }
79
80
    /**
81
     * Get LineArticles.
82
     *
83
     * @param array $articleLine tableau
84
     * @param Inventory $inventory Inventaire traité
85
     * @return array
86
     */
87
    protected function getLineArticles(array $articleLine, Inventory $inventory)
88
    {
89
        $inventoryArticles = array();
90
        $lineOk = 0;
91
        foreach ($inventory->getArticles() as $line) {
92
            foreach ($articleLine as $key => $art) {
93
                if (!empty($articleLine) && $line->getArticle()->getName() === $art['article']) {
94
                    $art['realstock'] = $art['realstock'] + $line->getRealstock();
95
                    $articleLine[$key]['realstock'] = strval(number_format($art['realstock'], 3));
96
                    $lineOk = 1;
97
                }
98
            }
99
            if ($lineOk === 0) {
100
                $inventoryArticles['article'] = $line->getArticle()->getName();
101
                $inventoryArticles['realstock'] = $line->getRealstock();
102
                array_push($articleLine, $inventoryArticles);
103
            }
104
            $lineOk = 0;
105
        }
106
        return $articleLine;
107
    }
108
109
    protected function updateArticles(array $articles, ObjectManager $etm, Inventory $inventory)
110
    {
111
        $articleLine = array();
112
        $articleLine = $this->getLineArticles($articleLine, $inventory);
113
114
        foreach ($articles as $article) {
115
            foreach ($articleLine as $line) {
116
                if ($article->getName() === $line['article']) {
117
                    $article->setQuantity($line['realstock']);
118
                    $etm->persist($article);
119
                }
120
            }
121
        }
122
    }
123
}
124