feat: Implement inventory policy for product variants

- Introduced InventoryPolicy enum to manage tracked and unlimited inventory types.
- Updated ProductVariant model to include inventory_policy and cantidad_vendida attributes.
- Modified addItem and updateItem methods in Cart to check available quantity based on inventory policy.
- Added migration to include inventory_policy and cantidad_vendida in productos_variantes table.
- Enhanced tests to validate inventory behavior for both tracked and unlimited policies.
- Updated various request and resource classes to handle new inventory fields.
This commit is contained in:
2026-07-16 08:36:59 -03:00
parent 716d8e447c
commit 10157d7c63
16 changed files with 547 additions and 73 deletions

View File

@@ -2,8 +2,12 @@
namespace Tests\Feature\Integration;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Cart\Models\Cart;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Integration\Models\Integration;
@@ -14,6 +18,7 @@ use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Tests\TestCase;
class TelepagosWebhookTest extends TestCase
@@ -24,7 +29,7 @@ class TelepagosWebhookTest extends TestCase
{
parent::setUp();
config(['services.integrations.secret' => 'base64:' . base64_encode(random_bytes(32))]);
config(['services.integrations.secret' => 'base64:'.base64_encode(random_bytes(32))]);
Cache::flush();
}
@@ -113,6 +118,13 @@ class TelepagosWebhookTest extends TestCase
'total' => 50,
]);
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'stock_real' => 9,
'stock_reservado' => 2,
'cantidad_vendida' => 1,
]);
$this->assertDatabaseMissing('compra_items', [
'compra_id' => $newerPurchase->id,
]);
@@ -122,6 +134,60 @@ class TelepagosWebhookTest extends TestCase
]);
}
public function test_transfer_webhook_buys_unlimited_inventory_without_reducing_real_stock(): void
{
$tenant = $this->createTenant('unlimited', 'Unlimited', 'unlimited.com.ar');
$this->configureTelepagosIntegration($tenant);
$user = User::factory()->create();
$variant = $this->createVariantForTenant(
'unlimited',
0,
'50.00',
'service',
InventoryPolicy::Unlimited,
);
$purchase = $this->createPendingTransferPurchase(
$tenant,
$user->id,
$variant->id,
3,
'87654321',
);
Http::fake([
'https://api.telepagos.com.ar/v2/auth/token' => Http::response([
'status' => 'ok',
'token' => 'test-token',
'expires_at' => now()->addHour()->toIso8601String(),
]),
'https://api.telepagos.com.ar/v2/payment/cashin/7000' => Http::response([
'status' => 'ok',
'data' => [
'amount' => 150,
'operation_id' => 1,
'transaction_id' => 'tx-unlimited',
'buyer' => ['cuit' => '20876543219'],
],
]),
]);
$this->postJson('/api/webhooks/telepagos/unlimited', ['id' => '7000'])
->assertOk()
->assertJsonPath('status', 'success');
$this->assertDatabaseHas('compras', [
'id' => $purchase->id,
'status' => Purchase::STATUS_PAID,
]);
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'inventory_policy' => InventoryPolicy::Unlimited->value,
'stock_real' => 0,
'stock_reservado' => 0,
'cantidad_vendida' => 3,
]);
}
private function createPendingTransferPurchase(
Tenant $tenant,
int $userId,
@@ -184,8 +250,9 @@ class TelepagosWebhookTest extends TestCase
int $stock,
string $price,
string $slugPrefix = 'shirt',
InventoryPolicy $inventoryPolicy = InventoryPolicy::Tracked,
): ProductVariant {
$category = \App\Domains\Catalog\Models\Category::query()->create([
$category = Category::query()->create([
'tenant_code' => $tenantCode,
'nombre' => "{$slugPrefix} category {$tenantCode}",
]);
@@ -201,6 +268,7 @@ class TelepagosWebhookTest extends TestCase
return ProductVariant::query()->create([
'producto_id' => $product->id,
'inventory_policy' => $inventoryPolicy->value,
'slug' => "{$slugPrefix}-variant-".ProductVariant::query()->count(),
'nombre' => ucfirst($slugPrefix).' Variant',
'stock' => $stock,
@@ -211,21 +279,21 @@ class TelepagosWebhookTest extends TestCase
private function createTenant(string $codigo, string $nombre, string $dominio): Tenant
{
$hdrKey = (string) \Illuminate\Support\Str::uuid();
$ftrKey = (string) \Illuminate\Support\Str::uuid();
$hdrKey = (string) Str::uuid();
$ftrKey = (string) Str::uuid();
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
$headerAttachment = Attachment::create([
'key' => $hdrKey,
'path' => 'tenants/' . $hdrKey . '.png',
'path' => 'tenants/'.$hdrKey.'.png',
'filename' => 'logo_header.png',
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
$footerAttachment = Attachment::create([
'key' => $ftrKey,
'path' => 'tenants/' . $ftrKey . '.png',
'path' => 'tenants/'.$ftrKey.'.png',
'filename' => 'logo_footer.png',
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);