Completed
Push — master ( 1af8b6...90bc50 )
by Laurent
03:35
created

AbstractInventoryController::getLineArticles()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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