205 lines
8.2 KiB
PHP
205 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\ConfeccionResource\Pages;
|
|
use App\Filament\Resources\ConfeccionResource\RelationManagers;
|
|
use App\Models\Confeccion;
|
|
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 ConfeccionResource extends Resource
|
|
{
|
|
protected static ?string $model = Confeccion::class;
|
|
|
|
protected static ?string $navigationGroup = 'Gestión';
|
|
protected static ?int $navigationSort = 3;
|
|
protected static ?string $navigationIcon = 'heroicon-o-cog';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form->schema([
|
|
Section::make('Datos de Confección')
|
|
->schema([
|
|
Select::make('proveedor_id')
|
|
->label('Tallerista')
|
|
->relationship(
|
|
'proveedor',
|
|
'nombre',
|
|
fn($query) => $query->where('categoria', 'talleres')
|
|
)
|
|
->searchable()
|
|
->preload()
|
|
->required(),
|
|
|
|
Select::make('orden_produccion_id')
|
|
->label('Orden de Producción')
|
|
->options(fn () => \App\Models\OrdenProduccion::whereRaw('(
|
|
cantidad_total - (
|
|
select coalesce(sum(cantidad_enviada),0) from confeccions where confeccions.orden_produccion_id = orden_produccions.id and confeccions.fecha_recepcion is null
|
|
)
|
|
) > 0')->pluck('numero_orden', 'id')->toArray())
|
|
->searchable()
|
|
->preload()
|
|
->reactive()
|
|
->required()
|
|
->afterStateUpdated(function ($state, $set) {
|
|
if ($state) {
|
|
$orden = \App\Models\OrdenProduccion::find($state);
|
|
$sentInProcess = \App\Models\Confeccion::where('orden_produccion_id', $state)
|
|
->whereNull('fecha_recepcion')
|
|
->sum('cantidad_enviada');
|
|
|
|
$pending = max(0, (int)($orden->cantidad_total ?? 0) - (int)$sentInProcess);
|
|
|
|
$set('cantidad_enviada', $pending ?: 0);
|
|
$set('fecha_envio', now()->toDateString());
|
|
}
|
|
}),
|
|
|
|
DatePicker::make('fecha_envio')
|
|
->required()
|
|
->default(now()),
|
|
|
|
TextInput::make('cantidad_enviada')
|
|
->numeric()
|
|
->required()
|
|
->minValue(1)
|
|
->maxValue(fn ($get) => $get('orden_produccion_id') ? \App\Models\OrdenProduccion::find($get('orden_produccion_id'))->cantidad_total : null)
|
|
->helperText(fn ($get) => $get('orden_produccion_id') ? 'Máximo: ' . \App\Models\OrdenProduccion::find($get('orden_produccion_id'))->cantidad_total : 'Selecciona una Orden de Producción')
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, $set, $get) {
|
|
$valor = $get('valor_por_prenda') ?? 0;
|
|
$cantidad = $get('cantidad_recibida') ?? $state;
|
|
$set('total_pagar', $cantidad && $valor ? $cantidad * $valor : 0);
|
|
}),
|
|
|
|
TextInput::make('valor_por_prenda')
|
|
->numeric()
|
|
->prefix('$')
|
|
->required()
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, $set, $get) {
|
|
$cantidad = $get('cantidad_recibida') ?? $get('cantidad_enviada');
|
|
$set('total_pagar', $cantidad && $state ? $cantidad * $state : 0);
|
|
}),
|
|
|
|
TextInput::make('total_pagar')
|
|
->prefix('$')
|
|
->numeric()
|
|
->readOnly()
|
|
->default(0),
|
|
])
|
|
->columns(2),
|
|
|
|
Section::make('Recepciones')
|
|
->description('Resumen de recepciones. Para registrar recepciones múltiples use el botón "Agregar recepción" en la cabecera o la relación "Recepciones" abajo.')
|
|
->schema([
|
|
Forms\Components\Placeholder::make('recepcion_summary')
|
|
->label('Resumen')
|
|
->content(function ($get) {
|
|
$id = $get('id');
|
|
if (! $id) return 'Sin recepciones registradas';
|
|
|
|
$total = \App\Models\Recepcion::where('referencia_type', \App\Models\Confeccion::class)
|
|
->where('referencia_id', $id)
|
|
->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
|
|
|
$enviada = (int) ($get('cantidad_enviada') ?? 0);
|
|
$faltan = max(0, $enviada - $total);
|
|
|
|
return "Total recibido: {$total} — Faltan: {$faltan}";
|
|
}),
|
|
|
|
Forms\Components\Placeholder::make('instrucciones')
|
|
->label('Acciones')
|
|
->content('Use el botón "Agregar recepción" en la cabecera para registrar una recepción rápida. También puede gestionar todas las recepciones en la sección relacionada al final de la página.'),
|
|
])
|
|
->columns(1)
|
|
->collapsed(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('ordenProduccion.numero_orden')
|
|
->label('OP')
|
|
->searchable(),
|
|
|
|
TextColumn::make('proveedor.nombre')
|
|
->label('Tallerista')
|
|
->searchable(),
|
|
|
|
TextColumn::make('cantidad_enviada'),
|
|
|
|
TextColumn::make('cantidad_recibida'),
|
|
|
|
TextColumn::make('faltantes')
|
|
->label('Faltan')
|
|
->sortable(),
|
|
|
|
BadgeColumn::make('estado')
|
|
->colors([
|
|
'warning' => 'pendiente',
|
|
'info' => 'parcial',
|
|
'success' => 'completo',
|
|
])
|
|
->formatStateUsing(fn($state) => ucfirst($state)),
|
|
|
|
TextColumn::make('total_pagar')
|
|
->money('COP'),
|
|
])
|
|
->defaultSort('created_at', 'desc')
|
|
->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\RecepcionesRelationManager::class,
|
|
RelationManagers\CobrosRelationManager::class,
|
|
RelationManagers\AjustesRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListConfeccions::route('/'),
|
|
'create' => Pages\CreateConfeccion::route('/create'),
|
|
'edit' => Pages\EditConfeccion::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|