2018-10-17 14:26:27 +02:00
< ? php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja ( https :// invoiceninja . com )
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
* @ copyright Copyright ( c ) 2019. Invoice Ninja LLC ( https :// invoiceninja . com )
*
* @ license https :// opensource . org / licenses / AAL
*/
2018-10-17 14:26:27 +02:00
namespace App\Http\Controllers ;
2019-05-15 11:20:52 +02:00
use App\Events\Invoice\InvoiceWasCreated ;
use App\Events\Invoice\InvoiceWasUpdated ;
2019-04-23 06:16:41 +02:00
use App\Factory\CloneInvoiceFactory ;
2019-04-23 15:17:49 +02:00
use App\Factory\CloneInvoiceToQuoteFactory ;
2019-04-15 02:10:54 +02:00
use App\Factory\InvoiceFactory ;
2019-04-17 02:58:23 +02:00
use App\Filters\InvoiceFilters ;
2019-04-23 06:16:41 +02:00
use App\Http\Requests\Invoice\ActionInvoiceRequest ;
2019-04-16 05:28:05 +02:00
use App\Http\Requests\Invoice\CreateInvoiceRequest ;
2019-04-17 08:20:32 +02:00
use App\Http\Requests\Invoice\DestroyInvoiceRequest ;
2019-04-16 05:28:05 +02:00
use App\Http\Requests\Invoice\EditInvoiceRequest ;
use App\Http\Requests\Invoice\ShowInvoiceRequest ;
use App\Http\Requests\Invoice\StoreInvoiceRequest ;
use App\Http\Requests\Invoice\UpdateInvoiceRequest ;
2019-04-23 08:19:45 +02:00
use App\Jobs\Entity\ActionEntity ;
2019-05-16 08:00:27 +02:00
use App\Jobs\Invoice\MarkInvoicePaid ;
2019-05-09 07:29:31 +02:00
use App\Jobs\Invoice\StoreInvoice ;
2019-04-02 07:16:39 +02:00
use App\Models\Invoice ;
2019-04-23 08:19:45 +02:00
use App\Repositories\BaseRepository ;
2019-04-17 02:58:23 +02:00
use App\Repositories\InvoiceRepository ;
2019-04-16 05:28:05 +02:00
use App\Transformers\InvoiceTransformer ;
2019-04-02 08:36:49 +02:00
use App\Utils\Traits\MakesHash ;
2018-10-17 14:26:27 +02:00
use Illuminate\Http\Request ;
2019-04-23 06:16:41 +02:00
use Illuminate\Support\Facades\Log ;
2018-10-17 14:26:27 +02:00
2019-04-04 11:28:53 +02:00
/**
2019-04-18 00:30:59 +02:00
* Class InvoiceController
* @ package App\Http\Controllers\InvoiceController
2019-04-04 11:28:53 +02:00
*/
2019-03-28 22:34:58 +01:00
class InvoiceController extends BaseController
2018-10-17 14:26:27 +02:00
{
2019-03-28 22:34:58 +01:00
2019-04-02 07:16:39 +02:00
use MakesHash ;
2019-04-04 01:17:15 +02:00
protected $entity_type = Invoice :: class ;
2019-04-02 07:16:39 +02:00
2019-04-04 01:17:15 +02:00
protected $entity_transformer = InvoiceTransformer :: class ;
2019-04-02 07:16:39 +02:00
/**
2019-04-18 00:30:59 +02:00
* @ var InvoiceRepository
2019-04-02 07:16:39 +02:00
*/
2019-04-04 01:17:15 +02:00
protected $invoice_repo ;
2019-04-02 07:16:39 +02:00
/**
2019-04-18 00:30:59 +02:00
* InvoiceController constructor .
*
* @ param \App\Repositories\InvoiceRepository $invoice_repo The invoice repo
2019-04-02 07:16:39 +02:00
*/
2019-04-17 02:58:23 +02:00
public function __construct ( InvoiceRepository $invoice_repo )
2019-03-28 22:34:58 +01:00
{
2019-04-18 00:30:59 +02:00
2019-03-28 22:34:58 +01:00
parent :: __construct ();
2019-04-04 01:17:15 +02:00
$this -> invoice_repo = $invoice_repo ;
2019-04-02 07:16:39 +02:00
2019-03-28 22:34:58 +01:00
}
2019-04-02 07:16:39 +02:00
2018-10-17 14:26:27 +02:00
/**
2019-04-18 00:30:59 +02:00
* Show the list of Invoices
*
* @ param \App\Filters\InvoiceFilters $filters The filters
*
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
* @ OA\Get (
* path = " /api/v1/invoices " ,
* operationId = " getInvoices " ,
* tags = { " invoices " },
* summary = " Gets a list of invoices " ,
* description = " Lists invoices, search and filters allow fine grained lists to be generated.
Query parameters can be added to performed more fine grained filtering of the invoices , these are handled by the InvoiceFilters class which defines the methods available " ,
* @ 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 list of invoices " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
2018-10-17 14:26:27 +02:00
*/
2019-04-17 02:58:23 +02:00
public function index ( InvoiceFilters $filters )
2018-10-17 14:26:27 +02:00
{
2019-04-02 07:16:39 +02:00
2019-04-04 03:38:17 +02:00
$invoices = Invoice :: filter ( $filters );
2019-04-02 08:36:49 +02:00
2019-04-04 03:38:17 +02:00
return $this -> listResponse ( $invoices );
2019-04-02 07:16:39 +02:00
2018-10-17 14:26:27 +02:00
}
/**
* Show the form for creating a new resource .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\CreateInvoiceRequest $request The request
*
2018-10-17 14:26:27 +02:00
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
*
* @ OA\Get (
* path = " /api/v1/invoices/create " ,
* operationId = " getInvoicesCreate " ,
* tags = { " invoices " },
* summary = " Gets a new blank invoice 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 invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
2018-10-17 14:26:27 +02:00
*/
2019-04-16 05:28:05 +02:00
public function create ( CreateInvoiceRequest $request )
2018-10-17 14:26:27 +02:00
{
2019-04-18 00:30:59 +02:00
2019-04-15 02:10:54 +02:00
$invoice = InvoiceFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id );
return $this -> itemResponse ( $invoice );
2019-04-18 00:30:59 +02:00
2018-10-17 14:26:27 +02:00
}
2019-04-18 00:30:59 +02:00
2018-10-17 14:26:27 +02:00
/**
* Store a newly created resource in storage .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\StoreInvoiceRequest $request The request
*
2018-10-17 14:26:27 +02:00
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
*
* @ OA\Post (
* path = " /api/v1/invoices " ,
* operationId = " storeInvoice " ,
* tags = { " invoices " },
* summary = " Adds a invoice " ,
* description = " Adds an invoice to the system " ,
* @ 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 invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
*/
2019-04-16 05:28:05 +02:00
public function store ( StoreInvoiceRequest $request )
2018-10-17 14:26:27 +02:00
{
2019-04-16 05:28:05 +02:00
2019-05-06 07:34:59 +02:00
$invoice = $this -> invoice_repo -> save ( $request -> all (), InvoiceFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id ));
2019-04-16 07:28:30 +02:00
2019-05-11 05:32:07 +02:00
$invoice = StoreInvoice :: dispatchNow ( $invoice , $request -> all ()); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI
2019-05-09 07:29:31 +02:00
2019-05-15 11:20:52 +02:00
event ( new InvoiceWasCreated ( $invoice ));
2019-04-16 05:28:05 +02:00
return $this -> itemResponse ( $invoice );
2018-10-17 14:26:27 +02:00
}
/**
* Display the specified resource .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\ShowInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 14:26:27 +02:00
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
*
* @ OA\Get (
* path = " /api/v1/invoices/ { id} " ,
* operationId = " showInvoice " ,
* tags = { " invoices " },
* summary = " Shows an invoice " ,
* description = " Displays an invoice 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 Invoice Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Returns the invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
*/
2019-04-16 07:28:30 +02:00
public function show ( ShowInvoiceRequest $request , Invoice $invoice )
2018-10-17 14:26:27 +02:00
{
2019-04-16 07:28:30 +02:00
return $this -> itemResponse ( $invoice );
2018-10-17 14:26:27 +02:00
}
/**
* Show the form for editing the specified resource .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\EditInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 14:26:27 +02:00
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
* @ OA\Get (
* path = " /api/v1/invoices/ { id}/edit " ,
* operationId = " editInvoice " ,
* tags = { " invoices " },
* summary = " Shows an invoice for editting " ,
* description = " Displays an invoice 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 Invoice Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Returns the invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
*/
2019-04-17 02:58:23 +02:00
public function edit ( EditInvoiceRequest $request , Invoice $invoice )
2018-10-17 14:26:27 +02:00
{
2019-04-16 07:28:30 +02:00
return $this -> itemResponse ( $invoice );
2018-10-17 14:26:27 +02:00
}
2019-04-18 00:30:59 +02:00
2018-10-17 14:26:27 +02:00
/**
* Update the specified resource in storage .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\UpdateInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 14:26:27 +02:00
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
*
* @ OA\Put (
* path = " /api/v1/invoices/ { id} " ,
* operationId = " updateInvoice " ,
* tags = { " invoices " },
* summary = " Updates an invoice " ,
* description = " Handles the updating of an invoice 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 Invoice Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Returns the invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
*/
2019-04-16 07:28:30 +02:00
public function update ( UpdateInvoiceRequest $request , Invoice $invoice )
2018-10-17 14:26:27 +02:00
{
2019-04-16 07:28:30 +02:00
2019-05-06 07:34:59 +02:00
$invoice = $this -> invoice_repo -> save ( $request -> all (), $invoice );
2019-04-16 07:28:30 +02:00
2019-10-07 12:21:02 +02:00
event ( new InvoiceWasUpdated ( $invoice ));
2019-05-15 11:20:52 +02:00
2019-04-16 07:28:30 +02:00
return $this -> itemResponse ( $invoice );
2018-10-17 14:26:27 +02:00
}
/**
* Remove the specified resource from storage .
*
2019-04-18 00:30:59 +02:00
* @ param \App\Http\Requests\Invoice\DestroyInvoiceRequest $request
* @ param \App\Models\Invoice $invoice
*
* @ return \Illuminate\Http\Response
2019-10-07 00:00:02 +02:00
*
* @ OA\Delete (
* path = " /api/v1/invoices/ { id} " ,
* operationId = " deleteInvoice " ,
* tags = { " invoices " },
* summary = " Deletes a invoice " ,
* description = " Handles the deletion of an invoice 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 Invoice Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Returns a HTTP status " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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 " ),
* ),
* )
*
2018-10-17 14:26:27 +02:00
*/
2019-04-16 05:28:05 +02:00
public function destroy ( DestroyInvoiceRequest $request , Invoice $invoice )
2018-10-17 14:26:27 +02:00
{
2019-04-16 07:28:30 +02:00
$invoice -> delete ();
return response () -> json ([], 200 );
2019-04-17 02:58:23 +02:00
2018-10-17 14:26:27 +02:00
}
2019-04-23 06:16:41 +02:00
2019-04-23 08:19:45 +02:00
/**
* Perform bulk actions on the list view
*
* @ return Collection
2019-10-07 00:00:02 +02:00
*
* @ OA\Post (
* path = " /api/v1/invoices/bulk " ,
* operationId = " bulkInvoices " ,
* tags = { " invoices " },
* summary = " Performs bulk actions on an array of invoices " ,
* description = " " ,
* @ 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/index " ),
* @ OA\RequestBody (
* description = " User credentials " ,
* 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 Company User response " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/CompanyUser " ),
* ),
* @ 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 " ),
* ),
* )
*
2019-04-23 08:19:45 +02:00
*/
public function bulk ()
{
$action = request () -> input ( 'action' );
$ids = request () -> input ( 'ids' );
2019-11-30 03:42:44 +01:00
$invoices = Invoice :: withTrashed () -> whereIn ( 'id' , $this -> transformKeys ( $ids ));
2019-11-26 11:27:44 +01:00
if ( ! $invoices )
return response () -> json ([ 'message' => 'No Invoices Found' ]);
2019-04-23 08:19:45 +02:00
$invoices -> each ( function ( $invoice , $key ) use ( $action ){
2019-12-02 05:18:36 +01:00
// $this->invoice_repo->{$action}($invoice);
2019-04-23 08:19:45 +02:00
if ( auth () -> user () -> can ( 'edit' , $invoice ))
2019-12-02 05:18:36 +01:00
$this -> performAction ( $invoice , $action , true );
2019-04-23 08:19:45 +02:00
});
2019-11-21 09:38:57 +01:00
return $this -> listResponse ( Invoice :: withTrashed () -> whereIn ( 'id' , $this -> transformKeys ( $ids )));
2019-04-23 08:19:45 +02:00
}
2019-10-07 00:00:02 +02:00
/**
*
* @ OA\Get (
* path = " /api/v1/invoices/ { id}/ { action} " ,
* operationId = " actionInvoice " ,
* tags = { " invoices " },
* summary = " Performs a custom action on an invoice " ,
* description = " Performs a custom action on an invoice.
The current range of actions are as follows
- clone_to_invoice
- clone_to_quote
- history
- delivery_note
- mark_paid
- download
- archive
- delete
- email " ,
* @ 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 Invoice Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Parameter (
* name = " action " ,
* in = " path " ,
* description = " The action string to be performed " ,
* example = " clone_to_quote " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Returns the invoice object " ,
2019-10-07 08:31:26 +02:00
* @ OA\Header ( header = " X-API-Version " , ref = " #/components/headers/X-API-Version " ),
2019-10-07 00:00:02 +02:00
* @ 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/Invoice " ),
* ),
* @ 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 " ),
* ),
* )
*
*/
2019-04-23 06:16:41 +02:00
public function action ( ActionInvoiceRequest $request , Invoice $invoice , $action )
{
2019-12-02 05:18:36 +01:00
return $this -> performAction ( $invoice , $action );
}
private function performAction ( Invoice $invoice , $action , $bulk = false )
{
/*If we are using bulk actions, we don't want to return anything */
2019-04-23 06:16:41 +02:00
switch ( $action ) {
2019-04-23 13:44:41 +02:00
case 'clone_to_invoice' :
2019-12-02 05:18:36 +01:00
$invoice = CloneInvoiceFactory :: create ( $invoice , auth () -> user () -> id );
2019-04-23 06:16:41 +02:00
return $this -> itemResponse ( $invoice );
break ;
case 'clone_to_quote' :
2019-04-23 15:17:49 +02:00
$quote = CloneInvoiceToQuoteFactory :: create ( $invoice , auth () -> user () -> id );
// todo build the quote transformer and return response here
2019-04-23 06:16:41 +02:00
break ;
case 'history' :
# code...
break ;
case 'delivery_note' :
# code...
break ;
case 'mark_paid' :
2019-05-23 00:28:03 +02:00
if ( $invoice -> balance <= 0 || $invoice -> status_id == Invoice :: STATUS_PAID )
2019-05-16 01:40:53 +02:00
return $this -> errorResponse ([ 'message' => 'Invoice has no balance owing' ], 400 );
2019-05-16 08:00:27 +02:00
$invoice = MarkInvoicePaid :: dispatchNow ( $invoice );
2019-12-02 05:18:36 +01:00
if ( ! $bulk )
2019-05-16 01:40:53 +02:00
return $this -> itemResponse ( $invoice );
2019-04-23 06:16:41 +02:00
break ;
2019-11-19 22:06:48 +01:00
case 'mark_sent' :
$invoice -> markSent ();
2019-12-02 05:18:36 +01:00
if ( ! $bulk )
2019-11-19 22:06:48 +01:00
return $this -> itemResponse ( $invoice );
break ;
2019-05-16 08:00:27 +02:00
case 'download' :
2019-11-25 10:38:55 +01:00
return response () -> download ( public_path ( $invoice -> pdf_file_path ()));
2019-05-16 08:00:27 +02:00
break ;
2019-04-23 06:16:41 +02:00
case 'archive' :
2019-11-25 10:38:55 +01:00
$this -> invoice_repo -> archive ( $invoice );
2019-12-02 05:18:36 +01:00
if ( ! $bulk )
2019-11-25 10:38:55 +01:00
return $this -> listResponse ( $invoice );
2019-04-23 06:16:41 +02:00
break ;
case 'delete' :
2019-11-25 10:38:55 +01:00
$this -> invoice_repo -> delete ( $invoice );
2019-12-02 05:18:36 +01:00
if ( ! $bulk )
2019-11-25 10:38:55 +01:00
return $this -> listResponse ( $invoice );
2019-04-23 06:16:41 +02:00
break ;
case 'email' :
2019-12-02 05:18:36 +01:00
if ( ! $bulk )
2019-11-25 10:38:55 +01:00
return response () -> json ([ 'message' => 'email sent' ], 200 );
2019-04-23 06:16:41 +02:00
break ;
default :
2019-11-25 10:38:55 +01:00
return response () -> json ([ 'message' => " The requested action ` { $action } ` is not available. " ], 400 );
2019-04-23 06:16:41 +02:00
break ;
}
}
2019-12-02 05:18:36 +01:00
2018-10-17 14:26:27 +02:00
}