1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Working on BankTransaction scaffolds

This commit is contained in:
David Bomba 2022-08-11 16:26:47 +10:00
parent 0c5efe3ec2
commit 90557a3083
6 changed files with 661 additions and 2 deletions

View File

@ -0,0 +1,448 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
use App\Factory\BankTransactionFactory;
use App\Helpers\Bank\Yodlee\Yodlee;
use App\Http\Requests\BankTransaction\AdminBankTransactionRequest;
use App\Http\Requests\BankTransaction\CreateBankTransactionRequest;
use App\Http\Requests\BankTransaction\DestroyBankTransactionRequest;
use App\Http\Requests\BankTransaction\EditBankTransactionRequest;
use App\Http\Requests\BankTransaction\ShowBankTransactionRequest;
use App\Http\Requests\BankTransaction\StoreBankTransactionRequest;
use App\Http\Requests\BankTransaction\UpdateBankTransactionRequest;
use App\Models\BankTransaction;
use App\Repositories\BankTransactionRepository;
use App\Services\Bank\BankService;
use App\Transformers\BankTransactionTransformer;
use Illuminate\Http\Request;
class BankTransactionController extends BaseController
{
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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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"),
* ),
* )
* @param Request $request
* @return Response|mixed
*/
public function index(Request $request)
{
$bank_transactions = BankTransaction::query()->company();
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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @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());
}
public function getTransactions(AdminBankTransactionRequest $request)
{
$bank_account_id = auth()->user()->account->bank_transaction_account_id;
$bank_account_id = 'sbMem62e1e69547bfb1';
if(!$bank_account_id)
return response()->json(['message' => 'Not yet authenticated with Bank Integration service'], 400);
$yodlee = new Yodlee($bank_account_id);
$yodlee->setTestMode();
$data = [
'CONTAINER' => 'bank',
'categoryType' => 'INCOME, UNCATEGORIZE',
'top' => 500,
'fromDate' => '2000-10-10', /// YYYY-MM-DD
];
$transactions = $yodlee->getTransactions($data);
$transactions = (new BankService(auth()->user()->company()))->match();
return response()->json($transactions, 200, [], JSON_PRETTY_PRINT);
}
}

View File

@ -662,7 +662,7 @@ class ClientController extends BaseController
*
*
* @OA\Post(
* path="/api/v1/clients/{id}/{mergaeble_client_hashed_id}/merge",
* path="/api/v1/clients/{id}/{mergeable_client_hashed_id}/merge",
* operationId="mergeClient",
* tags={"clients"},
* summary="Merges two clients",
@ -683,7 +683,7 @@ class ClientController extends BaseController
* ),
* ),
* @OA\Parameter(
* name="mergeable_client_hashedid",
* name="mergeable_client_hashed_id",
* in="path",
* description="The Mergeable Client Hashed ID",
* example="D2J234DFA",

View File

@ -0,0 +1,20 @@
<?php
/**
* @OA\Schema(
* schema="BankTransaction",
* type="object",
* @OA\Property(property="id", type="string", example="AS3df3A", description="The bank integration hashed id"),
* @OA\Property(property="company_id", type="string", example="AS3df3A", description="The company hashed id"),
* @OA\Property(property="user_id", type="string", example="AS3df3A", description="The user hashed id"),
* @OA\Property(property="transaction_id", type="integer", example=343434, description="The id of the transaction"),
* @OA\Property(property="amount", type="number", example=10.00, description="The transaction amount"),
* @OA\Property(property="currency_code", type="string", example="USD", description="The ISO 3166 3 character currency code"),
* @OA\Property(property="account_type", type="string", example="creditCard", description="The account type"),
* @OA\Property(property="description", type="string", example="Potato purchases for kevin", description="The description of the transaction"),
* @OA\Property(property="category_id", type="integer", example=1, description="The category id"),
* @OA\Property(property="category_type", type="string", example="Expenses", description="The category description"),
* @OA\Property(property="date", type="string", example="2022-09-01", description="The date of the transaction"),
* @OA\Property(property="bank_account_id", type="integer", example="1", description="The ID number of the bank account"),
* )
*/

View File

@ -0,0 +1,61 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
class BankTransaction extends BaseModel
{
use SoftDeletes;
protected $fillable = [
];
protected $dates = [
];
public function getEntityType()
{
return self::class;
}
public function company()
{
return $this->belongsTo(Company::class);
}
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
public function expense()
{
return $this->belongsTo(Expense::class);
}
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
public function bank_integration()
{
return $this->belongsTo(BankIntegration::class)->withTrashed();
}
public function account()
{
return $this->belongsTo(Account::class)->withTrashed();
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Repositories;
use App\Models\BankTransaction;
use App\Models\Task;
use App\Models\TaskStatus;
/**
* Class for bank transaction repository.
*/
class BankTransactionRepository extends BaseRepository
{
public function store($data, BankTransaction $bank_transaction)
{
//stub to store
return $bank_transaction->save();
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Transformers;
use App\Models\Account;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\Company;
use App\Models\Expense;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
/**
* Class BankTransactionTransformer.
*/
class BankTransactionTransformer extends EntityTransformer
{
use MakesHash;
/**
* @var array
*/
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
'company',
'account',
'invoice',
'expense',
'bank_account',
];
/**
* @param BankTransaction $bank_integration
* @return array
*/
public function transform(BankTransaction $bank_transaction)
{
return [
'id' => (string) $this->encodePrimaryKey($bank_transaction->id),
'transaction_id' => (int)$bank_transaction->transaction_id,
'amount' => (float)$bank_transaction->amount ?: 0,
'currency_code' => (string)$bank_transaction->currency_code ?: '',
'account_type' => (string)$bank_transaction->account_type ?: '',
'category_id' => (int) $bank_transaction->category_id,
'category_type' => (string)$bank_transaction->category_type ?: '',
'date' => (string)$bank_transaction->date ?: '',
'bank_account_id' => (int)$bank_transaction->bank_account_id,
'description' => (string)$bank_transaction->description ?: '',
'invoice_id' => (string)$this->encodePrimaryKey($bank_transaction->invoice_id) ?: '',
'expense_id'=> (string)$this->encodePrimaryKey($bank_transaction->expense_id) ?: '',
'is_matched'=> (bool)$bank_transaction->is_matched ?: '',
];
}
public function includeAccount(BankTransaction $bank_transaction)
{
$transformer = new AccountTransformer($this->serializer);
return $this->includeItem($bank_transaction->account, $transformer, Account::class);
}
public function includeCompany(BankTransaction $bank_transaction)
{
$transformer = new CompanyTransformer($this->serializer);
return $this->includeItem($bank_transaction->company, $transformer, Company::class);
}
public function includeInvoice(BankTransaction $bank_transaction)
{
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeItem($bank_transaction->invoice, $transformer, Invoice::class);
}
public function includeExpense(BankTransaction $bank_transaction)
{
$transformer = new ExpenseTransformer($this->serializer);
return $this->includeItem($bank_transaction->expense, $transformer, Expense::class);
}
}