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

DefaultController::testEntities()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 6
nop 0
1
<?php
2
/**
3
 * DefaultController Controller of the GLSR application.
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;
17
18
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Routing\Annotation\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
25
/**
26
 * Default controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/")
31
 */
32
class DefaultController extends AbstractController
33
{
34
    /**
35
     * Array of the entities.
36
     *
37
     * @var array
38
     */
39
    private $entities = [];
40
41
    public function __construct()
42
    {
43
        // Tableau des entitées
44
        $this->entities = array(
45
            ['1', 'App:Staff\Group'],
46
            ['2', 'App:Staff\User'],
47
            ['3', 'App:Settings\Company'],
48
            ['4', 'App:Settings\Settings'],
49
            ['5_1', 'App:Settings\Diverse\FamilyLog'],
50
            ['5_2', 'App:Settings\Diverse\ZoneStorage'],
51
            ['5_3', 'App:Settings\Diverse\Unit'],
52
            ['5_4', 'App:Settings\Diverse\Tva'],
53
            ['6', 'App:Settings\Supplier'],
54
            ['7', 'App:Settings\Article'],
55
            ['8', 'App:Settings\Diverse\Material'],
56
            ['9', 'App:Stocks\Inventory'], );
57
    }
58
59
    /**
60
     * @Route("/", name="_home")
61
     * @Method("GET")
62
     * @Template()
63
     *
64
     * @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\RedirectResponse
65
     */
66
    public function index()
67
    {
68
        /**
69
         * Installation test.
70
         */
71
        $url = $this->testEntities();
72
73
        if (empty($url)) {
74
            $redirect = $this->render('default/index.html.twig');
75
        } else {
76
            $redirect = $this->redirectToRoute($url[0], ['step' => $url[1]]);
77
        }
78
79
        return $redirect;
80
    }
81
82
    /**
83
     * Get stock alerts.
84
     *
85
     * @Route("/alert", name="stockalert")
86
     * @Method("GET")
87
     * @Template("default/stockAlert.html.twig")
88
     *
89
     * @param int $number number of alerts to display
90
     *
91
     * @return array|null
92
     */
93
    public function stockAlert($number)
94
    {
95
        $etm = $this->getDoctrine()->getManager();
96
        $listArticles = $etm->getRepository('App:Settings\Article')->getStockAlert($number);
97
        $helper = $this->get('app.helper.controller');
98
        foreach ($listArticles as $key => $article) {
99
            // Test the list if a supplier already has an order in progress
100
            if ($helper->isOrderInProgress($article, $etm)) {
101
                unset($listArticles[$key]);
102
            }
103
        }
104
105
        return array('listArticles' => $listArticles);
106
    }
107
108
    /**
109
     * Get the latest inventories.
110
     *
111
     * @Route("/alert", name="lastinventories")
112
     * @Method("GET")
113
     * @Template("default/lastInventory.html.twig")
114
     *
115
     * @param int $number Number of inventories to display
116
     *
117
     * @return array|null
118
     */
119
    public function lastInventory($number)
120
    {
121
        $etm = $this->getDoctrine()->getManager();
122
        $listInventories = $etm->getRepository('App:Stocks\Inventory')->getLastInventory($number);
123
124
        return array('listInventory' => $listInventories);
125
    }
126
127
    /**
128
     * Get the latest orders.
129
     *
130
     * @Route("/alert", name="lastorders")
131
     * @Method("GET")
132
     * @Template("default/lastOrder.html.twig")
133
     *
134
     * @param int $number Number of orders to display
135
     *
136
     * @return array|null
137
     */
138
    public function lastOrders($number)
139
    {
140
        $etm = $this->getDoctrine()->getManager();
141
        $listOrders = $etm->getRepository('App:Orders\Orders')->getLastOrder($number);
142
143
        return array('listOrders' => $listOrders);
144
    }
145
146
    /**
147
     * Get the latest deliveries.
148
     *
149
     * @Route("/alert", name="lastorders")
150
     * @Method("GET")
151
     * @Template("default/lastDelivery.html.twig")
152
     *
153
     * @param int $number Number of orders to display
154
     *
155
     * @return array|null
156
     */
157
    public function lastDeliveries($number)
158
    {
159
        $etm = $this->getDoctrine()->getManager();
160
        $listDeliveries = $etm->getRepository('App:Orders\Orders')->getLastDelivery($number);
161
162
        return array('listDeliveries' => $listDeliveries);
163
    }
164
165
    /**
166
     * Get the latest invoices.
167
     *
168
     * @Route("/alert", name="lastorders")
169
     * @Method("GET")
170
     * @Template("default/lastInvoice.html.twig")
171
     *
172
     * @param int $number Number of orders to display
173
     *
174
     * @return array|null
175
     */
176
    public function lastInvoices($number)
177
    {
178
        $etm = $this->getDoctrine()->getManager();
179
        $listInvoices = $etm->getRepository('App:Orders\Orders')->getLastInvoice($number);
180
181
        return ['listInvoices' => $listInvoices];
182
    }
183
184
    /**
185
     * Test entities.
186
     *
187
     * @return string|null
188
     */
189
    private function testEntities()
190
    {
191
        $url = null;
192
        $etm = $this->getDoctrine()->getManager();
193
        // Check that Entities are not empty
194
        $nbEntities = count($this->entities);
195
        for ($index = 0; $index < $nbEntities; ++$index) {
196
            $entity = $etm->getRepository($this->entities[$index][1]);
197
            $entityData = $entity->findAll();
198
199
            if (empty($entityData)) {
200
                $message = 'gestock.install.none';
201
                $url = ['gs_install', $this->entities[$index][0]];
202
                break;
203
            }
204
        }
205
        if (isset($message)) {
206
            $this->addFlash('warning', $message);
207
        }
208
209
        return $url;
210
    }
211
212
    /**
213
     * Get FamilyLog.
214
     *
215
     * @Route("/getfamilylog", name="getfamilylog")
216
     * @Method("POST")
217
     *
218
     * @param \Symfony\Component\HttpFoundation\Request $request Post request
219
     *
220
     * @return \Symfony\Component\HttpFoundation\Response
221
     */
222
    public function getFamilyLog(Request $request)
223
    {
224
        $return = new Response('Error');
225
        $etm = $this->getDoctrine()->getManager();
226
        if ($request->isXmlHttpRequest()) {
227
            $familyLog = array();
228
            $id = $request->get('id');
229
            if ('' != $id) {
230
                $supplier = $etm
231
                    ->getRepository('App:Settings\Supplier')
232
                    ->find($id);
233
                $familyLog['familylog'] = $supplier->getFamilyLog()->getId();
234
                $response = new Response();
235
                $data = json_encode($familyLog);
236
                $response->headers->set('Content-Type', 'application/json');
237
                $response->setContent($data);
238
                $return = $response;
239
            }
240
        }
241
242
        return $return;
243
    }
244
}
245