Completed
Push — master ( 4be90b...11f63d )
by Laurent
12:32 queued 08:08
created

DefaultController::lastOrdersAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * DefaultController controller de l'application GLSR.
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\Bundle\FrameworkBundle\Controller\Controller;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * Default controller.
26
 *
27
 * @category Controller
28
 *
29
 * @Route("/")
30
 */
31
class DefaultController extends Controller
32
{
33
    private $entities;
34
    
35
    public function __construct()
36
    {
37
        // Tableau des entitées
38
        $this->entities = array(
39
            'AppBundle:User',
40
            'AppBundle:Company',
41
            'AppBundle:Settings',
42
            'AppBundle:FamilyLog',
43
            'AppBundle:ZoneStorage',
44
            'AppBundle:UnitStorage',
45
            'AppBundle:Tva',
46
            'AppBundle:Supplier',
47
            'AppBundle:Article');
48
    }
49
50
    /**
51
     * @Route("/", name="_home")
52
     * @Method("GET")
53
     * @Template()
54
     *
55
     * @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\RedirectResponse
56
     */
57
    public function indexAction()
58
    {
59
        /**
60
         * Test d'installation.
61
         */
62
        $url = $this->testEntities();
63
        if (empty($url)) {
64
            $url = $this->render('default/index.html.twig');
65
        } else {
66
            $url = $this->redirectToRoute($url);
67
        }
68
        
69
        /**
70
         * Affichage du dernier inventaire.
71
         */
72
        /**
73
         * Affichage des stocks d'alerte.
74
         */
75
        return $url;
76
    }
77
78
    /**
79
     * Get Alerts.
80
     *
81
     * @Route("/alert", name="stockalert")
82
     * @Method("GET")
83
     * @Template("default/stockAlert.html.twig")
84
     *
85
     * @param integer $number Number of alerts to display.
86
     * @return array|null
87
     */
88
    public function stockAlertAction($number)
89
    {
90
        $etm = $this->getDoctrine()->getManager();
91
        $listArticles = $etm->getRepository('AppBundle:Article')->getStockAlert($number);
92
        $helper = $this->get('app.helper.controller');
93
        foreach ($listArticles as $key => $article) {
94
            // Tester la liste si un fournisseur à déjà une commande en cours
95
            if ($helper->testOrderInProgress($article, $etm)) {
96
                unset($listArticles[$key]);
97
            }
98
        }
99
100
        return array('listArticles' => $listArticles);
101
    }
102
103
    /**
104
     * Get the latest inventories.
105
     *
106
     * @Route("/alert", name="lastinventories")
107
     * @Method("GET")
108
     * @Template("default/lastInventory.html.twig")
109
     *
110
     * @param integer $number Number of inventories to display
111
     * @return array|null
112
     */
113
    public function lastInventoryAction($number)
114
    {
115
        $etm = $this->getDoctrine()->getManager();
116
        $listInventories = $etm->getRepository('AppBundle:Inventory')->getLastInventory($number);
117
118
        return array('listInventory' => $listInventories);
119
    }
120
121
    /**
122
     * Get the latest orders.
123
     *
124
     * @Route("/alert", name="lastorders")
125
     * @Method("GET")
126
     * @Template("default/lastorder.html.twig")
127
     *
128
     * @param integer $number Number of orders to display
129
     * @return array|null
130
     */
131
    public function lastOrdersAction($number)
132
    {
133
        $etm = $this->getDoctrine()->getManager();
134
        $listOrders = $etm->getRepository('AppBundle:Orders')->getLastOrder($number);
135
136
        return array('listOrders' => $listOrders);
137
    }
138
139
    /**
140
     * Get the latest orders.
141
     *
142
     * @Route("/alert", name="lastorders")
143
     * @Method("GET")
144
     * @Template("default/lastDelivery.html.twig")
145
     *
146
     * @param integer $number Number of orders to display
147
     * @return array|null
148
     */
149
    public function lastDeliveriesAction($number)
150
    {
151
        $etm = $this->getDoctrine()->getManager();
152
        $listDeliveries = $etm->getRepository('AppBundle:Orders')->getLastDelivery($number);
153
154
        return array('listDeliveries' => $listDeliveries);
155
    }
156
157
    /**
158
     * Test entities.
159
     *
160
     * @return string|null
161
     */
162
    private function testEntities()
163
    {
164
        $url = null;
165
        $etm = $this->getDoctrine()->getManager();
166
        // vérifie que les Entitées ne sont pas vides
167
        $nbEntities = count($this->entities);
168
        for ($index = 0; $index < $nbEntities; $index++) {
169
            $entity = $etm->getRepository($this->entities[$index]);
170
            $entityData = $entity->find(1);
171
172
            if (empty($entityData)) {
173
                $message = 'gestock.install.none';
174
                $url = 'gs_install';
175
                break;
176
            }
177
        }
178
        if (isset($message)) {
179
            $this->addFlash('warning', $message);
180
        }
181
        return $url;
182
    }
183
184
    /**
185
     * Get FamilyLog.
186
     *
187
     * @Route("/getfamilylog", name="getfamilylog")
188
     * @Method("POST")
189
     *
190
     * @param \Symfony\Component\HttpFoundation\Request $request Post request
191
     * @return \Symfony\Component\HttpFoundation\Response
192
     */
193
    public function getFamilyLogAction(Request $request)
194
    {
195
        $return = new Response('Error');
196
        $etm = $this->getDoctrine()->getManager();
197
        if ($request->isXmlHttpRequest()) {
198
            $familyLog = array();
199
            $id = $request->get('id');
200
            if ($id != '') {
201
                $supplier = $etm
202
                    ->getRepository('AppBundle:Supplier')
203
                    ->find($id);
204
                $familyLog['familylog'] = $supplier->getFamilyLog()->getId();
205
                $response = new Response();
206
                $data = json_encode($familyLog);
207
                $response->headers->set('Content-Type', 'application/json');
208
                $response->setContent($data);
209
                $return = $response;
210
            }
211
        }
212
        return $return;
213
    }
214
}
215