Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class ProductDomainObjectAbstract extends \HiEvents\DomainObjects\Abstr
final public const HIGHLIGHT_MESSAGE = 'highlight_message';
final public const WAITLIST_ENABLED = 'waitlist_enabled';
final public const IS_ADDON_ONLY = 'is_addon_only';
final public const SEQUENTIAL_TIER_RELEASE_ENABLED = 'sequential_tier_release_enabled';

protected int $id;
protected int $event_id;
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class ProductDomainObjectAbstract extends \HiEvents\DomainObjects\Abstr
protected ?string $highlight_message = null;
protected ?bool $waitlist_enabled = null;
protected bool $is_addon_only = false;
protected bool $sequential_tier_release_enabled = false;

public function toArray(): array
{
Expand Down Expand Up @@ -99,6 +101,7 @@ public function toArray(): array
'highlight_message' => $this->highlight_message ?? null,
'waitlist_enabled' => $this->waitlist_enabled ?? null,
'is_addon_only' => $this->is_addon_only ?? null,
'sequential_tier_release_enabled' => $this->sequential_tier_release_enabled ?? null,
];
}

Expand Down Expand Up @@ -409,4 +412,15 @@ public function getIsAddonOnly(): bool
{
return $this->is_addon_only;
}

public function setSequentialTierReleaseEnabled(bool $sequential_tier_release_enabled): self
{
$this->sequential_tier_release_enabled = $sequential_tier_release_enabled;
return $this;
}

public function getSequentialTierReleaseEnabled(): bool
{
return $this->sequential_tier_release_enabled;
}
}
27 changes: 26 additions & 1 deletion backend/app/DomainObjects/ProductDomainObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,34 @@ public function isSoldOut(): bool
return $this->getProductPrices()->every(fn (ProductPriceDomainObject $price) => $price->isSoldOut());
}

public function markLockedTiers(): void
{
if (! $this->isTieredType() || ! $this->getSequentialTierReleaseEnabled() || ! $this->getProductPrices()) {
return;
}

$earlierTierOpen = false;

$this->getProductPrices()
->sortBy(fn (ProductPriceDomainObject $price) => $price->getOrder())
->each(function (ProductPriceDomainObject $price) use (&$earlierTierOpen) {
if ($price->getIsHidden()) {
return;
}

$price->setIsLockedBehindEarlierTier($earlierTierOpen);

if (! $price->isExhausted()) {
$earlierTierOpen = true;
}
});
}

public function getQuantityAvailable(): int
{
$availableCount = $this->getProductPrices()->sum(fn (ProductPriceDomainObject $price) => $price->getQuantityAvailable());
$availableCount = $this->getProductPrices()
->reject(fn (ProductPriceDomainObject $price) => $price->isLockedBehindEarlierTier())
->sum(fn (ProductPriceDomainObject $price) => $price->getQuantityAvailable());

if ($this->quantityAvailable !== null) {
return min($availableCount, $this->quantityAvailable);
Expand Down
33 changes: 33 additions & 0 deletions backend/app/DomainObjects/ProductPriceDomainObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ProductPriceDomainObject extends Generated\ProductPriceDomainObjectAbstrac

private ?string $offSaleReason = null;

private int $quantityReserved = 0;

private bool $isLockedBehindEarlierTier = false;

public function getPriceBeforeDiscount(): ?float
{
return $this->priceBeforeDiscount;
Expand Down Expand Up @@ -91,6 +95,35 @@ public function isSoldOut(): bool
return $this->getQuantitySold() >= $this->getInitialQuantityAvailable();
}

public function isExhausted(): bool
{
if ($this->isAfterSaleEndDate()) {
return true;
}

return $this->getInitialQuantityAvailable() !== null
&& $this->getQuantitySold() + $this->quantityReserved >= $this->getInitialQuantityAvailable();
}

public function setQuantityReserved(int $quantityReserved): self
{
$this->quantityReserved = $quantityReserved;

return $this;
}

public function isLockedBehindEarlierTier(): bool
{
return $this->isLockedBehindEarlierTier;
}

public function setIsLockedBehindEarlierTier(bool $isLocked): self
{
$this->isLockedBehindEarlierTier = $isLocked;

return $this;
}

public function isAvailable(): ?bool
{
return $this->isAvailable;
Expand Down
22 changes: 22 additions & 0 deletions backend/app/Http/Request/Product/UpsertProductRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use HiEvents\Http\Request\BaseRequest;
use HiEvents\Validators\Rules\RulesHelper;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class UpsertProductRequest extends BaseRequest
{
Expand All @@ -34,6 +35,7 @@ public function rules(): array
'hide_before_sale_start_date' => 'boolean',
'hide_after_sale_end_date' => 'boolean',
'hide_when_sold_out' => 'boolean',
'sequential_tier_release_enabled' => 'boolean',
'start_collapsed' => 'boolean',
'show_quantity_remaining' => 'boolean',
'is_hidden_without_promo_code' => 'boolean',
Expand All @@ -50,6 +52,26 @@ public function rules(): array
];
}

public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator) {
if (! $this->boolean('sequential_tier_release_enabled') || $this->input('type') !== ProductPriceType::TIERED->name) {
return;
}

$prices = collect($this->input('prices', []));

$prices->slice(0, -1)->each(function (array $price, int $index) use ($validator) {
if (($price['initial_quantity_available'] ?? null) === null) {
$validator->errors()->add(
"prices.$index.initial_quantity_available",
__('Every tier except the last needs a quantity when tiers are released in order.'),
);
}
});
});
}

public function messages(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions backend/app/Resources/Product/ProductPriceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function toArray(Request $request): array
'initial_quantity_available' => $this->getInitialQuantityAvailable(),
'quantity_sold' => $this->getQuantitySold(),
'is_sold_out' => $this->isSoldOut(),
'is_locked_behind_earlier_tier' => $this->isLockedBehindEarlierTier(),
'is_hidden' => $this->getIsHidden(),
'off_sale_reason' => $this->getOffSaleReason(),
'price_including_taxes_and_fees' => $this->getPriceIncludingTaxAndServiceFee(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function toArray(Request $request): array
'is_after_sale_end_date' => $this->isAfterSaleEndDate(),
'is_available' => $this->isAvailable(),
'is_sold_out' => $this->isSoldOut(),
'is_locked_behind_earlier_tier' => $this->isLockedBehindEarlierTier(),
$this->mergeWhen($this->getAdditionalDataByKey(self::SHOW_QUANTITY_AVAILABLE), fn () => [
'quantity_remaining' => $this->getQuantityAvailable(),
]),
Expand Down
1 change: 1 addition & 0 deletions backend/app/Resources/Product/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function toArray(Request $request): array
'start_collapsed' => $this->getStartCollapsed(),
'show_quantity_remaining' => $this->getShowQuantityRemaining(),
'hide_when_sold_out' => $this->getHideWhenSoldOut(),
'sequential_tier_release_enabled' => $this->getSequentialTierReleaseEnabled(),
'is_hidden_without_promo_code' => $this->getIsHiddenWithoutPromoCode(),
'is_hidden' => $this->getIsHidden(),
'is_before_sale_start_date' => $this->isBeforeSaleStartDate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function handle(UpsertProductDTO $productsData): ProductDomainObject
->setHideBeforeSaleStartDate($productsData->hide_before_sale_start_date)
->setHideAfterSaleEndDate($productsData->hide_after_sale_end_date)
->setHideWhenSoldOut($productsData->hide_when_sold_out)
->setSequentialTierReleaseEnabled($productsData->type === ProductPriceType::TIERED && $productsData->sequential_tier_release_enabled)
->setShowQuantityRemaining($productsData->show_quantity_remaining)
->setIsHiddenWithoutPromoCode($productsData->is_hidden_without_promo_code)
->setIsHighlighted($productsData->is_highlighted ?? false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
public readonly ?bool $hide_before_sale_start_date = false,
public readonly ?bool $hide_after_sale_end_date = false,
public readonly ?bool $hide_when_sold_out = false,
public readonly ?bool $sequential_tier_release_enabled = false,
public readonly ?bool $start_collapsed = false,
public readonly ?bool $show_quantity_remaining = false,
public readonly ?bool $is_hidden_without_promo_code = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use HiEvents\DomainObjects\Enums\CapacityChangeDirection;
use HiEvents\DomainObjects\Enums\ProductPriceType;
use HiEvents\DomainObjects\Interfaces\DomainObjectInterface;
use HiEvents\DomainObjects\ProductDomainObject;
use HiEvents\DomainObjects\ProductPriceDomainObject;
Expand Down Expand Up @@ -127,6 +128,7 @@ private function updateProduct(UpsertProductDTO $productsData, array $where): Pr
'hide_before_sale_start_date' => $productsData->hide_before_sale_start_date,
'hide_after_sale_end_date' => $productsData->hide_after_sale_end_date,
'hide_when_sold_out' => $productsData->hide_when_sold_out,
'sequential_tier_release_enabled' => $productsData->type === ProductPriceType::TIERED && $productsData->sequential_tier_release_enabled,
'show_quantity_remaining' => $productsData->show_quantity_remaining,
'is_hidden_without_promo_code' => $productsData->is_hidden_without_promo_code,
'product_type' => $productsData->product_type->name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ private function validateSingleProductDetails(EventDomainObject $event, int $pro
throw new NotFoundHttpException(sprintf('Product ID %d not found', $productId));
}

$this->hydrateReservedQuantitiesAndMarkLockedTiers($product);

$this->validateProductEvent(
event: $event,
productId: $productId,
Expand Down Expand Up @@ -421,6 +423,19 @@ private function validateSingleProductDetails(EventDomainObject $event, int $pro
);
}

private function hydrateReservedQuantitiesAndMarkLockedTiers(ProductDomainObject $product): void
{
$reservedByPriceId = $this->availableProductQuantities
->productQuantities
->keyBy('price_id');

$product->getProductPrices()?->each(function (ProductPriceDomainObject $price) use ($reservedByPriceId) {
$price->setQuantityReserved($reservedByPriceId->get($price->getId())?->quantity_reserved ?? 0);
});

$product->markLockedTiers();
}

/**
* @throws NotFoundHttpException
*/
Expand Down Expand Up @@ -585,6 +600,12 @@ private function validatePriceIdAndQuantity(int $productIndex, array $productAnd
}

$selectedPrice = $productPrices?->first(fn (ProductPriceDomainObject $price) => $price->getId() === $priceId);
if ((int) $quantity > 0 && $selectedPrice?->isLockedBehindEarlierTier()) {
$errors["products.$productIndex.quantities.$quantityIndex.price_id"] = __('This price is not on sale yet');

continue;
}

if ((int) $quantity > 0 && $this->isPriceUnavailable($selectedPrice)) {
$errors["products.$productIndex.quantities.$quantityIndex.price_id"] = __('Invalid price ID');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private function persistProduct(ProductDomainObject $productsData): ProductDomai
'hide_before_sale_start_date' => $productsData->getHideBeforeSaleStartDate(),
'hide_after_sale_end_date' => $productsData->getHideAfterSaleEndDate(),
'hide_when_sold_out' => $productsData->getHideWhenSoldOut(),
'sequential_tier_release_enabled' => $productsData->getSequentialTierReleaseEnabled(),
'show_quantity_remaining' => $productsData->getShowQuantityRemaining(),
'is_hidden_without_promo_code' => $productsData->getIsHiddenWithoutPromoCode(),
'event_id' => $productsData->getEventId(),
Expand Down
17 changes: 14 additions & 3 deletions backend/app/Services/Domain/Product/ProductFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,16 @@ private function processProduct(
});
}

$product->getProductPrices()?->map(function (ProductPriceDomainObject $price) use ($productQuantities) {
$availableQuantity = $productQuantities->where('price_id', $price->getId())->first()?->quantity_available;
$quantitiesByPriceId = $productQuantities->keyBy('price_id');

$product->getProductPrices()?->each(function (ProductPriceDomainObject $price) use ($quantitiesByPriceId) {
$priceQuantities = $quantitiesByPriceId->get($price->getId());
$availableQuantity = $priceQuantities?->quantity_available;
$availableQuantity = $availableQuantity === Constants::INFINITE ? null : $availableQuantity;
$price->setQuantityAvailable(
max($availableQuantity, 0)
);
$price->setQuantityReserved($priceQuantities?->quantity_reserved ?? 0);
});

$productQuantities->each(function (AvailableProductQuantitiesDTO $quantity) use ($product) {
Expand Down Expand Up @@ -321,11 +325,17 @@ private function filterProductPrice(
$hidden = true;
}

if ($price->isLockedBehindEarlierTier() && $price->getOffSaleReason() === null) {
$price->setOffSaleReason(__('Price is locked until earlier tiers sell out'));
}

return $hidden && $hideSoldOutProducts;
}

private function processProductPrices(ProductDomainObject $product, bool $hideSoldOutProducts = true): void
{
$product->markLockedTiers();

$product->setProductPrices(
$product->getProductPrices()
?->each(fn (ProductPriceDomainObject $price) => $this->processProductPrice($product, $price))
Expand Down Expand Up @@ -356,7 +366,8 @@ private function getPriceAvailability(ProductPriceDomainObject $price, ProductDo
return ! $price->isSoldOut()
&& ! $price->isBeforeSaleStartDate()
&& ! $price->isAfterSaleEndDate()
&& ! $price->getIsHidden();
&& ! $price->getIsHidden()
&& ! $price->isLockedBehindEarlierTier();
}

return ! $product->isSoldOut()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->boolean('sequential_tier_release_enabled')->default(false);
});
}

public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('sequential_tier_release_enabled');
});
}
};
Loading
Loading