Passed
Push — master ( 31f09d...721b41 )
by Luiz Kim
02:25
created

PrintOrderAction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
C generatePrintData() 0 63 12
A __construct() 0 3 1
A __invoke() 0 14 2
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request 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 Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse 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 ControleOnline\Entity\Order;
9
use ControleOnline\Entity\OrderProduct;
10
use ControleOnline\Entity\Product;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Product 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 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...
12
13
class PrintOrderAction
14
{
15
    private $entityManager;
16
17
    public function __construct(EntityManagerInterface $entityManager)
18
    {
19
        $this->entityManager = $entityManager;
20
    }
21
22
    public function __invoke(Request $request, int $id): JsonResponse
23
    {
24
        $order = $this->entityManager->getRepository(Order::class)->find($id);
25
        if (!$order) {
26
            return new JsonResponse(['error' => 'Order not found'], 404);
27
        }
28
29
        $data = json_decode($request->getContent(), true);
30
        $printType = $data['print-type'] ?? 'pos';
31
        $deviceType = $data['device-type'] ?? 'cielo';
32
33
        $printData = $this->generatePrintData($order, $printType, $deviceType);
34
35
        return new JsonResponse($printData);
36
    }
37
38
    private function generatePrintData(Order $order, string $printType, string $deviceType)
39
    {
40
        if ($deviceType !== 'cielo') {
41
            return ['error' => 'Unsupported device type'];
42
        }
43
44
        if ($printType === 'pos') {
45
            $text = "PEDIDO #" . $order->getId() . "\n";
46
            $text .= "Data: " . $order->getOrderDate()->format('d/m/Y H:i') . "\n";
47
48
            // Correção: Verificar explicitamente se getClient() é null
49
            $client = $order->getClient();
50
            $text .= "Cliente: " . ($client !== null ? $client->getName() : 'Não informado') . "\n";
51
52
            $text .= "Total: R$ " . number_format($order->getPrice(), 2, ',', '.') . "\n";
53
            $text .= "------------------------\n";
54
55
            // Agrupar produtos por fila diretamente
56
            $queues = [];
57
            foreach ($order->getOrderProducts() as $orderProduct) {
58
                $queue = $orderProduct->getQueue();
59
                $queueName = $queue ? $queue->getQueue() : 'Sem fila definida';
60
                if (!isset($queues[$queueName])) {
61
                    $queues[$queueName] = [];
62
                }
63
                $queues[$queueName][] = $orderProduct;
64
            }
65
66
            // Exibir produtos organizados por fila
67
            foreach ($queues as $queueName => $products) {
68
                $text .= strtoupper($queueName) . ":\n";
69
                foreach ($products as $orderProduct) {
70
                    $product = $orderProduct->getProduct();
71
                    $unit = $product->getProductUnit()->getProductUnit();
72
                    $quantity = $orderProduct->getQuantity();
73
74
                    $text .= "- " . $product->getProduct() . " (" . $quantity . " " . $unit . ")\n";
75
                    $text .= "  R$ " . number_format($product->getPrice() * $quantity, 2, ',', '.') . "\n";
76
77
                    // Verifica se o produto é customizado
78
                    if ($product->getType() === 'custom') {
79
                        $text .= "  Personalizações:\n";
80
                        $productGroupProducts = $this->entityManager->getRepository(ProductGroupProduct::class)
81
                            ->findBy(['product' => $product->getId()]);
82
                        
83
                        foreach ($productGroupProducts as $pgp) {
84
                            $childProduct = $pgp->getProductChild();
85
                            if ($childProduct) {
86
                                $text .= "    - " . $childProduct->getProduct() . " (" . $pgp->getQuantity() . " " . $childProduct->getProductUnit()->getProductUnit() . ")\n";
87
                            }
88
                        }
89
                    }
90
                }
91
                $text .= "\n";
92
            }
93
94
            $text .= "------------------------\n";
95
            $text .= "Status: " . $order->getStatus()->getStatus() . "\n";
96
97
            return $text;
98
        }
99
100
        return ['error' => 'Unsupported print type'];
101
    }
102
}