148 lines
5.8 KiB
PHP
148 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\PrensillaResource\Pages;
|
|
use App\Models\Prensilla;
|
|
use Filament\Forms;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\DatePicker;
|
|
|
|
class PrensillaResource extends Resource
|
|
{
|
|
protected static ?string $model = Prensilla::class;
|
|
protected static ?string $navigationIcon = 'heroicon-o-scissors';
|
|
protected static ?string $navigationGroup = 'Gestión';
|
|
protected static ?string $navigationLabel = 'Presillas';
|
|
protected static ?int $navigationSort = 5;
|
|
|
|
public static function canViewAny(): bool
|
|
{
|
|
return auth()->user()?->can('ver prensillas') ?? false;
|
|
}
|
|
|
|
public static function form(Forms\Form $form): Forms\Form
|
|
{
|
|
return $form->schema([
|
|
Select::make('orden_produccion_id')
|
|
->label('Orden de Producción')
|
|
->options(function () {
|
|
return \App\Models\OrdenProduccion::whereHas('ojales', function ($q) {
|
|
$q->where('terminada', true);
|
|
})->get()->mapWithKeys(function ($op) {
|
|
$ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $op->id)
|
|
->where('terminada', true)
|
|
->sum('cantidad_enviada');
|
|
$yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $op->id)
|
|
->sum('cantidad_enviada');
|
|
$disponible = $ojalesTerminados - $yaEnviado;
|
|
if ($disponible > 0) {
|
|
return [$op->id => "{$op->numero_orden} - {$op->referencia} (Disponible: {$disponible})"];
|
|
}
|
|
return [];
|
|
})->filter();
|
|
})
|
|
->searchable()
|
|
->required()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, $set) {
|
|
if ($state) {
|
|
$op = \App\Models\OrdenProduccion::find($state);
|
|
if ($op) {
|
|
$ojalesTerminados = \App\Models\Ojal::where('orden_produccion_id', $state)
|
|
->where('terminada', true)
|
|
->sum('cantidad_enviada');
|
|
$yaEnviado = \App\Models\Prensilla::where('orden_produccion_id', $state)
|
|
->sum('cantidad_enviada');
|
|
$cantidadDisponible = $ojalesTerminados - $yaEnviado;
|
|
$set('cantidad_enviada', max(0, $cantidadDisponible));
|
|
$set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar');
|
|
}
|
|
}
|
|
})->helperText('⚠️ Solo se muestran órdenes con Ojales terminados disponibles'),
|
|
|
|
TextInput::make('prenda_nombre_info')
|
|
->label('Prenda')
|
|
->disabled()
|
|
->dehydrated(false)
|
|
->visible(fn ($get) => $get('orden_produccion_id') !== null),
|
|
|
|
Select::make('proveedor_id')
|
|
->label('Empleado Responsable')
|
|
->options(fn() => \App\Models\Proveedor::where('categoria', 'empleado')->pluck('nombre', 'id'))
|
|
->searchable()
|
|
->preload()
|
|
->required()
|
|
->helperText('Selecciona el empleado responsable del proceso'),
|
|
|
|
TextInput::make('cantidad_enviada')
|
|
->label('Cantidad')
|
|
->numeric()
|
|
->required()
|
|
->minValue(1),
|
|
|
|
Forms\Components\Checkbox::make('terminada')
|
|
->label('Marcar como terminado')
|
|
->helperText('✓ Al terminar Presillas, las prendas quedarán disponibles para Tintorería')
|
|
->reactive()
|
|
->default(false),
|
|
]);
|
|
}
|
|
|
|
public static function table(Tables\Table $table): Tables\Table
|
|
{
|
|
return $table->columns([
|
|
Tables\Columns\TextColumn::make('ordenProduccion.numero_orden')
|
|
->label('OP')
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
Tables\Columns\TextColumn::make('ordenProduccion.referencia')
|
|
->label('Referencia')
|
|
->searchable()
|
|
->toggleable(),
|
|
|
|
Tables\Columns\TextColumn::make('ordenProduccion.prenda_modelo')
|
|
->label('Prenda')
|
|
->searchable()
|
|
->limit(30)
|
|
->toggleable(),
|
|
|
|
Tables\Columns\TextColumn::make('proveedor.nombre')
|
|
->label('Empleado')
|
|
->searchable(),
|
|
|
|
Tables\Columns\TextColumn::make('cantidad_enviada')
|
|
->label('Cantidad'),
|
|
|
|
Tables\Columns\IconColumn::make('terminada')
|
|
->label('Terminado')
|
|
->boolean(),
|
|
|
|
Tables\Columns\TextColumn::make('fecha_envio')
|
|
->date()
|
|
->sortable(),
|
|
])
|
|
->defaultSort('created_at', 'desc')
|
|
->filters([
|
|
Tables\Filters\SelectFilter::make('terminada')
|
|
->options([
|
|
'1' => 'Terminados',
|
|
'0' => 'En proceso',
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListPrensillas::route('/'),
|
|
'create' => Pages\CreatePrensilla::route('/create'),
|
|
'edit' => Pages\EditPrensilla::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|