snipe-it/app/Http/Controllers/Api/ConsumablesController.php

252 lines
9 KiB
PHP
Raw Normal View History

2017-01-12 19:40:20 -08:00
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
2017-01-26 18:46:18 -08:00
use App\Models\Company;
use App\Models\Consumable;
2020-07-22 15:46:02 -07:00
use App\Models\User;
2017-01-26 18:46:18 -08:00
use App\Http\Transformers\ConsumablesTransformer;
use App\Helpers\Helper;
2017-01-12 19:40:20 -08:00
class ConsumablesController extends Controller
{
/**
* Display a listing of the resource.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
*
* @return \Illuminate\Http\Response
*/
2017-01-26 18:46:18 -08:00
public function index(Request $request)
2017-01-12 19:40:20 -08:00
{
2017-01-26 18:46:18 -08:00
$this->authorize('index', Consumable::class);
$consumables = Company::scopeCompanyables(
Consumable::select('consumables.*')
->with('company', 'location', 'category', 'users', 'manufacturer')
);
2019-05-23 17:39:50 -07:00
if ($request->filled('search')) {
2017-01-26 18:46:18 -08:00
$consumables = $consumables->TextSearch(e($request->input('search')));
}
2019-05-23 17:39:50 -07:00
if ($request->filled('company_id')) {
$consumables->where('company_id','=',$request->input('company_id'));
}
2019-05-23 17:39:50 -07:00
if ($request->filled('category_id')) {
$consumables->where('category_id','=',$request->input('category_id'));
}
2019-05-23 17:39:50 -07:00
if ($request->filled('manufacturer_id')) {
$consumables->where('manufacturer_id','=',$request->input('manufacturer_id'));
}
// 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 = (($consumables) && ($request->get('offset') > $consumables->count())) ? $consumables->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');
$allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','company','category','model_number', 'item_no', 'manufacturer','location','qty','image'];
2017-01-26 18:46:18 -08:00
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
2017-01-26 18:46:18 -08:00
switch ($sort) {
case 'category':
$consumables = $consumables->OrderCategory($order);
break;
case 'location':
$consumables = $consumables->OrderLocation($order);
break;
case 'manufacturer':
$consumables = $consumables->OrderManufacturer($order);
break;
case 'company':
$consumables = $consumables->OrderCompany($order);
break;
default:
$consumables = $consumables->orderBy($sort, $order);
break;
}
2017-01-26 18:46:18 -08:00
$total = $consumables->count();
$consumables = $consumables->skip($offset)->take($limit)->get();
return (new ConsumablesTransformer)->transformConsumables($consumables, $total);
2017-01-12 19:40:20 -08:00
}
/**
* Store a newly created resource in storage.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
2017-01-12 23:43:45 -08:00
* @param \Illuminate\Http\Request $request
2017-01-12 19:40:20 -08:00
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
2017-01-26 18:46:18 -08:00
$this->authorize('create', Consumable::class);
$consumable = new Consumable;
$consumable->fill($request->all());
if ($consumable->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $consumable, trans('admin/consumables/message.create.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $consumable->getErrors()));
2017-01-12 19:40:20 -08:00
}
/**
* Display the specified resource.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
2017-01-12 23:43:45 -08:00
* @param int $id
2017-01-12 19:40:20 -08:00
* @return \Illuminate\Http\Response
*/
public function show($id)
{
2017-01-26 18:46:18 -08:00
$this->authorize('view', Consumable::class);
$consumable = Consumable::findOrFail($id);
return (new ConsumablesTransformer)->transformConsumable($consumable);
2017-01-12 19:40:20 -08:00
}
/**
* Update the specified resource in storage.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
2017-01-12 23:43:45 -08:00
* @param \Illuminate\Http\Request $request
* @param int $id
2017-01-12 19:40:20 -08:00
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->authorize('update', Consumable::class);
2017-01-26 18:46:18 -08:00
$consumable = Consumable::findOrFail($id);
$consumable->fill($request->all());
if ($consumable->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $consumable, trans('admin/consumables/message.update.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $consumable->getErrors()));
2017-01-12 19:40:20 -08:00
}
/**
* Remove the specified resource from storage.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
2017-01-12 23:43:45 -08:00
* @param int $id
2017-01-12 19:40:20 -08:00
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2017-01-26 18:46:18 -08:00
$this->authorize('delete', Consumable::class);
$consumable = Consumable::findOrFail($id);
$this->authorize('delete', $consumable);
$consumable->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.delete.success')));
2017-01-12 19:40:20 -08:00
}
2020-07-22 15:46:02 -07:00
/**
* Returns a JSON response containing details on the users associated with this consumable.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @see ConsumablesController::getView() method that returns the form.
* @since [v1.0]
* @param int $consumableId
* @return array
*/
public function getDataView($consumableId)
{
$consumable = Consumable::with(array('consumableAssignments'=>
function ($query) {
$query->orderBy($query->getModel()->getTable().'.created_at', 'DESC');
},
'consumableAssignments.admin'=> function ($query) {
},
'consumableAssignments.user'=> function ($query) {
},
))->find($consumableId);
if (!Company::isCurrentUserHasAccess($consumable)) {
return ['total' => 0, 'rows' => []];
}
$this->authorize('view', Consumable::class);
$rows = array();
foreach ($consumable->consumableAssignments as $consumable_assignment) {
$rows[] = [
'name' => ($consumable_assignment->user) ? $consumable_assignment->user->present()->nameUrl() : 'Deleted User',
Notification improvements (#5254) * Added “show fields in email” to custom fields * Added “show images in email” to settings * Added nicer HTML emails * Break notifications out into their own, instead of trying to mash them all together * Remove old notification for accessory checkout * Janky fix for #5076 - “The asset you have attempted to accept was not checked out to you” * Add method for image url for accessories * Added accessory checkout email blade * Make accessory email notification on checkout screen consistent with assets * Added native consumables notifications * Fixes for asset notification * Updated notification blades with correct-er fields * Updated notifications * License checkin notification - does not work yet Need to figure out whether the license seat is assigned to a person or an asset before we can pass the target * Added alternate “cc” email for admins * Only try to trigger notifications if the target is a user * Fix tests * Fixed consumable URL * Removed unused notification * Pass target type in params * Show slack status * Pass additional parameters There is a logic bug in this :( Will send to slack twice, since the admin CC and the user are both using the same notification. Fuckity fuck fuck fuck. * Pass a variable to the notification to supress the duplicate slack message * Slack is broken :( Trying to fix Will try a git bisect * Put preview back into checkout * Pulled old archaic mail * Removed debugging * Fixed wrong email title * Fixed slack endpoint not firing * Poobot, we hardly knew ye. * Removed old, manual mail from API * Typo :-/ * Code cleanup * Use defined formatted date in JSON * Use static properties for checkin/checkout notifiers for cleaner code * Removed debugging * Use date formatter * Fixed target_type * Fixed language in consumable email
2018-03-25 13:46:57 -07:00
'created_at' => Helper::getFormattedDateObject($consumable_assignment->created_at, 'datetime'),
'admin' => ($consumable_assignment->admin) ? $consumable_assignment->admin->present()->nameUrl() : '',
];
}
$consumableCount = $consumable->users->count();
$data = array('total' => $consumableCount, 'rows' => $rows);
return $data;
}
2020-07-22 15:46:02 -07:00
/**
* Checkout a consumable
*
* @author [A. Gutierrez] [<andres@baller.tv>]
* @param int $id
* @since [v4.9.5]
* @return JsonResponse
*/
public function checkout(Request $request, $id)
{
// Check if the consumable exists
2020-07-22 16:57:06 -07:00
if (is_null($consumable = Consumable::find($id))) {
2020-07-22 15:46:02 -07:00
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.does_not_exist')));
}
$this->authorize('checkout', $consumable);
if ($consumable->qty > 0) {
// Check if the user exists
$assigned_to = $request->input('assigned_to');
if (is_null($user = User::find($assigned_to))) {
// Return error message
return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
}
// Update the consumable data
$consumable->assigned_to = e($assigned_to);
$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
'user_id' => $user->id,
'assigned_to' => $assigned_to
]);
// Log checkout event
$logaction = $consumable->logCheckout(e($request->input('note')), $user);
$data['log_id'] = $logaction->id;
$data['eula'] = $consumable->getEula();
$data['first_name'] = $user->first_name;
$data['item_name'] = $consumable->name;
$data['checkout_date'] = $logaction->created_at;
$data['note'] = $logaction->note;
$data['require_acceptance'] = $consumable->requireAcceptance();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, 'No consumables remaining'));
}
2017-01-12 23:43:45 -08:00
}