|
|
@@ -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,7 +32,7 @@ class TransactionService
|
|
31
|
32
|
|
|
32
|
33
|
$startingBalance = bcsub($adjustedBalance, $sumOfTransactions, 2);
|
|
33
|
34
|
|
|
34
|
|
- $this->createStartingBalanceTransaction($company, $account, $bankAccount, (float) $startingBalance, $startDate);
|
|
|
35
|
+ $this->createStartingBalanceTransaction($company, $bankAccount, (float) $startingBalance, $startDate);
|
|
35
|
36
|
}
|
|
36
|
37
|
}
|
|
37
|
38
|
|
|
|
@@ -42,7 +43,7 @@ class TransactionService
|
|
42
|
43
|
}
|
|
43
|
44
|
}
|
|
44
|
45
|
|
|
45
|
|
- public function createStartingBalanceTransaction(Company $company, Account $account, BankAccount $bankAccount, float $startingBalance, string $startDate): void
|
|
|
46
|
+ public function createStartingBalanceTransaction(Company $company, BankAccount $bankAccount, float $startingBalance, string $startDate): void
|
|
46
|
47
|
{
|
|
47
|
48
|
$transactionType = $startingBalance >= 0 ? TransactionType::Deposit : TransactionType::Withdrawal;
|
|
48
|
49
|
$accountName = $startingBalance >= 0 ? "Owner's Investment" : "Owner's Drawings";
|
|
|
@@ -53,12 +54,15 @@ class TransactionService
|
|
53
|
54
|
|
|
54
|
55
|
$postedAt = Carbon::parse($startDate)->subDay()->toDateTimeString();
|
|
55
|
56
|
|
|
|
57
|
+ $currencyCode = $bankAccount->account->currency_code ?? 'USD';
|
|
|
58
|
+ $amountInCents = CurrencyConverter::convertToCents(abs($startingBalance), $currencyCode);
|
|
|
59
|
+
|
|
56
|
60
|
Transaction::create([
|
|
57
|
61
|
'company_id' => $company->id,
|
|
58
|
62
|
'account_id' => $chartAccount->id,
|
|
59
|
63
|
'bank_account_id' => $bankAccount->id,
|
|
60
|
64
|
'type' => $transactionType,
|
|
61
|
|
- 'amount' => abs($startingBalance),
|
|
|
65
|
+ 'amount' => $amountInCents,
|
|
62
|
66
|
'payment_channel' => 'other',
|
|
63
|
67
|
'posted_at' => $postedAt,
|
|
64
|
68
|
'description' => 'Starting Balance',
|
|
|
@@ -75,13 +79,16 @@ class TransactionService
|
|
75
|
79
|
$postedAt = $transaction->datetime ?? Carbon::parse($transaction->date)->toDateTimeString();
|
|
76
|
80
|
$description = $transaction->name;
|
|
77
|
81
|
|
|
|
82
|
+ $currencyCode = $transaction->iso_currency_code ?? $bankAccount->account->currency_code ?? 'USD';
|
|
|
83
|
+ $amountInCents = CurrencyConverter::convertToCents(abs($transaction->amount), $currencyCode);
|
|
|
84
|
+
|
|
78
|
85
|
Transaction::create([
|
|
79
|
86
|
'company_id' => $company->id,
|
|
80
|
87
|
'account_id' => $chartAccount->id,
|
|
81
|
88
|
'bank_account_id' => $bankAccount->id,
|
|
82
|
89
|
'plaid_transaction_id' => $transaction->transaction_id,
|
|
83
|
90
|
'type' => $transactionType,
|
|
84
|
|
- 'amount' => abs($transaction->amount),
|
|
|
91
|
+ 'amount' => $amountInCents,
|
|
85
|
92
|
'payment_channel' => $paymentChannel,
|
|
86
|
93
|
'posted_at' => $postedAt,
|
|
87
|
94
|
'description' => $description,
|