Refactor for bank transactions

This commit is contained in:
David Bomba 2022-09-22 15:54:58 +10:00
parent 78a1a3020c
commit 8300b01504
11 changed files with 47 additions and 29 deletions

View File

@ -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');

View File

@ -12,6 +12,8 @@
namespace App\Helpers\Bank\Yodlee\Transformer;
use App\Helpers\Bank\BankRevenueInterface;
use App\Utils\Traits\AppSetup;
use Illuminate\Support\Facades\Cache;
/**
"date": "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;
}
}

View File

@ -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"),

View File

@ -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()

View File

@ -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';

View File

@ -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) {

View File

@ -26,7 +26,7 @@ class BankTransaction extends BaseModel
const STATUS_CONVERTED = 3;
protected $fillable = [
'currency_code',
'currency_id',
'category_id',
'ninja_category_id',
'date',

View File

@ -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 ?: '',

View File

@ -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,

View File

@ -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' ,

View File

@ -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();