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

OrdersController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 230
Duplicated Lines 10.43 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 24
loc 230
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 9 9 1
A showAction() 0 9 1
A newAction() 0 22 2
A createAction() 0 33 5
A editAction() 0 9 1
A updateAction() 0 19 2
A deleteAction() 0 6 1
A setDates() 0 17 2
A saveOrdersArticles() 15 15 3
A printAction() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * OrdersController Controller of orders.
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
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace App\Controller\Orders;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use App\Controller\Orders\AbstractOrdersController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
23
use App\Entity\Orders\Orders;
24
use App\Entity\Settings\Supplier;
25
use App\Form\Type\Orders\OrdersType;
26
use App\Form\Type\Orders\OrdersEditType;
27
use App\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)
0 ignored issues
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('App:Settings\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("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('App:Settings\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);
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, ['action' => $this
151
            ->generateUrl('orders_update', ['id' => $orders->getId()]), 'method' => 'PUT',]);
152
        $deleteForm = $this->createDeleteForm($orders->getId(), 'orders_delete');
153
154
        return ['orders' => $orders, 'edit_form' => $editForm->createView(),
155
            'delete_form' => $deleteForm->createView(),];
156
    }
157
158
    /**
159
     * Edits an existing Orders entity.
160
     *
161
     * @Route("/admin/{id}/update", name="orders_update", requirements={"id"="\d+"})
162
     * @Method("PUT")
163
     * @Template("orders/orders/edit.html.twig")
164
     */
165
    public function updateAction(Orders $orders, Request $request)
166
    {
167
        $editForm = $this->createForm(OrdersEditType::class, $orders, ['action' => $this
168
            ->generateUrl('orders_update', ['id' => $orders->getId()]), 'method' => 'PUT',]);
169
        $deleteForm = $this->createDeleteForm($orders->getId(), 'orders_delete');
170
        $editForm->handleRequest($request);
171
172
        $return = ['orders' => $orders, 'edit_form'   => $editForm->createView(),
173
            'delete_form' => $deleteForm->createView(),];
174
        
175
        if ($editForm->isValid()) {
176
            $this->getDoctrine()->getManager()->flush();
177
            $this->addFlash('info', 'gestock.edit.ok');
178
179
            $return = $this->redirect($this->generateUrl('orders_edit', ['id' => $orders->getId()]));
180
        }
181
182
        return $return;
183
    }
184
185
    /**
186
     * Deletes a Orders entity.
187
     *
188
     * @Route("/admin/{id}/delete", name="orders_delete", requirements={"id"="\d+"})
189
     * @Method("DELETE")
190
     */
191
    public function deleteAction(Orders $orders, Request $request)
192
    {
193
        $return = $this->abstractDeleteWithArticlesAction($orders, $request, 'Orders\Orders', 'orders');
194
        
195
        return $return;
196
    }
197
198
    /**
199
     * Set order Dates.
200
     *
201
     * @param integer                       $date     Order day
202
     * @param \App\Entity\Orders\Orders     $orders   The order to process
203
     * @param \App\Entity\Settings\Supplier $supplier The supplier concerned
204
     * @return \App\Entity\Orders\Orders              The amended order
205
     */
206
    private function setDates($date, Orders $orders, Supplier $supplier)
207
    {
208
        $setOrderDate = new \DateTime(date('Y-m-d'));
209
        if ($date >= date('w')) {
210
            $diffOrder = $date - date('w');
211
            $setOrderDate->add(new \DateInterval('P'.$diffOrder.'DT18H'));
212
            $diffDeliv = $this->get('app.helper.time')
213
                ->getNextOpenDay($setOrderDate->getTimestamp(), $supplier->getDelaydeliv());
214
215
            $setDelivDate = new \DateTime(date('Y-m-d', $setOrderDate->getTimestamp()));
216
            $setDelivDate->add(new \DateInterval('P'.$diffDeliv.'D'));
217
218
            $orders->setOrderdate($setOrderDate);
219
            $orders->setDelivdate($setDelivDate);
220
        }
221
        return $orders;
222
    }
223
224
    /**
225
     * Save OrdersArticles.
226
     *
227
     * @param array                                      $articles List of articles
228
     * @param \App\Entity\Orders\Orders                  $orders   The order to process
229
     * @param \Doctrine\Common\Persistence\ObjectManager $etm      Entity Manager
230
     */
231 View Code Duplication
    private function saveOrdersArticles($articles, $orders, $etm)
0 ignored issues
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...
232
    {
233
        foreach ($articles as $article) {
234
            $ordersArticles = new OrdersArticles();
235
            $ordersArticles->setOrders($orders);
236
            $ordersArticles->setArticle($article);
237
            $ordersArticles->setUnitStorage($article->getUnitStorage());
238
            if ($article->getMinstock() > $article->getQuantity()) {
239
                $ordersArticles->setQuantity($article->getMinstock() - $article->getQuantity());
240
            }
241
            $ordersArticles->setPrice($article->getPrice());
242
            $ordersArticles->setTva($article->getTva());
243
            $etm->persist($ordersArticles);
244
        }
245
    }
246
247
    /**
248
     * Print the current order.<br />Creating a `PDF` file for viewing on paper
249
     *
250
     * @Route("/{id}/print/", name="orders_print", requirements={"id"="\d+"})
251
     * @Method("GET")
252
     * @Template()
253
     *
254
     * @param \App\Entity\Orders\Orders $orders Order item to print
255
     * @return \Symfony\Component\HttpFoundation\Response
256
     */
257
    public function printAction(Orders $orders)
258
    {
259
        $return = $this->abstractPrintAction($orders, 'Orders');
260
        
261
        return $return;
262
    }
263
}
264