You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VendorFactory.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Database\Factories\Common;
  3. use App\Enums\Common\ContractorType;
  4. use App\Enums\Common\VendorType;
  5. use App\Models\Common\Address;
  6. use App\Models\Common\Contact;
  7. use App\Models\Common\Vendor;
  8. use App\Models\Company;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. /**
  11. * @extends Factory<Vendor>
  12. */
  13. class VendorFactory extends Factory
  14. {
  15. /**
  16. * The name of the factory's corresponding model.
  17. */
  18. protected $model = Vendor::class;
  19. /**
  20. * Define the model's default state.
  21. *
  22. * @return array<string, mixed>
  23. */
  24. public function definition(): array
  25. {
  26. return [
  27. 'company_id' => 1,
  28. 'name' => $this->faker->company,
  29. 'type' => $this->faker->randomElement(VendorType::cases()),
  30. 'contractor_type' => function (array $attributes) {
  31. return $attributes['type'] === VendorType::Contractor ? $this->faker->randomElement(ContractorType::cases()) : null;
  32. },
  33. 'ssn' => function (array $attributes) {
  34. return $attributes['contractor_type'] === ContractorType::Individual ? $this->faker->numerify(str_repeat('#', 9)) : null;
  35. },
  36. 'ein' => function (array $attributes) {
  37. return $attributes['contractor_type'] === ContractorType::Business ? $this->faker->numerify(str_repeat('#', 9)) : null;
  38. },
  39. 'currency_code' => fn (array $attributes) => Company::find($attributes['company_id'])->default->currency_code ?? 'USD',
  40. 'account_number' => $this->faker->unique()->numerify(str_repeat('#', 12)),
  41. 'website' => $this->faker->url,
  42. 'notes' => $this->faker->sentence,
  43. 'created_by' => 1,
  44. 'updated_by' => 1,
  45. ];
  46. }
  47. public function regular(): self
  48. {
  49. return $this->state([
  50. 'type' => VendorType::Regular,
  51. ]);
  52. }
  53. public function contractor(): self
  54. {
  55. return $this->state([
  56. 'type' => VendorType::Contractor,
  57. ]);
  58. }
  59. public function individualContractor(): self
  60. {
  61. return $this->state([
  62. 'type' => VendorType::Contractor,
  63. 'contractor_type' => ContractorType::Individual,
  64. ]);
  65. }
  66. public function businessContractor(): self
  67. {
  68. return $this->state([
  69. 'type' => VendorType::Contractor,
  70. 'contractor_type' => ContractorType::Business,
  71. ]);
  72. }
  73. public function withContact(): self
  74. {
  75. return $this->has(Contact::factory()->primary()->useParentCompany());
  76. }
  77. public function withAddress(): self
  78. {
  79. return $this->has(Address::factory()->general()->useParentCompany());
  80. }
  81. }