up
This commit is contained in:
@@ -102,42 +102,6 @@ class EditConfeccion extends EditRecord
|
||||
\Filament\Notifications\Notification::make()->danger()->title('Error')->body($e->getMessage())->send();
|
||||
}
|
||||
|
||||
$this->redirect(route('filament.admin.resources.confeccions.edit', ['record' => $record->id]));
|
||||
}),
|
||||
|
||||
// Cobros: descontar inventario y descontar valor del total a pagar
|
||||
Actions\Action::make('cobros')
|
||||
->label('Cobros')
|
||||
->modalHeading('Registrar Cobro')
|
||||
->form([
|
||||
Forms\Components\TextInput::make('cantidad')
|
||||
->label('Cantidad')
|
||||
->numeric()
|
||||
->required()
|
||||
->minValue(1),
|
||||
Forms\Components\TextInput::make('valor')
|
||||
->label('Valor a descontar')
|
||||
->numeric()
|
||||
->required()
|
||||
->minValue(0),
|
||||
Forms\Components\Textarea::make('notas'),
|
||||
])
|
||||
->action(function (array $data): void {
|
||||
$record = $this->getRecord();
|
||||
|
||||
$cantidad = (int) ($data['cantidad'] ?? 0);
|
||||
$valor = (float) ($data['valor'] ?? 0);
|
||||
|
||||
try {
|
||||
$record->registerCobro($cantidad, $valor, $data['notas'] ?? null, auth()->id() ?? null);
|
||||
|
||||
\Filament\Notifications\Notification::make()->success()->title('Cobro registrado')->body('Se descontó del inventario y se aplicó el descuento al valor a pagar.')->send();
|
||||
} catch (\Illuminate\Validation\ValidationException $e) {
|
||||
\Filament\Notifications\Notification::make()->danger()->title('Error')->body($e->validator->errors()->first() ?? 'Error al registrar cobro.')->send();
|
||||
} catch (\Throwable $e) {
|
||||
\Filament\Notifications\Notification::make()->danger()->title('Error')->body($e->getMessage())->send();
|
||||
}
|
||||
|
||||
$this->redirect(route('filament.admin.resources.confeccions.edit', ['record' => $record->id]));
|
||||
}),
|
||||
];
|
||||
|
||||
+36
-15
@@ -173,25 +173,46 @@ class Confeccion extends Model
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'La cantidad debe ser mayor que 0.']);
|
||||
}
|
||||
|
||||
// Buscar inventario disponible
|
||||
$inventario = \App\Models\InventarioPrenda::where('orden_produccion_id', $this->orden_produccion_id)
|
||||
->where('cantidad_disponible', '>=', $cantidad)
|
||||
->first();
|
||||
|
||||
if (! $inventario) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'No hay inventario disponible suficiente para descontar.']);
|
||||
if (!$this->orden_produccion_id) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['orden_produccion_id' => 'La confección debe tener una orden de producción asociada.']);
|
||||
}
|
||||
|
||||
// Descontar del inventario
|
||||
$inventario->cantidad_disponible = $inventario->cantidad_disponible - $cantidad;
|
||||
$inventario->save();
|
||||
// Buscar inventarios disponibles de esta orden de producción
|
||||
$inventarios = \App\Models\InventarioPrenda::where('orden_produccion_id', $this->orden_produccion_id)
|
||||
->where('cantidad_disponible', '>', 0)
|
||||
->orderBy('cantidad_disponible', 'desc')
|
||||
->get();
|
||||
|
||||
// Si está asociado a producto, decrementar stock
|
||||
if ($inventario->producto_id) {
|
||||
$producto = \App\Models\Producto::find($inventario->producto_id);
|
||||
if ($producto) {
|
||||
$producto->decrement('stock', $cantidad);
|
||||
if ($inventarios->isEmpty()) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => 'No hay inventario disponible para la orden de producción #' . $this->orden_produccion_id . '. Asegúrate de que exista inventario con cantidad_disponible > 0.']);
|
||||
}
|
||||
|
||||
$totalDisponible = $inventarios->sum('cantidad_disponible');
|
||||
|
||||
if ($totalDisponible < $cantidad) {
|
||||
throw \Illuminate\Validation\ValidationException::withMessages(['cantidad' => "Solo hay {$totalDisponible} unidades disponibles en inventario. No se puede descontar {$cantidad}."]);
|
||||
}
|
||||
|
||||
// Descontar del inventario de forma distribuida si es necesario
|
||||
$cantidadRestante = $cantidad;
|
||||
foreach ($inventarios as $inventario) {
|
||||
if ($cantidadRestante <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$aDescontar = min($cantidadRestante, $inventario->cantidad_disponible);
|
||||
$inventario->cantidad_disponible -= $aDescontar;
|
||||
$inventario->save();
|
||||
|
||||
// Si está asociado a producto, decrementar stock
|
||||
if ($inventario->producto_id) {
|
||||
$producto = \App\Models\Producto::find($inventario->producto_id);
|
||||
if ($producto) {
|
||||
$producto->decrement('stock', $aDescontar);
|
||||
}
|
||||
}
|
||||
|
||||
$cantidadRestante -= $aDescontar;
|
||||
}
|
||||
|
||||
// Crear registro de cobro
|
||||
|
||||
Reference in New Issue
Block a user