Refactor bank transactions

This commit is contained in:
David Bomba 2022-09-22 16:20:54 +10:00
parent 8300b01504
commit dd414fc588
5 changed files with 43 additions and 28 deletions

View File

@ -550,7 +550,10 @@ class BankTransactionController extends BaseController
MatchBankTransactions::dispatch(auth()->user()->company()->id, auth()->user()->company()->db, $request->all());
return response()->json(['message' => 'Processing....'], 200);
$bt = (new MatchBankTransactions(auth()->user()->company()->id, auth()->user()->company()->db, $request->all()))->handle();
return $this->itemResponse($bt);
// return response()->json(['message' => 'Processing....'], 200);
}

View File

@ -46,6 +46,12 @@ class MatchBankTransactionRequest extends Request
{
$input = $this->all();
if(array_key_exists('id', $input))
$input['id'] = $this->decodePrimaryKey($input['id']);
if(array_key_exists('ninja_category_id', $input))
$input['ninja_category_id'] = $this->decodePrimaryKey($input['ninja_category_id']);
$input = $this->decodePrimaryKeys($input);
$this->replace($input);

View File

@ -37,6 +37,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
class MatchBankTransactions implements ShouldQueue
{
@ -87,19 +88,23 @@ class MatchBankTransactions implements ShouldQueue
$this->company = Company::find($this->company_id);
$yodlee = new Yodlee($this->company->account->bank_integration_account_id);
$bank_categories = Cache::get('bank_categories');
$_categories = $yodlee->getTransactionCategories();
if($_categories)
if(!$bank_categories){
$_categories = $yodlee->getTransactionCategories();
$this->categories = collect($_categories->transactionCategory);
foreach($this->input as $match)
{
if(array_key_exists('invoice_ids', $match) && strlen($match['invoice_ids']) > 1)
$this->matchInvoicePayment($match);
else
$this->matchExpense($match);
}
else {
$this->categories = collect($bank_categories);
}
if(array_key_exists('invoice_ids', $this->input) && strlen($this->input['invoice_ids']) > 1)
$this->matchInvoicePayment();
else
$this->matchExpense();
return $this->bt;
}
@ -137,15 +142,12 @@ class MatchBankTransactions implements ShouldQueue
}
private function matchInvoicePayment(array $match) :void
private function matchInvoicePayment() :self
{
$this->bt = BankTransaction::find($match['id']);
$this->bt = BankTransaction::find($this->input['id']);
$_invoices = Invoice::withTrashed()->find($this->getInvoices($match['invoice_ids']));
$_invoices = Invoice::withTrashed()->find($this->getInvoices($this->input['invoice_ids']));
if(array_key_exists('amount', $match) && $match['amount'] > 0)
$amount = $match['amount'];
else
$amount = $this->bt->amount;
if($_invoices && $this->checkPayable($_invoices)){
@ -154,12 +156,13 @@ class MatchBankTransactions implements ShouldQueue
}
return $this;
}
private function matchExpense(array $match) :void
private function matchExpense() :self
{
//if there is a category id, pull it from Yodlee and insert - or just reuse!!
$this->bt = BankTransaction::find($match['id']);
$this->bt = BankTransaction::find($this->input['id']);
$expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id);
$expense->category_id = $this->resolveCategory();
@ -170,6 +173,7 @@ class MatchBankTransactions implements ShouldQueue
$expense->public_notes = $this->bt->description;
$expense->save();
return $this;
}
private function createPayment($invoices, float $amount) :void
@ -280,6 +284,9 @@ class MatchBankTransactions implements ShouldQueue
private function resolveCategory() :?int
{
if(array_key_exists('ninja_category_id', $this->input))
return $this->input['ninja_category_id'];
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bt->category_id);
$ec = ExpenseCategory::where('company_id', $this->bt->company_id)->where('bank_category_id', $this->bt->category_id)->first();

View File

@ -38,7 +38,7 @@ class BankTransactionTest extends TestCase
public function testMatchBankTransactionsValidationShouldFail()
{
$data = [
['bad_key' => 10],
'bad_key' => 10,
];
$response = $this->withHeaders([
@ -53,9 +53,12 @@ class BankTransactionTest extends TestCase
public function testMatchBankTransactionValidationShouldPass()
{
$data = [
['id' => 'Wpmbk5ezJn'],
'id' => $this->bank_transaction->hashed_id,
];
nlog($this->bank_transaction->hashed_id);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,

View File

@ -62,9 +62,7 @@ class YodleeApiTest extends TestCase
$data = [
[
'id' => $bt->id,
]
'id' => $bt->id,
];
MatchBankTransactions::dispatchSync($this->company->id, $this->company->db, $data);
@ -119,10 +117,8 @@ class YodleeApiTest extends TestCase
$bt->save();
$data = [
[
'id' => $bt->id,
'invoice_ids' => $invoice->hashed_id
]
'id' => $bt->id,
'invoice_ids' => $invoice->hashed_id
];
MatchBankTransactions::dispatchSync($this->company->id, $this->company->db, $data);