up
This commit is contained in:
@@ -110,7 +110,7 @@ class ConfeccionResource extends Resource
|
|||||||
|
|
||||||
$total = \App\Models\Recepcion::where('referencia_type', \App\Models\Confeccion::class)
|
$total = \App\Models\Recepcion::where('referencia_type', \App\Models\Confeccion::class)
|
||||||
->where('referencia_id', $id)
|
->where('referencia_id', $id)
|
||||||
->sum('cantidad');
|
->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||||
|
|
||||||
$enviada = (int) ($get('cantidad_enviada') ?? 0);
|
$enviada = (int) ($get('cantidad_enviada') ?? 0);
|
||||||
$faltan = max(0, $enviada - $total);
|
$faltan = max(0, $enviada - $total);
|
||||||
|
|||||||
+1
@@ -40,6 +40,7 @@ class RecepcionesRelationManager extends RelationManager
|
|||||||
TextColumn::make('id')->label('#'),
|
TextColumn::make('id')->label('#'),
|
||||||
TextColumn::make('fecha_recepcion')->dateTime()->label('Fecha'),
|
TextColumn::make('fecha_recepcion')->dateTime()->label('Fecha'),
|
||||||
TextColumn::make('cantidad')->label('Cantidad'),
|
TextColumn::make('cantidad')->label('Cantidad'),
|
||||||
|
TextColumn::make('prendas_defectuosas')->label('Defectos'),
|
||||||
TextColumn::make('usuario.name')->label('Usuario'),
|
TextColumn::make('usuario.name')->label('Usuario'),
|
||||||
TextColumn::make('notas')->limit(50)->wrap(),
|
TextColumn::make('notas')->limit(50)->wrap(),
|
||||||
TextColumn::make('created_at')->dateTime()->label('Creado'),
|
TextColumn::make('created_at')->dateTime()->label('Creado'),
|
||||||
|
|||||||
+1
@@ -40,6 +40,7 @@ class RecepcionesRelationManager extends RelationManager
|
|||||||
TextColumn::make('id')->label('#'),
|
TextColumn::make('id')->label('#'),
|
||||||
TextColumn::make('fecha_recepcion')->dateTime()->label('Fecha'),
|
TextColumn::make('fecha_recepcion')->dateTime()->label('Fecha'),
|
||||||
TextColumn::make('cantidad')->label('Cantidad'),
|
TextColumn::make('cantidad')->label('Cantidad'),
|
||||||
|
TextColumn::make('prendas_defectuosas')->label('Defectos'),
|
||||||
TextColumn::make('usuario.name')->label('Usuario'),
|
TextColumn::make('usuario.name')->label('Usuario'),
|
||||||
TextColumn::make('notas')->limit(50)->wrap(),
|
TextColumn::make('notas')->limit(50)->wrap(),
|
||||||
TextColumn::make('created_at')->dateTime()->label('Creado'),
|
TextColumn::make('created_at')->dateTime()->label('Creado'),
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class Confeccion extends Model
|
|||||||
// Total recibido calculado desde recepciones (fuente de la verdad)
|
// Total recibido calculado desde recepciones (fuente de la verdad)
|
||||||
public function getRecibidoTotalAttribute()
|
public function getRecibidoTotalAttribute()
|
||||||
{
|
{
|
||||||
return (int) $this->recepciones()->sum('cantidad');
|
return (int) $this->recepciones()->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cuantas faltan por recibir (usa la suma real de recepciones)
|
// Cuantas faltan por recibir (usa la suma real de recepciones)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class Recepcion extends Model
|
|||||||
'referencia_id',
|
'referencia_id',
|
||||||
'user_id',
|
'user_id',
|
||||||
'cantidad',
|
'cantidad',
|
||||||
|
'prendas_defectuosas',
|
||||||
'fecha_recepcion',
|
'fecha_recepcion',
|
||||||
'notas',
|
'notas',
|
||||||
];
|
];
|
||||||
@@ -22,6 +23,7 @@ class Recepcion extends Model
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'fecha_recepcion' => 'datetime',
|
'fecha_recepcion' => 'datetime',
|
||||||
'cantidad' => 'integer',
|
'cantidad' => 'integer',
|
||||||
|
'prendas_defectuosas' => 'integer',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function referencia()
|
public function referencia()
|
||||||
@@ -39,24 +41,38 @@ class Recepcion extends Model
|
|||||||
static::creating(function ($rec) {
|
static::creating(function ($rec) {
|
||||||
// Cantidad debe ser entero >= 1
|
// Cantidad debe ser entero >= 1
|
||||||
$rec->cantidad = (int) $rec->cantidad;
|
$rec->cantidad = (int) $rec->cantidad;
|
||||||
|
$rec->prendas_defectuosas = (int) ($rec->prendas_defectuosas ?? 0);
|
||||||
|
|
||||||
if ($rec->cantidad <= 0) {
|
if ($rec->cantidad <= 0) {
|
||||||
throw ValidationException::withMessages(['cantidad' => 'La cantidad debe ser un entero mayor que 0.']);
|
throw ValidationException::withMessages(['cantidad' => 'La cantidad debe ser un entero mayor que 0.']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($rec->prendas_defectuosas < 0) {
|
||||||
|
throw ValidationException::withMessages(['prendas_defectuosas' => 'La cantidad de prendas defectuosas no puede ser negativa.']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($rec->prendas_defectuosas > $rec->cantidad) {
|
||||||
|
throw ValidationException::withMessages(['prendas_defectuosas' => 'Las prendas defectuosas no pueden superar la cantidad recibida.']);
|
||||||
|
}
|
||||||
|
|
||||||
// Fecha por defecto
|
// Fecha por defecto
|
||||||
if (! $rec->fecha_recepcion) {
|
if (! $rec->fecha_recepcion) {
|
||||||
$rec->fecha_recepcion = now();
|
$rec->fecha_recepcion = now();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validación: no permitir sobrepasar la cantidad enviada en la referencia
|
// Validación: no permitir sobrepasar la cantidad enviada en la referencia (considerando defectuosos)
|
||||||
if ($rec->referencia) {
|
if ($rec->referencia) {
|
||||||
$referencia = $rec->referencia;
|
$referencia = $rec->referencia;
|
||||||
$enviada = (int) ($referencia->cantidad_enviada ?? 0);
|
$enviada = (int) ($referencia->cantidad_enviada ?? 0);
|
||||||
|
|
||||||
|
// ya recibido efectivo (cantidad - defectuosos)
|
||||||
$yaRecibido = (int) \App\Models\Recepcion::where('referencia_type', get_class($referencia))
|
$yaRecibido = (int) \App\Models\Recepcion::where('referencia_type', get_class($referencia))
|
||||||
->where('referencia_id', $referencia->id)
|
->where('referencia_id', $referencia->id)
|
||||||
->sum('cantidad');
|
->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||||
|
|
||||||
if (($yaRecibido + $rec->cantidad) > $enviada) {
|
$efectivo = $rec->cantidad - $rec->prendas_defectuosas;
|
||||||
|
|
||||||
|
if (($yaRecibido + $efectivo) > $enviada) {
|
||||||
throw ValidationException::withMessages(['cantidad' => "La recepción excede la cantidad pendiente (faltan: " . max(0, $enviada - $yaRecibido) . ")."]);
|
throw ValidationException::withMessages(['cantidad' => "La recepción excede la cantidad pendiente (faltan: " . max(0, $enviada - $yaRecibido) . ")."]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,11 +89,13 @@ class Recepcion extends Model
|
|||||||
// Crear un traslado parcial desde la referencia usando esta recepción
|
// Crear un traslado parcial desde la referencia usando esta recepción
|
||||||
if ($rec->referencia) {
|
if ($rec->referencia) {
|
||||||
$referencia = $rec->referencia;
|
$referencia = $rec->referencia;
|
||||||
|
$efectivo = (int) ($rec->cantidad - ($rec->prendas_defectuosas ?? 0));
|
||||||
$payload = [
|
$payload = [
|
||||||
'cantidad_enviada' => $rec->cantidad,
|
'cantidad_enviada' => $rec->cantidad,
|
||||||
'cantidad_recibida' => $rec->cantidad,
|
'cantidad_recibida' => $efectivo,
|
||||||
'fecha_recepcion' => $rec->fecha_recepcion,
|
'fecha_recepcion' => $rec->fecha_recepcion,
|
||||||
'notas' => 'Creado desde recepción #' . $rec->id . ($rec->notas ? (" - " . $rec->notas) : ''),
|
'notas' => 'Creado desde recepción #' . $rec->id . ($rec->notas ? (" - " . $rec->notas) : ''),
|
||||||
|
'prendas_defectuosas' => $rec->prendas_defectuosas ?? 0,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Map destino según tipo de referencia
|
// Map destino según tipo de referencia
|
||||||
@@ -99,6 +117,7 @@ class Recepcion extends Model
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
static::updated(function ($rec) {
|
static::updated(function ($rec) {
|
||||||
$rec->syncParentAggregates();
|
$rec->syncParentAggregates();
|
||||||
});
|
});
|
||||||
@@ -112,9 +131,10 @@ class Recepcion extends Model
|
|||||||
{
|
{
|
||||||
if (! $this->referencia) return;
|
if (! $this->referencia) return;
|
||||||
$referencia = $this->referencia;
|
$referencia = $this->referencia;
|
||||||
|
|
||||||
$sum = (int) static::where('referencia_type', get_class($referencia))
|
$sum = (int) static::where('referencia_type', get_class($referencia))
|
||||||
->where('referencia_id', $referencia->id)
|
->where('referencia_id', $referencia->id)
|
||||||
->sum('cantidad');
|
->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||||
|
|
||||||
$referencia->cantidad_recibida = $sum;
|
$referencia->cantidad_recibida = $sum;
|
||||||
$referencia->save();
|
$referencia->save();
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class Tintoreria extends Model
|
|||||||
// Total recibido calculado desde recepciones (fuente de la verdad)
|
// Total recibido calculado desde recepciones (fuente de la verdad)
|
||||||
public function getRecibidoTotalAttribute()
|
public function getRecibidoTotalAttribute()
|
||||||
{
|
{
|
||||||
return (int) $this->recepciones()->sum('cantidad');
|
return (int) $this->recepciones()->sum(\DB::raw('cantidad - COALESCE(prendas_defectuosas, 0)'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cuantas faltan por recibir (usa la suma real de recepciones)
|
// Cuantas faltan por recibir (usa la suma real de recepciones)
|
||||||
|
|||||||
@@ -25,13 +25,14 @@ class ConfeccionRecepcionesTest extends TestCase
|
|||||||
'cantidad_enviada' => 100,
|
'cantidad_enviada' => 100,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Crear recepciones 40 y 40
|
// Crear recepciones 40 y 40 con defectos en la segunda (10)
|
||||||
Recepcion::create(['referencia_type' => Confeccion::class,'referencia_id' => $conf->id,'cantidad' => 40]);
|
|
||||||
Recepcion::create(['referencia_type' => Confeccion::class,'referencia_id' => $conf->id,'cantidad' => 40]);
|
Recepcion::create(['referencia_type' => Confeccion::class,'referencia_id' => $conf->id,'cantidad' => 40]);
|
||||||
|
Recepcion::create(['referencia_type' => Confeccion::class,'referencia_id' => $conf->id,'cantidad' => 40, 'prendas_defectuosas' => 10]);
|
||||||
|
|
||||||
$conf->refresh();
|
$conf->refresh();
|
||||||
|
|
||||||
$this->assertEquals(80, $conf->recibido_total);
|
// recibido efectivo: 40 + (40 - 10) = 70
|
||||||
$this->assertEquals(20, $conf->faltantes);
|
$this->assertEquals(70, $conf->recibido_total);
|
||||||
|
$this->assertEquals(30, $conf->faltantes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user