Completed
Push — Order ( f4bad4 )
by Laurent
03:15
created

InvoicesController::showAction()   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 1
1
<?php
2
/**
3
 * InvoicesController controller de la facturation.
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 AppBundle\Controller\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;
24
use AppBundle\Form\Type\InvoicesEditType;
25
26
/**
27
 * Invoices controller.
28
 *
29
 * @Route("/invoices")
30
 */
31
class InvoicesController extends AbstractOrdersController
32
{
33
    /**
34
     * Lists all Orders entities.
35
     *
36
     * @Route("/", name="invoices")
37
     * @Method("GET")
38
     * @Template()
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        $etm = $this->getDoctrine()->getManager();
43
        $item = $this->container->getParameter('knp_paginator.page_range');
44
        $qbd = $etm->getRepository('AppBundle:Orders')->findInvoices();
45
        
46
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), $item);
47
        return array(
48
            'paginator' => $paginator,
49
        );
50
    }
51
52
    /**
53
     * Finds and displays a Orders entity.
54
     *
55
     * @Route("/{id}/show", name="invoices_show", requirements={"id"="\d+"})
56
     * @Method("GET")
57
     * @Template()
58
     */
59
    public function showAction(Orders $orders)
60
    {
61
        return array(
62
            'orders' => $orders,
63
        );
64
    }
65
66
    /**
67
     * Displays a form to edit an existing Orders entity.
68
     *
69
     * @Route("/admin/{id}/edit", name="invoices_edit", requirements={"id"="\d+"})
70
     * @Method("GET")
71
     * @Template()
72
     */
73 View Code Duplication
    public function editAction(Orders $orders)
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...
74
    {
75
        $editForm = $this->createForm(InvoicesEditType::class, $orders, array(
76
            'action' => $this->generateUrl('invoices_update', array('id' => $orders->getId())),
77
            'method' => 'PUT',
78
        ));
79
80
        return array(
81
            'orders' => $orders,
82
            'edit_form'   => $editForm->createView(),
83
        );
84
    }
85
86
    /**
87
     * Edits an existing Orders entity.
88
     *
89
     * @Route("/admin/{id}/update", name="invoices_update", requirements={"id"="\d+"})
90
     * @Method("PUT")
91
     * @Template("AppBundle:Deliveries:edit.html.twig")
92
     */
93 View Code Duplication
    public function updateAction(Orders $orders, 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...
94
    {
95
        $etm = $this->getDoctrine()->getManager();
96
97
        $editForm = $this->createForm(InvoicesEditType::class, $orders, array(
98
            'action' => $this->generateUrl('invoices_update', array('id' => $orders->getId())),
99
            'method' => 'PUT',
100
        ));
101
        $editForm->handleRequest($request);
102
103
        $return = array(
104
            'orders' => $orders,
105
            'edit_form'   => $editForm->createView(),
106
        );
107
        
108
        if ($editForm->isValid()) {
109
            $orders->setStatus(3);
110
            $etm->persist($orders);
111
            $this->updateArticles($orders, $etm);
112
            $this->addFlash('info', 'gestock.edit.ok');
113
            $etm->flush();
114
115
            $return = $this->redirect($this->generateUrl('_home'));
116
        }
117
118
        return $return;
119
    }
120
121
    /**
122
     * Print the current invoice.<br />Creating a `PDF` file for viewing on paper
123
     *
124
     * @Route("/{id}/print/", name="invoices_print", requirements={"id"="\d+"})
125
     * @Method("GET")
126
     * @Template()
127
     *
128
     * @param \AppBundle\Entity\Orders $orders Order item to print
129
     * @return \Symfony\Component\HttpFoundation\Response
130
     */
131
    public function printAction(Orders $orders)
132
    {
133
        $return = $this->abstractPrintAction($orders, 'Invoices');
134
        
135
        return $return;
136
    }
137
138
    /**
139
     * Update Articles.
140
     *
141
     * @param \AppBundle\Entity\Orders   $orders   Articles de la commande à traiter
142
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
143
     */
144
    private function updateArticles(Orders $orders, $etm)
145
    {
146
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($orders->getSupplier()->getId());
147
        foreach ($orders->getArticles() as $line) {
148
            foreach ($articles as $art) {
149
                if ($art->getId() === $line->getArticle()->getId() && $art->getPrice() !== $line->getPrice()) {
150
                    $stockAmount = ($art->getQuantity() - $line->getQuantity()) * $art->getPrice();
151
                    $orderAmount = $line->getQuantity() * $line->getPrice();
152
                    $newPrice = ($stockAmount + $orderAmount) / $art->getQuantity();
153
                    $art->setPrice($newPrice);
154
                    $etm->persist($art);
155
                }
156
            }
157
        }
158
    }
159
}
160