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.

ProcessTransactionImport.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Accounting\Account;
  4. use App\Models\Banking\BankAccount;
  5. use App\Models\Banking\ConnectedBankAccount;
  6. use App\Models\Company;
  7. use App\Services\PlaidService;
  8. use App\Services\TransactionService;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Queue\Queueable;
  11. use Illuminate\Support\Carbon;
  12. class ProcessTransactionImport implements ShouldQueue
  13. {
  14. use Queueable;
  15. public function __construct(
  16. protected Company $company,
  17. protected Account $account,
  18. protected BankAccount $bankAccount,
  19. protected ConnectedBankAccount $connectedBankAccount,
  20. protected string $startDate
  21. ) {}
  22. /**
  23. * Execute the job.
  24. */
  25. public function handle(PlaidService $plaid, TransactionService $transactionService): void
  26. {
  27. $accessToken = $this->connectedBankAccount->access_token;
  28. $endDate = Carbon::now()->toDateString();
  29. $startDate = Carbon::parse($this->startDate)->toDateString();
  30. $allTransactions = [];
  31. $offset = 0;
  32. $transactionsResponse = $plaid->getTransactions($accessToken, $startDate, $endDate, [
  33. 'account_ids' => [$this->connectedBankAccount->external_account_id],
  34. ]);
  35. $allTransactions = [...$allTransactions, ...$transactionsResponse->transactions];
  36. $totalTransactions = $transactionsResponse->total_transactions;
  37. while (count($allTransactions) < $totalTransactions) {
  38. $offset += count($transactionsResponse->transactions);
  39. $transactionsResponse = $plaid->getTransactions($accessToken, $startDate, $endDate, [
  40. 'account_ids' => [$this->connectedBankAccount->external_account_id],
  41. 'offset' => $offset,
  42. ]);
  43. $allTransactions = [...$allTransactions, ...$transactionsResponse->transactions];
  44. }
  45. $existingTransactionIds = $this->bankAccount->transactions->pluck('plaid_transaction_id')->toArray();
  46. $newTransactions = array_filter($allTransactions, static function ($transaction) use ($existingTransactionIds) {
  47. return ! in_array($transaction->transaction_id, $existingTransactionIds) && $transaction->pending === false;
  48. });
  49. if (count($newTransactions) > 0) {
  50. $currentBalance = $transactionsResponse->accounts[0]->balances->current;
  51. $transactionService->createStartingBalanceIfNeeded($this->company, $this->bankAccount, $newTransactions, $currentBalance, $startDate);
  52. $transactionService->storeTransactions($this->company, $this->bankAccount, $newTransactions);
  53. $this->connectedBankAccount->update([
  54. 'last_imported_at' => Carbon::now(),
  55. ]);
  56. }
  57. }
  58. }