diff --git a/app/Http/Controllers/BankTransactionController.php b/app/Http/Controllers/BankTransactionController.php index 901f1812a071..ef0354d1ea1e 100644 --- a/app/Http/Controllers/BankTransactionController.php +++ b/app/Http/Controllers/BankTransactionController.php @@ -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()); diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index d99c8fadb644..dad8d2467f70 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -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(); diff --git a/app/Repositories/BankTransactionRepository.php b/app/Repositories/BankTransactionRepository.php index 1a5bfba1662e..b9fe838376d0 100644 --- a/app/Repositories/BankTransactionRepository.php +++ b/app/Repositories/BankTransactionRepository.php @@ -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(); + + + } + + }