Completed
Push — Order ( a8dbff...a1a9f7 )
by Laurent
03:52
created

DefaultController::lastInvoicesAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
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
        return $url;
70
    }
71
72
    /**
73
     * Get stock alerts.
74
     *
75
     * @Route("/alert", name="stockalert")
76
     * @Method("GET")
77
     * @Template("default/stockAlert.html.twig")
78
     *
79
     * @param integer $number Number of alerts to display.
80
     * @return array|null
81
     */
82
    public function stockAlertAction($number)
83
    {
84
        $etm = $this->getDoctrine()->getManager();
85
        $listArticles = $etm->getRepository('AppBundle:Article')->getStockAlert($number);
86
        $helper = $this->get('app.helper.controller');
87
        foreach ($listArticles as $key => $article) {
88
            // Tester la liste si un fournisseur à déjà une commande en cours
89
            if ($helper->isOrderInProgress($article, $etm)) {
90
                unset($listArticles[$key]);
91
            }
92
        }
93
94
        return array('listArticles' => $listArticles);
95
    }
96
97
    /**
98
     * Get the latest inventories.
99
     *
100
     * @Route("/alert", name="lastinventories")
101
     * @Method("GET")
102
     * @Template("default/lastInventory.html.twig")
103
     *
104
     * @param integer $number Number of inventories to display
105
     * @return array|null
106
     */
107
    public function lastInventoryAction($number)
108
    {
109
        $etm = $this->getDoctrine()->getManager();
110
        $listInventories = $etm->getRepository('AppBundle:Inventory')->getLastInventory($number);
111
112
        return array('listInventory' => $listInventories);
113
    }
114
115
    /**
116
     * Get the latest orders.
117
     *
118
     * @Route("/alert", name="lastorders")
119
     * @Method("GET")
120
     * @Template("default/lastorder.html.twig")
121
     *
122
     * @param integer $number Number of orders to display
123
     * @return array|null
124
     */
125
    public function lastOrdersAction($number)
126
    {
127
        $etm = $this->getDoctrine()->getManager();
128
        $listOrders = $etm->getRepository('AppBundle:Orders')->getLastOrder($number);
129
130
        return array('listOrders' => $listOrders);
131
    }
132
133
    /**
134
     * Get the latest deliveries.
135
     *
136
     * @Route("/alert", name="lastorders")
137
     * @Method("GET")
138
     * @Template("default/lastDelivery.html.twig")
139
     *
140
     * @param integer $number Number of orders to display
141
     * @return array|null
142
     */
143
    public function lastDeliveriesAction($number)
144
    {
145
        $etm = $this->getDoctrine()->getManager();
146
        $listDeliveries = $etm->getRepository('AppBundle:Orders')->getLastDelivery($number);
147
148
        return array('listDeliveries' => $listDeliveries);
149
    }
150
151
    /**
152
     * Get the latest invoices.
153
     *
154
     * @Route("/alert", name="lastorders")
155
     * @Method("GET")
156
     * @Template("default/lastInvoice.html.twig")
157
     *
158
     * @param integer $number Number of orders to display
159
     * @return array|null
160
     */
161
    public function lastInvoicesAction($number)
162
    {
163
        $etm = $this->getDoctrine()->getManager();
164
        $listInvoices = $etm->getRepository('AppBundle:Orders')->getLastInvoice($number);
165
166
        return ['listInvoices' => $listInvoices];
167
    }
168
169
    /**
170
     * Test entities.
171
     *
172
     * @return string|null
173
     */
174
    private function testEntities()
175
    {
176
        $url = null;
177
        $etm = $this->getDoctrine()->getManager();
178
        // vérifie que les Entitées ne sont pas vides
179
        $nbEntities = count($this->entities);
180
        for ($index = 0; $index < $nbEntities; $index++) {
181
            $entity = $etm->getRepository($this->entities[$index]);
182
            $entityData = $entity->find(1);
183
184
            if (empty($entityData)) {
185
                $message = 'gestock.install.none';
186
                $url = 'gs_install';
187
                break;
188
            }
189
        }
190
        if (isset($message)) {
191
            $this->addFlash('warning', $message);
192
        }
193
        return $url;
194
    }
195
196
    /**
197
     * Get FamilyLog.
198
     *
199
     * @Route("/getfamilylog", name="getfamilylog")
200
     * @Method("POST")
201
     *
202
     * @param \Symfony\Component\HttpFoundation\Request $request Post request
203
     * @return \Symfony\Component\HttpFoundation\Response
204
     */
205
    public function getFamilyLogAction(Request $request)
206
    {
207
        $return = new Response('Error');
208
        $etm = $this->getDoctrine()->getManager();
209
        if ($request->isXmlHttpRequest()) {
210
            $familyLog = array();
211
            $id = $request->get('id');
212
            if ($id != '') {
213
                $supplier = $etm
214
                    ->getRepository('AppBundle:Supplier')
215
                    ->find($id);
216
                $familyLog['familylog'] = $supplier->getFamilyLog()->getId();
217
                $response = new Response();
218
                $data = json_encode($familyLog);
219
                $response->headers->set('Content-Type', 'application/json');
220
                $response->setContent($data);
221
                $return = $response;
222
            }
223
        }
224
        return $return;
225
    }
226
}
227