-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPriceQuery.php
More file actions
644 lines (583 loc) · 18.1 KB
/
PriceQuery.php
File metadata and controls
644 lines (583 loc) · 18.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\stripe\elements\db;
use craft\base\ElementInterface;
use craft\db\Connection;
use craft\db\QueryAbortedException;
use craft\db\Table;
use craft\elements\db\ElementQuery;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\stripe\elements\Price;
use craft\stripe\elements\Product;
use craft\stripe\enums\PriceType;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
/**
* Price query
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @method Price[]|array all($db = null)
* @method Price|array|null one($db = null)
* @method Price|array|null nth(int $n, Connection $db = null)
*/
class PriceQuery extends ElementQuery
{
/**
* @var mixed The Stripe price ID(s) that the resulting prices must have.
*/
public mixed $stripeId = null;
/**
* @var mixed
*/
public mixed $stripeStatus = null;
/**
* @var mixed|null Type of the price one-time or recurring
*/
public mixed $type = null;
/**
* @var mixed|null Price's main currency
*/
public mixed $primaryCurrency = null;
/**
* @var mixed|null Price's currency
*/
public mixed $currency = null;
/**
* @var mixed|null Unit amount
*/
public mixed $unitAmount = null;
/**
* @var mixed|null Stripe id of the product the price is associated with
*/
public mixed $stripeProductId = null;
/**
* @var mixed The primary owner element ID(s) that the resulting prices must belong to.
* @used-by primaryOwner()
* @used-by primaryOwnerId()
*/
public mixed $primaryOwnerId = null;
/**
* @var mixed The owner element ID(s) that the resulting prices must belong to.
* @used-by owner()
* @used-by ownerId()
*/
public mixed $ownerId = null;
/**
* @var bool|null Whether the owner elements can be drafts.
* @used-by allowOwnerDrafts()
*/
public ?bool $allowOwnerDrafts = null;
/**
* @var bool|null Whether the owner elements can be revisions.
* @used-by allowOwnerRevisions()
*/
public ?bool $allowOwnerRevisions = null;
/**
* @inheritdoc
*/
protected array $defaultOrderBy = ['stripe_pricedata.stripeId' => SORT_ASC];
/**
* @inheritdoc
*/
public function __construct($elementType, array $config = [])
{
// Default status
if (!isset($config['status'])) {
$config['status'] = 'enabled';
}
parent::__construct($elementType, $config);
}
/**
* Narrows the query results based on the Stripe status
*/
public function stripeStatus(mixed $value): self
{
$this->stripeStatus = $value;
return $this;
}
/**
* Narrows the query results based on the Stripe price ID
*/
public function stripeId(mixed $value): self
{
$this->stripeId = $value;
return $this;
}
/**
* Narrows the query results based on the price's type.
*
* Possible values include:
*
* | Value | Fetches {elements}…
* | - | -
* | `'one_time'` | that are one-time prices.
* | `'recurring'` | that are recurring prices.
*
* ---
*
* ```twig
* {# Fetch recurring prices #}
* {% set {elements-var} = {twig-method}
* .type('recurring')
* .all() %}
* ```
*
* ```php
* // Fetch recurring prices
* ${elements-var} = {element-class}::find()
* ->type(PriceType::Recurring)
* ->all();
* ```
*/
public function type(mixed $value): self
{
if ($value instanceof PriceType) {
$this->type = $value->value;
} else {
$this->type = $value;
}
return $this;
}
/**
* Narrows the query results based on the primary currency.
*
* ---
*
* ```twig
* {# Fetch prices where the primary currency is GBP #}
* {% set {elements-var} = {twig-method}
* .primaryCurrency('GBP')
* .all() %}
* ```
*
* ```php
* // Fetch prices where the primary currency is GBP
* ${elements-var} = {element-class}::find()
* ->primaryCurrency('GBP')
* ->all();
* ```
*/
public function primaryCurrency(mixed $value): self
{
$this->primaryCurrency = $value;
return $this;
}
/**
* Narrows the query results based on the currencies supported by the price.
*
* ---
*
* ```twig
* {# Fetch prices where the currency is GBP #}
* {% set {elements-var} = {twig-method}
* .currency('GBP')
* .all() %}
* ```
*
* ```php
* // Fetch prices where the currency is GBP
* ${elements-var} = {element-class}::find()
* ->currency('GBP')
* ->all();
* ```
*/
public function currency(mixed $value): self
{
$this->currency = $value;
return $this;
}
/**
* Narrows the query results based on the unit amount of the price.
*
* ---
*
* ```twig
* {# Fetch prices where the unit amount is > 10 #}
* {% set {elements-var} = {twig-method}
* .unitAmount('> 10')
* .all() %}
* ```
*
* ```php
* // Fetch prices where the unit amount is > 10
* ${elements-var} = {element-class}::find()
* ->unitAmount('> 10')
* ->all();
* ```
*/
public function unitAmount(mixed $value): self
{
$this->unitAmount = $value;
return $this;
}
/**
* Narrows the query results based on the product associated with the price.
*
* ---
*
* ```twig
* {# Fetch prices where the product id is prod_abcdefghi #}
* {% set {elements-var} = {twig-method}
* .stripeProductId('prod_abcdefghi')
* .all() %}
* ```
*
* ```php
* // Fetch prices where the product id is prod_abcdefghi
* ${elements-var} = {element-class}::find()
* ->stripeProductId('prod_abcdefghi')
* ->all();
* ```
*/
public function stripeProductId(mixed $value): self
{
$this->stripeProductId = $value;
return $this;
}
/**
* Narrows the query results based on the {elements}’ statuses.
*
* Possible values include:
*
* | Value | Fetches {elements}…
* | - | -
* | `'live'` _(default)_ | that are live (enabled in Craft, with an Active Stripe Status).
* | `'stripeArchived'` | that are enabled, with an Archived Stripe Status.
* | `'disabled'` | that are disabled in Craft (Regardless of Stripe Status).
* | `['live', 'stripeArchived']` | that are live or with an Archived Stripe Status.
*
* ---
*
* ```twig
* {# Fetch disabled {elements} #}
* {% set {elements-var} = {twig-method}
* .status('disabled')
* .all() %}
* ```
*
* ```php
* // Fetch disabled {elements}
* ${elements-var} = {element-class}::find()
* ->status('disabled')
* ->all();
* ```
*/
public function status(array|string|null $value): static
{
parent::status($value);
return $this;
}
/**
* @inheritdoc
*/
protected function statusCondition(string $status): mixed
{
$res = match ($status) {
strtolower(Product::STATUS_LIVE) => [
'elements.enabled' => true,
'elements_sites.enabled' => true,
'stripe_pricedata.stripeStatus' => 'active',
],
strtolower(Product::STATUS_STRIPE_ARCHIVED) => [
'elements.enabled' => true,
'elements_sites.enabled' => true,
'stripe_pricedata.stripeStatus' => 'archived',
],
default => parent::statusCondition($status),
};
return $res;
}
/**
* Narrows the query results based on the primary owner element of the addresses, per the owners’ IDs.
*
* Possible values include:
*
* | Value | Fetches addresses…
* | - | -
* | `1` | created for an element with an ID of 1.
* | `'not 1'` | not created for an element with an ID of 1.
* | `[1, 2]` | created for an element with an ID of 1 or 2.
* | `['not', 1, 2]` | not created for an element with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch addresses created for an element with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .primaryOwnerId(1)
* .all() %}
* ```
*
* ```php
* // Fetch addresses created for an element with an ID of 1
* ${elements-var} = {php-method}
* ->primaryOwnerId(1)
* ->all();
* ```
*
* @param mixed $value The property value
* @return static self reference
* @uses $primaryOwnerId
*/
public function primaryOwnerId(mixed $value): static
{
$this->primaryOwnerId = $value;
return $this;
}
/**
* Sets the [[primaryOwnerId()]] and [[siteId()]] parameters based on a given element.
*
* ---
*
* ```twig
* {# Fetch addresses created for this entry #}
* {% set {elements-var} = {twig-method}
* .primaryOwner(myEntry)
* .all() %}
* ```
*
* ```php
* // Fetch addresses created for this entry
* ${elements-var} = {php-method}
* ->primaryOwner($myEntry)
* ->all();
* ```
*
* @param ElementInterface $primaryOwner The primary owner element
* @return static self reference
* @uses $primaryOwnerId
*/
public function primaryOwner(ElementInterface $primaryOwner): static
{
$this->primaryOwnerId = [$primaryOwner->id];
$this->siteId = $primaryOwner->siteId;
return $this;
}
/**
* Sets the [[ownerId()]] parameter based on a given owner element.
*
* ---
*
* ```twig
* {# Fetch addresses for the current user #}
* {% set {elements-var} = {twig-method}
* .owner(currentUser)
* .all() %}
* ```
*
* ```php
* // Fetch addresses created for the current user
* ${elements-var} = {php-method}
* ->owner(Craft::$app->user->identity)
* ->all();
* ```
*
* @param ElementInterface $owner The owner element
* @return static self reference
* @uses $ownerId
*/
public function owner(ElementInterface $owner): static
{
$this->ownerId = [$owner->id];
return $this;
}
/**
* Narrows the query results based on the addresses’ owner elements, per their IDs.
*
* Possible values include:
*
* | Value | Fetches addresses…
* | - | -
* | `1` | created for an element with an ID of 1.
* | `[1, 2]` | created for an element with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch addresses created for an element with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .ownerId(1)
* .all() %}
* ```
*
* ```php
* // Fetch addresses created for an element with an ID of 1
* ${elements-var} = {php-method}
* ->ownerId(1)
* ->all();
* ```
*
* @param int|int[]|null $value The property value
* @return static self reference
* @uses $ownerId
*/
public function ownerId(array|int|null $value): static
{
$this->ownerId = $value;
return $this;
}
/**
* Narrows the query results based on whether the addresses’ owners are drafts.
*
* Possible values include:
*
* | Value | Fetches addresses…
* | - | -
* | `true` | which can belong to a draft.
* | `false` | which cannot belong to a draft.
*
* @param bool|null $value The property value
* @return static self reference
* @uses $allowOwnerDrafts
*/
public function allowOwnerDrafts(?bool $value = true): static
{
$this->allowOwnerDrafts = $value;
return $this;
}
/**
* Narrows the query results based on whether the addresses’ owners are revisions.
*
* Possible values include:
*
* | Value | Fetches addresses…
* | - | -
* | `true` | which can belong to a revision.
* | `false` | which cannot belong to a revision.
*
* @param bool|null $value The property value
* @return static self reference
* @uses $allowOwnerRevisions
*/
public function allowOwnerRevisions(?bool $value = true): static
{
$this->allowOwnerRevisions = $value;
return $this;
}
/**
* @inheritdoc
* @throws QueryAbortedException
*/
protected function beforePrepare(): bool
{
if (!parent::beforePrepare()) {
return false;
}
if ($this->stripeId === []) {
return false;
}
try {
$this->primaryOwnerId = $this->normalizeOwnerId($this->primaryOwnerId);
} catch (InvalidArgumentException) {
throw new InvalidConfigException('Invalid primaryOwnerId param value');
}
try {
$this->ownerId = $this->normalizeOwnerId($this->ownerId);
} catch (InvalidArgumentException) {
throw new InvalidConfigException('Invalid ownerId param value');
}
$priceTable = 'stripe_prices';
$priceDataTable = 'stripe_pricedata';
// join standard price element table that only contains the stripeId
$this->joinElementTable($priceTable);
$priceDataJoinTable = [$priceDataTable => "{{%$priceDataTable}}"];
$this->query->innerJoin($priceDataJoinTable, "[[$priceDataTable.stripeId]] = [[$priceTable.stripeId]]");
$this->subQuery->innerJoin($priceDataJoinTable, "[[$priceDataTable.stripeId]] = [[$priceTable.stripeId]]");
$this->query->select([
'stripe_prices.stripeId',
'stripe_prices.primaryOwnerId',
'stripe_pricedata.stripeStatus',
'stripe_pricedata.data',
'stripe_pricedata.currencies',
'stripe_pricedata.unitAmount',
'stripe_pricedata.type',
'stripe_pricedata.productId as stripeProductId',
'stripe_pricedata.primaryCurrency',
]);
if (!empty($this->ownerId) || !empty($this->primaryOwnerId)) {
// Join in the elements_owners table
$ownersCondition = [
'and',
'[[elements_owners.elementId]] = [[elements.id]]',
$this->ownerId ? ['elements_owners.ownerId' => $this->ownerId] : '[[elements_owners.ownerId]] = [[stripe_prices.primaryOwnerId]]',
];
$this->query
->addSelect([
'elements_owners.ownerId',
'elements_owners.sortOrder',
])
->innerJoin(['elements_owners' => Table::ELEMENTS_OWNERS], $ownersCondition);
$this->subQuery->innerJoin(['elements_owners' => Table::ELEMENTS_OWNERS], $ownersCondition);
//$this->subQuery->andWhere(['addresses.fieldId' => $this->fieldId]);
if ($this->primaryOwnerId) {
$this->subQuery->andWhere(['stripe_prices.primaryOwnerId' => $this->primaryOwnerId]);
}
// Ignore revision/draft blocks by default
$allowOwnerDrafts = $this->allowOwnerDrafts ?? ($this->id || $this->primaryOwnerId || $this->ownerId);
$allowOwnerRevisions = $this->allowOwnerRevisions ?? ($this->id || $this->primaryOwnerId || $this->ownerId);
if (!$allowOwnerDrafts || !$allowOwnerRevisions) {
$this->subQuery->innerJoin(
['owners' => Table::ELEMENTS],
$this->ownerId ? '[[owners.id]] = [[elements_owners.ownerId]]' : '[[owners.id]] = [[stripe_prices.primaryOwnerId]]'
);
if (!$allowOwnerDrafts) {
$this->subQuery->andWhere(['owners.draftId' => null]);
}
if (!$allowOwnerRevisions) {
$this->subQuery->andWhere(['owners.revisionId' => null]);
}
}
$this->defaultOrderBy = ['elements_owners.sortOrder' => SORT_ASC];
} elseif (isset($this->primaryOwnerId) || isset($this->ownerId)) {
$this->subQuery->andWhere(['stripe_prices.primaryOwnerId' => $this->primaryOwnerId ?? $this->ownerId]);
}
if (isset($this->stripeId)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.stripeId', $this->stripeId));
}
if (isset($this->stripeStatus)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.stripeStatus', $this->stripeStatus));
}
if (isset($this->type)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.type', $this->type));
}
if (isset($this->primaryCurrency)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.primaryCurrency', $this->primaryCurrency));
}
if (isset($this->currency)) {
$currency = \craft\stripe\helpers\Db::prepareForLikeSearch($this, 'currency');
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.currencies', $currency));
}
if (isset($this->unitAmount)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.unitAmount', $this->unitAmount));
}
if (isset($this->stripeProductId)) {
$this->subQuery->andWhere(Db::parseParam('stripe_pricedata.productId', $this->stripeProductId));
}
return parent::beforePrepare();
}
/**
* Normalizes the ownerId param to an array of IDs or null
*
* @param mixed $value
* @return int[]|null
* @throws InvalidArgumentException
*/
private function normalizeOwnerId(mixed $value): ?array
{
if (empty($value)) {
return null;
}
if (is_numeric($value)) {
return [$value];
}
if (!is_array($value) || !ArrayHelper::isNumeric($value)) {
throw new InvalidArgumentException();
}
return $value;
}
}