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

AbstractOrdersController::abstractPrintAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * AbstractOrdersController Common Methods InventoryController.
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 App\Controller\AbstractAppController;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
use App\Entity\Orders\Orders;
22
use App\Form\Type\Orders\OrdersType;
23
24
/**
25
 * Abstract controller.
26
 *
27
 * @category Controller
28
 */
29
class AbstractOrdersController extends AbstractAppController
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);
0 ignored issues
show
Compatibility introduced by
$editForm of type object<Symfony\Component\Form\FormInterface> is not a sub-type of object<Symfony\Component\Form\Form>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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 \App\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('App:Settings\Company')->find(1);
122
        // Create and save the PDF file to print
123
        $html = $this->renderView(
124
            'App: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 \App\Entity\Orders\Orders                  $orders Articles from order to deal
144
     * @param \Doctrine\Common\Persistence\ObjectManager $etm    Entity Manager
145
     */
146
    private function updateDeliveryArticles(Orders $orders, $etm)
147
    {
148
        $articles = $etm->getRepository('App:Settings\Article')
149
            ->getArticleFromSupplier($orders->getSupplier()->getId());
150
        foreach ($orders->getArticles() as $line) {
151
            foreach ($articles as $art) {
152
                if ($art->getId() === $line->getArticle()->getId()) {
153
                    $art->setQuantity($line->getQuantity() + $art->getQuantity());
154
                    $etm->persist($art);
155
                }
156
            }
157
        }
158
    }
159
160
    /**
161
     * Update Articles for invoices.
162
     *
163
     * @param \App\Entity\Orders\Orders                  $orders Articles from order to deal
164
     * @param \Doctrine\Common\Persistence\ObjectManager $etm    Entity Manager
165
     */
166
    private function updateInvoiceArticles(Orders $orders, $etm)
167
    {
168
        $articles = $etm->getRepository('App:Settings\Article')
169
            ->getArticleFromSupplier($orders->getSupplier()->getId());
170
        foreach ($orders->getArticles() as $line) {
171
            foreach ($articles as $art) {
172
                if ($art->getId() === $line->getArticle()->getId() && $art->getPrice() !== $line->getPrice()) {
173
                    $stockAmount = ($art->getQuantity() - $line->getQuantity()) * $art->getPrice();
174
                    $orderAmount = $line->getQuantity() * $line->getPrice();
175
                    $newPrice = ($stockAmount + $orderAmount) / $art->getQuantity();
176
                    $art->setPrice($newPrice);
177
                    $etm->persist($art);
178
                }
179
            }
180
        }
181
    }
182
}
183