diff --git a/app/Http/Controllers/BankTransactionController.php b/app/Http/Controllers/BankTransactionController.php index 0d2d6ac4fd95..4a3253540fe8 100644 --- a/app/Http/Controllers/BankTransactionController.php +++ b/app/Http/Controllers/BankTransactionController.php @@ -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); } diff --git a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php index 66d2bce0403b..65bda81d1087 100644 --- a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php +++ b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php @@ -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); diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index fbe8b5c1e976..d15c35f2981b 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -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(); diff --git a/tests/Feature/Bank/BankTransactionTest.php b/tests/Feature/Bank/BankTransactionTest.php index e3dc19a1e5a6..3cc966d35724 100644 --- a/tests/Feature/Bank/BankTransactionTest.php +++ b/tests/Feature/Bank/BankTransactionTest.php @@ -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, diff --git a/tests/Feature/Bank/YodleeApiTest.php b/tests/Feature/Bank/YodleeApiTest.php index 0f4ed89cffb9..155ee64bc165 100644 --- a/tests/Feature/Bank/YodleeApiTest.php +++ b/tests/Feature/Bank/YodleeApiTest.php @@ -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);