Andrew Wallo 4 months ago
parent
commit
ba4e637152
2 changed files with 7 additions and 7 deletions
  1. 1
    1
      app/Jobs/ProcessTransactionImport.php
  2. 6
    6
      app/Services/TransactionService.php

+ 1
- 1
app/Jobs/ProcessTransactionImport.php View File

60
         if (count($newTransactions) > 0) {
60
         if (count($newTransactions) > 0) {
61
             $currentBalance = $transactionsResponse->accounts[0]->balances->current;
61
             $currentBalance = $transactionsResponse->accounts[0]->balances->current;
62
 
62
 
63
-            $transactionService->createStartingBalanceIfNeeded($this->company, $this->account, $this->bankAccount, $newTransactions, $currentBalance, $startDate);
63
+            $transactionService->createStartingBalanceIfNeeded($this->company, $this->bankAccount, $newTransactions, $currentBalance, $startDate);
64
             $transactionService->storeTransactions($this->company, $this->bankAccount, $newTransactions);
64
             $transactionService->storeTransactions($this->company, $this->bankAccount, $newTransactions);
65
 
65
 
66
             $this->connectedBankAccount->update([
66
             $this->connectedBankAccount->update([

+ 6
- 6
app/Services/TransactionService.php View File

18
 
18
 
19
 class TransactionService
19
 class TransactionService
20
 {
20
 {
21
-    public function createStartingBalanceIfNeeded(Company $company, Account $account, BankAccount $bankAccount, array $transactions, float $currentBalance, string $startDate): void
21
+    public function createStartingBalanceIfNeeded(Company $company, BankAccount $bankAccount, array $transactions, float $currentBalance, string $startDate): void
22
     {
22
     {
23
-        if ($account->transactions()->doesntExist()) {
23
+        if ($bankAccount->transactions()->doesntExist()) {
24
+            $account = $bankAccount->account;
24
             $accountSign = $account->category === AccountCategory::Asset ? 1 : -1;
25
             $accountSign = $account->category === AccountCategory::Asset ? 1 : -1;
25
 
26
 
26
             $sumOfTransactions = collect($transactions)->reduce(static function ($carry, $transaction) {
27
             $sumOfTransactions = collect($transactions)->reduce(static function ($carry, $transaction) {
31
 
32
 
32
             $startingBalance = bcsub($adjustedBalance, $sumOfTransactions, 2);
33
             $startingBalance = bcsub($adjustedBalance, $sumOfTransactions, 2);
33
 
34
 
34
-            $currencyCode = $bankAccount->account->currency_code ?? 'USD';
35
-
36
-            $this->createStartingBalanceTransaction($company, $account, $bankAccount, (float) $startingBalance, $startDate, $currencyCode);
35
+            $this->createStartingBalanceTransaction($company, $bankAccount, (float) $startingBalance, $startDate);
37
         }
36
         }
38
     }
37
     }
39
 
38
 
44
         }
43
         }
45
     }
44
     }
46
 
45
 
47
-    public function createStartingBalanceTransaction(Company $company, Account $account, BankAccount $bankAccount, float $startingBalance, string $startDate, string $currencyCode): void
46
+    public function createStartingBalanceTransaction(Company $company, BankAccount $bankAccount, float $startingBalance, string $startDate): void
48
     {
47
     {
49
         $transactionType = $startingBalance >= 0 ? TransactionType::Deposit : TransactionType::Withdrawal;
48
         $transactionType = $startingBalance >= 0 ? TransactionType::Deposit : TransactionType::Withdrawal;
50
         $accountName = $startingBalance >= 0 ? "Owner's Investment" : "Owner's Drawings";
49
         $accountName = $startingBalance >= 0 ? "Owner's Investment" : "Owner's Drawings";
55
 
54
 
56
         $postedAt = Carbon::parse($startDate)->subDay()->toDateTimeString();
55
         $postedAt = Carbon::parse($startDate)->subDay()->toDateTimeString();
57
 
56
 
57
+        $currencyCode = $bankAccount->account->currency_code ?? 'USD';
58
         $amountInCents = CurrencyConverter::convertToCents(abs($startingBalance), $currencyCode);
58
         $amountInCents = CurrencyConverter::convertToCents(abs($startingBalance), $currencyCode);
59
 
59
 
60
         Transaction::create([
60
         Transaction::create([

Loading…
Cancel
Save