Skip to content

Commit

Permalink
Merge branch 'develop' into fix/form-grid-modal-redirect-GIVE-931
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh authored Jan 14, 2025
2 parents 539b676 + 45c6a36 commit 41f3acc
Show file tree
Hide file tree
Showing 49 changed files with 1,135 additions and 252 deletions.
15 changes: 15 additions & 0 deletions assets/src/css/admin/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,11 @@ 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: 1rem;
Expand All @@ -1166,6 +1171,16 @@ a.give-delete {
font-family: 'Inter' ,sans-serif;
font-weight: 400;
line-height: 1.5rem;
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 @@ -14,6 +14,7 @@
class ConvertEventTicketsBlockToFieldsApi
{
/**
* @unreleased Set event end date and time.
* @since 3.12.2 Remove event ID from field name
* @since 3.6.0
*
Expand All @@ -32,6 +33,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 );
}
}
};
8 changes: 7 additions & 1 deletion src/EventTickets/Models/EventTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Give\Framework\Models\ModelQueryBuilder;
use Give\Framework\Models\ValueObjects\Relationship;
use Give\Framework\Support\Facades\DateTime\Temporal;
use Give\Framework\Support\ValueObjects\Money;

/**
* @since 3.6.0
Expand All @@ -23,12 +24,16 @@ class EventTicket extends Model implements ModelCrud /*, ModelHasFactory */
{
/**
* @inheritdoc
*
* @unreleased Add amount to the properties array
* @since 3.6.0
*/
protected $properties = [
'id' => 'int', // @todo Maybe use UUID instead of auto-incrementing integer
'eventId' => 'int',
'ticketTypeId' => 'int',
'donationId' => 'int',
'amount' => Money::class,
'createdAt' => DateTime::class,
'updatedAt' => DateTime::class,
];
Expand Down Expand Up @@ -144,7 +149,6 @@ public function ticketType(): ModelQueryBuilder
return give(EventTicketTypeRepository::class)->queryById($this->ticketTypeId);
}


/**
* @since 3.6.0
*
Expand All @@ -156,6 +160,7 @@ public function donation(): ModelQueryBuilder
}

/**
* @unreleased Add amount to the properties array
* @since 3.6.0
*
* @param object $object
Expand All @@ -167,6 +172,7 @@ public static function fromQueryBuilderObject($object): EventTicket
'eventId' => (int)$object->event_id,
'ticketTypeId' => (int)$object->ticket_type_id,
'donationId' => (int)$object->donation_id,
'amount' => new Money($object->amount, give_get_currency()),
'createdAt' => Temporal::toDateTime($object->created_at),
'updatedAt' => Temporal::toDateTime($object->updated_at),
]);
Expand Down
Loading

0 comments on commit 41f3acc

Please sign in to comment.