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

539 lines
21 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;
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;
}
/**
* @OA\Get(
* path="/api/v1/bank_transactions",
* operationId="getBankTransactions",
* tags={"bank_transactions"},
* summary="Gets a list of bank_transactions",
* description="Lists all bank integrations",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Parameter(
* name="rows",
* in="query",
* description="The number of bank integrations to return",
* example="50",
* required=false,
* @OA\Schema(
* type="number",
* format="integer",
* ),
* ),
* @OA\Response(
* response=200,
* description="A list of bank integrations",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
2023-02-28 12:17:32 +01:00
* ),
2022-08-11 08:26:47 +02:00
* )
2022-11-11 00:28:25 +01:00
* @param BankTransactionFilters $filter
2022-08-11 08:26:47 +02:00
* @return Response|mixed
*/
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);
}
/**
* Display the specified resource.
*
* @param ShowBankTransactionRequest $request
* @param BankTransaction $bank_transaction
* @return Response
*
*
* @OA\Get(
* path="/api/v1/bank_transactions/{id}",
* operationId="showBankTransaction",
* tags={"bank_transactions"},
* summary="Shows a bank_transaction",
* description="Displays a bank_transaction by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The BankTransaction Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the bank_transaction object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function show(ShowBankTransactionRequest $request, BankTransaction $bank_transaction)
{
return $this->itemResponse($bank_transaction);
}
/**
* Show the form for editing the specified resource.
*
* @param EditBankTransactionRequest $request
* @param BankTransaction $bank_transaction
* @return Response
*
*
* @OA\Get(
* path="/api/v1/bank_transactions/{id}/edit",
* operationId="editBankTransaction",
* tags={"bank_transactions"},
* summary="Shows a bank_transaction for editing",
* description="Displays a bank_transaction by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The BankTransaction Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the bank_transaction object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function edit(EditBankTransactionRequest $request, BankTransaction $bank_transaction)
{
return $this->itemResponse($bank_transaction);
}
/**
* Update the specified resource in storage.
*
* @param UpdateBankTransactionRequest $request
* @param BankTransaction $bank_transaction
* @return Response
*
*
*
* @OA\Put(
* path="/api/v1/bank_transactions/{id}",
* operationId="updateBankTransaction",
* tags={"bank_transactions"},
* summary="Updates a bank_transaction",
* description="Handles the updating of a bank_transaction by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The BankTransaction Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the bank_transaction object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
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());
}
/**
* Show the form for creating a new resource.
*
* @param CreateBankTransactionRequest $request
* @return Response
*
*
*
* @OA\Get(
* path="/api/v1/bank_transactions/create",
* operationId="getBankTransactionsCreate",
* tags={"bank_transactions"},
* summary="Gets a new blank bank_transaction object",
* description="Returns a blank object with default values",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank bank_transaction object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function create(CreateBankTransactionRequest $request)
{
$bank_transaction = BankTransactionFactory::create(auth()->user()->company()->id, auth()->user()->id, auth()->user()->account_id);
return $this->itemResponse($bank_transaction);
}
/**
* Store a newly created resource in storage.
*
* @param StoreBankTransactionRequest $request
* @return Response
*
*
*
* @OA\Post(
* path="/api/v1/bank_transactions",
* operationId="storeBankTransaction",
* tags={"bank_transactions"},
* summary="Adds a bank_transaction",
* description="Adds an bank_transaction to a company",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved bank_transaction object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/BankTransaction"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function store(StoreBankTransactionRequest $request)
{
//stub to store the model
$bank_transaction = $this->bank_transaction_repo->save($request->all(), BankTransactionFactory::create(auth()->user()->company()->id, auth()->user()->id, auth()->user()->account_id));
return $this->itemResponse($bank_transaction);
}
/**
* Remove the specified resource from storage.
*
* @param DestroyBankTransactionRequest $request
* @param BankTransaction $bank_transaction
* @return Response
*
*
* @throws \Exception
* @OA\Delete(
* path="/api/v1/bank_transactions/{id}",
* operationId="deleteBankTransaction",
* tags={"bank_transactions"},
* summary="Deletes a bank_transaction",
* description="Handles the deletion of a bank_transaction by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 08:26:47 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The BankTransaction Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function destroy(DestroyBankTransactionRequest $request, BankTransaction $bank_transaction)
{
$this->bank_transaction_repo->delete($bank_transaction);
return $this->itemResponse($bank_transaction->fresh());
}
2022-08-11 09:13:40 +02:00
2022-09-15 05:58:42 +02:00
/**
2022-08-11 09:13:40 +02:00
* Perform bulk actions on the list view.
*
* @return Collection
*
* @OA\Post(
2022-08-16 01:47:05 +02:00
* path="/api/v1/bank_transations/bulk",
* operationId="bulkBankTransactions",
2022-09-15 05:58:42 +02:00
* tags={"bank_transactions"},
2022-08-16 01:47:05 +02:00
* summary="Performs bulk actions on an array of bank_transations",
2022-08-11 09:13:40 +02:00
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-08-11 09:13:40 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="Action paramters",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="The Bulk Action response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function bulk(BulkBankTransactionRequest $request)
2022-08-11 09:13:40 +02:00
{
$action = $request->input('action');
2022-08-11 09:13:40 +02:00
$ids = request()->input('ids');
$bank_transactions = BankTransaction::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2023-02-16 02:36:09 +01:00
if ($action == 'convert_matched') { //catch this action
$this->bank_transaction_repo->convert_matched($bank_transactions);
2023-02-16 02:36:09 +01:00
} else {
2022-11-24 21:52:47 +01:00
$bank_transactions->each(function ($bank_transaction, $key) use ($action) {
2023-02-16 02:36:09 +01:00
$this->bank_transaction_repo->{$action}($bank_transaction);
2022-11-24 21:52:47 +01:00
});
}
2022-08-11 09:13:40 +02:00
/* 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());
}
2022-09-15 05:58:42 +02:00
/**
* Perform bulk actions on the list view.
*
* @return Collection
*
* @OA\Post(
* path="/api/v1/bank_transations/match",
* operationId="matchBankTransactions",
* tags={"bank_transactions"},
* summary="Performs match actions on an array of bank_transactions",
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-09-15 05:58:42 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="Action paramters",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="The Bulk Action response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
2022-08-11 09:13:40 +02:00
2022-09-15 05:58:42 +02:00
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function match(MatchBankTransactionRequest $request)
2022-08-11 08:26:47 +02:00
{
2022-10-24 12:20:29 +02:00
$bts = (new MatchBankTransactions(auth()->user()->company()->id, auth()->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
}