|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Iain Cambridge 2020-2023. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.2.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Billing\Repository\Orm; |
|
16
|
|
|
|
|
17
|
|
|
use Parthenon\Athena\Repository\DoctrineCrudRepository; |
|
18
|
|
|
use Parthenon\Billing\Entity\CustomerInterface; |
|
19
|
|
|
use Parthenon\Billing\Entity\Payment; |
|
20
|
|
|
use Parthenon\Billing\Entity\Subscription; |
|
21
|
|
|
use Parthenon\Billing\Enum\SubscriptionStatus; |
|
22
|
|
|
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface; |
|
23
|
|
|
use Parthenon\Common\Exception\NoEntityFoundException; |
|
24
|
|
|
|
|
25
|
|
|
class SubscriptionRepository extends DoctrineCrudRepository implements SubscriptionRepositoryInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public function hasActiveSubscription(CustomerInterface $customer): bool |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
|
|
$this->getOneActiveSubscriptionForCustomer($customer); |
|
31
|
|
|
} catch (NoEntityFoundException $exception) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getOneActiveSubscriptionForCustomer(CustomerInterface $customer): Subscription |
|
39
|
|
|
{ |
|
40
|
|
|
$subscription = $this->entityRepository->findOneBy(['customer' => $customer, 'active' => true]); |
|
41
|
|
|
|
|
42
|
|
|
if (!$subscription instanceof Subscription) { |
|
43
|
|
|
throw new NoEntityFoundException(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $subscription; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getAllForCustomer(CustomerInterface $customer): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->entityRepository->findBy(['customer' => $customer]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getAllActiveForCustomer(CustomerInterface $customer): array |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->entityRepository->findBy(['customer' => $customer, 'active' => true]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function updateValidUntilForAllActiveSubscriptions(CustomerInterface $customer, string $mainExternalReference, \DateTimeInterface $validUntil): void |
|
60
|
|
|
{ |
|
61
|
|
|
$qb = $this->entityRepository->createQueryBuilder('s'); |
|
62
|
|
|
$qb->update() |
|
63
|
|
|
->set('s.validUntil', ':validUntil') |
|
64
|
|
|
->set('s.updatedAt', ':now') |
|
65
|
|
|
->where('s.customer = :customer') |
|
66
|
|
|
->andWhere('s.status = :active') |
|
67
|
|
|
->andWhere('s.mainExternalReference = :mainExternalReference') |
|
68
|
|
|
->setParameter('customer', $customer) |
|
69
|
|
|
->setParameter('mainExternalReference', $mainExternalReference) |
|
70
|
|
|
->setParameter('validUntil', $validUntil) |
|
71
|
|
|
->setParameter('now', new \DateTime()) |
|
72
|
|
|
->setParameter('active', SubscriptionStatus::ACTIVE); |
|
73
|
|
|
$query = $qb->getQuery(); |
|
74
|
|
|
$query->execute(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getActiveSubscriptionCount(CustomerInterface $customer): int |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->entityRepository->count(['customer' => $customer, 'active' => true]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getForPayment(Payment $payment): array |
|
83
|
|
|
{ |
|
84
|
|
|
$qb = $this->entityRepository->createQueryBuilder('s'); |
|
85
|
|
|
$qb->where(':payment MEMBER OF p.payment') |
|
86
|
|
|
->setParameter('payment', $payment) |
|
87
|
|
|
->orderBy('p.createdAt', 'DESC'); |
|
88
|
|
|
|
|
89
|
|
|
$result = $qb->getQuery()->getResult(); |
|
90
|
|
|
|
|
91
|
|
|
return $result; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getForMainAndChildExternalReference(string $mainExternalReference, string $childExternalReference): Subscription |
|
95
|
|
|
{ |
|
96
|
|
|
$subscription = $this->entityRepository->findOneBy(['mainExternalReference' => $mainExternalReference, 'childExternalReference' => $childExternalReference]); |
|
97
|
|
|
|
|
98
|
|
|
if (!$subscription instanceof Subscription) { |
|
99
|
|
|
throw new NoEntityFoundException(sprintf("No Subscription found for main external reference '%s' and child reference '%s'", $mainExternalReference, $childExternalReference)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $subscription; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|