Completed
Push — master ( 8974a5...43dd1c )
by Adam
06:18
created

OrderController::createOrderNoteResource()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 8
Code Lines 4

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OrderBundle\Controller\Admin;
14
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
18
use WellCommerce\Bundle\OrderBundle\Entity\Order;
19
use WellCommerce\Bundle\OrderBundle\Entity\OrderNote;
20
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
21
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
22
use WellCommerce\Component\Form\Elements\FormInterface;
23
24
/**
25
 * Class OrderController
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class OrderController extends AbstractAdminController
30
{
31
    public function indexAction(): Response
32
    {
33
        return $this->displayTemplate('index', [
34
            'datagrid' => $this->dataGrid,
35
            'statuses' => $this->get('order_status.dataset.admin')->getResult('select'),
36
        ]);
37
    }
38
    
39
    public function editAction(int $id): Response
40
    {
41
        $resource = $this->getManager()->getRepository()->find($id);
42
        if (!$resource instanceof Order) {
43
            return $this->redirectToAction('index');
44
        }
45
        
46
        $this->getOrderProvider()->setCurrentOrder($resource);
47
        
48
        $form = $this->formBuilder->createForm($resource, [
49
            'class' => 'editOrder',
50
        ]);
51
        
52
        if ($form->handleRequest()->isSubmitted()) {
53
            if ($form->isValid()) {
54
                $this->getManager()->updateResource($resource);
55
            }
56
            
57
            return $this->createFormDefaultJsonResponse($form);
58
        }
59
        
60
        $orderStatusHistory     = $this->createOrderStatusHistoryResource($resource);
61
        $orderStatusHistoryForm = $this->createOrderStatusHistoryForm($orderStatusHistory);
62
        $orderNote              = $this->createOrderNoteResource($resource);
63
        $orderNoteForm          = $this->createOrderNoteForm($orderNote);
64
        
65
        if ($orderStatusHistoryForm->handleRequest()->isSubmitted()) {
66
            if ($orderStatusHistoryForm->isValid()) {
67
                $this->get('order_status_history.manager')->createResource($orderStatusHistory);
68
            }
69
            
70
            return $this->jsonResponse([
71
                'valid'      => $orderStatusHistoryForm->isValid(),
72
                'next'       => false,
73
                'continue'   => false,
74
                'redirectTo' => $this->getRedirectToActionUrl('edit', ['id' => $resource->getId()]),
75
                'error'      => $orderStatusHistoryForm->getError(),
76
            ]);
77
        }
78
        
79
        if ($orderNoteForm->handleRequest()->isSubmitted()) {
80
            if ($orderNoteForm->isValid()) {
81
                $this->get('order_note.manager')->createResource($orderNote);
82
            }
83
            
84
            return $this->jsonResponse([
85
                'valid'      => $orderNoteForm->isValid(),
86
                'next'       => false,
87
                'continue'   => false,
88
                'redirectTo' => $this->getRedirectToActionUrl('edit', ['id' => $resource->getId()]),
89
                'error'      => $orderNoteForm->getError(),
90
            ]);
91
        }
92
        
93
        return $this->displayTemplate('edit', [
94
            'form'                   => $form,
95
            'orderStatusHistoryForm' => $orderStatusHistoryForm,
96
            'orderNoteForm'          => $orderNoteForm,
97
            'resource'               => $resource,
98
        ]);
99
    }
100
    
101
    public function ajaxChangeOrderStatusAction(Request $request)
102
    {
103
        $id       = $request->get('id');
104
        $statusId = $request->get('status');
105
        $status   = $this->get('order_status.repository')->find($statusId);
106
        
107
        if (!$status instanceof OrderStatus) {
108
            return $this->jsonResponse([
109
                'success' => false,
110
                'error'   => 'Wrong order status was given',
111
            ]);
112
        }
113
        
114
        $order = $this->getManager()->getRepository()->find($id);
115
        if ($order instanceof Order) {
116
            $order->setCurrentStatus($status);
117
            $history = $this->createOrderStatusHistoryResource($order);
118
            $history->setNotify(true);
119
            $this->get('order_status_history.manager')->createResource($history);
120
        }
121
        
122
        return $this->jsonResponse([
123
            'success' => true,
124
            'status'  => $status,
125
            'number'  => $order->getNumber(),
126
        ]);
127
    }
128
    
129
    public function ajaxChangeOrderStatusMultiAction(Request $request)
130
    {
131
        $ids      = $request->get('ids');
132
        $statusId = $request->get('status');
133
        $status   = $this->get('order_status.repository')->find($statusId);
134
        
135
        if (!$status instanceof OrderStatus) {
136
            return $this->jsonResponse([
137
                'success' => false,
138
                'error'   => 'Wrong order status was given',
139
            ]);
140
        }
141
        
142
        foreach ($ids as $id) {
143
            $order = $this->getManager()->getRepository()->find($id);
144
            if ($order instanceof Order) {
145
                $order->setCurrentStatus($status);
146
                $history = $this->createOrderStatusHistoryResource($order);
147
                $history->setNotify(true);
148
                $this->get('order_status_history.manager')->createResource($history);
149
            }
150
        }
151
        
152
        return $this->jsonResponse([
153
            'success' => true,
154
            'status'  => $status,
155
            'ids'     => implode(',', $ids),
156
        ]);
157
    }
158
    
159
    protected function createOrderStatusHistoryForm(OrderStatusHistory $orderStatusHistory): FormInterface
160
    {
161
        return $this->get('order_status_history.form_builder.admin')->createForm($orderStatusHistory, [
162
            'class' => 'statusChange',
163
        ]);
164
    }
165
    
166
    protected function createOrderNoteForm(OrderNote $orderNote): FormInterface
167
    {
168
        return $this->get('order_note.form_builder.admin')->createForm($orderNote, [
169
            'class' => 'statusChange',
170
        ]);
171
    }
172
    
173
    protected function createOrderNoteResource(Order $order): OrderNote
174
    {
175
        /** @var $orderNote OrderNote */
176
        $orderNote = $this->get('order_note.factory')->create();
177
        $orderNote->setOrder($order);
178
        
179
        return $orderNote;
180
    }
181
    
182
    protected function createOrderStatusHistoryResource(Order $order): OrderStatusHistory
183
    {
184
        /** @var $orderStatusHistory OrderStatusHistory */
185
        $orderStatusHistory = $this->get('order_status_history.factory')->create();
186
        $orderStatusHistory->setNotify(false);
187
        $orderStatusHistory->setComment('');
188
        $orderStatusHistory->setOrder($order);
189
        $orderStatusHistory->setOrderStatus($order->getCurrentStatus());
190
        
191
        return $orderStatusHistory;
192
    }
193
}
194