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

AbstractOrdersController::updateInvoiceArticles()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 10
nc 4
nop 2
1
<?php
2
/**
3
 * AbstractOrdersController Méthodes communes InventoryController.
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\AbstractController;
19
use Symfony\Component\HttpFoundation\Response;
20
21
use AppBundle\Entity\Orders\Orders;
22
use AppBundle\Form\Type\Orders\OrdersType;
23
24
/**
25
 * Abstract controller.
26
 *
27
 * @category Controller
28
 */
29
class AbstractOrdersController extends AbstractController
30
{
31
    /**
32
     * Create CreateForm.
33
     *
34
     * @param string $route Route of action form
35
     * @return \Symfony\Component\Form\Form
36
     */
37
    protected function createCreateForm($route)
38
    {
39
        $orders = new Orders();
40
        return $this->createForm(
41
            OrdersType::class,
42
            $orders,
43
            ['attr' => ['id' => 'create'], 'action' => $this->generateUrl($route), 'method' => 'POST',]
44
        );
45
    }
46
47
    /**
48
     * Displays a form to edit an existing item entity.
49
     *
50
     * @param Object $entity      Entity
51
     * @param string $prefixRoute Prefix of Route
52
     * @param string $typePath    Path of FormType
53
     * @return array
54
     */
55
    public function abstractEditAction($entity, $prefixRoute, $typePath)
56
    {
57
        $param = $this->testReturnParam($entity, $prefixRoute);
58
        $editForm = $this->createForm($typePath, $entity, array(
59
            'action' => $this->generateUrl($prefixRoute.'_update', $param),
60
            'method' => 'PUT',
61
        ));
62
        if ($prefixRoute === 'group') {
63
            $this->addRolesAction($editForm, $entity);
64
        }
65
66
        return [$prefixRoute => $entity, 'edit_form' => $editForm->createView(),];
67
    }
68
69
    /**
70
     * Edits an existing item entity.
71
     *
72
     * @param Object                                    $entity      Entity
73
     * @param \Symfony\Component\HttpFoundation\Request $request     Request in progress
74
     * @param string                                    $prefixRoute Prefix of Route
75
     * @param string                                    $typePath    Path of FormType
76
     * @return array
77
     */
78
    public function abstractUpdateAction($entity, Request $request, $prefixRoute, $typePath)
79
    {
80
        $etm = $this->getDoctrine()->getManager();
81
        $param = $this->testReturnParam($entity, $prefixRoute);
82
        $editForm = $this->createForm($typePath, $entity, array(
83
            'action' => $this->generateUrl($prefixRoute.'_update', $param),
84
            'method' => 'PUT',
85
        ));
86
87
        $editForm->handleRequest($request);
88
89
        $return = [$prefixRoute => $entity, 'edit_form'   => $editForm->createView(), ];
90
91
        if ($editForm->isValid()) {
92
            if ($prefixRoute === 'deliveries') {
93
                $entity->setStatus(2);
94
                $this->updateDeliveryArticles($entity, $etm);
95
            }
96
            if ($prefixRoute === 'invoices') {
97
                $entity->setStatus(3);
98
                $this->updateInvoiceArticles($entity, $etm);
99
            }
100
            $etm->persist($entity);
101
            $etm->flush();
102
103
            $this->addFlash('info', 'gestock.edit.ok');
104
105
            $return = $this->redirectToRoute('_home');
106
        }
107
108
        return $return;
109
    }
110
111
    /**
112
     * Print a document.<br />Creating a `PDF` file for viewing on paper
113
     *
114
     * @param \AppBundle\Entity\Orders\Orders $orders Order item to print
115
     * @param string $from The Controller who call this action
116
     * @return \Symfony\Component\HttpFoundation\Response
117
     */
118
    protected function abstractPrintAction(Orders $orders, $from)
119
    {
120
        $file = $from . '-' . $orders->getId() . '.pdf';
121
        $company = $this->getDoctrine()->getManager()->getRepository('AppBundle:Company')->find(1);
122
        // Create and save the PDF file to print
123
        $html = $this->renderView(
124
            'AppBundle:Orders/' . $from . ':print.pdf.twig',
125
            ['articles' => $orders->getArticles(), 'orders' => $orders, 'company' => $company, ]
126
        );
127
        return new Response(
128
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
129
                $html,
130
                $this->get('app.helper.controller')->getArray((string)date('d/m/y - H:i:s'), '')
131
            ),
132
            200,
133
            array(
134
                'Content-Type' => 'application/pdf',
135
                'Content-Disposition' => 'attachment; filename="' . $file . '"'
136
            )
137
        );
138
    }
139
140
    /**
141
     * Update Articles for deliveries.
142
     *
143
     * @param \AppBundle\Entity\Orders\Orders $orders Articles de la commande à traiter
144
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
145
     */
146
    private function updateDeliveryArticles(Orders $orders, $etm)
147
    {
148
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($orders->getSupplier()->getId());
149
        foreach ($orders->getArticles() as $line) {
150
            foreach ($articles as $art) {
151
                if ($art->getId() === $line->getArticle()->getId()) {
152
                    $art->setQuantity($line->getQuantity() + $art->getQuantity());
153
                    $etm->persist($art);
154
                }
155
            }
156
        }
157
    }
158
159
    /**
160
     * Update Articles for invoices.
161
     *
162
     * @param \AppBundle\Entity\Orders\Orders $orders Articles de la commande à traiter
163
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Entity Manager
164
     */
165
    private function updateInvoiceArticles(Orders $orders, $etm)
166
    {
167
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($orders->getSupplier()->getId());
168
        foreach ($orders->getArticles() as $line) {
169
            foreach ($articles as $art) {
170
                if ($art->getId() === $line->getArticle()->getId() && $art->getPrice() !== $line->getPrice()) {
171
                    $stockAmount = ($art->getQuantity() - $line->getQuantity()) * $art->getPrice();
172
                    $orderAmount = $line->getQuantity() * $line->getPrice();
173
                    $newPrice = ($stockAmount + $orderAmount) / $art->getQuantity();
174
                    $art->setPrice($newPrice);
175
                    $etm->persist($art);
176
                }
177
            }
178
        }
179
    }
180
}
181