您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TransactionFactory.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Models\Accounting\Transaction;
  4. use Illuminate\Database\Eloquent\Factories\Factory;
  5. /**
  6. * @extends Factory<Transaction>
  7. */
  8. class TransactionFactory extends Factory
  9. {
  10. /**
  11. * Define the model's default state.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function definition(): array
  16. {
  17. return [
  18. 'company_id' => 1,
  19. 'account_id' => $this->faker->numberBetween(1, 30),
  20. 'bank_account_id' => 1,
  21. 'type' => $this->faker->randomElement(['deposit', 'withdrawal', 'journal']),
  22. 'payment_channel' => $this->faker->randomElement(['online', 'in store', 'other']),
  23. 'description' => $this->faker->sentence,
  24. 'notes' => $this->faker->paragraph,
  25. 'reference' => $this->faker->word,
  26. 'amount' => $this->faker->numberBetween(100, 5000),
  27. 'pending' => $this->faker->boolean,
  28. 'reviewed' => $this->faker->boolean,
  29. 'posted_at' => $this->faker->dateTimeBetween('-2 years'),
  30. 'created_by' => 1,
  31. 'updated_by' => 1,
  32. ];
  33. }
  34. public function configure(): static
  35. {
  36. return $this->afterCreating(function (Transaction $transaction) {
  37. $chartAccount = $transaction->account;
  38. $bankAccount = $transaction->bankAccount->account;
  39. $debitAccount = $transaction->type->isWithdrawal() ? $chartAccount : $bankAccount;
  40. $creditAccount = $transaction->type->isWithdrawal() ? $bankAccount : $chartAccount;
  41. if ($debitAccount === null || $creditAccount === null) {
  42. return;
  43. }
  44. $debitAccount->journalEntries()->create([
  45. 'company_id' => $transaction->company_id,
  46. 'transaction_id' => $transaction->id,
  47. 'type' => 'debit',
  48. 'amount' => $transaction->amount,
  49. 'description' => $transaction->description,
  50. 'created_by' => $transaction->created_by,
  51. 'updated_by' => $transaction->updated_by,
  52. ]);
  53. $creditAccount->journalEntries()->create([
  54. 'company_id' => $transaction->company_id,
  55. 'transaction_id' => $transaction->id,
  56. 'type' => 'credit',
  57. 'amount' => $transaction->amount,
  58. 'description' => $transaction->description,
  59. 'created_by' => $transaction->created_by,
  60. 'updated_by' => $transaction->updated_by,
  61. ]);
  62. });
  63. }
  64. }