Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Event Ticket follow up #7329

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
17 changes: 16 additions & 1 deletion assets/src/css/admin/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1070,10 +1070,25 @@ a.give-delete {
display: none;
}

.give-subsubsub .givewp-feature-flag-notification-counter {
translate: 0 -0.125rem;
vertical-align: middle;
}

.give-admin-beta-features-message {
background-color: #fff;
padding: 0.5rem;
padding: 1rem;
border: 1px solid #F29718;
margin-bottom: 1.5rem;

.givewp-beta-icon {
margin-right: 0.25rem;
}

p {
line-height: 1.6;
margin: 0;
}
}

.give-admin-beta-features-feedback-link {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,28 @@

namespace Give\EventTickets\Actions;

use Give\EventTickets\Models\EventTicket;
use Give\EventTickets\Repositories\EventTicketRepository;
use Give\Framework\Receipts\DonationReceipt;
use Give\Framework\Receipts\Properties\ReceiptDetail;
use Give\Framework\Support\ValueObjects\Money;

/**
* @since 3.6.0
*/
class AddEventTicketsToDonationConfirmationPageDonationTotal
{
/**
* @unreleased Refactored to use getTotalByDonation method
* @since 3.6.0
*/
public function __invoke(DonationReceipt $receipt): void
{
$eventTickets = give(EventTicketRepository::class)->queryByDonationId($receipt->donation->id)->getAll();

if (!empty($eventTickets)) {
$currency = $receipt->donation->amount->getCurrency();
$total = array_reduce($eventTickets, function (Money $carry, EventTicket $eventTicket) {
$ticketType = $eventTicket->ticketType()->get();

return $carry->add(
$ticketType->price
);
}, new Money(0, $currency));
$totalTicketAmount = give(EventTicketRepository::class)->getTotalByDonation($receipt->donation);

if ($totalTicketAmount->getAmount() > 0) {
$receipt->donationDetails->addDetail(
new ReceiptDetail(
__('Event Tickets', 'give'),
$total->formatToLocale()
$totalTicketAmount->formatToLocale()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Give\EventTickets\Actions;

use Give\Donations\Models\Donation;
use Give\EventTickets\DataTransferObjects\TicketPurchaseData;
use Give\EventTickets\DataTransferObjects\EventTicketTypeData;
use Give\EventTickets\DataTransferObjects\TicketPurchaseData;
use Give\EventTickets\Fields\EventTickets;
use Give\EventTickets\Models\EventTicket;
use Give\EventTickets\Models\EventTicketType;
use Give\EventTickets\Repositories\EventRepository;
use Give\Framework\Blocks\BlockModel;
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException;
Expand All @@ -16,6 +14,7 @@
class ConvertEventTicketsBlockToFieldsApi
{
/**
* @unreleased Set event end date and time.
* @since 3.6.0
*
* @throws EmptyNameException
Expand All @@ -33,6 +32,7 @@ public function __invoke(BlockModel $block, int $formId)
$eventTicketsField
->title($event->title)
->startDateTime($event->startDateTime->format('Y-m-d H:i:s'))
->endDateTime($event->endDateTime->format('Y-m-d H:i:s'))
->description($event->description)
->ticketTypes($ticketTypes);

Expand Down
2 changes: 2 additions & 0 deletions src/EventTickets/Actions/GenerateTicketsFromPurchaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(Donation $donation)
}

/**
* @unreleased Add "amount" to the array of props
* @since 3.6.0
*/
public function __invoke(TicketPurchaseData $data)
Expand All @@ -35,6 +36,7 @@ public function __invoke(TicketPurchaseData $data)
'eventId' => $data->ticketType->eventId,
'ticketTypeId' => $data->ticketType->id,
'donationId' => $this->donation->id,
'amount' => $data->ticketType->price,
]);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/EventTickets/Factories/EventTicketFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
class EventTicketFactory extends ModelFactory
{
/**
* @unreleased Add amount to the properties array using the price from the ticket type
* @since 3.6.0
*
* @throws Exception
*/
public function definition(): array
{
$ticketType = EventTicketType::factory()->create();

return [
'eventId' => Event::factory()->create()->id,
'ticketTypeId' => EventTicketType::factory()->create()->id,
'ticketTypeId' => $ticketType->id,
'donationId' => Donation::factory()->create()->id,
'amount' => $ticketType->price,
'createdAt' => new DateTime(),
'updatedAt' => new DateTime(),
];
Expand Down
41 changes: 18 additions & 23 deletions src/EventTickets/Fields/EventTickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
class EventTickets extends Field
{
protected $title;
protected $startDateTime;
protected $description;
protected $startDateTime;
protected $endDateTime;
protected $ticketTypes = [];

const TYPE = 'eventTickets';
Expand Down Expand Up @@ -50,59 +51,53 @@ public function startDateTime(string $date): EventTickets
/**
* @since 3.6.0
*/
public function getDescription(): string
public function getEndDateTime(): string
{
return $this->description;
return $this->endDateTime;
}

/**
* @since 3.6.0
* @unreleased
*/
public function description(string $description): EventTickets
public function endDateTime(string $date): EventTickets
{
$this->description = $description;
$this->endDateTime = $date;

return $this;
}

/**
* @since 3.6.0
*/
public function getTicketTypes(): array
public function getDescription(): string
{
return $this->ticketTypes;
return $this->description;
}

/**
* @since 3.6.0
*/
public function ticketTypes(array $ticketTypes): EventTickets
public function description(string $description): EventTickets
{
$this->ticketTypes = $ticketTypes;
$this->description = $description;
return $this;
}

/**
* @since 3.6.0
*/
public function getTicketsLabel(): string
public function getTicketTypes(): array
{
return apply_filters(
'givewp_event_tickets_block/tickets_label',
__('Select Tickets', 'give')
);
return $this->ticketTypes;
}

/**
* @since 3.6.0
*/
public function getSoldOutMessage(): string
public function ticketTypes(array $ticketTypes): EventTickets
{
return apply_filters(
'givewp_event_tickets_block/sold_out_message',
__(
'Thank you for supporting our cause. Our fundraising event tickets are officially sold out. You can still contribute by making a donation.',
'give'
)
);
$this->ticketTypes = $ticketTypes;
return $this;
}

}
10 changes: 9 additions & 1 deletion src/EventTickets/ListTable/Columns/DescriptionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ public function getLabel(): string
/**
* @inheritDoc
*
* @unreleased Truncate description to 200 characters
* @since 3.6.0
*
* @param Event $model
*/
public function getCellValue($model): string
{
return wpautop($model->description);
$maxChars = 200;
$truncatedDescription = mb_substr($model->description, 0, $maxChars);

if (mb_strlen($model->description) > $maxChars) {
$truncatedDescription .= '...';
}

return sprintf('<div class="event-description">%s</div>', wpautop($truncatedDescription));
}
}
13 changes: 9 additions & 4 deletions src/EventTickets/ListTable/Columns/SalesAmountColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getLabel(): string
/**
* @inheritDoc
*
* @unreleased Refactored to use event ticket amount instead of ticket type price
* @since 3.6.0
*
* @param Event $model
Expand All @@ -46,22 +47,26 @@ public function getCellValue($model, $locale = ''): string
{
$ticketTypes = [];
foreach ($model->ticketTypes()->getAll() ?? [] as $ticketType) {
$salesCount = $ticketType->eventTickets()->count();
$ticketsAvailable = $ticketType->capacity - $salesCount;

$ticketTypes[$ticketType->id] = [
'price' => $ticketType->price,
'capacity' => $ticketType->capacity,
'ticketsAvailable' => $ticketsAvailable,
];
}

$salesTotal = array_reduce(
$model->eventTickets()->getAll() ?? [],
function (Money $carry, $eventTicket) use ($ticketTypes) {
return $carry->add($ticketTypes[$eventTicket->ticketTypeId]['price']);
function (Money $carry, $eventTicket) {
return $carry->add($eventTicket->amount);
},
new Money(0, give_get_currency())
);
$maxCapacitySales = array_reduce($ticketTypes, function (Money $carry, $ticketType) {
return $carry->add($ticketType['price']->multiply($ticketType['capacity']));
}, new Money(0, give_get_currency()));
return $carry->add($ticketType['price']->multiply($ticketType['ticketsAvailable']));
}, $salesTotal);

$salesPercentage = $maxCapacitySales->formatToMinorAmount() > 0 ? max(
min($salesTotal->formatToMinorAmount() / $maxCapacitySales->formatToMinorAmount(), 100),
Expand Down
74 changes: 74 additions & 0 deletions src/EventTickets/Migrations/AddAmountColumnToEventTicketsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Give\EventTickets\Migrations;

use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
*/
class AddAmountColumnToEventTicketsTable extends Migration {
/**
* @inheritdoc
*/
public static function id() {
return 'give-events-add-amount-column-to-events-tickets-table';
}

public static function title() {
return 'Add "amount" column to give_event_tickets table';
}

/**
* @inheritdoc
*/
public static function timestamp() {
return strtotime( '2022-03-18 12:00:00' );
}

/**
* @inheritdoc
* @throws DatabaseMigrationException
*/
public function run() {
global $wpdb;

$eventTicketsTable = $wpdb->give_event_tickets;
$eventTicketTypesTable = $wpdb->give_event_ticket_types;

$this->addAmountColumn($wpdb, $eventTicketsTable);
$this->migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable);
}

/**
* @throws DatabaseMigrationException
*/
private function addAmountColumn($wpdb, $eventTicketsTable) {
$sql = "ALTER TABLE $eventTicketsTable
ADD COLUMN amount INT UNSIGNED NOT NULL AFTER donation_id";

try {
$wpdb->query($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException( "An error occurred while adding the amount column to the $eventTicketsTable table", 0, $exception );
}
}

/**
* @throws DatabaseMigrationException
*/
private function migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable) {
$sql = "UPDATE $eventTicketsTable eventTickets
JOIN $eventTicketTypesTable evenTicketTypes
ON eventTickets.ticket_type_id = evenTicketTypes.id
SET eventTickets.amount = evenTicketTypes.price";

try {
$wpdb->query($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException( "An error occurred while migrating data to the amount column in the $eventTicketsTable table", 0, $exception );
}
}
};
Loading
Loading