Files
pos_heidiver/app/Filament/Resources/OjalResource.php
T
2026-02-04 20:46:05 -05:00

161 lines
7.0 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\OjalResource\Pages;
use App\Models\Ojal;
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 OjalResource extends Resource
{
protected static ?string $model = Ojal::class;
protected static ?string $navigationIcon = 'heroicon-o-scissors';
protected static ?string $navigationGroup = 'Gestión';
protected static ?string $navigationLabel = 'Ojales';
protected static ?int $navigationSort = 4;
public static function canViewAny(): bool
{
return auth()->user()?->can('ver ojals') ?? 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('confecciones', function ($q) {
$q->where('cantidad_recibida', '>', 0);
})->get()->mapWithKeys(function ($op) {
$cantidadRecibida = $op->confecciones->sum('cantidad_recibida');
$arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class)
->whereHas('referencia', function($q) use ($op) {
$q->where('orden_produccion_id', $op->id);
})->where('tipo', 'arreglo')->sum('cantidad');
$cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class)
->whereHas('referencia', function($q) use ($op) {
$q->where('orden_produccion_id', $op->id);
})->sum('cantidad');
$cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros;
$cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $op->id)
->sum('cantidad_enviada');
$saldoDisponible = $cantidadConfecciones - $cantidadEnOjales;
if ($saldoDisponible > 0) {
return [$op->id => "{$op->numero_orden} - {$op->referencia} (Disponible: {$saldoDisponible})"];
}
return [];
})->filter();
})
->searchable()
->required()
->reactive()
->afterStateUpdated(function ($state, $set) {
if ($state) {
$op = \App\Models\OrdenProduccion::find($state);
if ($op) {
$cantidadRecibida = $op->confecciones->sum('cantidad_recibida');
$arreglos = \App\Models\Ajuste::where('referencia_type', \App\Models\Confeccion::class)
->whereHas('referencia', function($q) use ($state) {
$q->where('orden_produccion_id', $state);
})->where('tipo', 'arreglo')->sum('cantidad');
$cobros = \App\Models\Cobro::where('referencia_type', \App\Models\Confeccion::class)
->whereHas('referencia', function($q) use ($state) {
$q->where('orden_produccion_id', $state);
})->sum('cantidad');
$cantidadConfecciones = $cantidadRecibida - $arreglos - $cobros;
$cantidadEnOjales = \App\Models\Ojal::where('orden_produccion_id', $state)->sum('cantidad_enviada');
$cantidadDisponible = $cantidadConfecciones - $cantidadEnOjales;
$set('cantidad_enviada', max(0, $cantidadDisponible));
$set('prenda_nombre_info', $op->prenda_modelo ?? 'Sin especificar');
}
}
})->helperText('Solo se muestran órdenes con prendas disponibles desde Confección'),
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 Ojales, las prendas quedarán disponibles para Presillas')
->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\ListOjales::route('/'),
'create' => Pages\CreateOjal::route('/create'),
'edit' => Pages\EditOjal::route('/{record}/edit'),
];
}
}