mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-14 09:34:10 -08:00
9aac1cbba4
Signed-off-by: snipe <snipe@snipe.net> # Conflicts: # README.md # app/Console/Commands/MoveUploadsToNewDisk.php # app/Http/Controllers/ActionlogController.php # app/Http/Controllers/Api/LicensesController.php # app/Http/Controllers/Api/StatuslabelsController.php # app/Http/Controllers/Assets/AssetCheckinController.php # app/Http/Controllers/Licenses/LicensesController.php # app/Http/Controllers/Users/BulkUsersController.php # app/Http/Requests/AssetCheckoutRequest.php # app/Importer/LicenseImporter.php # app/Models/Actionlog.php # app/Models/License.php # app/Models/User.php # app/Observers/AssetObserver.php # composer.lock # config/version.php # database/factories/LicenseFactory.php # database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php # database/migrations/2018_10_18_191228_add_kits_licenses_table.php # database/migrations/2018_10_19_153910_add_kits_table.php # database/migrations/2018_10_19_154013_add_kits_models_table.php # database/migrations/2019_02_07_185953_add_kits_consumables_table.php # database/migrations/2019_02_07_190030_add_kits_accessories_table.php # package-lock.json # package.json # public/css/dist/all.css # public/css/dist/bootstrap-table.css # public/js/dist/bootstrap-table.js # public/mix-manifest.json # resources/lang/ar/general.php # resources/lang/ar/passwords.php # resources/lang/cs/general.php # resources/lang/cs/passwords.php # resources/lang/de/admin/custom_fields/general.php # resources/lang/de/admin/settings/general.php # resources/lang/de/admin/settings/message.php # resources/lang/fr/admin/custom_fields/general.php # resources/lang/fr/admin/hardware/general.php # resources/lang/fr/admin/locations/table.php # resources/lang/fr/admin/settings/message.php # resources/lang/hu/admin/custom_fields/general.php # resources/lang/hu/admin/settings/general.php # resources/lang/hu/general.php # resources/lang/it/admin/settings/general.php # resources/lang/nl/admin/custom_fields/general.php # resources/lang/nl/admin/settings/general.php # resources/lang/nl/general.php # resources/lang/pl/admin/custom_fields/general.php # resources/lang/sv-SE/passwords.php # resources/lang/tr/general.php # resources/views/hardware/view.blade.php # resources/views/partials/bootstrap-table.blade.php # resources/views/reports/activity.blade.php # resources/views/users/print.blade.php
272 lines
9.7 KiB
PHP
272 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Transformers\LicenseSeatsTransformer;
|
|
use App\Http\Transformers\LicensesTransformer;
|
|
use App\Http\Transformers\SelectlistTransformer;
|
|
use App\Models\Company;
|
|
use App\Models\License;
|
|
use App\Models\LicenseSeat;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LicensesController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('view', License::class);
|
|
$licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'supplier','category')->withCount('freeSeats as free_seats_count'));
|
|
|
|
|
|
if ($request->filled('company_id')) {
|
|
$licenses->where('company_id', '=', $request->input('company_id'));
|
|
}
|
|
|
|
if ($request->filled('name')) {
|
|
$licenses->where('licenses.name', '=', $request->input('name'));
|
|
}
|
|
|
|
if ($request->filled('product_key')) {
|
|
$licenses->where('licenses.serial', '=', $request->input('product_key'));
|
|
}
|
|
|
|
if ($request->filled('order_number')) {
|
|
$licenses->where('order_number', '=', $request->input('order_number'));
|
|
}
|
|
|
|
if ($request->filled('purchase_order')) {
|
|
$licenses->where('purchase_order', '=', $request->input('purchase_order'));
|
|
}
|
|
|
|
if ($request->filled('license_name')) {
|
|
$licenses->where('license_name', '=', $request->input('license_name'));
|
|
}
|
|
|
|
if ($request->filled('license_email')) {
|
|
$licenses->where('license_email', '=', $request->input('license_email'));
|
|
}
|
|
|
|
if ($request->filled('manufacturer_id')) {
|
|
$licenses->where('manufacturer_id', '=', $request->input('manufacturer_id'));
|
|
}
|
|
|
|
if ($request->filled('supplier_id')) {
|
|
$licenses->where('supplier_id', '=', $request->input('supplier_id'));
|
|
}
|
|
|
|
if ($request->filled('category_id')) {
|
|
$licenses->where('category_id', '=', $request->input('category_id'));
|
|
}
|
|
|
|
if ($request->filled('depreciation_id')) {
|
|
$licenses->where('depreciation_id', '=', $request->input('depreciation_id'));
|
|
}
|
|
|
|
if ($request->filled('supplier_id')) {
|
|
$licenses->where('supplier_id', '=', $request->input('supplier_id'));
|
|
}
|
|
|
|
if (($request->filled('maintained')) && ($request->input('maintained')=='true')) {
|
|
$licenses->where('maintained','=',1);
|
|
} elseif (($request->filled('maintained')) && ($request->input('maintained')=='false')) {
|
|
$licenses->where('maintained','=',0);
|
|
}
|
|
|
|
if (($request->filled('expires')) && ($request->input('expires')=='true')) {
|
|
$licenses->whereNotNull('expiration_date');
|
|
} elseif (($request->filled('expires')) && ($request->input('expires')=='false')) {
|
|
$licenses->whereNull('expiration_date');
|
|
}
|
|
|
|
if ($request->filled('search')) {
|
|
$licenses = $licenses->TextSearch($request->input('search'));
|
|
}
|
|
|
|
if ($request->input('deleted')=='true') {
|
|
$licenses->onlyTrashed();
|
|
}
|
|
|
|
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which
|
|
// case we override with the actual count, so we should return 0 items.
|
|
$offset = (($licenses) && ($request->get('offset') > $licenses->count())) ? $licenses->count() : $request->get('offset', 0);
|
|
|
|
// Check to make sure the limit is not higher than the max allowed
|
|
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
|
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
switch ($request->input('sort')) {
|
|
case 'manufacturer':
|
|
$licenses = $licenses->leftJoin('manufacturers', 'licenses.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order);
|
|
break;
|
|
case 'supplier':
|
|
$licenses = $licenses->leftJoin('suppliers', 'licenses.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order);
|
|
break;
|
|
case 'category':
|
|
$licenses = $licenses->leftJoin('categories', 'licenses.category_id', '=', 'categories.id')->orderBy('categories.name', $order);
|
|
break;
|
|
case 'depreciation':
|
|
$licenses = $licenses->leftJoin('depreciations', 'licenses.depreciation_id', '=', 'depreciations.id')->orderBy('depreciations.name', $order);
|
|
break;
|
|
case 'company':
|
|
$licenses = $licenses->leftJoin('companies', 'licenses.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
|
|
break;
|
|
default:
|
|
$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',
|
|
'termination_date',
|
|
'depreciation_id',
|
|
];
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
|
$licenses = $licenses->orderBy($sort, $order);
|
|
break;
|
|
}
|
|
|
|
$total = $licenses->count();
|
|
|
|
$licenses = $licenses->skip($offset)->take($limit)->get();
|
|
return (new LicensesTransformer)->transformLicenses($licenses, $total);
|
|
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
$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()));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$this->authorize('view', License::class);
|
|
$license = License::withCount('freeSeats')->findOrFail($id);
|
|
$license = $license->load('assignedusers', 'licenseSeats.user', 'licenseSeats.asset');
|
|
|
|
return (new LicensesTransformer)->transformLicense($license);
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
$this->authorize('update', License::class);
|
|
|
|
$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());
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
$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(['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')));
|
|
}
|
|
|
|
/**
|
|
* Gets a paginated collection for the select2 menus
|
|
*
|
|
* @see \App\Http\Transformers\SelectlistTransformer
|
|
*/
|
|
public function selectlist(Request $request)
|
|
{
|
|
$licenses = License::select([
|
|
'licenses.id',
|
|
'licenses.name',
|
|
]);
|
|
|
|
if ($request->filled('search')) {
|
|
$licenses = $licenses->where('licenses.name', 'LIKE', '%'.$request->get('search').'%');
|
|
}
|
|
|
|
$licenses = $licenses->orderBy('name', 'ASC')->paginate(50);
|
|
|
|
return (new SelectlistTransformer)->transformSelectlist($licenses);
|
|
}
|
|
}
|