Donation.php
232 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Models; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use Give\Donations\DataTransferObjects\DonationQueryData; |
| 8 | use Give\Donations\Factories\DonationFactory; |
| 9 | use Give\Donations\Properties\BillingAddress; |
| 10 | use Give\Donations\ValueObjects\DonationMode; |
| 11 | use Give\Donations\ValueObjects\DonationStatus; |
| 12 | use Give\Donors\Models\Donor; |
| 13 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 14 | use Give\Framework\Models\Contracts\ModelCrud; |
| 15 | use Give\Framework\Models\Contracts\ModelHasFactory; |
| 16 | use Give\Framework\Models\Model; |
| 17 | use Give\Framework\Models\ModelQueryBuilder; |
| 18 | use Give\Framework\Models\ValueObjects\Relationship; |
| 19 | use Give\Subscriptions\Models\Subscription; |
| 20 | use Give\ValueObjects\Money; |
| 21 | |
| 22 | /** |
| 23 | * Class Donation |
| 24 | * |
| 25 | * @since 2.19.6 |
| 26 | * |
| 27 | * @property int $id |
| 28 | * @property int $formId |
| 29 | * @property string $formTitle |
| 30 | * @property DateTime $createdAt |
| 31 | * @property DateTime $updatedAt |
| 32 | * @property DonationStatus $status |
| 33 | * @property DonationMode $mode |
| 34 | * @property int $amount |
| 35 | * @property string $currency |
| 36 | * @property string $gateway |
| 37 | * @property int $donorId |
| 38 | * @property string $firstName |
| 39 | * @property string $lastName |
| 40 | * @property string $email |
| 41 | * @property int $parentId |
| 42 | * @property int $subscriptionId |
| 43 | * @property BillingAddress $billingAddress |
| 44 | * @property string $purchaseKey |
| 45 | * @property string $donorIp |
| 46 | * @property bool $anonymous |
| 47 | * @property int $levelId |
| 48 | * @property string $gatewayTransactionId |
| 49 | * @property Donor $donor |
| 50 | * @property Subscription $subscription |
| 51 | */ |
| 52 | class Donation extends Model implements ModelCrud, ModelHasFactory |
| 53 | { |
| 54 | /** |
| 55 | * @inheritdoc |
| 56 | */ |
| 57 | protected $properties = [ |
| 58 | 'id' => 'int', |
| 59 | 'formId' => 'int', |
| 60 | 'formTitle' => 'string', |
| 61 | 'purchaseKey' => 'string', |
| 62 | 'donorIp' => 'string', |
| 63 | 'createdAt' => DateTime::class, |
| 64 | 'updatedAt' => DateTime::class, |
| 65 | 'status' => DonationStatus::class, |
| 66 | 'mode' => DonationMode::class, |
| 67 | 'amount' => 'int', |
| 68 | 'currency' => 'string', |
| 69 | 'gateway' => 'string', |
| 70 | 'donorId' => 'int', |
| 71 | 'firstName' => 'string', |
| 72 | 'lastName' => 'string', |
| 73 | 'email' => 'string', |
| 74 | 'parentId' => 'int', |
| 75 | 'subscriptionId' => 'int', |
| 76 | 'billingAddress' => BillingAddress::class, |
| 77 | 'anonymous' => 'bool', |
| 78 | 'levelId' => 'int', |
| 79 | 'gatewayTransactionId' => 'string', |
| 80 | ]; |
| 81 | |
| 82 | /** |
| 83 | * @inheritdoc |
| 84 | */ |
| 85 | protected $relationships = [ |
| 86 | 'donor' => Relationship::BELONGS_TO, |
| 87 | 'subscription' => Relationship::BELONGS_TO, |
| 88 | ]; |
| 89 | |
| 90 | /** |
| 91 | * Find donation by ID |
| 92 | * |
| 93 | * @since 2.19.6 |
| 94 | * |
| 95 | * @param int $id |
| 96 | * |
| 97 | * @return Donation |
| 98 | */ |
| 99 | public static function find($id) |
| 100 | { |
| 101 | return give()->donations->getById($id); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 2.19.6 |
| 106 | * |
| 107 | * @param array $attributes |
| 108 | * |
| 109 | * @return Donation |
| 110 | * |
| 111 | * @throws Exception|InvalidArgumentException |
| 112 | */ |
| 113 | public static function create(array $attributes) |
| 114 | { |
| 115 | $donation = new static($attributes); |
| 116 | |
| 117 | return give()->donations->insert($donation); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @since 2.19.6 |
| 122 | * |
| 123 | * @return Donation |
| 124 | * |
| 125 | * @throws Exception|InvalidArgumentException |
| 126 | */ |
| 127 | public function save() |
| 128 | { |
| 129 | if (!$this->id) { |
| 130 | return give()->donations->insert($this); |
| 131 | } |
| 132 | |
| 133 | return give()->donations->update($this); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @since 2.19.6 |
| 138 | * |
| 139 | * @return bool |
| 140 | * |
| 141 | * @throws Exception|InvalidArgumentException |
| 142 | */ |
| 143 | public function delete() |
| 144 | { |
| 145 | return give()->donations->delete($this); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @since 2.19.6 |
| 150 | * |
| 151 | * @return ModelQueryBuilder<Donor> |
| 152 | */ |
| 153 | public function donor() |
| 154 | { |
| 155 | return give()->donors->queryById($this->donorId); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @since 2.19.6 |
| 160 | * |
| 161 | * @return ModelQueryBuilder<Subscription> |
| 162 | */ |
| 163 | public function subscription() |
| 164 | { |
| 165 | if ($this->subscriptionId) { |
| 166 | return give()->subscriptions->queryById($this->subscriptionId); |
| 167 | } |
| 168 | |
| 169 | return give()->subscriptions->queryByDonationId($this->id); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @since 2.19.6 |
| 174 | * |
| 175 | * @return int|null |
| 176 | */ |
| 177 | public function getSequentialId() |
| 178 | { |
| 179 | return give()->donations->getSequentialId($this->id); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @since 2.19.6 |
| 184 | * |
| 185 | * @return object[] |
| 186 | */ |
| 187 | public function getNotes() |
| 188 | { |
| 189 | return give()->donations->getNotesByDonationId($this->id); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @since 2.19.6 |
| 194 | * |
| 195 | * @return Money |
| 196 | */ |
| 197 | public function getMinorAmount() |
| 198 | { |
| 199 | return Money::ofMinor($this->amount, $this->currency); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @since 2.19.6 |
| 204 | * |
| 205 | * @return ModelQueryBuilder<Donation> |
| 206 | */ |
| 207 | public static function query() |
| 208 | { |
| 209 | return give()->donations->prepareQuery(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @since 2.19.6 |
| 214 | * |
| 215 | * @param object $object |
| 216 | * |
| 217 | * @return Donation |
| 218 | */ |
| 219 | public static function fromQueryBuilderObject($object) |
| 220 | { |
| 221 | return DonationQueryData::fromObject($object)->toDonation(); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @return DonationFactory<Donation> |
| 226 | */ |
| 227 | public static function factory() |
| 228 | { |
| 229 | return new DonationFactory(static::class); |
| 230 | } |
| 231 | } |
| 232 |