1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\InvoiceTax; |
6
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
|
|
|
10
|
|
|
use ApiPlatform\Core\Exception\InvalidValueException; |
|
|
|
|
11
|
|
|
use ControleOnline\Entity\People; |
|
|
|
|
12
|
|
|
use NFePHP\DA\NFe\Danfe; |
|
|
|
|
13
|
|
|
use NFePHP\DA\CTe\Dacte; |
|
|
|
|
14
|
|
|
use NFePHP\POS\PrintConnectors\Base64PrintConnector; |
|
|
|
|
15
|
|
|
use NFePHP\POS\DanfcePos; |
|
|
|
|
16
|
|
|
|
17
|
|
|
class DownloadNFService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Mimetypes |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $mimetypes = [ |
25
|
|
|
'xml' => 'application/xml', |
26
|
|
|
'pdf' => 'application/pdf', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* File output default names |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $filenames = [ |
35
|
|
|
'xml' => 'nota_fiscal.xml', |
36
|
|
|
'pdf' => 'nota_fiscal.pdf', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
public function __construct(private Request $request, private KernelInterface $kernel) |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function downloadNf(InvoiceTax $invoiceTax, $format = 'pdf') |
46
|
|
|
{ |
47
|
|
|
$method = 'get' . ucfirst(strtolower($format)); |
48
|
|
|
if (method_exists($this, $method) === false) |
49
|
|
|
throw new InvalidValueException( |
50
|
|
|
sprintf('Format "%s" is not available', $format) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if (($content = $this->$method($invoiceTax)) === null) |
54
|
|
|
throw new InvalidValueException('File content is empty'); |
55
|
|
|
|
56
|
|
|
$response = new StreamedResponse(function () use ($content) { |
57
|
|
|
fputs(fopen('php://output', 'wb'), $content); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
$response->headers->set('Content-Type', $this->mimetypes[$format]); |
61
|
|
|
|
62
|
|
|
$disposition = HeaderUtils::makeDisposition( |
63
|
|
|
$format == 'pdf' ? HeaderUtils::DISPOSITION_INLINE : HeaderUtils::DISPOSITION_ATTACHMENT, |
64
|
|
|
$this->filenames[$format] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getXml(InvoiceTax $invoiceTax): ?string |
73
|
|
|
{ |
74
|
|
|
return $invoiceTax->getInvoice(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getPdf(InvoiceTax $invoiceTax): ?string |
78
|
|
|
{ |
79
|
|
|
if ($invoiceTax->getOrder()[0]->getInvoiceType() == 55) { |
80
|
|
|
$file = $this->getPeopleFilePath($invoiceTax->getOrder()[0]->getOrder()->getClient()); |
81
|
|
|
$danfe = new Danfe($invoiceTax->getInvoice(), 'P', 'A4', $file, 'I', ''); |
82
|
|
|
return $danfe->render($file); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($invoiceTax->getOrder()[0]->getInvoiceType() == 57) { |
86
|
|
|
$file = $this->getPeopleFilePath($invoiceTax->getOrder()[0]->getOrder()->getProvider()); |
87
|
|
|
$danfe = new Dacte($invoiceTax->getInvoice(), 'P', 'A4', $file, 'I', ''); |
88
|
|
|
return $danfe->render($file); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getPng(InvoiceTax $invoiceTax): ?string |
95
|
|
|
{ |
96
|
|
|
if ($invoiceTax->getOrder()[0]->getInvoiceType() == 65) { |
97
|
|
|
$connector = new Base64PrintConnector(); |
98
|
|
|
$danfcepos = new DanfcePos($connector); |
99
|
|
|
$logopath = '../../fixtures/logo.png'; // Impressa no início da DANFCe |
100
|
|
|
$danfcepos->logo($logopath); |
101
|
|
|
$danfcepos->loadNFCe($invoiceTax->getInvoice()); |
102
|
|
|
$danfcepos->imprimir(); |
103
|
|
|
return $connector->getBase64Data(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function getPeopleFilePath(?People $people): string |
108
|
|
|
{ |
109
|
|
|
$root = $this->kernel->getProjectDir(); |
110
|
|
|
$pixel = sprintf('%s/data/files/users/white-pixel.jpg', $root); |
111
|
|
|
$path = $pixel; |
112
|
|
|
|
113
|
|
|
if ($people === null) |
114
|
|
|
return $pixel; |
115
|
|
|
|
116
|
|
|
if (($file = $people->getFile()) !== null) { |
117
|
|
|
$path = $root . '/' . $file->getPath(); |
118
|
|
|
|
119
|
|
|
if (strpos($file->getPath(), 'data/') !== false) |
120
|
|
|
$path = $root . '/' . str_replace('data/', 'public/', $file->getPath()); |
121
|
|
|
|
122
|
|
|
$parts = pathinfo($path); |
123
|
|
|
if ($parts['extension'] != 'jpg') |
124
|
|
|
return $pixel; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $path; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths