Skip to content

Commit

Permalink
412. add cart sort feature for sales
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed May 19, 2024
1 parent 037aaeb commit 15e09e8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/app/Enums/Promo/CartSortForSale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Enums\Promo;

use Filament\Support\Contracts\HasLabel;
use Illuminate\Support\Collection;

/**
* Enum representing sorting options for a cart sale.
*/
enum CartSortForSale: int implements HasLabel
{
case PRICE_ASC = 1;
case PRICE_DESC = 2;

/**
* Get the label for the sorting option.
*/
public function getLabel(): ?string
{
return match ($this) {
self::PRICE_ASC => 'Цена по возрастанию',
self::PRICE_DESC => 'Цена по убыванию',
};
}

/**
* Apply the sorting option to a collection of products.
*/
public function apply(Collection $products): Collection
{
return match ($this) {
self::PRICE_ASC => $products->sortBy('price'),
self::PRICE_DESC => $products->sortByDesc('price'),
};
}
}
9 changes: 8 additions & 1 deletion src/app/Filament/Resources/Promo/SaleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\Promo;

use App\Enums\Promo\CartSortForSale;
use App\Enums\Promo\SaleAlgorithm;
use App\Filament\Resources\Promo\SaleResource\Pages;
use App\Models\Category;
Expand Down Expand Up @@ -48,13 +49,19 @@ public static function form(Form $form): Form
->default(now()->endOfDay())
->native(false)
->required(),
Forms\Components\Grid::make(3)->schema([
Forms\Components\Grid::make(4)->schema([
Forms\Components\Select::make('algorithm')
->label('Алгоритм')
->options(SaleAlgorithm::class)
->default(SaleAlgorithm::SIMPLE)
->native(false)
->required(),
Forms\Components\Select::make('cart_sort')
->label('Сортировка для акции')
->options(CartSortForSale::class)
->default(CartSortForSale::PRICE_ASC)
->native(false)
->required(),
Forms\Components\TextInput::make('sale_percentage')
->label('Скидка в процентах')
->numeric()
Expand Down
1 change: 1 addition & 0 deletions src/app/Models/Orders/OrderTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property \App\Enums\DeliveryTypeEnum $delivery_type_enum Тип доставки
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int|null $displacement_id ID перемещения
*
* @property-read \App\Models\Orders\Order|null $order
*
Expand Down
3 changes: 3 additions & 0 deletions src/app/Models/Promo/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models\Promo;

use App\Enums\Promo\CartSortForSale;
use App\Enums\Promo\SaleAlgorithm;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -18,6 +19,7 @@
* @property float|null $sale_percentage discount amount in percentage
* @property float|null $sale_fix fixed discount amount
* @property \App\Enums\Promo\SaleAlgorithm $algorithm
* @property \App\Enums\Promo\CartSortForSale $cart_sort Sorting the cart before applying the discount
* @property array|null $categories
* @property array|null $collections
* @property array|null $styles
Expand Down Expand Up @@ -58,6 +60,7 @@ class Sale extends Model
*/
protected $casts = [
'algorithm' => SaleAlgorithm::class,
'cart_sort' => CartSortForSale::class,
'categories' => 'array',
'collections' => 'array',
'styles' => 'array',
Expand Down
2 changes: 1 addition & 1 deletion src/app/Services/SaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public function applyToCart(Cart $cart): void
$this->hasSaleProductsInCart = false;

$products = $cart->availableItems()->map(fn ($item) => $item->product);
$products = $products->sortBy('price');
$products = $this->sale->cart_sort->apply($products);

if ($this->hasSale()) {
/** @var Product $product */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use App\Enums\Promo\CartSortForSale;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('sales', function (Blueprint $table) {
$table->tinyInteger('cart_sort')
->default(CartSortForSale::PRICE_ASC)
->after('algorithm')
->comment('Sorting the cart before applying the discount');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('sales', function (Blueprint $table) {
$table->dropColumn('cart_sort');
});
}
};

0 comments on commit 15e09e8

Please sign in to comment.