1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2024 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Billing\Controller; |
23
|
|
|
|
24
|
|
|
use Parthenon\Billing\CustomerProviderInterface; |
25
|
|
|
use Parthenon\Billing\Invoice\InvoiceProviderInterface; |
26
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
27
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
31
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
32
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
33
|
|
|
|
34
|
|
|
class InvoiceController |
35
|
|
|
{ |
36
|
|
|
#[Route('/billing/invoices', name: 'parthenon_billing_invoices_list')] |
37
|
|
|
public function listAction( |
38
|
|
|
CustomerProviderInterface $customerProvider, |
39
|
|
|
InvoiceProviderInterface $invoiceProvider, |
40
|
|
|
SerializerInterface $serializer, |
41
|
|
|
): Response { |
42
|
|
|
$customer = $customerProvider->getCurrentCustomer(); |
43
|
|
|
$invoices = $invoiceProvider->fetchInvoices($customer); |
44
|
|
|
|
45
|
|
|
$returnData = $serializer->serialize(['invoices' => $invoices], 'json'); |
46
|
|
|
|
47
|
|
|
return JsonResponse::fromJsonString($returnData); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
#[Route('/billing/invoices/{id}/download', name: 'parthenon_billing_invoices_download')] |
51
|
|
|
public function downloadAction( |
52
|
|
|
Request $request, |
53
|
|
|
InvoiceProviderInterface $invoiceProvider, |
54
|
|
|
): Response { |
55
|
|
|
$download = $invoiceProvider->downloadInvoiceById($request->attributes->get('id')); |
56
|
|
|
|
57
|
|
|
$tmpFile = tempnam('/tmp', 'pdf'); |
58
|
|
|
file_put_contents($tmpFile, $download); |
59
|
|
|
|
60
|
|
|
$response = new BinaryFileResponse($tmpFile); |
61
|
|
|
$filename = sprintf('invoice-%s.pdf', $request->get('id')); |
62
|
|
|
|
63
|
|
|
$response->headers->set('Content-Type', 'application/pdf'); |
64
|
|
|
$response->setContentDisposition( |
65
|
|
|
ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
66
|
|
|
$filename |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|