diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 6535c43106..a9aaff4f8b 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -38,8 +38,8 @@ class AccessoryFactory extends Factory 'category_id' => Category::factory()->forAccessories(), 'model_number' => $this->faker->numberBetween(1000000, 50000000), 'location_id' => Location::factory(), - 'purchase_cost' => $this->faker->randomFloat(2, 5, 250), - 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), + 'default_purchase_cost' => $this->faker->randomFloat(2, 5, 250), + 'legacy_purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), 'qty' => 1, ]; } @@ -58,7 +58,7 @@ class AccessoryFactory extends Factory }, 'qty' => 10, 'min_amt' => 2, - 'supplier_id' => Supplier::factory(), + 'default_supplier_id' => Supplier::factory(), ]; }); } @@ -77,7 +77,7 @@ class AccessoryFactory extends Factory }, 'qty' => 15, 'min_amt' => 2, - 'supplier_id' => Supplier::factory(), + 'default_supplier_id' => Supplier::factory(), ]; }); } @@ -96,7 +96,7 @@ class AccessoryFactory extends Factory }, 'qty' => 13, 'min_amt' => 2, - 'supplier_id' => Supplier::factory(), + 'default_supplier_id' => Supplier::factory(), ]; }); } @@ -206,4 +206,35 @@ class AccessoryFactory extends Factory ]); }); } + + /** + * See ComponentFactory::withInitialAcquisition for docs. + */ + public function withInitialAcquisition( + ?Supplier $supplier = null, + ?float $unitCost = null, + ?string $purchaseDate = null, + ) { + return $this->afterCreating(function (Accessory $accessory) use ($supplier, $unitCost, $purchaseDate) { + $line = $accessory->orderItems()->latest('id')->first(); + if (! $line) { + return; + } + if ($unitCost !== null) { + $line->price = $unitCost; + $line->save(); + } + $order = $line->order; + if (! $order) { + return; + } + if ($supplier !== null) { + $order->supplier_id = $supplier->id; + } + if ($purchaseDate !== null) { + $order->purchase_date = Carbon::parse($purchaseDate); + } + $order->save(); + }); + } } diff --git a/database/factories/ComponentFactory.php b/database/factories/ComponentFactory.php index ed639c2cf8..488c1178e6 100644 --- a/database/factories/ComponentFactory.php +++ b/database/factories/ComponentFactory.php @@ -12,6 +12,7 @@ use App\Models\Supplier; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Carbon as SupportCarbon; class ComponentFactory extends Factory { @@ -38,11 +39,11 @@ class ComponentFactory extends Factory 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), 'model_number' => $this->faker->numberBetween(1000000, 50000000), 'name' => $this->faker->text(20), - 'purchase_cost' => $this->faker->randomFloat(2), - 'purchase_date' => $this->faker->dateTime()->format('Y-m-d'), + 'default_purchase_cost' => $this->faker->randomFloat(2), + 'legacy_purchase_date' => $this->faker->dateTime()->format('Y-m-d'), 'qty' => $this->faker->numberBetween(3, 10), 'serial' => $this->faker->uuid(), - 'supplier_id' => Supplier::factory(), + 'default_supplier_id' => Supplier::factory(), ]; } @@ -126,4 +127,40 @@ class ComponentFactory extends Factory ]); }); } + + /** + * Populate the observer-written initial Order + OrderItem with + * acquisition metadata (supplier / unit_cost / purchase_date) — + * simulating what the create form's `enrichInitialOrderFromRequest` + * writes when the user fills those fields at create time. Lets + * tests assert Order-level state without going through the HTTP + * controller. + */ + public function withInitialAcquisition( + ?Supplier $supplier = null, + ?float $unitCost = null, + ?string $purchaseDate = null, + ) { + return $this->afterCreating(function (Component $component) use ($supplier, $unitCost, $purchaseDate) { + $line = $component->orderItems()->latest('id')->first(); + if (! $line) { + return; + } + if ($unitCost !== null) { + $line->price = $unitCost; + $line->save(); + } + $order = $line->order; + if (! $order) { + return; + } + if ($supplier !== null) { + $order->supplier_id = $supplier->id; + } + if ($purchaseDate !== null) { + $order->purchase_date = SupportCarbon::parse($purchaseDate); + } + $order->save(); + }); + } } diff --git a/database/factories/ConsumableFactory.php b/database/factories/ConsumableFactory.php index b6849ae3ef..d6c338dd20 100644 --- a/database/factories/ConsumableFactory.php +++ b/database/factories/ConsumableFactory.php @@ -34,10 +34,10 @@ class ConsumableFactory extends Factory 'item_no' => $this->faker->numberBetween(1000000, 50000000), 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), 'name' => $this->faker->words(3, true), - 'purchase_cost' => $this->faker->randomFloat(2, 1, 50), - 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), + 'default_purchase_cost' => $this->faker->randomFloat(2, 1, 50), + 'legacy_purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), 'qty' => $this->faker->numberBetween(5, 10), - 'supplier_id' => Supplier::factory(), + 'default_supplier_id' => Supplier::factory(), ]; } @@ -135,4 +135,35 @@ class ConsumableFactory extends Factory ]); }); } + + /** + * See ComponentFactory::withInitialAcquisition for docs. + */ + public function withInitialAcquisition( + ?Supplier $supplier = null, + ?float $unitCost = null, + ?string $purchaseDate = null, + ) { + return $this->afterCreating(function (Consumable $consumable) use ($supplier, $unitCost, $purchaseDate) { + $line = $consumable->orderItems()->latest('id')->first(); + if (! $line) { + return; + } + if ($unitCost !== null) { + $line->price = $unitCost; + $line->save(); + } + $order = $line->order; + if (! $order) { + return; + } + if ($supplier !== null) { + $order->supplier_id = $supplier->id; + } + if ($purchaseDate !== null) { + $order->purchase_date = Carbon::parse($purchaseDate); + } + $order->save(); + }); + } }