1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/BankTransactionController.php

134 lines
4.5 KiB
PHP
Raw Normal View History

2022-08-11 08:26:47 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-08-11 08:26:47 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
use App\Factory\BankTransactionFactory;
2022-11-11 00:28:25 +01:00
use App\Filters\BankTransactionFilters;
use App\Http\Requests\BankTransaction\BulkBankTransactionRequest;
2022-08-11 08:26:47 +02:00
use App\Http\Requests\BankTransaction\CreateBankTransactionRequest;
use App\Http\Requests\BankTransaction\DestroyBankTransactionRequest;
use App\Http\Requests\BankTransaction\EditBankTransactionRequest;
2022-09-15 05:58:42 +02:00
use App\Http\Requests\BankTransaction\MatchBankTransactionRequest;
2022-08-11 08:26:47 +02:00
use App\Http\Requests\BankTransaction\ShowBankTransactionRequest;
use App\Http\Requests\BankTransaction\StoreBankTransactionRequest;
use App\Http\Requests\BankTransaction\UpdateBankTransactionRequest;
2022-09-15 07:02:39 +02:00
use App\Jobs\Bank\MatchBankTransactions;
2022-08-11 08:26:47 +02:00
use App\Models\BankTransaction;
use App\Repositories\BankTransactionRepository;
use App\Transformers\BankTransactionTransformer;
2022-08-11 09:13:40 +02:00
use App\Utils\Traits\MakesHash;
2022-08-11 08:26:47 +02:00
class BankTransactionController extends BaseController
{
2022-08-11 09:13:40 +02:00
use MakesHash;
2024-01-14 05:05:00 +01:00
2022-08-11 08:26:47 +02:00
protected $entity_type = BankTransaction::class;
protected $entity_transformer = BankTransactionTransformer::class;
protected $bank_transaction_repo;
public function __construct(BankTransactionRepository $bank_transaction_repo)
{
parent::__construct();
$this->bank_transaction_repo = $bank_transaction_repo;
}
2022-11-11 00:28:25 +01:00
public function index(BankTransactionFilters $filters)
2022-08-11 08:26:47 +02:00
{
2022-11-11 00:28:25 +01:00
$bank_transactions = BankTransaction::filter($filters);
2022-08-11 08:26:47 +02:00
return $this->listResponse($bank_transactions);
}
public function show(ShowBankTransactionRequest $request, BankTransaction $bank_transaction)
{
return $this->itemResponse($bank_transaction);
}
public function edit(EditBankTransactionRequest $request, BankTransaction $bank_transaction)
{
return $this->itemResponse($bank_transaction);
}
public function update(UpdateBankTransactionRequest $request, BankTransaction $bank_transaction)
{
//stubs for updating the model
$bank_transaction = $this->bank_transaction_repo->save($request->all(), $bank_transaction);
return $this->itemResponse($bank_transaction->fresh());
}
public function create(CreateBankTransactionRequest $request)
{
2023-08-29 00:53:25 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$bank_transaction = BankTransactionFactory::create($user->company()->id, $user->id);
2022-08-11 08:26:47 +02:00
return $this->itemResponse($bank_transaction);
}
public function store(StoreBankTransactionRequest $request)
{
2023-08-29 00:53:25 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2022-08-11 08:26:47 +02:00
//stub to store the model
2023-08-29 00:53:25 +02:00
$bank_transaction = $this->bank_transaction_repo->save($request->all(), BankTransactionFactory::create($user->company()->id, $user->id));
2022-08-11 08:26:47 +02:00
return $this->itemResponse($bank_transaction);
}
public function destroy(DestroyBankTransactionRequest $request, BankTransaction $bank_transaction)
{
$this->bank_transaction_repo->delete($bank_transaction);
return $this->itemResponse($bank_transaction->fresh());
}
public function bulk(BulkBankTransactionRequest $request)
2022-08-11 09:13:40 +02:00
{
2024-01-21 03:47:25 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
$action = $request->input('action');
2022-08-11 09:13:40 +02:00
$ids = request()->input('ids');
2024-01-14 05:05:00 +01:00
2022-08-11 09:13:40 +02:00
$bank_transactions = BankTransaction::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2024-01-21 03:47:25 +01:00
if ($action == 'convert_matched' && $user->can('edit', $bank_transactions->first())) { //catch this action
$this->bank_transaction_repo->convert_matched($bank_transactions);
2023-02-16 02:36:09 +01:00
} else {
2024-01-21 03:47:25 +01:00
$bank_transactions->each(function ($bank_transaction, $key) use ($action, $user) {
2024-02-13 05:25:18 +01:00
if($user->can('edit', $bank_transaction)) {
2024-01-21 03:47:25 +01:00
$this->bank_transaction_repo->{$action}($bank_transaction);
2024-02-13 05:25:18 +01:00
}
2022-11-24 21:52:47 +01:00
});
}
2024-01-14 05:05:00 +01:00
2022-08-11 09:13:40 +02:00
return $this->listResponse(BankTransaction::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
}
2022-09-15 05:58:42 +02:00
public function match(MatchBankTransactionRequest $request)
2022-08-11 08:26:47 +02:00
{
2023-08-05 04:26:07 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$bts = (new MatchBankTransactions($user->company()->id, $user->company()->db, $request->all()))->handle();
2022-09-22 08:20:54 +02:00
2022-10-24 12:20:29 +02:00
return $this->listResponse($bts);
2022-09-15 05:58:42 +02:00
}
2023-02-16 02:36:09 +01:00
}