171 lines
6.6 KiB
PHP
171 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\InventarioPrendaResource\Pages;
|
|
use App\Filament\Resources\InventarioPrendaResource\RelationManagers;
|
|
use App\Models\InventarioPrenda;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Filament\Forms\Components\{
|
|
Select,
|
|
TextInput,
|
|
DatePicker,
|
|
Section
|
|
};
|
|
use Filament\Tables\Columns\{
|
|
TextColumn,
|
|
BadgeColumn
|
|
};
|
|
|
|
class InventarioPrendaResource extends Resource
|
|
{
|
|
protected static ?string $model = InventarioPrenda::class;
|
|
|
|
protected static ?string $navigationGroup = 'Gestión';
|
|
protected static ?int $navigationSort = 6;
|
|
protected static ?string $navigationIcon = 'heroicon-o-circle-stack';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form->schema([
|
|
Section::make('Ingreso a Inventario')
|
|
->schema([
|
|
Select::make('orden_produccion_id')
|
|
->label('Orden de Producción')
|
|
->relationship('ordenProduccion', 'numero_orden')
|
|
->searchable()
|
|
->preload()
|
|
->reactive()
|
|
->required()
|
|
->afterStateUpdated(function ($state, $set) {
|
|
$remaining = 0;
|
|
|
|
if ($state) {
|
|
// Total recibido desde procesos de acabados completados
|
|
$total = \App\Models\ProcesoAcabado::where('orden_produccion_id', $state)
|
|
->whereNotNull('fecha_recepcion')
|
|
->sum('cantidad_recibida');
|
|
|
|
// Ya asignado en otros inventarios
|
|
$used = \App\Models\InventarioPrenda::where('orden_produccion_id', $state)->sum('cantidad_terminada');
|
|
|
|
$remaining = max(0, (int) $total - (int) $used);
|
|
|
|
// Colocar valores en formulario
|
|
$set('cantidad_terminada', $remaining);
|
|
$set('cantidad_disponible', $remaining);
|
|
|
|
// Si la OP tiene producto predeterminado, asignarlo como sugerencia
|
|
$op = \App\Models\OrdenProduccion::find($state);
|
|
if ($op && $op->producto_id) {
|
|
$set('producto_id', $op->producto_id);
|
|
}
|
|
}
|
|
})->helperText('La Orden de Producción es la fuente de verdad para el producto; si la OP tiene producto asociado, se usará automáticamente.'),
|
|
|
|
TextInput::make('cantidad_terminada')
|
|
->numeric()
|
|
->required()
|
|
->disabled()
|
|
->helperText('Se calcula automáticamente desde los procesos de acabados completados asociados a la Orden de Producción.'),
|
|
|
|
Select::make('producto_id')
|
|
->label('Producto (opcional)')
|
|
->relationship('producto', 'nombre')
|
|
->searchable()
|
|
->preload()
|
|
->nullable()
|
|
->helperText('Opcional. Si la Orden de Producción tiene un producto asociado, éste será la fuente de verdad. Si el producto seleccionado difiere, se mostrará un error y no se permitirá guardar.'),
|
|
|
|
TextInput::make('cantidad_disponible')
|
|
->disabled()
|
|
->dehydrated(false),
|
|
|
|
DatePicker::make('fecha_ingreso')
|
|
->required()
|
|
->default(now()),
|
|
|
|
Select::make('estado')
|
|
->options([
|
|
'en_bodega' => 'En bodega',
|
|
'lista_despacho' => 'Lista para despacho',
|
|
])
|
|
->required(),
|
|
])
|
|
->columns(2),
|
|
|
|
Section::make('Distribuciones')
|
|
->schema([
|
|
\Filament\Forms\Components\Placeholder::make('distribuciones_notice')
|
|
->label('Distribuciones por bodega / color / talla')
|
|
->content('Gestiona las distribuciones desde la sección "Distribuciones" más abajo en la página. Usar el botón "Crear" para añadir nuevas distribuciones.'),
|
|
])
|
|
->columns(1),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('ordenProduccion.numero_orden')
|
|
->label('OP')
|
|
->searchable(),
|
|
|
|
TextColumn::make('cantidad_terminada'),
|
|
|
|
TextColumn::make('cantidad_disponible'),
|
|
|
|
TextColumn::make('fecha_ingreso')
|
|
->date(),
|
|
|
|
BadgeColumn::make('estado')
|
|
->formatStateUsing(fn($state) => match ($state) {
|
|
'en_bodega' => 'En bodega',
|
|
'lista_despacho' => 'Lista para despacho',
|
|
})
|
|
->colors([
|
|
'secondary' => 'en_bodega',
|
|
'success' => 'lista_despacho',
|
|
]),
|
|
])
|
|
->filters([
|
|
Tables\Filters\SelectFilter::make('orden_produccion_id')
|
|
->label('Orden de Producción')
|
|
->relationship('ordenProduccion', 'numero_orden')
|
|
->searchable()
|
|
->preload(),
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RelationManagers\DistribucionesRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListInventarioPrendas::route('/'),
|
|
'create' => Pages\CreateInventarioPrenda::route('/create'),
|
|
'edit' => Pages\EditInventarioPrenda::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|