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

OrdersController::deleteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * OrdersController controller des commandes.
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\Orders;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\Orders\AbstractOrdersController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
23
use AppBundle\Entity\Orders\Orders;
24
use AppBundle\Entity\Supplier;
25
use AppBundle\Form\Type\Orders\OrdersType;
26
use AppBundle\Form\Type\Orders\OrdersEditType;
27
use AppBundle\Entity\Orders\OrdersArticles;
28
29
/**
30
 * Orders controller.
31
 *
32
 * @Route("/orders")
33
 */
34
class OrdersController extends AbstractOrdersController
35
{
36
    /**
37
     * Lists all Orders entities.
38
     *
39
     * @Route("/", name="orders")
40
     * @Method("GET")
41
     * @Template()
42
     */
43 View Code Duplication
    public function indexAction(Request $request)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $return = $this->abstractIndexAction('Orders\Orders', 'orders', $request);
46
47
        $createForm = $this->createCreateForm('orders_create');
48
        $return['create_form'] = $createForm->createView();
49
50
        return $return;
51
    }
52
53
    /**
54
     * Finds and displays a Orders entity.
55
     *
56
     * @Route("/{id}/show", name="orders_show", requirements={"id"="\d+"})
57
     * @Method("GET")
58
     * @Template()
59
     */
60
    public function showAction(Orders $orders)
61
    {
62
        $deleteForm = $this->createDeleteForm($orders->getId(), 'orders_delete');
63
64
        return array(
65
            'orders' => $orders,
66
            'delete_form' => $deleteForm->createView(),
67
        );
68
    }
69
70
    /**
71
     * Displays a form to create a new Orders entity.
72
     *
73
     * @Route("/new/{supplier}", name="orders_new")
74
     * @Method("GET")
75
     * @Template()
76
     */
77
    public function newAction(Supplier $supplier)
78
    {
79
        $etm = $this->getDoctrine()->getManager();
80
81
        $orders = new Orders();
82
        $orders->setSupplier($supplier);
83
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier->getId());
84
85
        // Set Orders dates (order and delivery)
86
        $orderDate = $supplier->getOrderdate();
87
        foreach ($orderDate as $date) {
88
            $orders = $this->setDates($date, $orders, $supplier);
89
        }
90
91
        $etm->persist($orders);
92
        // Saving articles of supplier in order
93
        $this->saveOrdersArticles($articles, $orders, $etm);
94
95
        $etm->flush();
96
97
        return $this->redirect($this->generateUrl('orders_edit', array('id' => $orders->getId())));
98
    }
99
100
    /**
101
     * Creates a new Orders entity.
102
     *
103
     * @Route("/admin/create", name="orders_create")
104
     * @Method("POST")
105
     * @Template("AppBundle:Orders/Orders:new.html.twig")
106
     */
107
    public function createAction(Request $request)
108
    {
109
        $etm = $this->getDoctrine()->getManager();
110
111
        $orders = new Orders();
112
        $form = $this->createForm(OrdersType::class, $orders);
113
        $return = ['orders' => $orders, 'form' => $form->createView(),];
114
        $form->handleRequest($request);
115
        $supplier = $orders->getSupplier();
116
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier->getId());
117
        // Tester la liste si un fournisseur à déjà une commande en cours
118
        $test = $this->get('app.helper.controller')->hasSupplierArticles($articles);
119
        if ($test === false) {
120
            $return = $this->redirectToRoute('orders');
121
        } else {
122
            // Set Orders dates (order and delivery)
123
            $orderDate = $supplier->getOrderdate();
124
            foreach ($orderDate as $date) {
125
                $orders = $this->setDates($date, $orders, $supplier);
1 ignored issue
show
Bug introduced by
It seems like $supplier defined by $orders->getSupplier() on line 115 can also be of type string; however, AppBundle\Controller\Ord...sController::setDates() does only seem to accept object<AppBundle\Entity\Supplier>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
126
            }
127
128
            if ($form->isValid() && $return !== $this->redirectToRoute('orders')) {
129
                $etm->persist($orders);
130
                // Saving articles of supplier in order
131
                $this->saveOrdersArticles($articles, $orders, $etm);
132
133
                $etm->flush();
134
135
                $return = $this->redirect($this->generateUrl('orders_edit', array('id' => $orders->getId())));
136
            }
137
        }
138
        return $return;
139
    }
140
141
    /**
142
     * Displays a form to edit an existing Orders entity.
143
     *
144
     * @Route("/admin/{id}/edit", name="orders_edit", requirements={"id"="\d+"})
145
     * @Method("GET")
146
     * @Template()
147
     */
148
    public function editAction(Orders $orders)
149
    {
150
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
151
            'action' => $this->generateUrl('orders_update', array('id' => $orders->getId())),
152
            'method' => 'PUT',
153
        ));
154
        $deleteForm = $this->createDeleteForm($orders->getId(), 'orders_delete');
155
156
        return array(
157
            'orders' => $orders,
158
            'edit_form'   => $editForm->createView(),
159
            'delete_form' => $deleteForm->createView(),
160
        );
161
    }
162
163
    /**
164
     * Edits an existing Orders entity.
165
     *
166
     * @Route("/admin/{id}/update", name="orders_update", requirements={"id"="\d+"})
167
     * @Method("PUT")
168
     * @Template("AppBundle:Orders/Orders:edit.html.twig")
169
     */
170
    public function updateAction(Orders $orders, Request $request)
171
    {
172
        $editForm = $this->createForm(OrdersEditType::class, $orders, array(
173
            'action' => $this->generateUrl('orders_update', array('id' => $orders->getId())),
174
            'method' => 'PUT',
175
        ));
176
        $deleteForm = $this->createDeleteForm($orders->getId(), 'orders_delete');
177
        $editForm->handleRequest($request);
178
179
        $return = array(
180
            'orders' => $orders,
181
            'edit_form'   => $editForm->createView(),
182
            'delete_form' => $deleteForm->createView(),
183
        );
184
        
185
        if ($editForm->isValid()) {
186
            $this->getDoctrine()->getManager()->flush();
187
            $this->addFlash('info', 'gestock.edit.ok');
188
189
            $return = $this->redirect($this->generateUrl('orders_edit', ['id' => $orders->getId()]));
190
        }
191
192
        return $return;
193
    }
194
195
    /**
196
     * Deletes a Orders entity.
197
     *
198
     * @Route("/admin/{id}/delete", name="orders_delete", requirements={"id"="\d+"})
199
     * @Method("DELETE")
200
     */
201
    public function deleteAction(Orders $orders, Request $request)
202
    {
203
        $return = $this->abstractDeleteWithArticlesAction($orders, $request, 'Orders\Orders', 'orders');
204
        
205
        return $return;
206
    }
207
208
    /**
209
     * Set order Dates.
210
     *
211
     * @param integer                           $date     Jour de la commande
212
     * @param \AppBundle\Entity\Orders\Orders   $orders   La commande à traiter
213
     * @param \AppBundle\Entity\Supplier        $supplier Le fournisseur concerné
214
     * @return \AppBundle\Entity\Orders\Orders            La commande modifiée
215
     */
216
    private function setDates($date, Orders $orders, Supplier $supplier)
217
    {
218
        $setOrderDate = new \DateTime(date('Y-m-d'));
219
        if ($date >= date('w')) {
220
            $diffOrder = $date - date('w');
221
            $setOrderDate->add(new \DateInterval('P'.$diffOrder.'DT18H'));
222
            $diffDeliv = $this->get('app.helper.time')
223
                ->getNextOpenDay($setOrderDate->getTimestamp(), $supplier->getDelaydeliv());
224
225
            $setDelivDate = new \DateTime(date('Y-m-d', $setOrderDate->getTimestamp()));
226
            $setDelivDate->add(new \DateInterval('P'.$diffDeliv.'D'));
227
228
            $orders->setOrderdate($setOrderDate);
229
            $orders->setDelivdate($setDelivDate);
230
        }
231
        return $orders;
232
    }
233
234
    /**
235
     * Save OrdersArticles.
236
     *
237
     * @param array                             $articles Liste des articles
238
     * @param \AppBundle\Entity\Orders\Orders   $orders   La commande à traiter
239
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
240
     */
241 View Code Duplication
    private function saveOrdersArticles($articles, $orders, $etm)
242
    {
243
        foreach ($articles as $article) {
244
            $ordersArticles = new OrdersArticles();
245
            $ordersArticles->setOrders($orders);
246
            $ordersArticles->setArticle($article);
247
            $ordersArticles->setUnitStorage($article->getUnitStorage());
248
            if ($article->getMinstock() > $article->getQuantity()) {
249
                $ordersArticles->setQuantity($article->getMinstock() - $article->getQuantity());
250
            }
251
            $ordersArticles->setPrice($article->getPrice());
252
            $ordersArticles->setTva($article->getTva());
253
            $etm->persist($ordersArticles);
254
        }
255
    }
256
257
    /**
258
     * Print the current order.<br />Creating a `PDF` file for viewing on paper
259
     *
260
     * @Route("/{id}/print/", name="orders_print", requirements={"id"="\d+"})
261
     * @Method("GET")
262
     * @Template()
263
     *
264
     * @param \AppBundle\Entity\Orders\Orders $orders Order item to print
265
     * @return \Symfony\Component\HttpFoundation\Response
266
     */
267
    public function printAction(Orders $orders)
268
    {
269
        $return = $this->abstractPrintAction($orders, 'Orders');
270
        
271
        return $return;
272
    }
273
}
274