1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewebe\SyliusProductDepositPlugin\Entity; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
10
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait that implements the deposit functionality |
14
|
|
|
* Used in: |
15
|
|
|
* <li>@see ProductVariant</li> |
16
|
|
|
*/ |
17
|
|
|
trait ProductVariantDepositTrait |
18
|
|
|
{ |
19
|
|
|
public function initProductVariantDepositTrait(): void |
20
|
|
|
{ |
21
|
|
|
$this->channelDeposits = new ArrayCollection(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Collection |
26
|
|
|
* |
27
|
|
|
* @psalm-var Collection<array-key, ChannelDepositInterface> |
28
|
|
|
* |
29
|
|
|
* @ORM\OneToMany( |
30
|
|
|
* targetEntity="\Gewebe\SyliusProductDepositPlugin\Entity\ChannelDepositInterface", |
31
|
|
|
* mappedBy="productVariant", |
32
|
|
|
* indexBy="channelCode", |
33
|
|
|
* cascade={"all"}, |
34
|
|
|
* orphanRemoval=true |
35
|
|
|
* ) |
36
|
|
|
*/ |
37
|
|
|
protected $channelDeposits; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TaxCategoryInterface |
41
|
|
|
* |
42
|
|
|
* @ORM\ManyToOne(targetEntity="\Sylius\Component\Taxation\Model\TaxCategoryInterface", cascade={"all"}, fetch="EAGER") |
43
|
|
|
* @ORM\JoinColumn(name="deposit_tax_category_id", referencedColumnName="id", onDelete="SET NULL") |
44
|
|
|
*/ |
45
|
|
|
protected $depositTaxCategory; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns all deposit elements for all channels |
49
|
|
|
* @return Collection|ChannelDepositInterface[] |
50
|
|
|
* |
51
|
|
|
* @psalm-return Collection<array-key, ChannelDepositInterface> |
52
|
|
|
*/ |
53
|
|
|
public function getChannelDeposits(): Collection |
54
|
|
|
{ |
55
|
|
|
return $this->channelDeposits; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the deposit element for one channel |
60
|
|
|
* @param ChannelInterface $channel |
61
|
|
|
* @return ChannelDepositInterface|null |
62
|
|
|
*/ |
63
|
|
|
public function getChannelDepositForChannel(ChannelInterface $channel): ?ChannelDepositInterface |
64
|
|
|
{ |
65
|
|
|
$channelCode = $channel->getCode(); |
66
|
|
|
|
67
|
|
|
if ($channelCode !== null && $this->channelDeposits->containsKey($channelCode)) { |
68
|
|
|
return $this->channelDeposits->get($channelCode); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if channel has deposit element |
76
|
|
|
* @param ChannelInterface $channel |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function hasChannelDepositForChannel(ChannelInterface $channel): bool |
80
|
|
|
{ |
81
|
|
|
return null !== $this->getChannelDepositForChannel($channel); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if has deposit element |
86
|
|
|
* @param ChannelDepositInterface $channelDeposit |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasChannelDeposit(ChannelDepositInterface $channelDeposit): bool |
90
|
|
|
{ |
91
|
|
|
return $this->channelDeposits->contains($channelDeposit); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds an deposit element to the list |
96
|
|
|
* @param ChannelDepositInterface $channelDeposit |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function addChannelDeposit(ChannelDepositInterface $channelDeposit): void |
100
|
|
|
{ |
101
|
|
|
$channelCode = $channelDeposit->getChannelCode(); |
102
|
|
|
|
103
|
|
|
if ($channelCode !== null && !$this->hasChannelDeposit($channelDeposit)) { |
104
|
|
|
$channelDeposit->setProductVariant($this); |
|
|
|
|
105
|
|
|
$this->channelDeposits->set($channelCode, $channelDeposit); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Removes a deposit element from the list |
111
|
|
|
* @param ChannelDepositInterface $channelDeposit |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function removeChannelDeposit(ChannelDepositInterface $channelDeposit): void |
115
|
|
|
{ |
116
|
|
|
$channelCode = $channelDeposit->getChannelCode(); |
117
|
|
|
|
118
|
|
|
if ($channelCode !== null && $this->hasChannelDeposit($channelDeposit)) { |
119
|
|
|
$channelDeposit->setProductVariant(null); |
120
|
|
|
$this->channelDeposits->remove($channelCode); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return TaxCategoryInterface |
126
|
|
|
*/ |
127
|
|
|
public function getDepositTaxCategory(): ?TaxCategoryInterface |
128
|
|
|
{ |
129
|
|
|
return $this->depositTaxCategory; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param TaxCategoryInterface $depositTaxCategory |
134
|
|
|
*/ |
135
|
|
|
public function setDepositTaxCategory(TaxCategoryInterface $depositTaxCategory): void |
136
|
|
|
{ |
137
|
|
|
$this->depositTaxCategory = $depositTaxCategory; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|