2018-10-17 23:26:27 +11:00
< ? php
2019-05-11 13:32:07 +10: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 23:26:27 +11:00
namespace App\Http\Controllers ;
2019-04-23 14:16:41 +10:00
use App\Factory\CloneInvoiceFactory ;
2019-04-23 23:17:49 +10:00
use App\Factory\CloneInvoiceToQuoteFactory ;
2019-04-15 10:10:54 +10:00
use App\Factory\InvoiceFactory ;
2019-04-17 10:58:23 +10:00
use App\Filters\InvoiceFilters ;
2019-04-23 14:16:41 +10:00
use App\Http\Requests\Invoice\ActionInvoiceRequest ;
2019-04-16 13:28:05 +10:00
use App\Http\Requests\Invoice\CreateInvoiceRequest ;
2019-04-17 16:20:32 +10:00
use App\Http\Requests\Invoice\DestroyInvoiceRequest ;
2019-04-16 13:28:05 +10: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 16:19:45 +10:00
use App\Jobs\Entity\ActionEntity ;
2019-05-09 15:29:31 +10:00
use App\Jobs\Invoice\StoreInvoice ;
2019-04-02 16:16:39 +11:00
use App\Models\Invoice ;
2019-04-23 16:19:45 +10:00
use App\Repositories\BaseRepository ;
2019-04-17 10:58:23 +10:00
use App\Repositories\InvoiceRepository ;
2019-04-16 13:28:05 +10:00
use App\Transformers\InvoiceTransformer ;
2019-04-02 17:36:49 +11:00
use App\Utils\Traits\MakesHash ;
2018-10-17 23:26:27 +11:00
use Illuminate\Http\Request ;
2019-04-23 14:16:41 +10:00
use Illuminate\Support\Facades\Log ;
2018-10-17 23:26:27 +11:00
2019-04-04 20:28:53 +11:00
/**
2019-04-18 08:30:59 +10:00
* Class InvoiceController
* @ package App\Http\Controllers\InvoiceController
2019-04-04 20:28:53 +11:00
*/
2019-03-29 08:34:58 +11:00
class InvoiceController extends BaseController
2018-10-17 23:26:27 +11:00
{
2019-03-29 08:34:58 +11:00
2019-04-02 16:16:39 +11:00
use MakesHash ;
2019-04-04 10:17:15 +11:00
protected $entity_type = Invoice :: class ;
2019-04-02 16:16:39 +11:00
2019-04-04 10:17:15 +11:00
protected $entity_transformer = InvoiceTransformer :: class ;
2019-04-02 16:16:39 +11:00
/**
2019-04-18 08:30:59 +10:00
* @ var InvoiceRepository
2019-04-02 16:16:39 +11:00
*/
2019-04-04 10:17:15 +11:00
protected $invoice_repo ;
2019-04-02 16:16:39 +11:00
2019-04-23 16:19:45 +10:00
protected $base_repo ;
2019-04-02 16:16:39 +11:00
/**
2019-04-18 08:30:59 +10:00
* InvoiceController constructor .
*
* @ param \App\Repositories\InvoiceRepository $invoice_repo The invoice repo
2019-04-02 16:16:39 +11:00
*/
2019-04-17 10:58:23 +10:00
public function __construct ( InvoiceRepository $invoice_repo )
2019-03-29 08:34:58 +11:00
{
2019-04-18 08:30:59 +10:00
2019-03-29 08:34:58 +11:00
parent :: __construct ();
2019-04-04 10:17:15 +11:00
$this -> invoice_repo = $invoice_repo ;
2019-04-02 16:16:39 +11:00
2019-03-29 08:34:58 +11:00
}
2019-04-02 16:16:39 +11:00
2018-10-17 23:26:27 +11:00
/**
2019-04-18 08:30:59 +10:00
* Show the list of Invoices
*
* @ param \App\Filters\InvoiceFilters $filters The filters
*
* @ return \Illuminate\Http\Response
2018-10-17 23:26:27 +11:00
*/
2019-04-17 10:58:23 +10:00
public function index ( InvoiceFilters $filters )
2018-10-17 23:26:27 +11:00
{
2019-04-02 16:16:39 +11:00
2019-04-04 12:38:17 +11:00
$invoices = Invoice :: filter ( $filters );
2019-04-02 17:36:49 +11:00
2019-04-04 12:38:17 +11:00
return $this -> listResponse ( $invoices );
2019-04-02 16:16:39 +11:00
2018-10-17 23:26:27 +11:00
}
/**
* Show the form for creating a new resource .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\CreateInvoiceRequest $request The request
*
2018-10-17 23:26:27 +11:00
* @ return \Illuminate\Http\Response
*/
2019-04-16 13:28:05 +10:00
public function create ( CreateInvoiceRequest $request )
2018-10-17 23:26:27 +11:00
{
2019-04-18 08:30:59 +10:00
2019-04-15 10:10:54 +10:00
$invoice = InvoiceFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id );
return $this -> itemResponse ( $invoice );
2019-04-18 08:30:59 +10:00
2018-10-17 23:26:27 +11:00
}
2019-04-18 08:30:59 +10:00
2018-10-17 23:26:27 +11:00
/**
* Store a newly created resource in storage .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\StoreInvoiceRequest $request The request
*
2018-10-17 23:26:27 +11:00
* @ return \Illuminate\Http\Response
*/
2019-04-16 13:28:05 +10:00
public function store ( StoreInvoiceRequest $request )
2018-10-17 23:26:27 +11:00
{
2019-04-16 13:28:05 +10:00
2019-05-06 15:34:59 +10:00
$invoice = $this -> invoice_repo -> save ( $request -> all (), InvoiceFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id ));
2019-04-16 15:28:30 +10:00
2019-05-11 13:32:07 +10: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 15:29:31 +10:00
2019-04-16 13:28:05 +10:00
return $this -> itemResponse ( $invoice );
2018-10-17 23:26:27 +11:00
}
/**
* Display the specified resource .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\ShowInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 23:26:27 +11:00
* @ return \Illuminate\Http\Response
*/
2019-04-16 15:28:30 +10:00
public function show ( ShowInvoiceRequest $request , Invoice $invoice )
2018-10-17 23:26:27 +11:00
{
2019-04-16 15:28:30 +10:00
return $this -> itemResponse ( $invoice );
2018-10-17 23:26:27 +11:00
}
/**
* Show the form for editing the specified resource .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\EditInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 23:26:27 +11:00
* @ return \Illuminate\Http\Response
*/
2019-04-17 10:58:23 +10:00
public function edit ( EditInvoiceRequest $request , Invoice $invoice )
2018-10-17 23:26:27 +11:00
{
2019-04-16 15:28:30 +10:00
return $this -> itemResponse ( $invoice );
2018-10-17 23:26:27 +11:00
}
2019-04-18 08:30:59 +10:00
2018-10-17 23:26:27 +11:00
/**
* Update the specified resource in storage .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\UpdateInvoiceRequest $request The request
* @ param \App\Models\Invoice $invoice The invoice
*
2018-10-17 23:26:27 +11:00
* @ return \Illuminate\Http\Response
*/
2019-04-16 15:28:30 +10:00
public function update ( UpdateInvoiceRequest $request , Invoice $invoice )
2018-10-17 23:26:27 +11:00
{
2019-04-16 15:28:30 +10:00
2019-05-06 15:34:59 +10:00
$invoice = $this -> invoice_repo -> save ( $request -> all (), $invoice );
2019-04-16 15:28:30 +10:00
return $this -> itemResponse ( $invoice );
2018-10-17 23:26:27 +11:00
}
/**
* Remove the specified resource from storage .
*
2019-04-18 08:30:59 +10:00
* @ param \App\Http\Requests\Invoice\DestroyInvoiceRequest $request
* @ param \App\Models\Invoice $invoice
*
* @ return \Illuminate\Http\Response
2018-10-17 23:26:27 +11:00
*/
2019-04-16 13:28:05 +10:00
public function destroy ( DestroyInvoiceRequest $request , Invoice $invoice )
2018-10-17 23:26:27 +11:00
{
2019-04-16 15:28:30 +10:00
$invoice -> delete ();
return response () -> json ([], 200 );
2019-04-17 10:58:23 +10:00
2018-10-17 23:26:27 +11:00
}
2019-04-23 14:16:41 +10:00
2019-04-23 16:19:45 +10:00
/**
* Perform bulk actions on the list view
*
* @ return Collection
*/
public function bulk ()
{
$action = request () -> input ( 'action' );
$ids = request () -> input ( 'ids' );
$invoices = Invoice :: withTrashed () -> find ( $ids );
$invoices -> each ( function ( $invoice , $key ) use ( $action ){
if ( auth () -> user () -> can ( 'edit' , $invoice ))
$this -> invoice_repo -> { $action }( $invoice );
});
//todo need to return the updated dataset
return $this -> listResponse ( Invoice :: withTrashed () -> whereIn ( 'id' , $ids ));
}
2019-04-23 14:16:41 +10:00
public function action ( ActionInvoiceRequest $request , Invoice $invoice , $action )
{
switch ( $action ) {
2019-04-23 21:44:41 +10:00
case 'clone_to_invoice' :
2019-04-23 14:16:41 +10:00
$invoice = CloneInvoiceFactory :: create ( $invoice , auth () -> user () -> id );
return $this -> itemResponse ( $invoice );
break ;
case 'clone_to_quote' :
2019-04-23 23:17:49 +10:00
$quote = CloneInvoiceToQuoteFactory :: create ( $invoice , auth () -> user () -> id );
// todo build the quote transformer and return response here
2019-04-23 14:16:41 +10:00
break ;
case 'history' :
# code...
break ;
case 'delivery_note' :
# code...
break ;
case 'mark_paid' :
# code...
break ;
case 'archive' :
# code...
break ;
case 'delete' :
# code...
break ;
case 'email' :
//dispatch email to queue
break ;
default :
# code...
break ;
}
}
2019-04-18 08:30:59 +10:00
2018-10-17 23:26:27 +11:00
}