Passed
Push — master ( 6e3c1d...d05c2b )
by Luiz Kim
02:36
created

OrderPrintService::printQueues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Order;
6
use ControleOnline\Entity\ProductGroupProduct;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\ProductGroupProduct was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Exception;
9
10
class OrderPrintService
11
{
12
    private $noQueue = 'Sem fila definida';
13
14
    public function __construct(
15
        private EntityManagerInterface $entityManager,
16
        private PrintService $printService
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PrintService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
    ) {}
18
19
20
    private function getQueues(Order $order)
21
    {
22
        $queues = [];
23
        foreach ($order->getOrderProducts() as $orderProduct) {
24
            $queueEntries = $orderProduct->getOrderProductQueues();
25
            if ($queueEntries->isEmpty()) {
26
                if (!isset($queues[$this->noQueue])) {
27
                    $queues[$this->noQueue] = [];
28
                }
29
                $queues[$this->noQueue][] = $orderProduct;
30
            } else {
31
                foreach ($queueEntries as $queueEntry) {
32
                    $queue = $queueEntry->getQueue();
33
                    $queueName = $queue ? $queue->getQueue() : $this->noQueue;
34
                    if (!isset($queues[$queueName])) {
35
                        $queues[$queueName] = [];
36
                    }
37
                    $queues[$queueName][] = $orderProduct;
38
                }
39
            }
40
        }
41
        return $queues;
42
    }
43
44
    private function printProduct($orderProduct, $indent = "- ")
45
    {
46
        $product = $orderProduct->getProduct();
47
48
        $quantity = $orderProduct->getQuantity();
49
        $this->printService->addLine(
50
            $indent . $quantity . ' X ' . $product->getProduct(),
51
            " R$ " . number_format($orderProduct->getTotal(), 2, ',', '.'),
52
            '.'
53
        );
54
    }
55
56
    private function printChildren($orderProducts)
57
    {
58
        $groupedChildren = [];
59
        foreach ($orderProducts as $orderProductChild) {
60
            $productGroup = $orderProductChild->getProductGroup();
61
            $groupName = $productGroup ? $productGroup->getProductGroup() : 'Sem Grupo';
62
            if (!isset($groupedChildren[$groupName])) {
63
                $groupedChildren[$groupName] = [];
64
            }
65
            $groupedChildren[$groupName][] = $orderProductChild;
66
        }
67
68
        foreach ($groupedChildren as $groupName => $orderProductChildren) {
69
            $this->printService->addLine(strtoupper($groupName) . ":");
70
            foreach ($orderProductChildren as $orderProductChild) {
71
                $product = $orderProductChild->getProduct();
72
                $this->printService->addLine("  - " . $product->getProduct());
73
            }
74
        }
75
        $this->printService->addLine('', '', '-');
76
    }
77
78
    private function printQueueProducts($orderProducts)
79
    {
80
        $parentOrderProducts = array_filter($orderProducts, fn($orderProduct) => $orderProduct->getOrderProduct() === null);
81
82
83
        foreach ($parentOrderProducts as $parentOrderProduct) {
84
            $this->printProduct($parentOrderProduct);
85
86
            $childs = $parentOrderProduct->getOrderProductComponents();
87
            if (!empty($childs))
88
                $this->printChildren($childs);
89
90
            $this->printService->addLine('', '', '-');
91
        }
92
    }
93
94
    private function printQueues($queues)
95
    {
96
        foreach ($queues as $queueName => $orderProducts) {
97
            $parentOrderProducts = array_filter($orderProducts, fn($orderProduct) => $orderProduct->getOrderProduct() === null);
98
            if (!empty($parentOrderProducts)) {
99
                $this->printService->addLine(strtoupper($queueName) . ":");
100
                $this->printQueueProducts($orderProducts);
101
                $this->printService->addLine('', '', ' ');
102
            }
103
        }
104
    }
105
106
    public function generatePrintData(Order $order, string $printType, string $deviceType)
107
    {
108
109
        $this->printService->addLine("PEDIDO #" . $order->getId());
110
        $this->printService->addLine($order->getOrderDate()->format('d/m/Y H:i'));
111
        $client = $order->getClient();
112
        $this->printService->addLine(($client !== null ? $client->getName() : 'Não informado'));
113
        $this->printService->addLine("R$ " . number_format($order->getPrice(), 2, ',', '.'));
114
        $this->printService->addLine("", "", "-");
115
        $queues = $this->getQueues($order);
116
117
        $this->printQueues($queues);
118
        $this->printService->addLine("", "", "-");
119
        return $this->printService->generatePrintData($printType, $deviceType);
120
    }
121
}
122