OrderPrintService::printOrder()   A
last analyzed

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