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.

BankAccountService.php 1.1KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Banking\BankAccount;
  4. use App\Models\Banking\ConnectedBankAccount;
  5. use App\Models\Company;
  6. class BankAccountService
  7. {
  8. public function getOrProcessBankAccount(Company $company, ConnectedBankAccount $connectedBankAccount, int | string $selectedBankAccountId): BankAccount
  9. {
  10. if ($selectedBankAccountId === 'new') {
  11. return $this->processNewBankAccount($company, $connectedBankAccount);
  12. }
  13. return $company->bankAccounts()->find($selectedBankAccountId);
  14. }
  15. protected function processNewBankAccount(Company $company, ConnectedBankAccount $connectedBankAccount): BankAccount
  16. {
  17. return $connectedBankAccount->bankAccount()->create([
  18. 'company_id' => $company->id,
  19. 'institution_id' => $connectedBankAccount->institution_id,
  20. 'type' => $connectedBankAccount->type,
  21. 'number' => $connectedBankAccount->mask,
  22. 'enabled' => BankAccount::where('company_id', $company->id)->where('enabled', true)->doesntExist(),
  23. ]);
  24. }
  25. }