SparePartOrderFactory.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 'spare_part_id' => SparePart::factory(),
  18. 'source_text' => fake()->company(),
  19. 'status' => SparePartOrder::STATUS_IN_STOCK,
  20. 'ordered_quantity' => $quantity,
  21. 'available_qty' => $quantity,
  22. 'with_documents' => fake()->boolean(),
  23. 'note' => fake()->optional()->sentence(),
  24. 'user_id' => User::factory(),
  25. ];
  26. }
  27. public function ordered(): static
  28. {
  29. return $this->state(fn (array $attributes) => [
  30. 'status' => SparePartOrder::STATUS_ORDERED,
  31. ]);
  32. }
  33. public function inStock(): static
  34. {
  35. return $this->state(fn (array $attributes) => [
  36. 'status' => SparePartOrder::STATUS_IN_STOCK,
  37. ]);
  38. }
  39. public function shipped(): static
  40. {
  41. return $this->state(fn (array $attributes) => [
  42. 'status' => SparePartOrder::STATUS_SHIPPED,
  43. 'available_qty' => 0,
  44. ]);
  45. }
  46. public function withDocuments(bool $withDocs = true): static
  47. {
  48. return $this->state(fn (array $attributes) => [
  49. 'with_documents' => $withDocs,
  50. ]);
  51. }
  52. public function withQuantity(int $quantity): static
  53. {
  54. return $this->state(fn (array $attributes) => [
  55. 'ordered_quantity' => $quantity,
  56. 'available_qty' => $quantity,
  57. ]);
  58. }
  59. public function forSparePart(SparePart $sparePart): static
  60. {
  61. return $this->state(fn (array $attributes) => [
  62. 'spare_part_id' => $sparePart->id,
  63. ]);
  64. }
  65. }