SparePartOrderFactory.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\SparePart;
  4. use App\Models\SparePartOrder;
  5. use App\Models\User;
  6. use Illuminate\Database\Eloquent\Factories\Factory;
  7. /**
  8. * @extends Factory<SparePartOrder>
  9. */
  10. class SparePartOrderFactory extends Factory
  11. {
  12. protected $model = SparePartOrder::class;
  13. public function definition(): array
  14. {
  15. $quantity = fake()->numberBetween(5, 100);
  16. return [
  17. 'year' => (int) date('Y'),
  18. 'spare_part_id' => SparePart::factory(),
  19. 'source_text' => fake()->company(),
  20. 'status' => SparePartOrder::STATUS_IN_STOCK,
  21. 'ordered_quantity' => $quantity,
  22. 'available_qty' => $quantity,
  23. 'with_documents' => fake()->boolean(),
  24. 'note' => fake()->optional()->sentence(),
  25. 'user_id' => User::factory(),
  26. ];
  27. }
  28. public function ordered(): static
  29. {
  30. return $this->state(fn (array $attributes) => [
  31. 'status' => SparePartOrder::STATUS_ORDERED,
  32. ]);
  33. }
  34. public function inStock(): static
  35. {
  36. return $this->state(fn (array $attributes) => [
  37. 'status' => SparePartOrder::STATUS_IN_STOCK,
  38. ]);
  39. }
  40. public function shipped(): static
  41. {
  42. return $this->state(fn (array $attributes) => [
  43. 'status' => SparePartOrder::STATUS_SHIPPED,
  44. 'available_qty' => 0,
  45. ]);
  46. }
  47. public function withDocuments(bool $withDocs = true): static
  48. {
  49. return $this->state(fn (array $attributes) => [
  50. 'with_documents' => $withDocs,
  51. ]);
  52. }
  53. public function withQuantity(int $quantity): static
  54. {
  55. return $this->state(fn (array $attributes) => [
  56. 'ordered_quantity' => $quantity,
  57. 'available_qty' => $quantity,
  58. ]);
  59. }
  60. public function forSparePart(SparePart $sparePart): static
  61. {
  62. return $this->state(fn (array $attributes) => [
  63. 'spare_part_id' => $sparePart->id,
  64. ]);
  65. }
  66. }