Complex classes like Shopware_Controllers_Backend_Order often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Shopware_Controllers_Backend_Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Shopware_Controllers_Backend_Order extends Shopware_Controllers_Backend_ExtJs implements CSRFWhitelistAware |
||
53 | { |
||
54 | /** |
||
55 | * Order repository. Declared for an fast access to the order repository. |
||
56 | * |
||
57 | * @var \Shopware\Models\Order\Repository |
||
58 | */ |
||
59 | public static $repository; |
||
60 | |||
61 | /** |
||
62 | * Shop repository. Declared for an fast access to the shop repository. |
||
63 | * |
||
64 | * @var \Shopware\Models\Shop\Repository |
||
65 | */ |
||
66 | public static $shopRepository; |
||
67 | |||
68 | /** |
||
69 | * Country repository. Declared for an fast access to the country repository. |
||
70 | * |
||
71 | * @var \Shopware\Models\Country\Repository |
||
72 | */ |
||
73 | public static $countryRepository; |
||
74 | |||
75 | /** |
||
76 | * Payment repository. Declared for an fast access to the country repository. |
||
77 | * |
||
78 | * @var \Shopware\Models\Payment\Repository |
||
79 | */ |
||
80 | public static $paymentRepository; |
||
81 | |||
82 | /** |
||
83 | * Dispatch repository. Declared for an fast access to the dispatch repository. |
||
84 | * |
||
85 | * @var \Shopware\Models\Dispatch\Repository |
||
86 | */ |
||
87 | public static $dispatchRepository; |
||
88 | |||
89 | /** |
||
90 | * Contains the shopware model manager |
||
91 | * |
||
92 | * @var \Shopware\Components\Model\ModelManager |
||
93 | */ |
||
94 | public static $manager; |
||
95 | |||
96 | /** |
||
97 | * Contains the dynamic receipt repository |
||
98 | * |
||
99 | * @var \Shopware\Components\Model\ModelRepository |
||
100 | */ |
||
101 | public static $documentRepository; |
||
102 | |||
103 | /** |
||
104 | * Registers the different acl permission for the different controller actions. |
||
105 | */ |
||
106 | public function initAcl() |
||
107 | { |
||
108 | $this->addAclPermission('loadStores', 'read', 'Insufficient Permissions'); |
||
109 | $this->addAclPermission('save', 'update', 'Insufficient Permissions'); |
||
110 | $this->addAclPermission('deletePosition', 'update', 'Insufficient Permissions'); |
||
111 | $this->addAclPermission('savePosition', 'update', 'Insufficient Permissions'); |
||
112 | $this->addAclPermission('createDocument', 'update', 'Insufficient Permissions'); |
||
113 | $this->addAclPermission('batchProcess', 'update', 'Insufficient Permissions'); |
||
114 | $this->addAclPermission('delete', 'delete', 'Insufficient Permissions'); |
||
115 | $this->addAclPermission('deleteDocument', 'deleteDocument', 'Insufficient Permissions'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get a list of available payment status |
||
120 | */ |
||
121 | public function getPaymentStatusAction() |
||
122 | { |
||
123 | $orderStatus = $this->getRepository()->getPaymentStatusQuery()->getArrayResult(); |
||
124 | |||
125 | $this->View()->assign([ |
||
126 | 'success' => true, |
||
127 | 'data' => $orderStatus, |
||
128 | ]); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Enable json renderer for index / load action |
||
133 | * Check acl rules |
||
134 | */ |
||
135 | public function preDispatch() |
||
136 | { |
||
137 | $actions = ['index', 'load', 'skeleton', 'extends', 'mergeDocuments']; |
||
138 | if (!in_array($this->Request()->getActionName(), $actions)) { |
||
139 | $this->Front()->Plugins()->Json()->setRenderer(); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getWhitelistedCSRFActions() |
||
147 | { |
||
148 | return [ |
||
149 | 'openPdf', |
||
150 | 'createDocument', |
||
151 | 'mergeDocuments', |
||
152 | ]; |
||
153 | } |
||
154 | |||
155 | public function loadListAction() |
||
156 | { |
||
157 | $filters = [['property' => 'status.id', 'expression' => '!=', 'value' => '-1']]; |
||
158 | $orderState = $this->getRepository()->getOrderStatusQuery($filters)->getArrayResult(); |
||
159 | $paymentState = $this->getRepository()->getPaymentStatusQuery()->getArrayResult(); |
||
160 | $positionStatus = $this->getRepository()->getDetailStatusQuery()->getArrayResult(); |
||
161 | |||
162 | $stateTranslator = $this->get('shopware.components.state_translator'); |
||
163 | |||
164 | $orderState = array_map(function ($orderStateItem) use ($stateTranslator) { |
||
165 | $orderStateItem = $stateTranslator->translateState(StateTranslatorService::STATE_ORDER, $orderStateItem); |
||
166 | |||
167 | return $orderStateItem; |
||
168 | }, $orderState); |
||
169 | |||
170 | $paymentState = array_map(function ($paymentStateItem) use ($stateTranslator) { |
||
171 | $paymentStateItem = $stateTranslator->translateState(StateTranslatorService::STATE_PAYMENT, $paymentStateItem); |
||
172 | |||
173 | return $paymentStateItem; |
||
174 | }, $paymentState); |
||
175 | |||
176 | $this->View()->assign([ |
||
177 | 'success' => true, |
||
178 | 'data' => [ |
||
179 | 'orderStatus' => $orderState, |
||
180 | 'paymentStatus' => $paymentState, |
||
181 | 'positionStatus' => $positionStatus, |
||
182 | ], |
||
183 | ]); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get documents of a specific type for the given orders |
||
188 | * |
||
189 | * @param int[] $orderIds |
||
190 | * @param string $docType |
||
191 | * |
||
192 | * @return \Doctrine\ORM\Query |
||
193 | */ |
||
194 | public function getOrderDocumentsQuery($orderIds, $docType) |
||
195 | { |
||
196 | $builder = Shopware()->Models()->createQueryBuilder(); |
||
197 | $builder->select([ |
||
198 | 'orders', |
||
199 | 'documents', |
||
200 | ]); |
||
201 | |||
202 | $builder->from(Order::class, 'orders'); |
||
203 | $builder->leftJoin('orders.documents', 'documents') |
||
204 | ->where('documents.typeId = :type') |
||
205 | ->andWhere('orders.id IN (:orderIds)') |
||
206 | ->setParameter('orderIds', $orderIds, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY) |
||
207 | ->setParameter(':type', $docType); |
||
208 | |||
209 | return $builder->getQuery(); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * This class has its own OrderStatusQuery as we need to get rid of states with status.id = -1 |
||
214 | * |
||
215 | * @param array|null $filter |
||
216 | * @param array|null $order |
||
217 | * @param int|null $offset |
||
218 | * @param int|null $limit |
||
219 | * |
||
220 | * @return \Doctrine\ORM\Query |
||
221 | */ |
||
222 | public function getOrderStatusQuery($filter = null, $order = null, $offset = null, $limit = null) |
||
223 | { |
||
224 | $builder = Shopware()->Models()->createQueryBuilder(); |
||
225 | $builder->select(['status']) |
||
226 | ->from(Status::class, 'status') |
||
227 | ->andWhere("status.group = 'state'"); |
||
228 | |||
229 | if ($filter !== null) { |
||
230 | $builder->addFilter($filter); |
||
231 | } |
||
232 | if ($order !== null) { |
||
233 | $builder->addOrderBy($order); |
||
234 | } else { |
||
235 | $builder->orderBy('status.position', 'ASC'); |
||
236 | } |
||
237 | |||
238 | if ($offset !== null) { |
||
239 | $builder->setFirstResult($offset) |
||
240 | ->setMaxResults($limit); |
||
241 | } |
||
242 | |||
243 | return $builder->getQuery(); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * batch function which summarizes some queries in order to speed up the order-detail startup |
||
248 | */ |
||
249 | public function loadStoresAction() |
||
250 | { |
||
251 | $id = $this->Request()->getParam('orderId'); |
||
252 | if ($id === null) { |
||
253 | $this->View()->assign(['success' => false, 'message' => 'No orderId passed']); |
||
254 | |||
255 | return; |
||
256 | } |
||
257 | |||
258 | $stateTranslator = $this->get('shopware.components.state_translator'); |
||
259 | |||
260 | $orderState = $this->getOrderStatusQuery()->getArrayResult(); |
||
261 | $paymentState = $this->getRepository()->getPaymentStatusQuery()->getArrayResult(); |
||
262 | |||
263 | $orderState = array_map(function ($orderStateItem) use ($stateTranslator) { |
||
264 | $orderStateItem = $stateTranslator->translateState(StateTranslatorService::STATE_ORDER, $orderStateItem); |
||
265 | |||
266 | return $orderStateItem; |
||
267 | }, $orderState); |
||
268 | |||
269 | $paymentState = array_map(function ($paymentStateItem) use ($stateTranslator) { |
||
270 | $paymentStateItem = $stateTranslator->translateState(StateTranslatorService::STATE_PAYMENT, $paymentStateItem); |
||
271 | |||
272 | return $paymentStateItem; |
||
273 | }, $paymentState); |
||
274 | |||
275 | $countriesSort = [ |
||
276 | [ |
||
277 | 'property' => 'countries.active', |
||
278 | 'direction' => 'DESC', |
||
279 | ], |
||
280 | [ |
||
281 | 'property' => 'countries.name', |
||
282 | 'direction' => 'ASC', |
||
283 | ], |
||
284 | ]; |
||
285 | |||
286 | $shops = $this->getShopRepository()->getBaseListQuery()->getArrayResult(); |
||
287 | $countries = $this->getCountryRepository()->getCountriesQuery(null, $countriesSort)->getArrayResult(); |
||
288 | $payments = $this->getPaymentRepository()->getAllPaymentsQuery()->getArrayResult(); |
||
289 | $dispatches = $this->getDispatchRepository()->getDispatchesQuery()->getArrayResult(); |
||
290 | $documentTypes = $this->getRepository()->getDocumentTypesQuery()->getArrayResult(); |
||
291 | |||
292 | // translate objects |
||
293 | $translationComponent = $this->get('translation'); |
||
294 | $payments = $translationComponent->translatePaymentMethods($payments); |
||
295 | $documentTypes = $translationComponent->translateDocuments($documentTypes); |
||
296 | $dispatches = $translationComponent->translateDispatchMethods($dispatches); |
||
297 | |||
298 | $this->View()->assign([ |
||
299 | 'success' => true, |
||
300 | 'data' => [ |
||
301 | 'orderStatus' => $orderState, |
||
302 | 'paymentStatus' => $paymentState, |
||
303 | 'shops' => $shops, |
||
304 | 'countries' => $countries, |
||
305 | 'payments' => $payments, |
||
306 | 'dispatches' => $dispatches, |
||
307 | 'documentTypes' => $documentTypes, |
||
308 | ], |
||
309 | ]); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Event listener method which fires when the order store is loaded. Returns an array of order data |
||
314 | * which displayed in an Ext.grid.Panel. The order data contains all associations of an order (positions, shop, |
||
315 | * customer, ...). The limit, filter and order parameter are used in the id query. The result of the id query are |
||
316 | * used to filter the detailed query which created over the getListQuery function. |
||
317 | */ |
||
318 | public function getListAction() |
||
319 | { |
||
320 | // Read store parameter to filter and paginate the data. |
||
321 | $limit = (int) $this->Request()->getParam('limit', 20); |
||
322 | $offset = (int) $this->Request()->getParam('start', 0); |
||
323 | $sort = $this->Request()->getParam('sort', []); |
||
324 | $filter = $this->Request()->getParam('filter', []); |
||
325 | $orderId = $this->Request()->getParam('orderID'); |
||
326 | |||
327 | if ($orderId !== null) { |
||
328 | $orderIdFilter = ['property' => 'orders.id', 'value' => $orderId]; |
||
329 | if (!is_array($filter)) { |
||
330 | $filter = []; |
||
331 | } |
||
332 | $filter[] = $orderIdFilter; |
||
333 | } |
||
334 | |||
335 | $list = $this->getList($filter, $sort, $offset, $limit); |
||
336 | |||
337 | $translationComponent = $this->get('translation'); |
||
338 | $list['data'] = $translationComponent->translateOrders($list['data']); |
||
339 | |||
340 | $this->View()->assign($list); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Returns an array of all defined taxes. Used for the position grid combo box on the detail page of the backend |
||
345 | * order module. |
||
346 | */ |
||
347 | public function getTaxAction() |
||
348 | { |
||
349 | $builder = Shopware()->Models()->createQueryBuilder(); |
||
350 | $tax = $builder->select(['tax']) |
||
351 | ->from(Tax::class, 'tax') |
||
352 | ->getQuery() |
||
353 | ->getArrayResult(); |
||
354 | |||
355 | $this->View()->assign(['success' => true, 'data' => $tax]); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * The getVouchers function is used by the extJs voucher store which used for a |
||
360 | * combo box on the order detail page. |
||
361 | */ |
||
362 | public function getVouchersAction() |
||
363 | { |
||
364 | $vouchers = $this->getRepository()->getVoucherQuery()->getArrayResult(); |
||
365 | $this->View()->assign(['success' => true, 'data' => $vouchers]); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Returns all supported document types. The data is used for the configuration panel. |
||
370 | */ |
||
371 | public function getDocumentTypesAction() |
||
372 | { |
||
373 | $types = $this->getRepository()->getDocumentTypesQuery()->getArrayResult(); |
||
374 | $this->View()->assign(['success' => true, 'data' => $types]); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Event listener function of the history store in the order backend module. |
||
379 | * Returns the status history of the passed order. |
||
380 | */ |
||
381 | public function getStatusHistoryAction() |
||
382 | { |
||
383 | $orderId = $this->Request()->getParam('orderID'); |
||
384 | $limit = $this->Request()->getParam('limit', 20); |
||
385 | $offset = $this->Request()->getParam('start', 0); |
||
386 | $sort = $this->Request()->getParam('sort', [['property' => 'history.changeDate', 'direction' => 'DESC']]); |
||
387 | |||
388 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
389 | $namespace = Shopware()->Snippets()->getNamespace('backend/order'); |
||
390 | |||
391 | //the backend order module have no function to create a new order so an order id must be passed. |
||
392 | if (empty($orderId)) { |
||
393 | $this->View()->assign([ |
||
394 | 'success' => false, |
||
395 | 'data' => $this->Request()->getParams(), |
||
396 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
397 | ]); |
||
398 | |||
399 | return; |
||
400 | } |
||
401 | |||
402 | $history = $this->getRepository() |
||
403 | ->getOrderStatusHistoryListQuery($orderId, $sort, $offset, $limit) |
||
404 | ->getArrayResult(); |
||
405 | |||
406 | $this->View()->assign([ |
||
407 | 'success' => true, |
||
408 | 'data' => $history, |
||
409 | ]); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * CRUD function save and update of the order model. |
||
414 | * |
||
415 | * Event listener method of the backend/order/model/order.js model which |
||
416 | * is used for the backend order module detail page to edit a single order. |
||
417 | */ |
||
418 | public function saveAction() |
||
419 | { |
||
420 | $id = (int) $this->Request()->getParam('id'); |
||
421 | |||
422 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
423 | $namespace = Shopware()->Snippets()->getNamespace('backend/order/main'); |
||
424 | |||
425 | // The backend order module have no function to create a new order so an order id must be passed. |
||
426 | if (empty($id)) { |
||
427 | $this->View()->assign([ |
||
428 | 'success' => false, |
||
429 | 'data' => $this->Request()->getParams(), |
||
430 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
431 | ]); |
||
432 | |||
433 | return; |
||
434 | } |
||
435 | |||
436 | $order = $this->getRepository()->find($id); |
||
437 | |||
438 | // The backend order module have no function to create a new order so an order id must be passed. |
||
439 | if (!($order instanceof Order)) { |
||
440 | $this->View()->assign([ |
||
441 | 'success' => false, |
||
442 | 'data' => $this->Request()->getParams(), |
||
443 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
444 | ]); |
||
445 | |||
446 | return; |
||
447 | } |
||
448 | |||
449 | $billing = $order->getBilling(); |
||
450 | $shipping = $order->getShipping(); |
||
451 | |||
452 | // Check if the shipping and billing model already exist. If not create a new instance. |
||
453 | if (!$shipping instanceof \Shopware\Models\Order\Shipping) { |
||
454 | $shipping = new Shipping(); |
||
|
|||
455 | } |
||
456 | |||
457 | if (!$billing instanceof \Shopware\Models\Order\Billing) { |
||
458 | $billing = new Billing(); |
||
459 | } |
||
460 | // Get all passed order data |
||
461 | $data = $this->Request()->getParams(); |
||
462 | |||
463 | if ($order->getChanged() !== null) { |
||
464 | try { |
||
465 | $changed = new \DateTime($data['changed']); |
||
466 | } catch (Exception $e) { |
||
467 | // If we have a invalid date caused by imports |
||
468 | $changed = $order->getChanged(); |
||
469 | } |
||
470 | |||
471 | if ($changed->getTimestamp() < 0 && $changed->getChanged()->getTimestamp() < 0) { |
||
472 | $changed = $order->getChanged(); |
||
473 | } |
||
474 | |||
475 | // We have timestamp conversion issues on Windows Users |
||
476 | $diff = abs($order->getChanged()->getTimestamp() - $changed->getTimestamp()); |
||
477 | |||
478 | // Check whether the order has been modified in the meantime |
||
479 | if ($diff > 1) { |
||
480 | $this->View()->assign([ |
||
481 | 'success' => false, |
||
482 | 'data' => $this->getOrder($order->getId()), |
||
483 | 'overwriteAble' => true, |
||
484 | 'message' => $namespace->get('order_has_been_changed', 'The order has been changed in the meantime. To prevent overwriting these changes, saving the order was aborted. Please close the order and re-open it.'), |
||
485 | ]); |
||
486 | |||
487 | return; |
||
488 | } |
||
489 | } |
||
490 | |||
491 | // Prepares the associated data of an order. |
||
492 | $data = $this->getAssociatedData($data); |
||
493 | |||
494 | // Before we can create the status mail, we need to save the order data. Otherwise |
||
495 | // the status mail would be created with the old order status and amount. |
||
496 | /** @var \Shopware\Models\Order\Order $order */ |
||
497 | $statusBefore = $order->getOrderStatus(); |
||
498 | $clearedBefore = $order->getPaymentStatus(); |
||
499 | $invoiceShippingBefore = $order->getInvoiceShipping(); |
||
500 | $invoiceShippingNetBefore = $order->getInvoiceShippingNet(); |
||
501 | |||
502 | if (!empty($data['clearedDate'])) { |
||
503 | try { |
||
504 | $data['clearedDate'] = new \DateTime($data['clearedDate']); |
||
505 | } catch (\Exception $e) { |
||
506 | $data['clearedDate'] = null; |
||
507 | } |
||
508 | } |
||
509 | |||
510 | if (isset($data['orderTime'])) { |
||
511 | unset($data['orderTime']); |
||
512 | } |
||
513 | |||
514 | $order->fromArray($data); |
||
515 | |||
516 | // Check if the invoice shipping has been changed |
||
517 | $invoiceShippingChanged = (bool) ($invoiceShippingBefore != $order->getInvoiceShipping()); |
||
518 | $invoiceShippingNetChanged = (bool) ($invoiceShippingNetBefore != $order->getInvoiceShippingNet()); |
||
519 | if ($invoiceShippingChanged || $invoiceShippingNetChanged) { |
||
520 | // Recalculate the new invoice amount |
||
521 | $order->calculateInvoiceAmount(); |
||
522 | } |
||
523 | |||
524 | Shopware()->Models()->flush(); |
||
525 | Shopware()->Models()->clear(); |
||
526 | $order = $this->getRepository()->find($id); |
||
527 | |||
528 | //if the status has been changed an status mail is created. |
||
529 | $warning = null; |
||
530 | $mail = null; |
||
531 | if ($order->getOrderStatus()->getId() !== $statusBefore->getId() || $order->getPaymentStatus()->getId() !== $clearedBefore->getId()) { |
||
532 | if ($order->getOrderStatus()->getId() !== $statusBefore->getId()) { |
||
533 | $status = $order->getOrderStatus(); |
||
534 | } else { |
||
535 | $status = $order->getPaymentStatus(); |
||
536 | } |
||
537 | try { |
||
538 | $mail = $this->getMailForOrder($order->getId(), $status->getId()); |
||
539 | } catch (\Exception $e) { |
||
540 | $warning = sprintf( |
||
541 | $namespace->get('warning/mail_creation_failed'), |
||
542 | $status->getName(), |
||
543 | $e->getMessage() |
||
544 | ); |
||
545 | } |
||
546 | } |
||
547 | |||
548 | $data = $this->getOrder($order->getId()); |
||
549 | if (!empty($mail)) { |
||
550 | $data['mail'] = $mail['data']; |
||
551 | } else { |
||
552 | $data['mail'] = null; |
||
553 | } |
||
554 | |||
555 | $result = [ |
||
556 | 'success' => true, |
||
557 | 'data' => $data, |
||
558 | ]; |
||
559 | if (isset($warning)) { |
||
560 | $result['warning'] = $warning; |
||
561 | } |
||
562 | |||
563 | $this->View()->assign($result); |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * Deletes a single order from the database. |
||
568 | * Expects a single order id which placed in the parameter id |
||
569 | */ |
||
570 | public function deleteAction() |
||
571 | { |
||
572 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
573 | $namespace = Shopware()->Snippets()->getNamespace('backend/order'); |
||
574 | |||
575 | // Get posted customers |
||
576 | $orderId = $this->Request()->getParam('id'); |
||
577 | |||
578 | if (empty($orderId) || !is_numeric($orderId)) { |
||
579 | $this->View()->assign([ |
||
580 | 'success' => false, |
||
581 | 'data' => $this->Request()->getParams(), |
||
582 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
583 | ]); |
||
584 | |||
585 | return; |
||
586 | } |
||
587 | |||
588 | $entity = $this->getRepository()->find($orderId); |
||
589 | $this->getManager()->remove($entity); |
||
590 | |||
591 | // Performs all of the collected actions. |
||
592 | $this->getManager()->flush(); |
||
593 | |||
594 | $this->View()->assign(['success' => true]); |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * CRUD function save and update of the position store of the backend order module. |
||
599 | * The function handles the update and insert routine of a single order position. |
||
600 | * After the position has been added to the order, the order invoice amount will be recalculated. |
||
601 | * The refreshed order will be assigned to the view to refresh the panels and grids. |
||
602 | */ |
||
603 | public function savePositionAction() |
||
604 | { |
||
605 | $id = $this->Request()->getParam('id'); |
||
606 | |||
607 | $orderId = $this->Request()->getParam('orderId'); |
||
608 | |||
609 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
610 | $namespace = Shopware()->Snippets()->getNamespace('backend/order/controller/main'); |
||
611 | |||
612 | // Check if an order id is passed. If no order id passed, return success false |
||
613 | if (empty($orderId)) { |
||
614 | $this->View()->assign([ |
||
615 | 'success' => false, |
||
616 | 'data' => $this->Request()->getParams(), |
||
617 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
618 | ]); |
||
619 | |||
620 | return; |
||
621 | } |
||
622 | |||
623 | // Find the order model. If no model founded, return success false |
||
624 | $order = $this->getRepository()->find($orderId); |
||
625 | if (empty($order)) { |
||
626 | $this->View()->assign([ |
||
627 | 'success' => false, |
||
628 | 'data' => $this->Request()->getParams(), |
||
629 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
630 | ]); |
||
631 | |||
632 | return; |
||
633 | } |
||
634 | |||
635 | // Check whether the order has been modified in the meantime |
||
636 | $lastOrderChange = new \DateTime($this->Request()->getParam('changed')); |
||
637 | if ($order->getChanged() !== null && $order->getChanged()->getTimestamp() != $lastOrderChange->getTimestamp()) { |
||
638 | $params = $this->Request()->getParams(); |
||
639 | $params['changed'] = $order->getChanged(); |
||
640 | $this->View()->assign([ |
||
641 | 'success' => false, |
||
642 | 'data' => $params, |
||
643 | 'overwriteAble' => true, |
||
644 | 'message' => $namespace->get('order_has_been_changed', 'The order has been changed in the meantime. To prevent overwriting these changes, saving the order was aborted. Please close the order and re-open it.'), |
||
645 | ]); |
||
646 | |||
647 | return; |
||
648 | } |
||
649 | |||
650 | // Check if the passed position data is a new position or an existing position. |
||
651 | if (empty($id)) { |
||
652 | $position = new Detail(); |
||
653 | $attribute = new Shopware\Models\Attribute\OrderDetail(); |
||
654 | $position->setAttribute($attribute); |
||
655 | Shopware()->Models()->persist($position); |
||
656 | } else { |
||
657 | $detailRepository = Shopware()->Models()->getRepository(Detail::class); |
||
658 | $position = $detailRepository->find($id); |
||
659 | } |
||
660 | |||
661 | $data = $this->Request()->getParams(); |
||
662 | $data['number'] = $order->getNumber(); |
||
663 | |||
664 | $data = $this->getPositionAssociatedData($data); |
||
665 | // If $data === null, the product was not found |
||
666 | if ($data === null) { |
||
667 | $this->View()->assign([ |
||
668 | 'success' => false, |
||
669 | 'data' => [], |
||
670 | 'message' => 'The productnumber "' . $this->Request()->getParam('articleNumber', '') . '" is not valid', |
||
671 | ]); |
||
672 | |||
673 | return; |
||
674 | } |
||
675 | |||
676 | $position->fromArray($data); |
||
677 | $position->setOrder($order); |
||
678 | |||
679 | Shopware()->Models()->flush(); |
||
680 | |||
681 | // If the passed data is a new position, the flush function will add the new id to the position model |
||
682 | $data['id'] = $position->getId(); |
||
683 | |||
684 | // The position model will refresh the product stock, so the product stock |
||
685 | // will be assigned to the view to refresh the grid or form panel. |
||
686 | $variantRepository = Shopware()->Models()->getRepository(ArticleDetail::class); |
||
687 | $variant = $variantRepository->findOneBy(['number' => $position->getArticleNumber()]); |
||
688 | if ($variant instanceof ArticleDetail) { |
||
689 | $data['inStock'] = $variant->getInStock(); |
||
690 | } |
||
691 | $order = $this->getRepository()->find($order->getId()); |
||
692 | |||
693 | Shopware()->Models()->persist($order); |
||
694 | Shopware()->Models()->flush(); |
||
695 | |||
696 | $invoiceAmount = $order->getInvoiceAmount(); |
||
697 | $changed = $order->getChanged(); |
||
698 | |||
699 | if ($position->getOrder() instanceof \Shopware\Models\Order\Order) { |
||
700 | $invoiceAmount = $position->getOrder()->getInvoiceAmount(); |
||
701 | $changed = $position->getOrder()->getChanged(); |
||
702 | } |
||
703 | |||
704 | $this->View()->assign([ |
||
705 | 'success' => true, |
||
706 | 'data' => $data, |
||
707 | 'invoiceAmount' => $invoiceAmount, |
||
708 | 'changed' => $changed, |
||
709 | ]); |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * CRUD function delete of the position and list store of the backend order module. |
||
714 | * The function can delete one or many order positions. After the positions has been deleted |
||
715 | * the order invoice amount will be recalculated. The refreshed order will be assigned to the |
||
716 | * view to refresh the panels and grids. |
||
717 | */ |
||
718 | public function deletePositionAction() |
||
719 | { |
||
720 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
721 | $namespace = Shopware()->Snippets()->getNamespace('backend/order/controller/main'); |
||
722 | |||
723 | $positions = $this->Request()->getParam('positions', [['id' => $this->Request()->getParam('id')]]); |
||
724 | |||
725 | // Check if any positions is passed. |
||
726 | if (empty($positions)) { |
||
727 | $this->View()->assign([ |
||
728 | 'success' => false, |
||
729 | 'data' => $this->Request()->getParams(), |
||
730 | 'message' => $namespace->get('no_order_passed', 'No orders passed'), |
||
731 | ]); |
||
732 | |||
733 | return; |
||
734 | } |
||
735 | |||
736 | // If no order id passed it isn't possible to update the order amount, so we will cancel the position deletion here. |
||
737 | $orderId = $this->Request()->getParam('orderID'); |
||
738 | |||
739 | if (empty($orderId)) { |
||
740 | $this->View()->assign([ |
||
741 | 'success' => false, |
||
742 | 'data' => $this->Request()->getParams(), |
||
743 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
744 | ]); |
||
745 | |||
746 | return; |
||
747 | } |
||
748 | |||
749 | /** @var \Shopware\Models\Order\Order $order */ |
||
750 | $order = $this->getRepository()->find($orderId); |
||
751 | if (empty($order)) { |
||
752 | $this->View()->assign([ |
||
753 | 'success' => false, |
||
754 | 'data' => $this->Request()->getParams(), |
||
755 | 'message' => $namespace->get('no_order_id_passed', 'No valid order id passed.'), |
||
756 | ]); |
||
757 | |||
758 | return; |
||
759 | } |
||
760 | |||
761 | // Check whether the order has been modified in the meantime |
||
762 | $lastOrderChange = new \DateTime($this->Request()->getParam('changed')); |
||
763 | if ($order->getChanged() !== null && $order->getChanged()->getTimestamp() != $lastOrderChange->getTimestamp()) { |
||
764 | $params = $this->Request()->getParams(); |
||
765 | $params['changed'] = $order->getChanged(); |
||
766 | $this->View()->assign([ |
||
767 | 'success' => false, |
||
768 | 'data' => $params, |
||
769 | 'overwriteAble' => true, |
||
770 | 'message' => $namespace->get('order_has_been_changed', 'The order has been changed in the meantime. To prevent overwriting these changes, saving the order was aborted. Please close the order and re-open it.'), |
||
771 | ]); |
||
772 | |||
773 | return; |
||
774 | } |
||
775 | |||
776 | foreach ($positions as $position) { |
||
777 | if (empty($position['id'])) { |
||
778 | continue; |
||
779 | } |
||
780 | $model = Shopware()->Models()->find(Detail::class, $position['id']); |
||
781 | |||
782 | // Check if the model was founded. |
||
783 | if ($model instanceof \Shopware\Models\Order\Detail) { |
||
784 | Shopware()->Models()->remove($model); |
||
785 | } |
||
786 | } |
||
787 | // After each model has been removed to executes the doctrine flush. |
||
788 | Shopware()->Models()->flush(); |
||
789 | |||
790 | $order->calculateInvoiceAmount(); |
||
791 | Shopware()->Models()->flush(); |
||
792 | |||
793 | $data = $this->getOrder($order->getId()); |
||
794 | $this->View()->assign([ |
||
795 | 'success' => true, |
||
796 | 'data' => $data, |
||
797 | ]); |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * The batchProcessAction function handles the request of the batch window in order backend module. |
||
802 | * It is responsible to create the order document for the passed parameters and updates the order |
||
803 | * or|and payment status that passed for each order. If the order or payment status has been changed |
||
804 | * the function will create for each order an status mail which will be assigned to the passed order |
||
805 | * and will be displayed in the email panel on the right side of the batch window. |
||
806 | * If the parameter "autoSend" is set to true (configurable over the checkbox in the form panel) each |
||
807 | * created status mail will be send directly. |
||
808 | */ |
||
809 | public function batchProcessAction() |
||
918 | |||
919 | /** |
||
920 | * This function is called by the batch controller after all documents were created |
||
921 | * It will read the created documents' hashes from database and merge them |
||
922 | */ |
||
923 | public function mergeDocumentsAction() |
||
967 | |||
968 | /** |
||
969 | * The sendMailAction fired from the batch window in the order backend module when the user want to send the order |
||
970 | * status mail manually. |
||
971 | */ |
||
972 | public function sendMailAction() |
||
973 | { |
||
974 | $data = $this->Request()->getParams(); |
||
975 | $orderId = $this->request->getParam('orderId'); |
||
976 | $attachments = $this->request->getParam('attachment'); |
||
977 | |||
978 | /** @var Enlight_Components_Snippet_Namespace $namespace */ |
||
979 | $namespace = Shopware()->Snippets()->getNamespace('backend/order'); |
||
980 | |||
981 | if (empty($data)) { |
||
982 | $this->View()->assign([ |
||
983 | 'success' => false, |
||
984 | 'data' => $data, |
||
1038 | |||
1039 | /** |
||
1040 | * Deletes a document by the requested document id. |
||
1041 | */ |
||
1042 | public function deleteDocumentAction() |
||
1077 | |||
1078 | /** |
||
1079 | * Creates a mail by the requested orderId and assign it to the view. |
||
1080 | */ |
||
1081 | public function createMailAction() |
||
1104 | |||
1105 | /** |
||
1106 | * Retrieves all available mail templates |
||
1107 | */ |
||
1108 | public function getMailTemplatesAction() |
||
1153 | |||
1154 | /** |
||
1155 | * CRUD function of the document store. The function creates the order document with the passed |
||
1156 | * request parameters. |
||
1157 | */ |
||
1158 | public function createDocumentAction() |
||
1183 | |||
1184 | /** |
||
1185 | * Fires when the user want to open a generated order document from the backend order module. |
||
1186 | * Returns the created pdf file. |
||
1187 | */ |
||
1188 | public function openPdfAction() |
||
1228 | |||
1229 | /** |
||
1230 | * Returns filterable partners |
||
1231 | */ |
||
1232 | public function getPartnersAction() |
||
1256 | |||
1257 | /** |
||
1258 | * Returns the shopware model manager |
||
1259 | * |
||
1260 | * @return Shopware\Components\Model\ModelManager |
||
1261 | */ |
||
1262 | protected function getManager() |
||
1270 | |||
1271 | /** |
||
1272 | * Helper function to get access on the static declared repository |
||
1273 | * |
||
1274 | * @return Shopware\Models\Order\Repository|null |
||
1275 | */ |
||
1276 | protected function getRepository() |
||
1284 | |||
1285 | /** |
||
1286 | * Helper function to get access on the static declared repository |
||
1287 | * |
||
1288 | * @return Shopware\Models\Shop\Repository|null |
||
1289 | */ |
||
1290 | protected function getShopRepository() |
||
1298 | |||
1299 | /** |
||
1300 | * Helper function to get access on the static declared repository |
||
1301 | * |
||
1302 | * @return Shopware\Models\Country\Repository|null |
||
1303 | */ |
||
1304 | protected function getCountryRepository() |
||
1312 | |||
1313 | /** |
||
1314 | * Helper function to get access on the static declared repository |
||
1315 | * |
||
1316 | * @return Shopware\Models\Payment\Repository|null |
||
1317 | */ |
||
1318 | protected function getPaymentRepository() |
||
1326 | |||
1327 | /** |
||
1328 | * Helper function to get access on the static declared repository |
||
1329 | * |
||
1330 | * @return Shopware\Models\Dispatch\Repository|null |
||
1331 | */ |
||
1332 | protected function getDispatchRepository() |
||
1340 | |||
1341 | /** |
||
1342 | * @deprecated in 5.6, will be removed in 5.7 without a replacement |
||
1343 | * |
||
1344 | * Helper function to get access on the static declared repository |
||
1345 | * |
||
1346 | * @return \Shopware\Components\Model\ModelRepository |
||
1347 | */ |
||
1348 | protected function getDocumentRepository() |
||
1358 | |||
1359 | /** |
||
1360 | * @param array[] $filter |
||
1361 | * @param array[] $sort |
||
1362 | * @param int|null $offset |
||
1363 | * @param int|null $limit |
||
1364 | * |
||
1365 | * @return array |
||
1366 | */ |
||
1367 | protected function getList($filter, $sort, $offset, $limit) |
||
1442 | |||
1443 | /** |
||
1444 | * Prepare address data - loads countryModel from a given countryId |
||
1445 | * |
||
1446 | * @return array |
||
1447 | */ |
||
1448 | protected function prepareAddressData(array $data) |
||
1468 | |||
1469 | /** |
||
1470 | * @param array[] $sorts |
||
1471 | * |
||
1472 | * @return array[] |
||
1473 | */ |
||
1474 | private function resolveSortParameter($sorts) |
||
1511 | |||
1512 | /** |
||
1513 | * @param array[] $orders |
||
1514 | * @param array[] $associations |
||
1515 | * @param string $arrayKey |
||
1516 | * |
||
1517 | * @return array[] |
||
1518 | */ |
||
1519 | private function assignAssociation($orders, $associations, $arrayKey) |
||
1532 | |||
1533 | /** |
||
1534 | * Helper function to select a single order. |
||
1535 | * |
||
1536 | * @param int $id |
||
1537 | */ |
||
1538 | private function getOrder($id) |
||
1545 | |||
1546 | /** |
||
1547 | * Simple helper function which actually merges a given array of document-paths |
||
1548 | * |
||
1549 | * @return string The created document's url |
||
1550 | */ |
||
1551 | private function mergeDocuments(array $paths) |
||
1571 | |||
1572 | /** |
||
1573 | * Internal helper function which checks if the batch process needs a document creation. |
||
1574 | * |
||
1575 | * @param int $documentTypeId |
||
1576 | * @param int $documentMode |
||
1577 | * @param \Shopware\Models\Order\Order $order |
||
1578 | */ |
||
1579 | private function createOrderDocuments($documentTypeId, $documentMode, $order) |
||
1605 | |||
1606 | /** |
||
1607 | * Internal helper function to check if the order or payment status has been changed. |
||
1608 | * If one of the status changed, the function will create a status mail. |
||
1609 | * If the autoSend parameter is true, the created status mail will be sent directly, |
||
1610 | * if addAttachments and documentType are true/selected as well, the according documents will be attached. |
||
1611 | * |
||
1612 | * @param Order $order |
||
1613 | * @param \Shopware\Models\Order\Status $statusBefore |
||
1614 | * @param \Shopware\Models\Order\Status $clearedBefore |
||
1615 | * @param bool $autoSend |
||
1616 | * @param int|null $documentTypeId |
||
1617 | * @param bool $addAttachments |
||
1618 | * |
||
1619 | * @return array|null |
||
1620 | */ |
||
1621 | private function checkOrderStatus($order, $statusBefore, $clearedBefore, $autoSend, $documentTypeId, $addAttachments) |
||
1665 | |||
1666 | /** |
||
1667 | * @param int $documentTypeId |
||
1668 | * |
||
1669 | * @return array |
||
1670 | */ |
||
1671 | private function getDocument($documentTypeId, Order $order) |
||
1692 | |||
1693 | /** |
||
1694 | * @param int $orderId |
||
1695 | * @param int $documentTypeId |
||
1696 | * |
||
1697 | * @return array |
||
1698 | */ |
||
1699 | private function getDocumentFromDatabase($orderId, $documentTypeId) |
||
1726 | |||
1727 | /** |
||
1728 | * Adds the requested attachments to the given $mail object |
||
1729 | * |
||
1730 | * @param int|string $orderId |
||
1731 | * |
||
1732 | * @return Enlight_Components_Mail |
||
1733 | */ |
||
1734 | private function addAttachments(Enlight_Components_Mail $mail, $orderId, array $attachments = []) |
||
1751 | |||
1752 | /** |
||
1753 | * Creates a attachment by a file path. |
||
1754 | * |
||
1755 | * @param string $filePath |
||
1756 | * @param string $fileName |
||
1757 | * |
||
1758 | * @return Zend_Mime_Part |
||
1759 | */ |
||
1760 | private function createAttachment($filePath, $fileName) |
||
1773 | |||
1774 | /** |
||
1775 | * @param int|string $orderId |
||
1776 | * @param int|string $typeId |
||
1777 | * @param string $fileExtension |
||
1778 | * |
||
1779 | * @return string |
||
1780 | */ |
||
1781 | private function getFileName($orderId, $typeId, $fileExtension = '.pdf') |
||
1794 | |||
1795 | /** |
||
1796 | * Returns the locale id from the order |
||
1797 | * |
||
1798 | * @param int|string $orderId |
||
1799 | * |
||
1800 | * @return bool|string |
||
1801 | */ |
||
1802 | private function getOrderLocaleId($orderId) |
||
1813 | |||
1814 | /** |
||
1815 | * Gets the default name from the document template |
||
1816 | * |
||
1817 | * @param int|string $typeId |
||
1818 | * |
||
1819 | * @return bool|string |
||
1820 | */ |
||
1821 | private function getDefaultName($typeId) |
||
1833 | |||
1834 | /** |
||
1835 | * Internal helper function which is used from the batch function and the createDocumentAction. |
||
1836 | * The batch function fired from the batch window to create multiple documents for many orders. |
||
1837 | * The createDocumentAction fired from the detail page when the user clicks the "create Document button" |
||
1838 | * |
||
1839 | * @param int $orderId |
||
1840 | * @param int $documentType |
||
1841 | * |
||
1842 | * @return bool |
||
1843 | */ |
||
1844 | private function createDocument($orderId, $documentType) |
||
1890 | |||
1891 | /** |
||
1892 | * Internal helper function which insert the order detail association data into the passed data array |
||
1893 | * |
||
1894 | * @param array $data |
||
1895 | * |
||
1896 | * @return array|null |
||
1897 | */ |
||
1898 | private function getPositionAssociatedData($data) |
||
1987 | |||
1988 | /** |
||
1989 | * Internal helper function which insert the order association data into the passed data array. |
||
1990 | * |
||
1991 | * @return array |
||
1992 | */ |
||
1993 | private function getAssociatedData(array $data) |
||
2052 | |||
2053 | /** |
||
2054 | * Creates the status mail order for the passed order id and new status object. |
||
2055 | * |
||
2056 | * @param int $orderId |
||
2057 | * @param int|null $statusId |
||
2058 | * @param int|null $documentTypeId |
||
2059 | * |
||
2060 | * @throws \Doctrine\DBAL\DBALException |
||
2061 | * |
||
2062 | * @return array |
||
2063 | */ |
||
2064 | private function getMailForOrder($orderId, $statusId, $documentTypeId = null) |
||
2095 | |||
2096 | /** |
||
2097 | * @param string[] $numbers |
||
2098 | * |
||
2099 | * @return array |
||
2100 | */ |
||
2101 | private function getVariantsStock(array $numbers) |
||
2111 | |||
2112 | /** |
||
2113 | * @param string $path |
||
2114 | * |
||
2115 | * @return string |
||
2116 | */ |
||
2117 | private function downloadFileFromFilesystem($path) |
||
2127 | |||
2128 | /** |
||
2129 | * @return SearchCriteria |
||
2130 | */ |
||
2131 | private function createCriteria() |
||
2193 | |||
2194 | /** |
||
2195 | * @param int|null $documentTypeId |
||
2196 | * |
||
2197 | * @throws \Doctrine\DBAL\DBALException |
||
2198 | * |
||
2199 | * @return string |
||
2200 | */ |
||
2201 | private function getTemplateNameForDocumentTypeId($documentTypeId = null) |
||
2223 | |||
2224 | /** |
||
2225 | * @return \Shopware\Models\Shop\Locale|null |
||
2226 | */ |
||
2227 | private function getCurrentLocale() |
||
2233 | } |
||
2234 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.