diff --git a/app/Factory/BankTransactionFactory.php b/app/Factory/BankTransactionFactory.php index b8d33c039ac1..4fff5906bc93 100644 --- a/app/Factory/BankTransactionFactory.php +++ b/app/Factory/BankTransactionFactory.php @@ -23,7 +23,7 @@ class BankTransactionFactory $bank_transaction->company_id = $company_id; $bank_transaction->amount = 0; - $bank_transaction->currency_code = ''; + $bank_transaction->currency_id = 1; $bank_transaction->account_type = ''; $bank_transaction->category_type = ''; $bank_transaction->date = now()->format('Y-m-d'); diff --git a/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php b/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php index 6da207fb3e6e..725bcc57f907 100644 --- a/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php +++ b/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php @@ -12,7 +12,9 @@ namespace App\Helpers\Bank\Yodlee\Transformer; use App\Helpers\Bank\BankRevenueInterface; - +use App\Utils\Traits\AppSetup; +use Illuminate\Support\Facades\Cache; + /** "date": "string", "sourceId": "string", @@ -113,6 +115,7 @@ use App\Helpers\Bank\BankRevenueInterface; class IncomeTransformer implements BankRevenueInterface { + use AppSetup; public function transform($transaction) { @@ -136,7 +139,7 @@ class IncomeTransformer implements BankRevenueInterface return [ 'transaction_id' => $transaction->id, 'amount' => $transaction->amount->amount, - 'currency_code' => $transaction->amount->currency, + 'currency_id' => $this->convertCurrency($transaction->amount->currency), 'account_type' => $transaction->CONTAINER, 'category_id' => $transaction->highLevelCategoryId, 'category_type' => $transaction->categoryType, @@ -147,6 +150,27 @@ class IncomeTransformer implements BankRevenueInterface ]; } + private function convertCurrency(string $code) + { + + $currencies = Cache::get('currencies'); + + if (! $currencies) { + $this->buildCache(true); + } + + $currency = $currencies->filter(function ($item) use($code){ + return $item->code == $code; + })->first(); + + if($currency) + return $currency->id; + + return 1; + + } + + } diff --git a/app/Http/Controllers/OpenAPI/BankTransaction.php b/app/Http/Controllers/OpenAPI/BankTransaction.php index 5e9185c13fea..c3e5f3d19c13 100644 --- a/app/Http/Controllers/OpenAPI/BankTransaction.php +++ b/app/Http/Controllers/OpenAPI/BankTransaction.php @@ -8,7 +8,7 @@ * @OA\Property(property="user_id", type="string", example="AS3df3A", description="The user hashed id"), * @OA\Property(property="transaction_id", type="integer", example=343434, description="The id of the transaction"), * @OA\Property(property="amount", type="number", example=10.00, description="The transaction amount"), - * @OA\Property(property="currency_code", type="string", example="USD", description="The ISO 3166 3 character currency code"), + * @OA\Property(property="currency_id", type="string", example="1", description="The currency ID of the currency"), * @OA\Property(property="account_type", type="string", example="creditCard", description="The account type"), * @OA\Property(property="description", type="string", example="Potato purchases for kevin", description="The description of the transaction"), * @OA\Property(property="category_id", type="integer", example=1, description="The category id"), diff --git a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php index e7cca9c3471d..66d2bce0403b 100644 --- a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php +++ b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php @@ -29,13 +29,17 @@ class MatchBankTransactionRequest extends Request public function rules() { - return [ - '*.id' => 'required|bail', - '*.invoice_id' => 'nullable|sometimes', - '*.is_expense' => 'nullable|sometimes|bool', - '*.amount' => 'nullable|sometimes|numeric' + $rules = [ + 'id' => 'required|bail', + 'invoice_ids' => 'nullable|string|sometimes', + 'ninja_category_id' => 'nullable|string|sometimes' ]; + if(isset($this->vendor_id)) + $rules['vendor_id'] = 'bail|required|exists:vendors,id,company_id,'.auth()->user()->company()->id.',is_deleted,0'; + + return $rules; + } public function prepareForValidation() diff --git a/app/Http/Requests/BankTransaction/UpdateBankTransactionRequest.php b/app/Http/Requests/BankTransaction/UpdateBankTransactionRequest.php index d101cd45239d..c5d5f16245ae 100644 --- a/app/Http/Requests/BankTransaction/UpdateBankTransactionRequest.php +++ b/app/Http/Requests/BankTransaction/UpdateBankTransactionRequest.php @@ -36,8 +36,8 @@ class UpdateBankTransactionRequest extends Request 'description', 'bail|required|string' ]; - if (isset($this->currency_code)) - $rules['currency_code'] = 'sometimes|exists:currencies,code'; + if (isset($this->currency_id)) + $rules['currency_id'] = 'sometimes|exists:currencies,id'; if(isset($this->vendor_id)) $rules['vendor_id'] = 'bail|required|exists:vendors,id,company_id,'.auth()->user()->company()->id.',is_deleted,0'; diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index 5f6d7b0527b7..fbe8b5c1e976 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -165,7 +165,7 @@ class MatchBankTransactions implements ShouldQueue $expense->category_id = $this->resolveCategory(); $expense->amount = $this->bt->amount; $expense->number = $this->getNextExpenseNumber($expense); - $expense->currency_id = $this->harvestCurrencyId(); + $expense->currency_id = $this->bt->currency_id; $expense->date = Carbon::parse($this->bt->date); $expense->public_notes = $this->bt->description; $expense->save(); @@ -219,7 +219,7 @@ class MatchBankTransactions implements ShouldQueue $payment->client_id = $this->invoice->client_id; $payment->transaction_reference = $this->bt->description; $payment->transaction_id = $this->bt->transaction_id; - $payment->currency_id = $this->harvestCurrencyId(); + $payment->currency_id = $this->bt->currency_id; $payment->is_manual = false; $payment->date = $this->bt->date ? Carbon::parse($this->bt->date) : now(); @@ -301,17 +301,6 @@ class MatchBankTransactions implements ShouldQueue return null; } - private function harvestCurrencyId() :int - { - $currency = Currency::where('code', $this->bt->currency_code)->first(); - - if($currency) - return $currency->id; - - return $this->invoice->client->getSetting('currency_id'); - - } - private function setExchangeRate(Payment $payment) { if ($payment->exchange_rate != 1) { diff --git a/app/Models/BankTransaction.php b/app/Models/BankTransaction.php index 2397a05b1c55..79387774f7ce 100644 --- a/app/Models/BankTransaction.php +++ b/app/Models/BankTransaction.php @@ -26,7 +26,7 @@ class BankTransaction extends BaseModel const STATUS_CONVERTED = 3; protected $fillable = [ - 'currency_code', + 'currency_id', 'category_id', 'ninja_category_id', 'date', diff --git a/app/Transformers/BankTransactionTransformer.php b/app/Transformers/BankTransactionTransformer.php index 58ad829752e4..db9bc91c02d7 100644 --- a/app/Transformers/BankTransactionTransformer.php +++ b/app/Transformers/BankTransactionTransformer.php @@ -54,14 +54,14 @@ class BankTransactionTransformer extends EntityTransformer 'bank_integration_id' => (string) $this->encodePrimaryKey($bank_transaction->bank_integration_id), 'transaction_id' => (int) $bank_transaction->transaction_id, 'amount' => (float) $bank_transaction->amount ?: 0, - 'currency_code' => (string) $bank_transaction->currency_code ?: '', + 'currency_id' => (string) $bank_transaction->currency_id ?: '1', 'account_type' => (string) $bank_transaction->account_type ?: '', 'category_id' => (int) $bank_transaction->category_id, 'ninja_category_id' => (int) $bank_transaction->ninja_category_id, 'category_type' => (string) $bank_transaction->category_type ?: '', 'date' => (string) $bank_transaction->date ?: '', 'bank_account_id' => (int) $bank_transaction->bank_account_id, - 'status_id' => (int) $bank_transaction->status_id, + 'status_id' => (string) $bank_transaction->status_id, 'description' => (string) $bank_transaction->description ?: '', 'base_type' => (string) $bank_transaction->base_type ?: '', 'invoice_ids' => (string) $bank_transaction->invoice_ids ?: '', diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index e25762b6f4b9..77607f2b1497 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -97,7 +97,7 @@ class InvoiceTransformer extends EntityTransformer 'balance' => (float) $invoice->balance, 'client_id' => (string) $this->encodePrimaryKey($invoice->client_id), 'vendor_id' => (string) $this->encodePrimaryKey($invoice->vendor_id), - 'status_id' => (string) ($invoice->status_id ?: 1), + 'status_id' => (string) ($invoice->status_id ?: '1'), 'design_id' => (string) $this->encodePrimaryKey($invoice->design_id), 'recurring_id' => (string) $this->encodePrimaryKey($invoice->recurring_id), 'created_at' => (int) $invoice->created_at, diff --git a/database/factories/BankTransactionFactory.php b/database/factories/BankTransactionFactory.php index cc3dbb3f5f81..ddba2b428d2c 100644 --- a/database/factories/BankTransactionFactory.php +++ b/database/factories/BankTransactionFactory.php @@ -27,7 +27,7 @@ class BankTransactionFactory extends Factory return [ 'transaction_id' => $this->faker->randomNumber(9, true) , 'amount' => $this->faker->randomFloat(2,10,10000) , - 'currency_code' => 'USD', + 'currency_id' => '1', 'account_type' => 'creditCard', 'category_id' => 1, 'category_type' => 'Random' , diff --git a/database/migrations/2022_08_05_023357_bank_integration.php b/database/migrations/2022_08_05_023357_bank_integration.php index e78efe8869b2..fec9c4747a34 100644 --- a/database/migrations/2022_08_05_023357_bank_integration.php +++ b/database/migrations/2022_08_05_023357_bank_integration.php @@ -54,6 +54,7 @@ return new class extends Migration $table->unsignedBigInteger('transaction_id')->index(); $table->decimal('amount', 20, 6)->default(0); $table->string('currency_code')->nullable(); + $table->unsignedInteger('currency_id')->nullable(); $table->string('account_type')->nullable(); $table->unsignedInteger('category_id')->nullable(); $table->unsignedInteger('ninja_category_id')->nullable();