@@ -8,61 +8,83 @@ class PrintController
8
8
{
9
9
private \PDO $ pdo ;
10
10
private $ userModel ;
11
+ private $ logger ;
11
12
12
- public function __construct (\PDO $ pdo , $ userModel )
13
+ public function __construct (\PDO $ pdo , $ userModel, $ logger )
13
14
{
14
15
$ this ->pdo = $ pdo ;
15
16
$ this ->userModel = $ userModel ;
17
+ $ this ->logger = $ logger ;
16
18
}
17
19
18
- public function printOrder (Request $ request , Response $ response , array $ args ): Response
19
- {
20
- $ pedidoId = $ args ['id ' ] ?? null ;
21
- if (!$ pedidoId ) {
22
- $ response ->getBody ()->write (json_encode (['error ' => 'Pedido não encontrado ' ]));
23
- return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (404 );
24
- }
20
+ public function printOrder (Request $ request , Response $ response , array $ args ): Response
21
+ {
22
+
23
+ $ orderId = $ args ['id ' ] ?? null ;
24
+ if (!$ orderId ) {
25
+ $ response ->getBody ()->write (json_encode (['error ' => 'Pedido não encontrado ' ]));
26
+ $ this ->logger ->error ('Erro ao imprimir ' , ['order_id ' => $ orderId ]);
27
+ return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (404 );
28
+ }
25
29
26
- // Busca pedido no banco
27
- $ stmt = $ this ->pdo ->prepare ("SELECT * FROM pedidos WHERE id = ? " );
28
- $ stmt ->execute ([$ pedidoId ]);
29
- $ pedido = $ stmt ->fetch (\PDO ::FETCH_ASSOC );
30
+ // Busca pedido na tabela orders
31
+ $ stmt = $ this ->pdo ->prepare ("SELECT * FROM orders WHERE id = ? " );
32
+ $ stmt ->execute ([$ orderId ]);
33
+ $ order = $ stmt ->fetch (\PDO ::FETCH_ASSOC );
30
34
31
- if (!$ pedido ) {
32
- $ response ->getBody ()->write (json_encode (['error ' => 'Pedido não encontrado ' ]));
33
- return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (404 );
34
- }
35
+ if (!$ order ) {
36
+ $ response ->getBody ()->write (json_encode (['error ' => 'Pedido não encontrado ' ]));
37
+ $ this ->logger ->error ('Erro ao imprimir *Pedido não encontrado* ' , ['order_id ' => $ orderId ]);
38
+ return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (404 );
39
+ }
40
+
41
+ $ store = $ this ->userModel ->getById ($ order ['user_id ' ]);
35
42
36
- $ store = $ this ->userModel ->getStoreById ($ pedido ['store_id ' ]);
37
- $ cart = json_decode ($ pedido ['cart ' ], true );
38
-
39
- // Monta array para impressão
40
- $ printData = [
41
- ["type " =>0 ,"content " =>strtoupper ($ store ['store_name ' ]),"bold " =>1 ,"align " =>1 ,"format " =>2 ],
42
- ["type " =>0 ,"content " =>"PEDIDO # " .$ pedido ['id ' ],"bold " =>1 ,"align " =>1 ],
43
- ["type " =>0 ,"content " =>"Cliente: " .$ pedido ['customer_name ' ],"bold " =>0 ,"align " =>0 ],
44
- ["type " =>0 ,"content " =>"Endereço: " .$ pedido ['customer_address ' ],"bold " =>0 ,"align " =>0 ],
45
- ["type " =>0 ,"content " =>"------------------------------ " ,"bold " =>0 ,"align " =>1 ]
46
- ];
47
-
48
- foreach ($ cart as $ item ) {
49
- $ linha = $ item ['quantity ' ]."x " .$ item ['product_name ' ]." - R$ " .number_format ($ item ['unit_price ' ],2 ,', ' ,'. ' );
50
- $ printData [] = ["type " =>0 ,"content " =>$ linha ,"bold " =>0 ,"align " =>0 ];
51
- if (!empty ($ item ['ingredients ' ])) {
52
- foreach ($ item ['ingredients ' ] as $ ing ) {
53
- $ printData [] = ["type " =>0 ,"content " =>" + " .$ ing ['name ' ]." ( " .$ ing ['quantity ' ]."x) " ,"bold " =>0 ,"align " =>0 ];
54
- }
43
+ // Busca itens do pedido
44
+ $ stmt = $ this ->pdo ->prepare ("SELECT * FROM order_items WHERE order_id = ? " );
45
+ $ stmt ->execute ([$ orderId ]);
46
+ $ items = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
47
+
48
+ // Monta array para impressão
49
+ $ printData = [
50
+ ["type " =>0 ,"content " =>strtoupper ($ store ['store_name ' ]),"bold " =>1 ,"align " =>1 ,"format " =>2 ],
51
+ ["type " =>0 ,"content " =>"PEDIDO # " .$ order ['id ' ],"bold " =>1 ,"align " =>1 ],
52
+ ["type " =>0 ,"content " =>"Cliente: " .$ order ['customer_name ' ],"bold " =>0 ,"align " =>0 ],
53
+ ["type " =>0 ,"content " =>"Endereço: " .$ order ['customer_address ' ],"bold " =>0 ,"align " =>0 ],
54
+ ["type " =>0 ,"content " =>"------------------------------ " ,"bold " =>0 ,"align " =>1 ]
55
+ ];
56
+
57
+ foreach ($ items as $ item ) {
58
+ // Busca nome do produto
59
+ $ stmtProd = $ this ->pdo ->prepare ("SELECT name FROM products WHERE id = ? " );
60
+ $ stmtProd ->execute ([$ item ['product_id ' ]]);
61
+ $ product = $ stmtProd ->fetch (\PDO ::FETCH_ASSOC );
62
+
63
+ $ linha = $ item ['quantity ' ]."x " .$ product ['name ' ]." - R$ " .number_format ($ item ['unit_price ' ],2 ,', ' ,'. ' );
64
+ $ printData [] = ["type " =>0 ,"content " =>$ linha ,"bold " =>0 ,"align " =>0 ];
65
+
66
+ // Busca ingredientes/adicionais do item
67
+ $ stmtIng = $ this ->pdo ->prepare ("SELECT oi.*, i.name FROM order_item_ingredients oi JOIN ingredients i ON oi.ingredient_id = i.id WHERE oi.order_item_id = ? " );
68
+ $ stmtIng ->execute ([$ item ['id ' ]]);
69
+ $ ingredients = $ stmtIng ->fetchAll (\PDO ::FETCH_ASSOC );
70
+
71
+ if (!empty ($ ingredients )) {
72
+ foreach ($ ingredients as $ ing ) {
73
+ $ printData [] = ["type " =>0 ,"content " =>" + " .$ ing ['name ' ]." ( " .$ ing ['quantity ' ]."x) " ,"bold " =>0 ,"align " =>0 ];
55
74
}
56
75
}
76
+ }
57
77
58
- $ printData [] = ["type " =>0 ,"content " =>"------------------------------ " ,"bold " =>0 ,"align " =>1 ];
59
- $ printData [] = ["type " =>0 ,"content " =>"TOTAL: R$ " .number_format ($ pedido ['total_amount ' ],2 ,', ' ,'. ' ),"bold " =>1 ,"align " =>2 ];
60
- $ printData [] = ["type " =>0 ,"content " =>" " ,"bold " =>0 ,"align " =>0 ];
61
- // Adiciona QR Code PIX no final
62
- $ pixQrCode = '00020126460014br.gov.bcb.pix0124fortalecai2025@gmail.com5204000053039865802BR5925Alefe Augusto Lima Da Sil6014RIO DE JANEIRO622805242bf17e522526e2838a7b30ac6304993A ' ;
63
- $ printData [] = ["type " =>3 ,"value " =>$ pixQrCode ,"size " =>30 ,"align " =>1 ];
78
+ $ printData [] = ["type " =>0 ,"content " =>"------------------------------ " ,"bold " =>0 ,"align " =>1 ];
79
+ $ printData [] = ["type " =>0 ,"content " =>"TOTAL: R$ " .number_format ($ order ['total_amount ' ],2 ,', ' ,'. ' ),"bold " =>1 ,"align " =>2 ];
80
+ $ printData [] = ["type " =>0 ,"content " =>" " ,"bold " =>0 ,"align " =>0 ];
81
+ // Adiciona QR Code PIX no final
82
+ $ pixQrCode = '00020126460014br.gov.bcb.pix0124fortalecai2025@gmail.com5204000053039865802BR5925Alefe Augusto Lima Da Sil6014RIO DE JANEIRO622805242bf17e522526e2838a7b30ac6304993A ' ;
83
+ $ printData [] = ["type " =>3 ,"value " =>$ pixQrCode ,"size " =>30 ,"align " =>1 ];
64
84
65
- $ response ->getBody ()->write (json_encode ($ printData , JSON_UNESCAPED_UNICODE ));
66
- return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (200 );
67
- }
85
+ $ response ->getBody ()->write (json_encode ($ printData , JSON_UNESCAPED_UNICODE ));
86
+
87
+ $ this ->logger ->info ('Pedido enviado para impressão ' , ['order_id ' => $ orderId ]);
88
+ return $ response ->withHeader ('Content-Type ' , 'application/json ' )->withStatus (200 );
89
+ }
68
90
}
0 commit comments