feat: add ProductVariant model and service layer for CRUD operations with attachment handling
This commit is contained in:
@@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
#[Fillable([
|
||||
'producto_id',
|
||||
'stock',
|
||||
'is_default',
|
||||
'is_placeholder',
|
||||
])]
|
||||
class ProductVariant extends Model
|
||||
{
|
||||
@@ -26,7 +26,7 @@ class ProductVariant extends Model
|
||||
return [
|
||||
'producto_id' => 'integer',
|
||||
'stock' => 'integer',
|
||||
'is_default' => 'boolean',
|
||||
'is_placeholder' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ class ProductService
|
||||
// Create default variant with stock
|
||||
$this->createVariant($product, [
|
||||
'stock' => $stock,
|
||||
'is_default' => true,
|
||||
'is_placeholder' => true,
|
||||
'definitions' => [],
|
||||
]);
|
||||
|
||||
@@ -117,14 +117,14 @@ class ProductService
|
||||
$images = $data['images'] ?? [];
|
||||
unset($data['images']);
|
||||
|
||||
// Determine if the variant being created is a default one
|
||||
// Determine if the variant being created is a placeholder one
|
||||
$hasDefinitions = ! empty($data['definitions']);
|
||||
$isDefault = $data['is_default'] ?? (! $hasDefinitions);
|
||||
$data['is_default'] = $isDefault;
|
||||
$isPlaceholder = $data['is_placeholder'] ?? (! $hasDefinitions);
|
||||
$data['is_placeholder'] = $isPlaceholder;
|
||||
|
||||
// If the variant being created is not a default variant, remove any existing default variants
|
||||
if (! $isDefault) {
|
||||
$defaultVariants = $product->variants()->where('is_default', true)->get();
|
||||
// If the variant being created is not a placeholder variant, remove any existing placeholder variants
|
||||
if (! $isPlaceholder) {
|
||||
$defaultVariants = $product->variants()->where('is_placeholder', true)->get();
|
||||
foreach ($defaultVariants as $defaultVariant) {
|
||||
$this->deleteVariantAttachments($defaultVariant);
|
||||
$product->deleteVariant($defaultVariant);
|
||||
@@ -180,7 +180,7 @@ class ProductService
|
||||
if ($product->variants()->count() === 0) {
|
||||
$product->createVariant([
|
||||
'stock' => 0,
|
||||
'is_default' => true,
|
||||
'is_placeholder' => true,
|
||||
'definitions' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user