Andrew Wallo 4 个月前
父节点
当前提交
5468b5a186

+ 9
- 2
app/Listeners/CreateConnectedAccount.php 查看文件

6
 use App\Models\Banking\Institution;
6
 use App\Models\Banking\Institution;
7
 use App\Models\Company;
7
 use App\Models\Company;
8
 use App\Services\PlaidService;
8
 use App\Services\PlaidService;
9
+use App\Utilities\Currency\CurrencyConverter;
9
 use Illuminate\Support\Facades\DB;
10
 use Illuminate\Support\Facades\DB;
10
 
11
 
11
 class CreateConnectedAccount
12
 class CreateConnectedAccount
59
     {
60
     {
60
         $identifierHash = md5($company->id . $institution->external_institution_id . $plaidAccount->name . $plaidAccount->mask);
61
         $identifierHash = md5($company->id . $institution->external_institution_id . $plaidAccount->name . $plaidAccount->mask);
61
 
62
 
63
+        $currencyCode = $plaidAccount->balances->iso_currency_code ?? 'USD';
64
+
65
+        $currentBalance = $plaidAccount->balances->current ?? 0;
66
+
67
+        $currentBalanceCents = CurrencyConverter::convertToCents($currentBalance, $currencyCode);
68
+
62
         $company->connectedBankAccounts()->updateOrCreate([
69
         $company->connectedBankAccounts()->updateOrCreate([
63
             'identifier' => $identifierHash,
70
             'identifier' => $identifierHash,
64
         ], [
71
         ], [
66
             'external_account_id' => $plaidAccount->account_id,
73
             'external_account_id' => $plaidAccount->account_id,
67
             'access_token' => $accessToken,
74
             'access_token' => $accessToken,
68
             'item_id' => $authResponse->item->item_id,
75
             'item_id' => $authResponse->item->item_id,
69
-            'currency_code' => $plaidAccount->balances->iso_currency_code ?? 'USD',
70
-            'current_balance' => $plaidAccount->balances->current ?? 0,
76
+            'currency_code' => $currencyCode,
77
+            'current_balance' => $currentBalanceCents,
71
             'name' => $plaidAccount->name,
78
             'name' => $plaidAccount->name,
72
             'mask' => $plaidAccount->mask,
79
             'mask' => $plaidAccount->mask,
73
             'type' => $plaidAccount->type,
80
             'type' => $plaidAccount->type,

+ 2
- 2
app/Livewire/Company/Service/ConnectedAccount/ListInstitutions.php 查看文件

79
                     )),
79
                     )),
80
                 Placeholder::make('info')
80
                 Placeholder::make('info')
81
                     ->hiddenLabel()
81
                     ->hiddenLabel()
82
-                    ->visible(static fn (ConnectedBankAccount $connectedBankAccount) => $connectedBankAccount->bank_account_id === null)
82
+                    ->visible(static fn (ConnectedBankAccount $connectedBankAccount) => ! $connectedBankAccount->bank_account_id)
83
                     ->content(static fn (ConnectedBankAccount $connectedBankAccount) => 'If ' . $connectedBankAccount->name . ' already has transactions for an existing account, select the account to import transactions into.'),
83
                     ->content(static fn (ConnectedBankAccount $connectedBankAccount) => 'If ' . $connectedBankAccount->name . ' already has transactions for an existing account, select the account to import transactions into.'),
84
                 Select::make('bank_account_id')
84
                 Select::make('bank_account_id')
85
                     ->label('Select account')
85
                     ->label('Select account')
86
-                    ->visible(static fn (ConnectedBankAccount $connectedBankAccount) => $connectedBankAccount->bank_account_id === null)
86
+                    ->visible(static fn (ConnectedBankAccount $connectedBankAccount) => ! $connectedBankAccount->bank_account_id)
87
                     ->options(fn (ConnectedBankAccount $connectedBankAccount) => $this->getBankAccountOptions($connectedBankAccount))
87
                     ->options(fn (ConnectedBankAccount $connectedBankAccount) => $this->getBankAccountOptions($connectedBankAccount))
88
                     ->required(),
88
                     ->required(),
89
                 DatePicker::make('start_date')
89
                 DatePicker::make('start_date')

+ 11
- 4
app/Services/TransactionService.php 查看文件

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

正在加载...
取消
保存