This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
InvoiceController.php
117 lines (99 loc) · 3.05 KB
/
InvoiceController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\InvoiceBundle\Controller;
use Sonata\Component\Customer\CustomerInterface;
use Sonata\Component\Invoice\InvoiceManagerInterface;
use Sonata\Component\Order\OrderManagerInterface;
use Sonata\Component\Transformer\InvoiceTransformer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class InvoiceController extends Controller
{
/**
* @throws \RuntimeException
*/
public function indexAction(): void
{
throw new \RuntimeException('not implemented');
}
/**
* @param string $reference
*
* @throws AccessDeniedException
*
* @return Response
*/
public function viewAction($reference)
{
$order = $this->getOrderManager()->findOneBy(['reference' => $reference]);
if (null === $order) {
throw new AccessDeniedException();
}
$this->checkAccess($order->getCustomer());
$invoice = $this->getInvoiceManager()->findOneBy(['reference' => $reference]);
if (null === $invoice) {
$invoice = $this->getInvoiceManager()->create();
$this->getInvoiceTransformer()->transformFromOrder($order, $invoice);
$this->getInvoiceManager()->save($invoice);
}
$this->get('sonata.seo.page')->setTitle($this->get('translator')->trans('invoice_view_title', [], 'SonataInvoiceBundle'));
return $this->render('@SonataInvoice/Invoice/view.html.twig', [
'invoice' => $invoice,
'order' => $order,
]);
}
/**
* @param string $reference
*
* @throws \RuntimeException
*/
public function downloadAction($reference): void
{
throw new \RuntimeException('not implemented');
}
/**
* Checks that the current logged in user has access to given invoice.
*
* @param CustomerInterface $customer The linked customer
*
* @throws AccessDeniedException
*/
protected function checkAccess(CustomerInterface $customer): void
{
if (!($user = $this->getUser())
|| !$customer->getUser()
|| $customer->getUser()->getId() !== $user->getId()) {
throw new AccessDeniedException();
}
}
/**
* @return InvoiceManagerInterface
*/
protected function getInvoiceManager()
{
return $this->get('sonata.invoice.manager');
}
/**
* @return OrderManagerInterface
*/
protected function getOrderManager()
{
return $this->get('sonata.order.manager');
}
/**
* @return InvoiceTransformer
*/
protected function getInvoiceTransformer()
{
return $this->get('sonata.payment.transformer.invoice');
}
}