|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Enum\ProductType; |
|
8
|
|
|
use Application\Repository\OrderLineRepository; |
|
9
|
|
|
use Application\Repository\UserRepository; |
|
10
|
|
|
use Application\Traits\HasBalance; |
|
11
|
|
|
use Application\Traits\HasBalanceInterface; |
|
12
|
|
|
use Application\Traits\HasProductType; |
|
13
|
|
|
use Application\Traits\HasQuantity; |
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
15
|
|
|
use Ecodev\Felix\Model\Traits\HasName; |
|
16
|
|
|
use GraphQL\Doctrine\Attribute as API; |
|
17
|
|
|
use Money\Money; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A single line in the shopping cart when making an order. |
|
21
|
|
|
*/ |
|
22
|
|
|
#[ORM\Entity(OrderLineRepository::class)] |
|
23
|
|
|
class OrderLine extends AbstractModel implements HasBalanceInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use HasBalance; |
|
26
|
|
|
use HasName; |
|
27
|
|
|
use HasProductType; |
|
28
|
|
|
use HasQuantity; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Additional emails for subscription for a company. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string[] |
|
34
|
|
|
*/ |
|
35
|
|
|
#[ORM\Column(type: 'json', options: ['default' => '[]'])] |
|
36
|
|
|
private array $additionalEmails = []; |
|
37
|
|
|
|
|
38
|
|
|
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] |
|
39
|
|
|
#[ORM\ManyToOne(targetEntity: Order::class, inversedBy: 'orderLines')] |
|
40
|
|
|
private ?Order $order = null; |
|
41
|
|
|
|
|
42
|
|
|
#[ORM\JoinColumn(onDelete: 'SET NULL')] |
|
43
|
|
|
#[ORM\ManyToOne(targetEntity: Product::class)] |
|
44
|
|
|
private ?Product $product = null; |
|
45
|
|
|
|
|
46
|
|
|
#[ORM\JoinColumn(onDelete: 'SET NULL')] |
|
47
|
|
|
#[ORM\ManyToOne(targetEntity: Subscription::class)] |
|
48
|
|
|
private ?Subscription $subscription = null; |
|
49
|
|
|
|
|
50
|
24 |
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
24 |
|
$this->balanceCHF = Money::CHF(0); |
|
53
|
24 |
|
$this->balanceEUR = Money::EUR(0); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
17 |
|
#[API\Exclude] |
|
57
|
|
|
public function setOrder(Order $order): void |
|
58
|
|
|
{ |
|
59
|
17 |
|
if ($this->order) { |
|
60
|
1 |
|
$this->order->orderLineRemoved($this); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
17 |
|
$this->order = $order; |
|
64
|
17 |
|
$order->orderLineAdded($this); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function getOrder(): Order |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->order; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get related product, if it still exists in DB. |
|
74
|
|
|
*/ |
|
75
|
22 |
|
public function getProduct(): ?Product |
|
76
|
|
|
{ |
|
77
|
22 |
|
return $this->product; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set related product. |
|
82
|
|
|
*/ |
|
83
|
15 |
|
public function setProduct(?Product $product): void |
|
84
|
|
|
{ |
|
85
|
15 |
|
$this->product = $product; |
|
86
|
15 |
|
if ($product) { |
|
87
|
15 |
|
$this->subscription = null; |
|
88
|
15 |
|
$this->setName($product->getName()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get related subscription, if it still exists in DB. |
|
94
|
|
|
*/ |
|
95
|
16 |
|
public function getSubscription(): ?Subscription |
|
96
|
|
|
{ |
|
97
|
16 |
|
return $this->subscription; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set related subscription. |
|
102
|
|
|
*/ |
|
103
|
9 |
|
public function setSubscription(?Subscription $subscription): void |
|
104
|
|
|
{ |
|
105
|
9 |
|
$this->subscription = $subscription; |
|
106
|
|
|
|
|
107
|
9 |
|
if ($subscription) { |
|
108
|
9 |
|
$this->product = null; |
|
109
|
9 |
|
$this->setName($subscription->getName()); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
public function setDonation(): void |
|
114
|
|
|
{ |
|
115
|
3 |
|
$this->product = null; |
|
116
|
3 |
|
$this->subscription = null; |
|
117
|
3 |
|
$this->setName('Don'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Additional emails for subscription for a company. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string[] |
|
124
|
|
|
*/ |
|
125
|
13 |
|
public function getAdditionalEmails(): array |
|
126
|
|
|
{ |
|
127
|
13 |
|
return $this->additionalEmails; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Additional emails for subscription for a company. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string[] $additionalEmails |
|
134
|
|
|
*/ |
|
135
|
21 |
|
public function setAdditionalEmails(array $additionalEmails): void |
|
136
|
|
|
{ |
|
137
|
21 |
|
$this->additionalEmails = $additionalEmails; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Create temporary users to give them immediate access to web, |
|
142
|
|
|
* until their access is confirmed permanently via a CSV import. |
|
143
|
|
|
*/ |
|
144
|
4 |
|
public function maybeGiveTemporaryAccess(): void |
|
145
|
|
|
{ |
|
146
|
4 |
|
$isDigital = $this->getSubscription() && ProductType::includesDigital($this->getSubscription()->getType()); |
|
147
|
4 |
|
if (!$isDigital) { |
|
148
|
3 |
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
foreach ($this->getAdditionalEmails() as $email) { |
|
152
|
|
|
/** @var UserRepository $userRepository */ |
|
153
|
1 |
|
$userRepository = _em()->getRepository(User::class); |
|
154
|
1 |
|
$user = $userRepository->getOrCreate($email); |
|
155
|
|
|
|
|
156
|
1 |
|
$user->setWebTemporaryAccess(true); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
if ($this->getOwner()) { |
|
160
|
2 |
|
$this->getOwner()->setWebTemporaryAccess(true); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|