2026_08_03_000003_create_stock_tables.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. return new class extends Migration
  7. {
  8. public function up(): void
  9. {
  10. Schema::create('stock_orders', function (Blueprint $table): void {
  11. $table->id();
  12. $table->foreignId('common_catalog_item_id')->constrained('common_catalog_items')->restrictOnDelete();
  13. $table->string('order_number');
  14. $table->string('status', 32)->default('ordered');
  15. $table->unsignedInteger('ordered_quantity');
  16. $table->unsignedInteger('available_quantity');
  17. $table->text('note')->nullable();
  18. $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
  19. $table->timestamps();
  20. $table->softDeletes();
  21. $table->unique(['order_number', 'common_catalog_item_id']);
  22. $table->index(['common_catalog_item_id', 'status', 'available_quantity'], 'stock_order_item_status_available_index');
  23. });
  24. Schema::create('stock_reservations', function (Blueprint $table): void {
  25. $table->id();
  26. $table->foreignId('common_catalog_item_id')->constrained('common_catalog_items')->restrictOnDelete();
  27. $table->foreignId('stock_order_id')->constrained('stock_orders')->restrictOnDelete();
  28. $table->foreignId('manager_id')->nullable()->constrained('users')->nullOnDelete();
  29. $table->unsignedInteger('quantity');
  30. $table->string('status', 32)->default('active');
  31. $table->text('note')->nullable();
  32. $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
  33. $table->timestamp('issued_at')->nullable();
  34. $table->timestamp('cancelled_at')->nullable();
  35. $table->timestamps();
  36. $table->index(['common_catalog_item_id', 'status']);
  37. $table->index(['stock_order_id', 'status']);
  38. });
  39. Schema::create('stock_movements', function (Blueprint $table): void {
  40. $table->id();
  41. $table->foreignId('common_catalog_item_id')->constrained('common_catalog_items')->restrictOnDelete();
  42. $table->foreignId('stock_order_id')->nullable()->constrained('stock_orders')->nullOnDelete();
  43. $table->foreignId('stock_reservation_id')->nullable()->constrained('stock_reservations')->nullOnDelete();
  44. $table->string('movement_type', 32);
  45. $table->unsignedInteger('quantity');
  46. $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
  47. $table->text('note')->nullable();
  48. $table->timestamps();
  49. $table->index(['common_catalog_item_id', 'movement_type']);
  50. $table->index(['stock_order_id', 'movement_type']);
  51. });
  52. }
  53. public function down(): void
  54. {
  55. Schema::dropIfExists('stock_movements');
  56. Schema::dropIfExists('stock_reservations');
  57. Schema::dropIfExists('stock_orders');
  58. }
  59. };