-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFund.php
159 lines (136 loc) · 5.07 KB
/
Fund.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace MatchBot\Domain;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use MatchBot\Application\Assertion;
/**
* Represents a commitment of match funds, i.e. a Champion Fund or Pledge. Because a Fund (most
* typically a Champion Fund) can be split up and allocated to multiple Campaigns, the Fund in
* MatchBot doesn't contain an allocated amount and is mostly a container for metadata to help understand
* where any linked {@see CampaignFunding}s' money comes from.
*
* Concrete subclasses {@see ChampionFund} & {@see Pledge} are instantiated using Doctrine's
* single table inheritance. The discriminator column is 'fundType' and the API field which determines
* it originally, in {@see FundRepository::getNewFund()}, is 'type'.
*/
#[ORM\Table]
#[ORM\Entity(repositoryClass: FundRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'fundType', type: 'string')]
#[ORM\DiscriminatorMap([
ChampionFund::DISCRIMINATOR_VALUE => ChampionFund::class,
Pledge::DISCRIMINATOR_VALUE => Pledge::class,
self::DISCRIMINATOR_VALUE => self::class,
])]
#[ORM\HasLifecycleCallbacks]
abstract class Fund extends SalesforceReadProxy
{
use TimestampsTrait;
/** @var 'championFund'|'pledge'|'unknownFund' */
public const string DISCRIMINATOR_VALUE = 'unknownFund';
/**
* @var string ISO 4217 code for the currency used with this fund, and in which FundingWithdrawals are denominated.
*/
#[ORM\Column(type: 'string', length: 3)]
protected string $currencyCode;
/**
* @var string
*/
#[ORM\Column(type: 'string')]
protected string $name;
/**
* @var Collection<int, CampaignFunding>
*/
#[ORM\OneToMany(mappedBy: 'fund', targetEntity: CampaignFunding::class)]
protected Collection $campaignFundings;
final public function __construct(string $currencyCode, string $name, ?Salesforce18Id $salesforceId)
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->campaignFundings = new ArrayCollection();
$this->currencyCode = $currencyCode;
$this->name = $name;
$this->salesforceId = $salesforceId?->value;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
public function setCurrencyCode(string $currencyCode): void
{
$this->currencyCode = $currencyCode;
}
public function getCurrencyCode(): string
{
return $this->currencyCode;
}
/**
* Uses database copies of all data, not the Redis `Matching\Adapter`. Intended for use
* after a campaign closes.
*
* @return array{totalAmount: Money, usedAmount: Money}
*/
public function getAmounts(): array
{
$totalAmount = Money::fromPoundsGBP(0);
$usedAmount = Money::fromPoundsGBP(0);
foreach ($this->campaignFundings as $campaignFunding) {
$thisAmount = Money::fromNumericStringGBP($campaignFunding->getAmount());
$thisAmountAvailable = Money::fromNumericStringGBP($campaignFunding->getAmountAvailable());
$thisAmountUsed = $thisAmount->minus($thisAmountAvailable);
$totalAmount = $totalAmount->plus($thisAmount);
$usedAmount = $usedAmount->plus($thisAmountUsed);
}
return [
'totalAmount' => $totalAmount,
'usedAmount' => $usedAmount,
];
}
/**
* @return array{
* fundId: ?int,
* fundType: 'championFund'|'pledge'|'unknownFund',
* salesforceFundId: string,
* totalAmount: numeric-string,
* usedAmount: numeric-string,
* currencyCode: string
* }
*/
public function toAmountUsedUpdateModel(): array
{
$sfId = $this->getSalesforceId();
Assertion::notNull($sfId); // Only updating existing SF fund objects supported.
$amounts = $this->getAmounts();
return [
'currencyCode' => $amounts['totalAmount']->currency->isoCode(),
'fundId' => $this->getId(),
'fundType' => static::DISCRIMINATOR_VALUE,
'salesforceFundId' => $sfId,
'totalAmount' => $amounts['totalAmount']->toNumericString(),
'usedAmount' => $amounts['usedAmount']->toNumericString(),
];
}
/**
* @param CampaignFunding $funding which must already refer to this Fund. The field on this class the
* 'inverse' side of the relationship between the two in Doctrine, meaning that calling this function doesn't
* actually affect what gets saved to the DB. Only the values of \MatchBot\Domain\CampaignFunding::$fund are
* monitored by the ORM.
*/
public function addCampaignFunding(CampaignFunding $funding): void
{
Assertion::same($funding->getFund(), $this);
$this->campaignFundings->add($funding);
}
}