|
@@ -18,9 +18,10 @@ use Illuminate\Support\Facades\DB;
|
18
|
18
|
|
19
|
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
|
25
|
$accountSign = $account->category === AccountCategory::Asset ? 1 : -1;
|
25
|
26
|
|
26
|
27
|
$sumOfTransactions = collect($transactions)->reduce(static function ($carry, $transaction) {
|
|
@@ -31,9 +32,7 @@ class TransactionService
|
31
|
32
|
|
32
|
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,7 +43,7 @@ class TransactionService
|
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
|
48
|
$transactionType = $startingBalance >= 0 ? TransactionType::Deposit : TransactionType::Withdrawal;
|
50
|
49
|
$accountName = $startingBalance >= 0 ? "Owner's Investment" : "Owner's Drawings";
|
|
@@ -55,6 +54,7 @@ class TransactionService
|
55
|
54
|
|
56
|
55
|
$postedAt = Carbon::parse($startDate)->subDay()->toDateTimeString();
|
57
|
56
|
|
|
57
|
+ $currencyCode = $bankAccount->account->currency_code ?? 'USD';
|
58
|
58
|
$amountInCents = CurrencyConverter::convertToCents(abs($startingBalance), $currencyCode);
|
59
|
59
|
|
60
|
60
|
Transaction::create([
|