Faza 3.3: Depozit — Furnizori + Catalog piese + Achiziții
Schema: - suppliers: name, contact, phone/email/website, pay_terms, delivery_days, rating (1-5), discount_pct, categories (JSON), is_active, notes - parts: name, article (UNIQUE per tenant), brand, category, qty/unit/min_qty, buy_price/sell_price, location (rack/bin), barcode, preferred_supplier_id, is_active. Index pe (company_id, category) și (company_id, is_active). - purchases: număr unique per tenant + an, supplier_id, status workflow (draft/ordered/received/cancelled), order/expected/received/paid_at, total - purchase_items: name, article, qty, unit, buy_price, total auto, received bool; link opțional la part_id - wo_parts + part_id: linkare opțională la catalog (alter migration) Modele cu logică: - Part::adjustStock($delta) — modifică qty cu validare ≥ 0 - Part::isLow() / isOut() helpers - Purchase::markReceived() — atomic: marchează items ca received + creste qty pe pieces din catalog (DB::transaction) - WorkOrderPart::updating event — la trecerea status='installed' decrementează stoc auto. La revenire (ex: storno) incrementează la loc. - PurchaseItem::saving — total = qty * buy_price; recalc parent total Filament resources (group Depozit): - SupplierResource: form 3 secțiuni, rating ★★★★★, TagsInput pentru categorii - PartResource: form 4 secțiuni, badge nav cu nr. piese sub stoc minim, filtre low_stock + out_of_stock, coloană qty colorată după stoc - PurchaseResource: form antet + RelationManager Items. Action 'Recepționează' care apelează markReceived() — un click = stoc actualizat WorkOrder PartsRelationManager updated: - Selector din catalog (Part::active) cu stoc afișat - Auto-fill name/article/brand/unit/buy_price/sell_price din piesa selectată - Helper text: la status='installed' se scade din stoc Widget low-stock: - TableWidget pe dashboard tenant, listează piesele cu qty <= min_qty - Span full, sortat după qty (cele mai critice sus) Seed: - 2 furnizori (AutoParts Moldova SRL ★5, Inter Cars Moldova ★4) - 5 piese demo: Ulei Shell, Filtru Mann, Plăcuțe Brembo, Antigel (qty=0!), Bujii NGK - 1 achiziție recepționată (P-26-0001) cu 2 articole linked la catalog Total Filament tenant routes: 63 (de la 31).
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources;
|
||||
|
||||
use App\Filament\Tenant\Resources\PartResource\Pages;
|
||||
use App\Models\Tenant\Part;
|
||||
use App\Models\Tenant\Supplier;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PartResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Part::class;
|
||||
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-cube';
|
||||
|
||||
protected static ?string $navigationLabel = 'Depozit';
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
|
||||
|
||||
protected static ?string $modelLabel = 'piesă';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'piese';
|
||||
|
||||
protected static ?int $navigationSort = 40;
|
||||
|
||||
public static function getNavigationBadge(): ?string
|
||||
{
|
||||
$low = static::getModel()::query()
|
||||
->where('is_active', true)
|
||||
->whereColumn('qty', '<=', 'min_qty')
|
||||
->count();
|
||||
return $low > 0 ? (string) $low : null;
|
||||
}
|
||||
|
||||
public static function getNavigationBadgeColor(): ?string
|
||||
{
|
||||
return 'warning';
|
||||
}
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Schemas\Components\Section::make('Identificare')
|
||||
->columns(3)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('name')->label('Denumire')->required()->columnSpan(3)->maxLength(200),
|
||||
Forms\Components\TextInput::make('article')->label('Cod articol')->maxLength(64),
|
||||
Forms\Components\TextInput::make('brand')->maxLength(64),
|
||||
Forms\Components\Select::make('category')
|
||||
->label('Categorie')
|
||||
->options(array_combine(Part::CATEGORIES, Part::CATEGORIES))
|
||||
->searchable(),
|
||||
Forms\Components\TextInput::make('barcode')->label('Cod bare')->maxLength(64),
|
||||
Forms\Components\TextInput::make('location')->label('Locație rack/bin')->maxLength(64),
|
||||
]),
|
||||
Schemas\Components\Section::make('Stoc')
|
||||
->columns(4)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(0)->required(),
|
||||
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),
|
||||
]),
|
||||
Schemas\Components\Section::make('Prețuri')
|
||||
->columns(2)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('buy_price')->label('Preț achiziție')->numeric()->default(0),
|
||||
Forms\Components\TextInput::make('sell_price')->label('Preț vânzare')->numeric()->default(0),
|
||||
]),
|
||||
Schemas\Components\Section::make('Furnizor preferat')
|
||||
->columns(1)
|
||||
->schema([
|
||||
Forms\Components\Select::make('preferred_supplier_id')
|
||||
->label('Furnizor')
|
||||
->options(fn () => Supplier::pluck('name', 'id'))
|
||||
->searchable(),
|
||||
]),
|
||||
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('name')->searchable()->sortable()->wrap(),
|
||||
Tables\Columns\TextColumn::make('article')->label('Cod')->searchable()->copyable()->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('brand')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('qty')
|
||||
->label('Stoc')
|
||||
->numeric(decimalPlaces: 2)
|
||||
->alignRight()
|
||||
->color(fn ($state, $record) => $record->qty <= 0 ? 'danger' : ($record->qty <= $record->min_qty ? 'warning' : null))
|
||||
->weight(fn ($state, $record) => $record->qty <= $record->min_qty ? 'bold' : null),
|
||||
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\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—')->toggleable(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('category')
|
||||
->options(array_combine(Part::CATEGORIES, Part::CATEGORIES)),
|
||||
Tables\Filters\Filter::make('low_stock')
|
||||
->label('Stoc minim')
|
||||
->query(fn ($q) => $q->whereColumn('qty', '<=', 'min_qty')),
|
||||
Tables\Filters\Filter::make('out_of_stock')
|
||||
->label('Lipsă')
|
||||
->query(fn ($q) => $q->where('qty', '<=', 0)),
|
||||
])
|
||||
->actions([
|
||||
Actions\EditAction::make(),
|
||||
Actions\DeleteAction::make(),
|
||||
])
|
||||
->defaultSort('name');
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListParts::route('/'),
|
||||
'create' => Pages\CreatePart::route('/create'),
|
||||
'edit' => Pages\EditPart::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PartResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PartResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreatePart extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PartResource::class;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PartResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PartResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPart extends EditRecord
|
||||
{
|
||||
protected static string $resource = PartResource::class;
|
||||
|
||||
protected function getHeaderActions(): array { return [Actions\DeleteAction::make()]; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PartResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PartResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListParts extends ListRecords
|
||||
{
|
||||
protected static string $resource = PartResource::class;
|
||||
|
||||
protected function getHeaderActions(): array { return [Actions\CreateAction::make()]; }
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources;
|
||||
|
||||
use App\Filament\Tenant\Resources\PurchaseResource\Pages;
|
||||
use App\Filament\Tenant\Resources\PurchaseResource\RelationManagers;
|
||||
use App\Models\Tenant\Purchase;
|
||||
use App\Models\Tenant\Supplier;
|
||||
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 PurchaseResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Purchase::class;
|
||||
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shopping-cart';
|
||||
|
||||
protected static ?string $navigationLabel = 'Achiziții';
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
|
||||
|
||||
protected static ?string $modelLabel = 'achiziție';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'achiziții';
|
||||
|
||||
protected static ?int $navigationSort = 43;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Schemas\Components\Section::make('Antet')
|
||||
->columns(3)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('number')->label('Nr.')->disabled()->dehydrated(false)->placeholder('Generat automat'),
|
||||
Forms\Components\Select::make('supplier_id')
|
||||
->label('Furnizor')
|
||||
->options(fn () => Supplier::where('is_active', true)->pluck('name', 'id'))
|
||||
->searchable()
|
||||
->required(),
|
||||
Forms\Components\Select::make('status')
|
||||
->options(Purchase::STATUSES)
|
||||
->default('draft')
|
||||
->required(),
|
||||
Forms\Components\DatePicker::make('order_date')->label('Data comandă')->default(today())->required(),
|
||||
Forms\Components\DatePicker::make('expected_at')->label('Așteptată'),
|
||||
Forms\Components\DatePicker::make('received_at')->label('Recepționată'),
|
||||
Forms\Components\DatePicker::make('paid_at')->label('Plătită')->columnSpanFull(),
|
||||
]),
|
||||
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('supplier.name')->label('Furnizor')->searchable(),
|
||||
Tables\Columns\TextColumn::make('order_date')->label('Comandată')->date('d.m.Y'),
|
||||
Tables\Columns\TextColumn::make('expected_at')->label('Așteptată')->date('d.m.Y')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('received_at')->label('Recepționată')->date('d.m.Y')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('status')
|
||||
->formatStateUsing(fn ($s) => Purchase::STATUSES[$s] ?? $s)
|
||||
->badge()
|
||||
->colors([
|
||||
'gray' => ['draft'],
|
||||
'warning' => ['ordered'],
|
||||
'success' => ['received'],
|
||||
'danger' => ['cancelled'],
|
||||
]),
|
||||
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('status')->options(Purchase::STATUSES),
|
||||
Tables\Filters\SelectFilter::make('supplier_id')
|
||||
->label('Furnizor')
|
||||
->options(fn () => Supplier::pluck('name', 'id')),
|
||||
])
|
||||
->actions([
|
||||
Actions\Action::make('receive')
|
||||
->label('Recepționează')
|
||||
->icon('heroicon-m-check-circle')
|
||||
->color('success')
|
||||
->visible(fn (Purchase $r) => $r->status !== 'received' && $r->status !== 'cancelled')
|
||||
->requiresConfirmation()
|
||||
->modalDescription('Se va incrementa stocul pieselor legate.')
|
||||
->action(function (Purchase $r) {
|
||||
$r->markReceived();
|
||||
Notification::make()
|
||||
->title('Recepționat — stoc actualizat')
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
Actions\EditAction::make(),
|
||||
Actions\DeleteAction::make(),
|
||||
])
|
||||
->defaultSort('order_date', 'desc');
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
RelationManagers\ItemsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListPurchases::route('/'),
|
||||
'create' => Pages\CreatePurchase::route('/create'),
|
||||
'edit' => Pages\EditPurchase::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PurchaseResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PurchaseResource;
|
||||
use App\Models\Tenant\Purchase;
|
||||
use App\Tenancy\TenantManager;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreatePurchase extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PurchaseResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$companyId = app(TenantManager::class)->currentId();
|
||||
$data['number'] = Purchase::generateNumber($companyId);
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PurchaseResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PurchaseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPurchase extends EditRecord
|
||||
{
|
||||
protected static string $resource = PurchaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [Actions\DeleteAction::make()];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PurchaseResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\PurchaseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListPurchases extends ListRecords
|
||||
{
|
||||
protected static string $resource = PurchaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [Actions\CreateAction::make()];
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\PurchaseResource\RelationManagers;
|
||||
|
||||
use App\Models\Tenant\Part;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class ItemsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'items';
|
||||
|
||||
protected static ?string $title = 'Articole';
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Forms\Components\Select::make('part_id')
|
||||
->label('Piesă din catalog')
|
||||
->options(fn () => Part::where('is_active', true)
|
||||
->get()
|
||||
->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}]" : '')])
|
||||
->toArray())
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(function ($state, Set $set) {
|
||||
if ($state && $part = Part::find($state)) {
|
||||
$set('name', $part->name);
|
||||
$set('article', $part->article);
|
||||
$set('unit', $part->unit);
|
||||
$set('buy_price', $part->buy_price);
|
||||
}
|
||||
})
|
||||
->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('name')->label('Denumire')->required()->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('article')->label('Cod articol'),
|
||||
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)->required(),
|
||||
Forms\Components\TextInput::make('unit')->label('UM')->default('buc'),
|
||||
Forms\Components\TextInput::make('buy_price')->label('Preț achiziție')->numeric()->required(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('name')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')->wrap(),
|
||||
Tables\Columns\TextColumn::make('article')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('qty')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('unit')->label('UM'),
|
||||
Tables\Columns\TextColumn::make('buy_price')->money('MDL')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
|
||||
Tables\Columns\IconColumn::make('received')->boolean()->label('Recepț.'),
|
||||
])
|
||||
->headerActions([
|
||||
Actions\CreateAction::make(),
|
||||
])
|
||||
->actions([
|
||||
Actions\EditAction::make(),
|
||||
Actions\DeleteAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources;
|
||||
|
||||
use App\Filament\Tenant\Resources\SupplierResource\Pages;
|
||||
use App\Models\Tenant\Supplier;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SupplierResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Supplier::class;
|
||||
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-building-storefront';
|
||||
|
||||
protected static ?string $navigationLabel = 'Furnizori';
|
||||
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
|
||||
|
||||
protected static ?string $modelLabel = 'furnizor';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'furnizori';
|
||||
|
||||
protected static ?int $navigationSort = 42;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Schemas\Components\Section::make('Identificare')
|
||||
->columns(2)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('name')->label('Nume')->required()->maxLength(160),
|
||||
Forms\Components\TextInput::make('contact_name')->label('Persoană contact')->maxLength(120),
|
||||
Forms\Components\TextInput::make('phone')->tel()->maxLength(40),
|
||||
Forms\Components\TextInput::make('email')->email()->maxLength(120),
|
||||
Forms\Components\TextInput::make('website')->url()->maxLength(160),
|
||||
]),
|
||||
Schemas\Components\Section::make('Comercial')
|
||||
->columns(3)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('pay_terms')->label('Termeni plată')->placeholder('Net 30 / Avans')->maxLength(60),
|
||||
Forms\Components\TextInput::make('delivery_days')->label('Zile livrare')->numeric()->default(0),
|
||||
Forms\Components\Select::make('rating')
|
||||
->label('Rating')
|
||||
->options([1 => '★', 2 => '★★', 3 => '★★★', 4 => '★★★★', 5 => '★★★★★'])
|
||||
->default(3)
|
||||
->required(),
|
||||
Forms\Components\TextInput::make('discount_pct')->label('Discount %')->numeric()->default(0),
|
||||
Forms\Components\TagsInput::make('categories')->label('Categorii')->placeholder('Frâne, Ulei, ...')->columnSpan(2),
|
||||
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
|
||||
]),
|
||||
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('name')->searchable()->sortable(),
|
||||
Tables\Columns\TextColumn::make('contact_name')->label('Contact')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('phone')->copyable()->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('rating')
|
||||
->label('Rating')
|
||||
->formatStateUsing(fn ($s) => str_repeat('★', (int) $s)),
|
||||
Tables\Columns\TextColumn::make('delivery_days')->label('Livrare (zile)')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('discount_pct')->label('Discount')
|
||||
->formatStateUsing(fn ($s) => $s . '%')->alignRight(),
|
||||
Tables\Columns\IconColumn::make('is_active')->boolean(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\TernaryFilter::make('is_active')->label('Activi'),
|
||||
])
|
||||
->actions([
|
||||
Actions\EditAction::make(),
|
||||
Actions\DeleteAction::make(),
|
||||
])
|
||||
->defaultSort('name');
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListSuppliers::route('/'),
|
||||
'create' => Pages\CreateSupplier::route('/create'),
|
||||
'edit' => Pages\EditSupplier::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\SupplierResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\SupplierResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateSupplier extends CreateRecord
|
||||
{
|
||||
protected static string $resource = SupplierResource::class;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\SupplierResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\SupplierResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditSupplier extends EditRecord
|
||||
{
|
||||
protected static string $resource = SupplierResource::class;
|
||||
|
||||
protected function getHeaderActions(): array { return [Actions\DeleteAction::make()]; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Resources\SupplierResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\SupplierResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListSuppliers extends ListRecords
|
||||
{
|
||||
protected static string $resource = SupplierResource::class;
|
||||
|
||||
protected function getHeaderActions(): array { return [Actions\CreateAction::make()]; }
|
||||
}
|
||||
+24
-2
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
|
||||
|
||||
use App\Models\Tenant\Part;
|
||||
use App\Models\Tenant\WorkOrderPart;
|
||||
use Filament\Actions;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
@@ -19,9 +21,28 @@ class PartsRelationManager extends RelationManager
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Forms\Components\Select::make('part_id')
|
||||
->label('Din catalog (lasă gol pentru text liber)')
|
||||
->options(fn () => Part::where('is_active', true)
|
||||
->get()
|
||||
->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}] " : '') . "(stoc: {$p->qty})"])
|
||||
->toArray())
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(function ($state, Set $set) {
|
||||
if ($state && $part = Part::find($state)) {
|
||||
$set('name', $part->name);
|
||||
$set('article', $part->article);
|
||||
$set('brand', $part->brand);
|
||||
$set('unit', $part->unit);
|
||||
$set('buy_price', $part->buy_price);
|
||||
$set('sell_price', $part->sell_price);
|
||||
}
|
||||
})
|
||||
->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('name')->label('Denumire')->required()->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('article')->label('Cod articol')->maxLength(64),
|
||||
Forms\Components\TextInput::make('brand')->label('Brand')->maxLength(64),
|
||||
Forms\Components\TextInput::make('brand')->maxLength(64),
|
||||
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)->required(),
|
||||
Forms\Components\TextInput::make('unit')->label('UM')->maxLength(16)->default('buc'),
|
||||
Forms\Components\TextInput::make('buy_price')->label('Preț achiziție')->numeric()->default(0),
|
||||
@@ -30,7 +51,8 @@ class PartsRelationManager extends RelationManager
|
||||
Forms\Components\Select::make('status')
|
||||
->options(WorkOrderPart::STATUSES)
|
||||
->default('needed')
|
||||
->required(),
|
||||
->required()
|
||||
->helperText('La trecere pe „Montată" se scade automat din stoc.'),
|
||||
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Tenant\Widgets;
|
||||
|
||||
use App\Models\Tenant\Part;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as BaseWidget;
|
||||
|
||||
class LowStockTable extends BaseWidget
|
||||
{
|
||||
protected static ?int $sort = 5;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
protected static ?string $heading = '⚠️ Stoc minim atins';
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->query(
|
||||
Part::query()
|
||||
->where('is_active', true)
|
||||
->whereColumn('qty', '<=', 'min_qty')
|
||||
->orderBy('qty')
|
||||
)
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')->label('Piesă')->wrap()->searchable(),
|
||||
Tables\Columns\TextColumn::make('article')->label('Cod')->placeholder('—'),
|
||||
Tables\Columns\TextColumn::make('qty')->label('Stoc')->alignRight()
|
||||
->color(fn ($state) => $state <= 0 ? 'danger' : 'warning')
|
||||
->weight('bold'),
|
||||
Tables\Columns\TextColumn::make('min_qty')->label('Min.')->alignRight(),
|
||||
Tables\Columns\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—'),
|
||||
])
|
||||
->paginated(false)
|
||||
->defaultSort('qty');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Part extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
public const CATEGORIES = [
|
||||
'Ulei', 'Filtre', 'Frâne', 'Suspensie', 'Lichide',
|
||||
'Distribuție', 'Anvelope', 'Electrică', 'Caroserie', 'Altele',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'name', 'article', 'brand', 'category',
|
||||
'qty', 'unit', 'min_qty',
|
||||
'buy_price', 'sell_price',
|
||||
'location', 'barcode', 'preferred_supplier_id',
|
||||
'is_active', 'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'qty' => 'decimal:2',
|
||||
'min_qty' => 'decimal:2',
|
||||
'buy_price' => 'decimal:2',
|
||||
'sell_price' => 'decimal:2',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function preferredSupplier(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Supplier::class, 'preferred_supplier_id');
|
||||
}
|
||||
|
||||
public function isLow(): bool
|
||||
{
|
||||
return (float) $this->qty <= (float) $this->min_qty;
|
||||
}
|
||||
|
||||
public function isOut(): bool
|
||||
{
|
||||
return (float) $this->qty <= 0;
|
||||
}
|
||||
|
||||
public function adjustStock(float $delta, ?string $reason = null): void
|
||||
{
|
||||
$this->qty = max(0, (float) $this->qty + $delta);
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Purchase extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
public const STATUSES = [
|
||||
'draft' => 'Ciornă',
|
||||
'ordered' => 'Comandată',
|
||||
'received' => 'Recepționată',
|
||||
'cancelled' => 'Anulată',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'number', 'supplier_id',
|
||||
'order_date', 'expected_at', 'received_at', 'paid_at',
|
||||
'status', 'total', 'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'order_date' => 'date',
|
||||
'expected_at' => 'date',
|
||||
'received_at' => 'date',
|
||||
'paid_at' => 'date',
|
||||
'total' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function supplier(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Supplier::class);
|
||||
}
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(PurchaseItem::class);
|
||||
}
|
||||
|
||||
public function recalcTotal(): void
|
||||
{
|
||||
$this->total = (float) $this->items()->sum('total');
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public static function generateNumber(int $companyId): string
|
||||
{
|
||||
$year = date('y');
|
||||
$count = static::withoutGlobalScopes()
|
||||
->where('company_id', $companyId)
|
||||
->whereYear('created_at', date('Y'))
|
||||
->count();
|
||||
return sprintf('P-%s-%04d', $year, $count + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all items received and increment Part.qty for linked items.
|
||||
*/
|
||||
public function markReceived(): void
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::transaction(function () {
|
||||
foreach ($this->items as $item) {
|
||||
if (! $item->received) {
|
||||
if ($item->part_id) {
|
||||
$part = Part::find($item->part_id);
|
||||
$part?->adjustStock((float) $item->qty);
|
||||
}
|
||||
$item->received = true;
|
||||
$item->save();
|
||||
}
|
||||
}
|
||||
$this->status = 'received';
|
||||
$this->received_at = now();
|
||||
$this->save();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PurchaseItem extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'purchase_id', 'part_id',
|
||||
'name', 'article', 'qty', 'unit', 'buy_price', 'total', 'received',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'qty' => 'decimal:2',
|
||||
'buy_price' => 'decimal:2',
|
||||
'total' => 'decimal:2',
|
||||
'received' => 'boolean',
|
||||
];
|
||||
|
||||
public function purchase(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Purchase::class);
|
||||
}
|
||||
|
||||
public function part(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Part::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (self $row) {
|
||||
$row->total = round((float) $row->qty * (float) $row->buy_price, 2);
|
||||
});
|
||||
static::saved(fn (self $row) => $row->purchase?->recalcTotal());
|
||||
static::deleted(fn (self $row) => $row->purchase?->recalcTotal());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Tenant;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Supplier extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'name', 'contact_name', 'phone', 'email', 'website',
|
||||
'pay_terms', 'delivery_days', 'rating', 'discount_pct',
|
||||
'categories', 'is_active', 'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'categories' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
'discount_pct' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function purchases(): HasMany
|
||||
{
|
||||
return $this->hasMany(Purchase::class);
|
||||
}
|
||||
|
||||
public function parts(): HasMany
|
||||
{
|
||||
return $this->hasMany(Part::class, 'preferred_supplier_id');
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class WorkOrderPart extends Model
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'work_order_id',
|
||||
'company_id', 'work_order_id', 'part_id',
|
||||
'name', 'article', 'brand',
|
||||
'qty', 'unit', 'buy_price', 'sell_price',
|
||||
'discount_pct', 'total', 'status', 'notes',
|
||||
@@ -39,6 +39,11 @@ class WorkOrderPart extends Model
|
||||
return $this->belongsTo(WorkOrder::class);
|
||||
}
|
||||
|
||||
public function part(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Part::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (self $row) {
|
||||
@@ -46,6 +51,22 @@ class WorkOrderPart extends Model
|
||||
$disc = (float) $row->discount_pct;
|
||||
$row->total = round($sub * (1 - $disc / 100), 2);
|
||||
});
|
||||
|
||||
// When a part is marked installed, decrement catalog stock once.
|
||||
static::updating(function (self $row) {
|
||||
$wasInstalled = $row->getOriginal('status') === 'installed';
|
||||
$isInstalled = $row->status === 'installed';
|
||||
if (! $wasInstalled && $isInstalled && $row->part_id) {
|
||||
$part = Part::find($row->part_id);
|
||||
$part?->adjustStock(-(float) $row->qty);
|
||||
}
|
||||
// If reverting from installed → restore stock
|
||||
if ($wasInstalled && ! $isInstalled && $row->part_id) {
|
||||
$part = Part::find($row->part_id);
|
||||
$part?->adjustStock((float) $row->qty);
|
||||
}
|
||||
});
|
||||
|
||||
static::saved(fn (self $row) => $row->workOrder?->recalcTotal());
|
||||
static::deleted(fn (self $row) => $row->workOrder?->recalcTotal());
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ class TenantPanelProvider extends PanelProvider
|
||||
->discoverWidgets(in: app_path('Filament/Tenant/Widgets'), for: 'App\\Filament\\Tenant\\Widgets')
|
||||
->widgets([
|
||||
\App\Filament\Tenant\Widgets\StatsOverview::class,
|
||||
\App\Filament\Tenant\Widgets\LowStockTable::class,
|
||||
])
|
||||
->middleware([
|
||||
// CRITICAL: tenant resolution must run BEFORE Filament's
|
||||
|
||||
Reference in New Issue
Block a user