DateColumn.php
2 years ago
DescriptionColumn.php
2 years ago
IdColumn.php
2 years ago
SalesAmountColumn.php
2 years ago
SalesCountColumn.php
2 years ago
TitleColumn.php
2 years ago
SalesAmountColumn.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\EventTickets\ListTable\Columns; |
| 6 | |
| 7 | use Give\EventTickets\Models\Event; |
| 8 | use Give\Framework\ListTable\ModelColumn; |
| 9 | use Give\Framework\Support\ValueObjects\Money; |
| 10 | |
| 11 | /** |
| 12 | * @since 3.6.0 |
| 13 | * |
| 14 | * @extends ModelColumn<Event> |
| 15 | */ |
| 16 | class SalesAmountColumn extends ModelColumn |
| 17 | { |
| 18 | /** |
| 19 | * @inheritDoc |
| 20 | * |
| 21 | * @since 3.6.0 |
| 22 | */ |
| 23 | public static function getId(): string |
| 24 | { |
| 25 | return 'salesAmount'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | * |
| 31 | * @since 3.6.0 |
| 32 | */ |
| 33 | public function getLabel(): string |
| 34 | { |
| 35 | return __('Ticket Sales', 'give'); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | * |
| 41 | * @since 3.6.0 |
| 42 | * |
| 43 | * @param Event $model |
| 44 | */ |
| 45 | public function getCellValue($model, $locale = ''): string |
| 46 | { |
| 47 | $ticketTypes = []; |
| 48 | foreach ($model->ticketTypes()->getAll() ?? [] as $ticketType) { |
| 49 | $ticketTypes[$ticketType->id] = [ |
| 50 | 'price' => $ticketType->price, |
| 51 | 'capacity' => $ticketType->capacity, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | $salesTotal = array_reduce( |
| 56 | $model->eventTickets()->getAll() ?? [], |
| 57 | function (Money $carry, $eventTicket) use ($ticketTypes) { |
| 58 | return $carry->add($ticketTypes[$eventTicket->ticketTypeId]['price']); |
| 59 | }, |
| 60 | new Money(0, give_get_currency()) |
| 61 | ); |
| 62 | $maxCapacitySales = array_reduce($ticketTypes, function (Money $carry, $ticketType) { |
| 63 | return $carry->add($ticketType['price']->multiply($ticketType['capacity'])); |
| 64 | }, new Money(0, give_get_currency())); |
| 65 | |
| 66 | $salesPercentage = $maxCapacitySales->formatToMinorAmount() > 0 ? max( |
| 67 | min($salesTotal->formatToMinorAmount() / $maxCapacitySales->formatToMinorAmount(), 100), |
| 68 | 0 |
| 69 | ) : 0; |
| 70 | |
| 71 | $template = ' |
| 72 | <div |
| 73 | role="progressbar" |
| 74 | aria-labelledby="giveEventTicketsProgressBar-%1$d" |
| 75 | aria-valuenow="%2$s" |
| 76 | aria-valuemin="0" |
| 77 | aria-valuemax="100" |
| 78 | class="goalProgress" |
| 79 | > |
| 80 | <span style="width: %2$s%%"></span> |
| 81 | </div> |
| 82 | <div id="giveEventTicketsProgressBar-%1$d">%3$s %4$s %5$s</div> |
| 83 | '; |
| 84 | |
| 85 | return sprintf( |
| 86 | $template, |
| 87 | $model->id, |
| 88 | $salesPercentage, |
| 89 | $salesTotal->formatToLocale($locale), |
| 90 | __('of', 'give'), |
| 91 | $maxCapacitySales->formatToLocale($locale) |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 |