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 Doctrine\ORM\Mapping as ORM; |
10
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
11
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait that implements the deposit functionality |
15
|
|
|
* Used in: |
16
|
|
|
* <li>@see ProductVariant</li> |
17
|
|
|
*/ |
18
|
|
|
trait ProductVariantDepositTrait |
19
|
|
|
{ |
20
|
|
|
public function initProductVariantDepositTrait(): void |
21
|
|
|
{ |
22
|
|
|
$this->channelDeposits = new ArrayCollection(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Collection |
27
|
|
|
* |
28
|
|
|
* @psalm-var Collection<array-key, ChannelDepositInterface> |
29
|
|
|
* |
30
|
|
|
* @ORM\OneToMany( |
31
|
|
|
* targetEntity="\Gewebe\SyliusProductDepositPlugin\Entity\ChannelDepositInterface", |
32
|
|
|
* mappedBy="productVariant", |
33
|
|
|
* indexBy="channelCode", |
34
|
|
|
* cascade={"all"}, |
35
|
|
|
* orphanRemoval=true |
36
|
|
|
* ) |
37
|
|
|
*/ |
38
|
|
|
protected $channelDeposits; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TaxCategoryInterface|null |
42
|
|
|
* |
43
|
|
|
* @ORM\ManyToOne(targetEntity="\Sylius\Component\Taxation\Model\TaxCategoryInterface", cascade={"all"}, fetch="EAGER") |
44
|
|
|
* @ORM\JoinColumn(name="deposit_tax_category_id", referencedColumnName="id", onDelete="SET NULL") |
45
|
|
|
*/ |
46
|
|
|
protected $depositTaxCategory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns all deposit elements for all channels |
50
|
|
|
* @psalm-return Collection<array-key, ChannelDepositInterface> |
51
|
|
|
*/ |
52
|
|
|
public function getChannelDeposits(): Collection |
53
|
|
|
{ |
54
|
|
|
return $this->channelDeposits; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getDepositPriceByChannel(ChannelInterface $channel): ?int |
58
|
|
|
{ |
59
|
|
|
$channelCode = $channel->getCode(); |
60
|
|
|
if ($channelCode === null) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$channelDeposit = $this->channelDeposits->get($channelCode); |
65
|
|
|
if (!$channelDeposit instanceof ChannelDepositInterface) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $channelDeposit->getPrice() ?? null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function hasChannelDeposit(ChannelDepositInterface $channelDeposit): bool |
73
|
|
|
{ |
74
|
|
|
return $this->channelDeposits->contains($channelDeposit); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function addChannelDeposit(ChannelDepositInterface $channelDeposit): void |
78
|
|
|
{ |
79
|
|
|
$channelCode = $channelDeposit->getChannelCode(); |
80
|
|
|
|
81
|
|
|
if ($channelCode !== null && !$this->hasChannelDeposit($channelDeposit)) { |
82
|
|
|
$channelDeposit->setProductVariant($this); |
|
|
|
|
83
|
|
|
$this->channelDeposits->set($channelCode, $channelDeposit); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function removeChannelDeposit(ChannelDepositInterface $channelDeposit): void |
88
|
|
|
{ |
89
|
|
|
$channelCode = $channelDeposit->getChannelCode(); |
90
|
|
|
|
91
|
|
|
if ($channelCode !== null && $this->hasChannelDeposit($channelDeposit)) { |
92
|
|
|
$channelDeposit->setProductVariant(null); |
93
|
|
|
$this->channelDeposits->remove($channelCode); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getDepositTaxCategory(): ?TaxCategoryInterface |
98
|
|
|
{ |
99
|
|
|
return $this->depositTaxCategory; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setDepositTaxCategory(TaxCategoryInterface $depositTaxCategory): void |
103
|
|
|
{ |
104
|
|
|
$this->depositTaxCategory = $depositTaxCategory; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|