Passed
Push — master ( 5db4eb...043ac8 )
by Luiz Kim
03:35 queued 01:11
created

OrderPrintService::printOrder()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
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 10
rs 10
cc 4
nc 8
nop 2
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Device;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Device 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...
6
use ControleOnline\Entity\Order;
7
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...
8
use ControleOnline\Entity\Spool;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Spool 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...
9
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...
10
use Exception;
11
12
class OrderPrintService
13
{
14
    private $noQueue = 'Sem fila definida';
15
16
    public function __construct(
17
        private EntityManagerInterface $entityManager,
18
        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...
19
        private ConfigService $configService,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\ConfigService 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...
20
        private DeviceService $deviceService,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\DeviceService 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...
21
    ) {}
22
23
24
    private function getQueues(Order $order)
25
    {
26
        $queues = [];
27
        foreach ($order->getOrderProducts() as $orderProduct) {
28
            $queueEntries = $orderProduct->getOrderProductQueues();
29
            if ($queueEntries->isEmpty()) {
30
                if (!isset($queues[$this->noQueue])) {
31
                    $queues[$this->noQueue] = [];
32
                }
33
                $queues[$this->noQueue][] = $orderProduct;
34
            } else {
35
                foreach ($queueEntries as $queueEntry) {
36
                    $queue = $queueEntry->getQueue();
37
                    $queueName = $queue ? $queue->getQueue() : $this->noQueue;
38
                    if (!isset($queues[$queueName])) {
39
                        $queues[$queueName] = [];
40
                    }
41
                    $queues[$queueName][] = $orderProduct;
42
                }
43
            }
44
        }
45
        return $queues;
46
    }
47
48
    public  function printOrder(Order $order, ?array $devices = [])
49
    {
50
        if (empty($devices))
51
            $devices = $this->configService->getConfig($order->getProvider(), 'order-print-devices', true);
52
53
        if ($devices)
54
            $devices = $this->deviceService->findDevices($devices);
55
56
        foreach ($devices as $device)
57
            $this->generatePrintData($order, $device, ['sound' => 'iFood']);
58
    }
59
60
    private function printProduct($orderProduct, $indent = "- ")
61
    {
62
        $product = $orderProduct->getProduct();
63
64
        $quantity = $orderProduct->getQuantity();
65
        $this->printService->addLine(
66
            $indent . $quantity . ' X ' . $product->getProduct(),
67
            " R$ " . number_format($orderProduct->getTotal(), 2, ',', '.'),
68
            '.'
69
        );
70
    }
71
72
    private function printChildren($orderProducts)
73
    {
74
        $groupedChildren = [];
75
        foreach ($orderProducts as $orderProductChild) {
76
            $productGroup = $orderProductChild->getProductGroup();
77
            $groupName = $productGroup ? $productGroup->getProductGroup() : 'Sem Grupo';
78
            if (!isset($groupedChildren[$groupName])) {
79
                $groupedChildren[$groupName] = [];
80
            }
81
            $groupedChildren[$groupName][] = $orderProductChild;
82
        }
83
84
        foreach ($groupedChildren as $groupName => $orderProductChildren) {
85
            $this->printService->addLine(strtoupper($groupName) . ":");
86
            foreach ($orderProductChildren as $orderProductChild) {
87
                $product = $orderProductChild->getProduct();
88
                $this->printService->addLine("  - " . $product->getProduct());
89
            }
90
        }
91
        $this->printService->addLine('', '', '-');
92
    }
93
94
    private function printQueueProducts($orderProducts)
95
    {
96
        $parentOrderProducts = array_filter($orderProducts, fn($orderProduct) => $orderProduct->getOrderProduct() === null);
97
98
99
        foreach ($parentOrderProducts as $parentOrderProduct) {
100
            $this->printProduct($parentOrderProduct);
101
102
            $childs = $parentOrderProduct->getOrderProductComponents();
103
            if (!empty($childs))
104
                $this->printChildren($childs);
105
106
            $this->printService->addLine('', '', '-');
107
        }
108
    }
109
110
    private function printQueues($queues)
111
    {
112
        foreach ($queues as $queueName => $orderProducts) {
113
            $parentOrderProducts = array_filter($orderProducts, fn($orderProduct) => $orderProduct->getOrderProduct() === null);
114
            if (!empty($parentOrderProducts)) {
115
                $this->printService->addLine(strtoupper($queueName) . ":");
116
                $this->printQueueProducts($orderProducts);
117
                $this->printService->addLine('', '', ' ');
118
            }
119
        }
120
    }
121
122
    public function generatePrintData(Order $order, Device $device, ?array $aditionalData = []): Spool
123
    {
124
125
        $this->printService->addLine("PEDIDO #" . $order->getId());
126
        $this->printService->addLine($order->getOrderDate()->format('d/m/Y H:i'));
127
        $client = $order->getClient();
128
        $this->printService->addLine(($client !== null ? $client->getName() : 'Não informado'));
129
        $this->printService->addLine("R$ " . number_format($order->getPrice(), 2, ',', '.'));
130
        $this->printService->addLine("", "", "-");
131
        $queues = $this->getQueues($order);
132
133
        $this->printQueues($queues);
134
        $this->printService->addLine("", "", "-");
135
136
137
        return $this->printService->generatePrintData($device, $order->getProvider(), $aditionalData);
138
    }
139
}
140