2017-01-12 19:40:20 -08:00
< ? php
namespace App\Http\Controllers\Api ;
2018-02-25 12:10:02 -08:00
use App\Helpers\Helper ;
2017-01-12 19:40:20 -08:00
use App\Http\Controllers\Controller ;
2018-01-10 18:47:57 -08:00
use App\Http\Transformers\LicenseSeatsTransformer ;
2018-02-25 12:10:02 -08:00
use App\Http\Transformers\LicensesTransformer ;
use App\Models\Company ;
2017-01-24 18:56:27 -08:00
use App\Models\License ;
2018-01-10 18:47:57 -08:00
use App\Models\LicenseSeat ;
2018-02-25 12:10:02 -08:00
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
2017-01-12 19:40:20 -08:00
class LicensesController extends Controller
{
/**
* Display a listing of the resource .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
*
* @ return \Illuminate\Http\Response
*/
2017-01-24 18:56:27 -08:00
public function index ( Request $request )
2017-01-12 19:40:20 -08:00
{
2017-01-24 18:56:27 -08:00
$this -> authorize ( 'view' , License :: class );
2019-05-24 15:44:18 -07:00
$licenses = Company :: scopeCompanyables ( License :: with ( 'company' , 'manufacturer' , 'freeSeats' , 'supplier' , 'category' ) -> withCount ( 'freeSeats as free_seats_count' ));
2017-01-24 18:56:27 -08:00
2017-03-11 02:49:24 -08:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'company_id' )) {
2017-03-11 02:49:24 -08:00
$licenses -> where ( 'company_id' , '=' , $request -> input ( 'company_id' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'name' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'licenses.name' , '=' , $request -> input ( 'name' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'product_key' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'licenses.serial' , '=' , $request -> input ( 'product_key' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'order_number' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'order_number' , '=' , $request -> input ( 'order_number' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'purchase_order' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'purchase_order' , '=' , $request -> input ( 'purchase_order' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'license_name' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'license_name' , '=' , $request -> input ( 'license_name' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'license_email' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'license_email' , '=' , $request -> input ( 'license_email' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'manufacturer_id' )) {
2017-03-11 02:49:24 -08:00
$licenses -> where ( 'manufacturer_id' , '=' , $request -> input ( 'manufacturer_id' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'supplier_id' )) {
2017-08-10 14:38:04 -07:00
$licenses -> where ( 'supplier_id' , '=' , $request -> input ( 'supplier_id' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'category_id' )) {
2018-07-16 14:23:24 -07:00
$licenses -> where ( 'category_id' , '=' , $request -> input ( 'category_id' ));
2018-05-04 21:01:38 -07:00
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'depreciation_id' )) {
2017-03-11 02:57:19 -08:00
$licenses -> where ( 'depreciation_id' , '=' , $request -> input ( 'depreciation_id' ));
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'supplier_id' )) {
2017-03-11 02:49:24 -08:00
$licenses -> where ( 'supplier_id' , '=' , $request -> input ( 'supplier_id' ));
}
2018-01-11 15:17:34 -08:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'search' )) {
2018-01-11 15:17:34 -08:00
$licenses = $licenses -> TextSearch ( $request -> input ( 'search' ));
}
2019-02-14 14:49:08 -08:00
$offset = (( $licenses ) && ( request ( 'offset' ) > $licenses -> count ())) ? 0 : request ( 'offset' , 0 );
2019-09-03 14:02:08 -07:00
// Check to make sure the limit is not higher than the max allowed
2019-09-03 20:28:49 -07:00
(( config ( 'app.max_results' ) >= $request -> input ( 'limit' )) && ( $request -> filled ( 'limit' ))) ? $limit = $request -> input ( 'limit' ) : $limit = config ( 'app.max_results' );
2019-09-03 14:02:08 -07:00
2017-01-24 18:56:27 -08:00
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
2017-08-10 14:38:04 -07:00
switch ( $request -> input ( 'sort' )) {
2017-11-02 20:01:39 -07:00
case 'manufacturer' :
$licenses = $licenses -> leftJoin ( 'manufacturers' , 'licenses.manufacturer_id' , '=' , 'manufacturers.id' ) -> orderBy ( 'manufacturers.name' , $order );
2017-01-24 18:56:27 -08:00
break ;
2017-08-10 14:38:04 -07:00
case 'supplier' :
2017-11-02 20:01:39 -07:00
$licenses = $licenses -> leftJoin ( 'suppliers' , 'licenses.supplier_id' , '=' , 'suppliers.id' ) -> orderBy ( 'suppliers.name' , $order );
2017-08-10 14:38:04 -07:00
break ;
2018-05-04 21:01:38 -07:00
case 'category' :
$licenses = $licenses -> leftJoin ( 'categories' , 'licenses.category_id' , '=' , 'categories.id' ) -> orderBy ( 'categories.name' , $order );
break ;
2017-01-24 18:56:27 -08:00
case 'company' :
2017-11-02 20:01:39 -07:00
$licenses = $licenses -> leftJoin ( 'companies' , 'licenses.company_id' , '=' , 'companies.id' ) -> orderBy ( 'companies.name' , $order );
2017-01-24 18:56:27 -08:00
break ;
default :
2018-05-04 21:01:38 -07:00
$allowed_columns = [ 'id' , 'name' , 'purchase_cost' , 'expiration_date' , 'purchase_order' , 'order_number' , 'notes' , 'purchase_date' , 'serial' , 'company' , 'category' , 'license_name' , 'license_email' , 'free_seats_count' , 'seats' ];
2017-08-10 14:38:04 -07:00
$sort = in_array ( $request -> input ( 'sort' ), $allowed_columns ) ? e ( $request -> input ( 'sort' )) : 'created_at' ;
2017-01-24 18:56:27 -08:00
$licenses = $licenses -> orderBy ( $sort , $order );
break ;
}
2018-01-11 15:17:34 -08:00
2017-01-24 18:56:27 -08:00
$total = $licenses -> count ();
2017-11-02 20:01:39 -07:00
2017-01-24 18:56:27 -08:00
$licenses = $licenses -> skip ( $offset ) -> take ( $limit ) -> get ();
return ( new LicensesTransformer ) -> transformLicenses ( $licenses , $total );
2017-01-12 19:40:20 -08:00
}
2017-01-24 18:56:27 -08:00
2017-01-12 19:40:20 -08:00
/**
* Store a newly created resource in storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\Response
*/
public function store ( Request $request )
{
//
2018-02-25 12:10:02 -08:00
$this -> authorize ( 'create' , License :: class );
$license = new License ;
$license -> fill ( $request -> all ());
if ( $license -> save ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $license , trans ( 'admin/licenses/message.create.success' )));
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $license -> getErrors ()));
2017-01-12 19:40:20 -08:00
}
/**
* Display the specified resource .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $id
* @ return \Illuminate\Http\Response
*/
public function show ( $id )
{
2018-02-25 12:10:02 -08:00
$this -> authorize ( 'view' , License :: class );
$license = License :: findOrFail ( $id );
$license = $license -> load ( 'assignedusers' , 'licenseSeats.user' , 'licenseSeats.asset' );
return ( new LicensesTransformer ) -> transformLicense ( $license );
2017-01-12 19:40:20 -08:00
}
/**
* Update the specified resource in storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ param \Illuminate\Http\Request $request
* @ param int $id
* @ return \Illuminate\Http\Response
*/
public function update ( Request $request , $id )
{
//
2018-07-12 18:28:02 -07:00
$this -> authorize ( 'update' , License :: class );
2018-02-25 12:10:02 -08:00
$license = License :: findOrFail ( $id );
$license -> fill ( $request -> all ());
if ( $license -> save ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $license , trans ( 'admin/licenses/message.update.success' )));
}
return Helper :: formatStandardApiResponse ( 'error' , null , $license -> getErrors ());
2017-01-12 19:40:20 -08:00
}
/**
* Remove the specified resource from storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ param int $id
* @ return \Illuminate\Http\Response
*/
public function destroy ( $id )
{
//
2018-02-25 12:10:02 -08:00
$license = License :: findOrFail ( $id );
$this -> authorize ( 'delete' , $license );
if ( $license -> assigned_seats_count == 0 ) {
// Delete the license and the associated license seats
DB :: table ( 'license_seats' )
-> where ( 'id' , $license -> id )
-> update ( array ( 'assigned_to' => null , 'asset_id' => null ));
$licenseSeats = $license -> licenseseats ();
$licenseSeats -> delete ();
$license -> delete ();
// Redirect to the licenses management page
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , null , trans ( 'admin/licenses/message.delete.success' )));
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/licenses/message.assoc_users' )));
2017-01-12 19:40:20 -08:00
}
2018-01-10 18:47:57 -08:00
/**
* Get license seat listing
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param int $licenseId
* @ return \Illuminate\Contracts\View\View
*/
public function seats ( Request $request , $licenseId )
{
if ( $license = License :: find ( $licenseId )) {
2018-07-12 18:28:02 -07:00
$this -> authorize ( 'view' , $license );
2018-01-10 18:47:57 -08:00
$seats = LicenseSeat :: where ( 'license_id' , $licenseId ) -> with ( 'license' , 'user' , 'asset' );
2019-05-02 15:20:47 -07:00
$offset = (( $seats ) && ( request ( 'offset' ) > $seats -> count ())) ? 0 : request ( 'offset' , 0 );
2018-01-10 18:47:57 -08:00
$limit = request ( 'limit' , 50 );
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
$total = $seats -> count ();
$seats = $seats -> skip ( $offset ) -> take ( $limit ) -> get ();
if ( $seats ) {
return ( new LicenseSeatsTransformer ) -> transformLicenseSeats ( $seats , $total );
}
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/licenses/message.does_not_exist' )), 200 );
}
2017-01-12 19:40:20 -08:00
}