tablas y modelos
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Appointment extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
// Así podrás hacer cosas como $turno->fecha_programada->format('d/m/Y')
|
||||
protected $casts = [
|
||||
'fecha_programada' => 'datetime',
|
||||
];
|
||||
public function clients()
|
||||
{
|
||||
return $this->belongsTo(Client::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Client extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
// Un usuario (cliente) realiza muchas compras (ventas)
|
||||
public function sales()
|
||||
{
|
||||
return $this->hasMany(Sale::class);
|
||||
}
|
||||
}
|
||||
+29
-7
@@ -9,13 +9,35 @@ class Product extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'codigo',
|
||||
'nombre',
|
||||
'marca',
|
||||
'modelo',
|
||||
'rodado',
|
||||
'color',
|
||||
'tipo',
|
||||
'descripcion',
|
||||
'precio'
|
||||
'costo',
|
||||
'precio',
|
||||
'stock',
|
||||
'min_stock',
|
||||
'categoria',
|
||||
'descripcion'
|
||||
];
|
||||
public function supplier()
|
||||
{
|
||||
return $this->belongsTo(Suppliers::class);
|
||||
}
|
||||
|
||||
// Opción 1: Relación directa con los detalles (Renglones de ticket)
|
||||
// Útil para saber cantidad total vendida: $product->saleDetails->sum('quantity')
|
||||
public function saleDetails()
|
||||
{
|
||||
return $this->hasMany(SaleDetail::class);
|
||||
}
|
||||
|
||||
// Opción 2 (Pro Tip): Relación directa con las Ventas a través de los detalles
|
||||
// Útil para saber EN QUÉ fechas se vendió: $product->sales
|
||||
public function sales()
|
||||
{
|
||||
return $this->belongsToMany(Sale::class, 'sale_details');
|
||||
}
|
||||
|
||||
}
|
||||
/**$user->sales: Te da la lista de todas las compras de ese usuario.
|
||||
|
||||
$product->saleDetails->count(): Te dice cuántas veces aparece ese producto en tickets.**/
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Sale extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'metodo_pago'
|
||||
];
|
||||
|
||||
// Permitimos asignación masiva para poder guardar rápido
|
||||
protected $guarded = [];
|
||||
|
||||
// Relación 1: Una venta pertenece a un Cliente (User)
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// Relación 2: Una venta tiene muchos items o detalles
|
||||
public function details()
|
||||
{
|
||||
return $this->hasMany(SaleDetail::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SaleDetail extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $guarded = [];
|
||||
|
||||
// este detalle pertenece a una Venta específica
|
||||
public function sale()
|
||||
{
|
||||
return $this->belongsTo(Sale::class);
|
||||
}
|
||||
|
||||
// este detalle corresponde a un Producto
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Supplier extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'nombre',
|
||||
'telefono',
|
||||
'email'
|
||||
];
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('suppliers', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('nombre');
|
||||
$table->string('telefono');
|
||||
$table->string('email');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('suppliers');
|
||||
}
|
||||
};
|
||||
+8
-7
@@ -13,14 +13,15 @@ return new class extends Migration
|
||||
{
|
||||
Schema::create('products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('codigo')->unique();
|
||||
$table->string('nombre');
|
||||
$table->string('marca');
|
||||
$table->string('modelo');
|
||||
$table->string('rodado');
|
||||
$table->string('color');
|
||||
$table->string('tipo');
|
||||
$table->decimal('costo', 12, 2);
|
||||
$table->decimal('precio', 12, 2);
|
||||
$table->integer('stock');
|
||||
$table->integer('min_stock')->default(5);
|
||||
$table->string('categoria');
|
||||
$table->text('descripcion');
|
||||
$table->float('precio');
|
||||
$table->foreignId('suppliers_id')->constrained();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
@@ -32,4 +33,4 @@ return new class extends Migration
|
||||
{
|
||||
Schema::dropIfExists('products');
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('clients', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('nombre');
|
||||
$table->string('telefono');
|
||||
$table->string('email');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('clients');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('sales', function (Blueprint $table) {
|
||||
$table->id();
|
||||
// 1. Cliente que compra
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
// 2. Datos generales de la venta
|
||||
$table->decimal('total', 10, 2); // Total final del ticket
|
||||
$table->string('metodo_pago'); // Ej: "Efectivo", "Tarjeta", "MercadoPago"
|
||||
//capaz van mas datos ni idea
|
||||
// 3. Fecha y Hora (created_at servirá como fecha de venta)
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('sales');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('sale_details', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('sale_id')->constrained()->onDelete('cascade');
|
||||
$table->foreignId('product_id')->constrained();
|
||||
$table->integer('cantidad');
|
||||
$table->decimal('precio', 10, 2);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('sale_details');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('appointments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('client_id')->constrained();
|
||||
$table->datetime('fecha_programada');
|
||||
$table->string('modelo_bici')->nullable();
|
||||
$table->string('descripcion')->nullable();
|
||||
$table->string('estado')->default('pendiente');
|
||||
$table->string('notas')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('appointments');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user