Stage 12 — Online Store: public catalog + cart + orders
Schema: - online_orders (token-tracked, status workflow, delivery method/fee) - online_order_items (price snapshot, fulfilled flag) - part_cross_refs (OEM/equivalent codes for search) - parts.is_published (shop visibility) Storefront (ShopController, tenant subdomain, /shop): - Catalog with search across name/article/brand/cross-refs, category + in-stock filters, live stock, white-label themed layout - Part detail page with cross-ref codes - VIN search → VinDecoder → guided catalog search - Session cart (per-tenant key), guest checkout, order confirmation page - Respects settings.shop.enabled (404 when off); tenant-guarded Part::searchPublished matches cross-ref articles via whereHas. Order notifications (ShopOrderNotifier, best-effort): - Staff: Web Push to active users - Customer: Telegram if phone matches a linked client Filament (tenant): - OnlineOrderResource under "Magazin" nav group, status workflow, items relation, "Onorează" action issues stock via WarehouseService (FIFO) - PartResource: is_published toggle + column + bulk publish/unpublish + CrossRefsRelationManager - Settings: shop section (enable, delivery methods, fee, free-over) - Landing page: shop button when enabled Tests (6 new): - catalog 404 when disabled; lists published only; cross-ref search; order placement (token + items + total); fulfill issues stock; cross-tenant token isolation Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources;
|
||||
|
||||
use App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
|
||||
use App\Filament\Tenant\Resources\OnlineOrderResource\RelationManagers;
|
||||
use App\Models\Tenant\OnlineOrder;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class OnlineOrderResource extends Resource
|
||||
{
|
||||
protected static ?string $model = OnlineOrder::class;
|
||||
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shopping-bag';
|
||||
|
||||
protected static ?string $navigationLabel = 'Comenzi online';
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Magazin';
|
||||
|
||||
protected static ?string $modelLabel = 'comandă';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'comenzi online';
|
||||
|
||||
protected static ?int $navigationSort = 50;
|
||||
|
||||
public static function getNavigationBadge(): ?string
|
||||
{
|
||||
$new = static::getModel()::query()->where('status', 'new')->count();
|
||||
return $new > 0 ? (string) $new : null;
|
||||
}
|
||||
|
||||
public static function getNavigationBadgeColor(): ?string
|
||||
{
|
||||
return 'warning';
|
||||
}
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Schemas\Components\Section::make('Comandă')
|
||||
->columns(3)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('number')->label('Nr.')->disabled()->dehydrated(false),
|
||||
Forms\Components\Select::make('status')->options(OnlineOrder::STATUSES)->required(),
|
||||
Forms\Components\Select::make('delivery_method')->label('Livrare')->options(OnlineOrder::DELIVERY)->required(),
|
||||
Forms\Components\TextInput::make('customer_name')->label('Client')->required(),
|
||||
Forms\Components\TextInput::make('customer_phone')->label('Telefon')->required(),
|
||||
Forms\Components\TextInput::make('customer_email')->label('Email'),
|
||||
Forms\Components\TextInput::make('address')->label('Adresă')->columnSpan(2),
|
||||
Forms\Components\TextInput::make('delivery_fee')->label('Taxă livrare')->numeric(),
|
||||
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('number')->label('Nr.')->searchable()->sortable(),
|
||||
Tables\Columns\TextColumn::make('created_at')->label('Data')->dateTime('d.m.Y H:i')->sortable(),
|
||||
Tables\Columns\TextColumn::make('customer_name')->label('Client')->searchable(),
|
||||
Tables\Columns\TextColumn::make('customer_phone')->label('Telefon')->copyable(),
|
||||
Tables\Columns\TextColumn::make('delivery_method')
|
||||
->label('Livrare')
|
||||
->formatStateUsing(fn ($s) => OnlineOrder::DELIVERY[$s] ?? $s),
|
||||
Tables\Columns\TextColumn::make('status')
|
||||
->formatStateUsing(fn ($s) => OnlineOrder::STATUSES[$s] ?? $s)
|
||||
->badge()
|
||||
->colors([
|
||||
'warning' => ['new'],
|
||||
'info' => ['confirmed', 'packed'],
|
||||
'primary' => ['shipped'],
|
||||
'success' => ['delivered'],
|
||||
'danger' => ['cancelled'],
|
||||
]),
|
||||
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('status')->options(OnlineOrder::STATUSES),
|
||||
])
|
||||
->actions([
|
||||
Actions\Action::make('fulfill')
|
||||
->label('Onorează (scade stoc)')
|
||||
->icon('heroicon-m-check-badge')
|
||||
->color('success')
|
||||
->visible(fn (OnlineOrder $r) => ! in_array($r->status, ['delivered', 'cancelled'], true))
|
||||
->requiresConfirmation()
|
||||
->modalDescription('Scade din stoc piesele legate de catalog (FIFO) și marchează comanda confirmată.')
|
||||
->action(function (OnlineOrder $r) {
|
||||
$svc = app(\App\Services\Warehouse\WarehouseService::class);
|
||||
$issued = 0; $skipped = 0;
|
||||
foreach ($r->items as $item) {
|
||||
if ($item->fulfilled) continue;
|
||||
if (! $item->part_id) { $skipped++; continue; }
|
||||
$part = \App\Models\Tenant\Part::find($item->part_id);
|
||||
if (! $part) { $skipped++; continue; }
|
||||
try {
|
||||
$svc->issue($part, (float) $item->qty, null, $r, "Comandă online #{$r->number}");
|
||||
$item->fulfilled = true;
|
||||
$item->save();
|
||||
$issued++;
|
||||
} catch (\App\Services\Warehouse\InsufficientStockException $e) {
|
||||
$skipped++;
|
||||
}
|
||||
}
|
||||
if ($r->status === 'new') {
|
||||
$r->status = 'confirmed';
|
||||
$r->save();
|
||||
}
|
||||
Notification::make()
|
||||
->title("Onorat: {$issued} linii scăzute" . ($skipped ? ", {$skipped} sărite (stoc/lipsă link)" : ''))
|
||||
->{$skipped ? 'warning' : 'success'}()
|
||||
->send();
|
||||
}),
|
||||
Actions\EditAction::make(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
RelationManagers\ItemsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListOnlineOrders::route('/'),
|
||||
'edit' => Pages\EditOnlineOrder::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\OnlineOrderResource;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditOnlineOrder extends EditRecord
|
||||
{
|
||||
protected static string $resource = OnlineOrderResource::class;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\OnlineOrderResource;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListOnlineOrders extends ListRecords
|
||||
{
|
||||
protected static string $resource = OnlineOrderResource::class;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\OnlineOrderResource\RelationManagers;
|
||||
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class ItemsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'items';
|
||||
|
||||
protected static ?string $title = 'Produse';
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('name')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')->label('Piesă')->wrap(),
|
||||
Tables\Columns\TextColumn::make('article')->label('Cod')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('qty')->label('Cant.')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('price')->label('Preț')->money('MDL')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
|
||||
Tables\Columns\IconColumn::make('fulfilled')->label('Onorat')->boolean(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,10 @@ class PartResource extends Resource
|
||||
Forms\Components\TextInput::make('unit')->label('UM')->default('buc')->maxLength(16),
|
||||
Forms\Components\TextInput::make('min_qty')->label('Minim')->numeric()->default(0),
|
||||
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
|
||||
Forms\Components\Toggle::make('is_published')
|
||||
->label('Publicat în magazin')
|
||||
->helperText('Apare în magazinul online public.')
|
||||
->default(false),
|
||||
]),
|
||||
Schemas\Components\Section::make('Prețuri')
|
||||
->columns(2)
|
||||
@@ -122,6 +126,7 @@ class PartResource extends Resource
|
||||
Tables\Columns\TextColumn::make('unit')->label('UM'),
|
||||
Tables\Columns\TextColumn::make('location')->label('Loc.')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('sell_price')->label('Preț vz.')->money('MDL')->alignRight(),
|
||||
Tables\Columns\IconColumn::make('is_published')->label('Magazin')->boolean()->toggleable(),
|
||||
Tables\Columns\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—')->toggleable(),
|
||||
])
|
||||
->filters([
|
||||
@@ -216,6 +221,18 @@ class PartResource extends Resource
|
||||
return redirect()->away('/parts/labels?ids=' . $ids);
|
||||
})
|
||||
->deselectRecordsAfterCompletion(),
|
||||
Actions\BulkAction::make('publish')
|
||||
->label('Publică în magazin')
|
||||
->icon('heroicon-m-globe-alt')
|
||||
->color('success')
|
||||
->action(fn ($records) => collect($records)->each->update(['is_published' => true]))
|
||||
->deselectRecordsAfterCompletion(),
|
||||
Actions\BulkAction::make('unpublish')
|
||||
->label('Scoate din magazin')
|
||||
->icon('heroicon-m-eye-slash')
|
||||
->color('gray')
|
||||
->action(fn ($records) => collect($records)->each->update(['is_published' => false]))
|
||||
->deselectRecordsAfterCompletion(),
|
||||
])
|
||||
->emptyStateHeading('Depozit gol')
|
||||
->emptyStateDescription('Adaugă piese manual, sau folosește Achiziții ca să le adaugi prin recepție de la furnizor (cu prețuri și stoc auto). Procentaj poate seta automat prețul de vânzare.')
|
||||
@@ -228,6 +245,7 @@ class PartResource extends Resource
|
||||
return [
|
||||
RelationManagers\BatchesRelationManager::class,
|
||||
RelationManagers\PriceHistoryRelationManager::class,
|
||||
RelationManagers\CrossRefsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PartResource\RelationManagers;
|
||||
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class CrossRefsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'crossRefs';
|
||||
|
||||
protected static ?string $title = 'Coduri cross (OEM/echivalente)';
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Forms\Components\TextInput::make('cross_article')->label('Cod echivalent')->required()->maxLength(64),
|
||||
Forms\Components\TextInput::make('brand')->label('Brand')->maxLength(64),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('cross_article')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('cross_article')->label('Cod')->searchable(),
|
||||
Tables\Columns\TextColumn::make('brand')->placeholder('—'),
|
||||
])
|
||||
->headerActions([Actions\CreateAction::make()])
|
||||
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()])
|
||||
->emptyStateHeading('Niciun cod cross')
|
||||
->emptyStateDescription('Adaugă coduri echivalente OEM/aftermarket ca să fie găsite în căutarea din magazin.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user