akaunting/app/Http/Controllers/Api/Document/DocumentTransactions.php

90 lines
2.7 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2020-12-24 01:28:38 +03:00
namespace App\Http\Controllers\Api\Document;
2019-11-16 10:21:14 +03:00
2021-01-02 13:06:02 +03:00
use App\Abstracts\Http\ApiController;
2019-11-16 10:21:14 +03:00
use App\Http\Requests\Banking\Transaction as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Banking\Transaction as Resource;
2020-12-24 01:28:38 +03:00
use App\Jobs\Banking\CreateBankingDocumentTransaction;
2019-11-16 10:21:14 +03:00
use App\Jobs\Banking\DeleteTransaction;
use App\Models\Banking\Transaction;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2019-11-16 10:21:14 +03:00
2021-01-02 13:06:02 +03:00
class DocumentTransactions extends ApiController
2019-11-16 10:21:14 +03:00
{
2021-01-02 13:06:02 +03:00
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
// Add CRUD permission check
$this->middleware('permission:create-banking-transactions')->only('create', 'store', 'duplicate', 'import');
$this->middleware('permission:read-banking-transactions')->only('index', 'show', 'edit', 'export');
$this->middleware('permission:update-banking-transactions')->only('update', 'enable', 'disable');
$this->middleware('permission:delete-banking-transactions')->only('destroy');
}
2019-11-16 10:21:14 +03:00
/**
* Display a listing of the resource.
*
2020-12-24 01:28:38 +03:00
* @param $document_id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
2020-12-24 01:28:38 +03:00
public function index($document_id)
2019-11-16 10:21:14 +03:00
{
2021-01-02 13:06:02 +03:00
$transactions = Transaction::documentId($document_id)->get();
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
return Resource::collection($transactions);
2019-11-16 10:21:14 +03:00
}
/**
* Display the specified resource.
*
2020-12-24 01:28:38 +03:00
* @param $document_id
2019-11-16 10:21:14 +03:00
* @param $id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
2020-12-24 01:28:38 +03:00
public function show($document_id, $id)
2019-11-16 10:21:14 +03:00
{
2021-01-02 13:06:02 +03:00
$transaction = Transaction::documentId($document_id)->find($id);
2019-11-16 10:21:14 +03:00
if (! $transaction instanceof Transaction) {
return $this->errorInternal('No query results for model [' . Transaction::class . '] ' . $id);
}
2022-06-01 10:15:55 +03:00
return new Resource($transaction);
2019-11-16 10:21:14 +03:00
}
/**
* Store a newly created resource in storage.
*
2020-12-24 01:28:38 +03:00
* @param $document_id
2019-11-16 10:21:14 +03:00
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
2020-12-24 01:28:38 +03:00
public function store($document_id, Request $request)
2019-11-16 10:21:14 +03:00
{
2020-12-24 01:28:38 +03:00
$document = Document::find($document_id);
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$transaction = $this->dispatch(new CreateBankingDocumentTransaction($document, $request));
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
return $this->created(route('api.documents.transactions.show', [$document_id, $transaction->id]), new Resource($transaction));
2019-11-16 10:21:14 +03:00
}
/**
* Remove the specified resource from storage.
*
2020-12-24 01:28:38 +03:00
* @param $document_id
2019-11-16 10:21:14 +03:00
* @param $id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2019-11-16 10:21:14 +03:00
*/
2020-12-24 01:28:38 +03:00
public function destroy($document_id, $id)
2019-11-16 10:21:14 +03:00
{
2021-01-02 13:06:02 +03:00
$transaction = Transaction::documentId($document_id)->find($id);
2019-11-16 10:21:14 +03:00
$this->dispatch(new DeleteTransaction($transaction));
2022-06-01 10:15:55 +03:00
return $this->noContent();
2019-11-16 10:21:14 +03:00
}
}