Bulk match transactions

This commit is contained in:
David Bomba 2022-11-25 07:52:47 +11:00
parent 990b43299c
commit ff38a91302
3 changed files with 42 additions and 8 deletions

View File

@ -481,19 +481,32 @@ class BankTransactionController extends BaseController
{
$action = request()->input('action');
if(!in_array($action, ['archive', 'restore', 'delete']))
if(!in_array($action, ['archive', 'restore', 'delete', 'convert_matched']))
return response()->json(['message' => 'Unsupported action.'], 400);
$ids = request()->input('ids');
$bank_transactions = BankTransaction::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
$bank_transactions->each(function ($bank_transaction, $key) use ($action) {
if (auth()->user()->can('edit', $bank_transaction)) {
$this->bank_transaction_repo->{$action}($bank_transaction);
if($action == 'convert_matched') //catch this action
{
if(auth()->user()->isAdmin())
{
$this->bank_transaction_repo->convert_matched($bank_transactions);
}
});
else
return;
}
else {
$bank_transactions->each(function ($bank_transaction, $key) use ($action) {
if (auth()->user()->can('edit', $bank_transaction)) {
$this->bank_transaction_repo->{$action}($bank_transaction);
}
});
}
/* Need to understand which permission are required for the given bulk action ie. view / edit */
return $this->listResponse(BankTransaction::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());

View File

@ -179,7 +179,7 @@ class MatchBankTransactions implements ShouldQueue
}
private function matchExpense($input) :self
{
{ nlog($input);
//if there is a category id, pull it from Yodlee and insert - or just reuse!!
$this->bt = BankTransaction::find($input['id']);
@ -196,13 +196,19 @@ class MatchBankTransactions implements ShouldQueue
$expense->payment_date = Carbon::parse($this->bt->date);
$expense->transaction_reference = $this->bt->description;
$expense->transaction_id = $this->bt->id;
$expense->vendor_id = array_key_exists('vendor_id', $input) ? $input['vendor_id'] : null;
if(array_key_exists('vendor_id', $input))
$expense->vendor_id = $input['vendor_id'];
$expense->invoice_documents = $this->company->invoice_expense_documents;
$expense->should_be_invoiced = $this->company->mark_expenses_invoiceable;
$expense->save();
$this->bt->expense_id = $expense->id;
$this->bt->vendor_id = array_key_exists('vendor_id', $input) ? $input['vendor_id'] : null;
if(array_key_exists('vendor_id', $input))
$this->bt->vendor_id = $input['vendor_id'];
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
$this->bt->save();

View File

@ -11,6 +11,7 @@
namespace App\Repositories;
use App\Jobs\Bank\MatchBankTransactions;
use App\Models\BankTransaction;
use App\Models\Task;
use App\Models\TaskStatus;
@ -35,4 +36,18 @@ class BankTransactionRepository extends BaseRepository
return $bank_transaction->fresh();
}
public function convert_matched($bank_transactions)
{
$data['transactions'] = $bank_transactions->map(function ($bt){
return ['id' => $bt->id, 'invoice_ids' => $bt->invoice_ids];
})->toArray();
$bts = (new MatchBankTransactions(auth()->user()->company()->id, auth()->user()->company()->db, $data))->handle();
}
}