diff --git a/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php b/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php new file mode 100644 index 0000000000..4893c4b166 --- /dev/null +++ b/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php @@ -0,0 +1,80 @@ +id(); + $table->string('order_number')->nullable(); + $table->unsignedBigInteger('supplier_id')->nullable(); + // Parent-level FMCS scope — a PO belongs to the company that + // placed it. Same convention as accessories.company_id / + // consumables.company_id / etc. so CompanyableScope hooks + // through cleanly if / when this model opts into it. + $table->unsignedBigInteger('company_id')->nullable(); + $table->date('purchase_date')->nullable(); + $table->unsignedBigInteger('created_by')->nullable(); + $table->text('notes')->nullable(); + $table->timestamps(); + $table->softDeletes(); + + // Free-text search on inventory models hits order_number + // through the polymorphic order_items relation, so the join + // needs a fast index. Same for the supplier and company + // filters used by FMCS scoping. + $table->index('order_number'); + $table->index('supplier_id'); + $table->index('company_id'); + $table->index('created_by'); + }); + + Schema::create('order_items', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('order_id'); + // Polymorphic pointer at the inventory row this line refers + // to. Uses the same shape as action_logs.item_type / + // action_logs.item_id so the two tables read consistently. + $table->string('item_type'); + $table->unsignedBigInteger('item_id'); + $table->integer('qty')->default(1); + // (20,4) matches the widest precision already in use in the + // codebase (components.purchase_cost). Covers 3-decimal + // currencies and low-per-unit fractional prices without + // rounding drift on aggregate totals. + $table->decimal('price', 20, 4)->nullable(); + $table->timestamps(); + + $table->index('order_id'); + $table->index(['item_type', 'item_id']); + }); + } + + public function down(): void + { + Schema::dropIfExists('order_items'); + Schema::dropIfExists('orders'); + } +};