feat: implement Bundle and BundleItem models, migrations, and polymorphic relationships for cart and purchase items
This commit is contained in:
104
app/Domains/Bundle/Models/Bundle.php
Normal file
104
app/Domains/Bundle/Models/Bundle.php
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Bundle\Models;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'tenant_codigo',
|
||||||
|
'nombre',
|
||||||
|
'descripcion',
|
||||||
|
'precio',
|
||||||
|
])]
|
||||||
|
class Bundle extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'bundles';
|
||||||
|
|
||||||
|
protected $appends = ['stock_tecnico'];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'precio' => 'decimal:2',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsTo<Tenant, $this>
|
||||||
|
*/
|
||||||
|
public function tenant(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return HasMany<BundleItem, $this>
|
||||||
|
*/
|
||||||
|
public function items(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(BundleItem::class, 'bundle_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function incrementReservedStock(int $amount): void
|
||||||
|
{
|
||||||
|
if ($amount < 0) {
|
||||||
|
throw new \InvalidArgumentException('El monto a incrementar debe ser positivo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
$item->variant->incrementReservedStock($amount * $item->cantidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function decrementReservedStock(int $amount): void
|
||||||
|
{
|
||||||
|
if ($amount < 0) {
|
||||||
|
throw new \InvalidArgumentException('El monto a decrementar debe ser positivo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
$item->variant->decrementReservedStock($amount * $item->cantidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function confirmReservedStock(int $amount): void
|
||||||
|
{
|
||||||
|
if ($amount < 0) {
|
||||||
|
throw new \InvalidArgumentException('El monto a confirmar debe ser positivo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
$item->variant->confirmReservedStock($amount * $item->cantidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function stockTecnico(): Attribute
|
||||||
|
{
|
||||||
|
return Attribute::get(function () {
|
||||||
|
if ($this->items->isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$minStock = PHP_INT_MAX;
|
||||||
|
foreach ($this->items as $item) {
|
||||||
|
// Ensure variant is loaded
|
||||||
|
if ($item->variant) {
|
||||||
|
$itemStock = floor($item->variant->stock_tecnico / $item->cantidad);
|
||||||
|
if ($itemStock < $minStock) {
|
||||||
|
$minStock = $itemStock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $minStock === PHP_INT_MAX ? 0 : (int) $minStock;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
46
app/Domains/Bundle/Models/BundleItem.php
Normal file
46
app/Domains/Bundle/Models/BundleItem.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Bundle\Models;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Models\ProductVariant;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'bundle_id',
|
||||||
|
'producto_variante_id',
|
||||||
|
'cantidad',
|
||||||
|
])]
|
||||||
|
class BundleItem extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'bundle_items';
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'bundle_id' => 'integer',
|
||||||
|
'producto_variante_id' => 'integer',
|
||||||
|
'cantidad' => 'integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsTo<Bundle, $this>
|
||||||
|
*/
|
||||||
|
public function bundle(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Bundle::class, 'bundle_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsTo<ProductVariant, $this>
|
||||||
|
*/
|
||||||
|
public function variant(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(ProductVariant::class, 'producto_variante_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'cart_id',
|
'cart_id',
|
||||||
'producto_variante_id',
|
'buyable_id',
|
||||||
|
'buyable_type',
|
||||||
'cantidad',
|
'cantidad',
|
||||||
])]
|
])]
|
||||||
class CartItem extends Model
|
class CartItem extends Model
|
||||||
@@ -23,7 +24,8 @@ class CartItem extends Model
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'cart_id' => 'integer',
|
'cart_id' => 'integer',
|
||||||
'producto_variante_id' => 'integer',
|
'buyable_id' => 'integer',
|
||||||
|
'buyable_type' => 'string',
|
||||||
'cantidad' => 'integer',
|
'cantidad' => 'integer',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -37,10 +39,10 @@ class CartItem extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return BelongsTo<ProductVariant, $this>
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||||
*/
|
*/
|
||||||
public function variant(): BelongsTo
|
public function buyable()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(ProductVariant::class, 'producto_variante_id');
|
return $this->morphTo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'compra_id',
|
'compra_id',
|
||||||
'producto_variante_id',
|
'buyable_id',
|
||||||
|
'buyable_type',
|
||||||
'cantidad',
|
'cantidad',
|
||||||
'precio_unitario',
|
'precio_unitario',
|
||||||
'discount_total',
|
'discount_total',
|
||||||
@@ -27,7 +28,8 @@ class PurchaseItem extends Model
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'compra_id' => 'integer',
|
'compra_id' => 'integer',
|
||||||
'producto_variante_id' => 'integer',
|
'buyable_id' => 'integer',
|
||||||
|
'buyable_type' => 'string',
|
||||||
'cantidad' => 'integer',
|
'cantidad' => 'integer',
|
||||||
'precio_unitario' => 'decimal:2',
|
'precio_unitario' => 'decimal:2',
|
||||||
'discount_total' => 'decimal:2',
|
'discount_total' => 'decimal:2',
|
||||||
@@ -45,10 +47,10 @@ class PurchaseItem extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return BelongsTo<ProductVariant, $this>
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||||
*/
|
*/
|
||||||
public function variant(): BelongsTo
|
public function buyable()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(ProductVariant::class, 'producto_variante_id');
|
return $this->morphTo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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('bundles', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('tenant_codigo', 10);
|
||||||
|
$table->string('nombre');
|
||||||
|
$table->text('descripcion')->nullable();
|
||||||
|
$table->decimal('precio', 10, 2);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('tenant_codigo')->references('codigo')->on('tenants')->onDelete('cascade');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('bundles');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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('bundle_items', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('bundle_id')->constrained('bundles')->onDelete('cascade');
|
||||||
|
$table->foreignId('producto_variante_id')->constrained('productos_variantes')->onDelete('cascade');
|
||||||
|
$table->integer('cantidad');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('bundle_items');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?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::table('carrito_items', function (Blueprint $table) {
|
||||||
|
$table->string('buyable_type')->nullable();
|
||||||
|
$table->unsignedBigInteger('buyable_id')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Copy existing data
|
||||||
|
\Illuminate\Support\Facades\DB::table('carrito_items')->update([
|
||||||
|
'buyable_type' => 'App\Domains\Catalog\Models\ProductVariant',
|
||||||
|
'buyable_id' => \Illuminate\Support\Facades\DB::raw('producto_variante_id'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('carrito_items', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['producto_variante_id']);
|
||||||
|
$table->dropColumn('producto_variante_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('carrito_items', function (Blueprint $table) {
|
||||||
|
$table->foreignId('producto_variante_id')->nullable()->constrained('productos_variantes')->onDelete('cascade');
|
||||||
|
});
|
||||||
|
|
||||||
|
\Illuminate\Support\Facades\DB::table('carrito_items')->update([
|
||||||
|
'producto_variante_id' => \Illuminate\Support\Facades\DB::raw('buyable_id'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('carrito_items', function (Blueprint $table) {
|
||||||
|
$table->dropColumn(['buyable_type', 'buyable_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?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::table('compra_items', function (Blueprint $table) {
|
||||||
|
$table->string('buyable_type')->nullable();
|
||||||
|
$table->unsignedBigInteger('buyable_id')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Copy existing data
|
||||||
|
\Illuminate\Support\Facades\DB::table('compra_items')->update([
|
||||||
|
'buyable_type' => 'App\Domains\Catalog\Models\ProductVariant',
|
||||||
|
'buyable_id' => \Illuminate\Support\Facades\DB::raw('producto_variante_id'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('compra_items', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['producto_variante_id']);
|
||||||
|
$table->dropColumn('producto_variante_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('compra_items', function (Blueprint $table) {
|
||||||
|
$table->foreignId('producto_variante_id')->nullable()->constrained('productos_variantes')->onDelete('cascade');
|
||||||
|
});
|
||||||
|
|
||||||
|
\Illuminate\Support\Facades\DB::table('compra_items')->update([
|
||||||
|
'producto_variante_id' => \Illuminate\Support\Facades\DB::raw('buyable_id'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('compra_items', function (Blueprint $table) {
|
||||||
|
$table->dropColumn(['buyable_type', 'buyable_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user