up
This commit is contained in:
@@ -59,18 +59,18 @@ class TelaResource extends Resource
|
||||
}
|
||||
}),
|
||||
|
||||
TextInput::make('valor_total')->numeric()->required()->prefix('$')->live(onBlur: true)
|
||||
TextInput::make('valor_total')->numeric()->required()->prefix('$')->step(0.01)->live(onBlur: true)
|
||||
->afterStateUpdated(function ($state, Forms\Set $set, $get) {
|
||||
$metros_comprados = $get('metros_comprados');
|
||||
$valor_total = $state;
|
||||
|
||||
if ($metros_comprados && $valor_total) {
|
||||
$costo_por_metro = $valor_total / $metros_comprados;
|
||||
$set('costo_por_metro', $costo_por_metro);
|
||||
$set('costo_por_metro', round($costo_por_metro, 2));
|
||||
}
|
||||
}),
|
||||
|
||||
TextInput::make('costo_por_metro')->numeric()->prefix('$')->readOnly()->dehydrated()->default(0),
|
||||
TextInput::make('costo_por_metro')->numeric()->prefix('$')->readOnly()->dehydrated()->default(0)->step(0.01),
|
||||
|
||||
TextInput::make('metros_disponibles')->numeric()->reactive()->disabled()->dehydrated()
|
||||
->default(function ($get) {
|
||||
|
||||
@@ -22,6 +22,8 @@ class Tela extends Model
|
||||
|
||||
protected $casts = [
|
||||
'foto' => 'string',
|
||||
'valor_total' => 'decimal:2',
|
||||
'costo_por_metro' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function proveedor()
|
||||
@@ -33,4 +35,24 @@ class Tela extends Model
|
||||
{
|
||||
return $this->hasMany(OrdenProduccion::class);
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::saving(function ($tela) {
|
||||
// Protección temporal: si la columna en la BD aún es integer, convertir a entero
|
||||
try {
|
||||
$type = \Illuminate\Support\Facades\Schema::getColumnType('telas', 'costo_por_metro');
|
||||
if ($type === 'integer') {
|
||||
$tela->costo_por_metro = (int) round($tela->costo_por_metro ?? 0);
|
||||
}
|
||||
|
||||
$typeVal = \Illuminate\Support\Facades\Schema::getColumnType('telas', 'valor_total');
|
||||
if ($typeVal === 'integer') {
|
||||
$tela->valor_total = (int) round($tela->valor_total ?? 0);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Ignorar si no se puede determinar el tipo de columna (por ejemplo en pruebas antes de migraciones)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('telas', function (Blueprint $table) {
|
||||
// Cambiar a decimal para permitir valores con centavos
|
||||
$table->decimal('valor_total', 12, 2)->change();
|
||||
$table->decimal('costo_por_metro', 12, 2)->change();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('telas', function (Blueprint $table) {
|
||||
$table->integer('valor_total')->change();
|
||||
$table->integer('costo_por_metro')->change();
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user