Skip to content

A free open source IT asset/license management system

Basic consumables API

snipe5e9e6da57717
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 3f987fcd46..8f608e67e2 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -4,6 +4,10 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Http\Transformers\ConsumablesTransformer;
+use App\Helpers\Helper;
class ConsumablesController extends Controller
{
@@ -15,9 +19,47 @@ class ConsumablesController extends Controller
*
* @return \Illuminate\Http\Response
*/
- public function index()
+ public function index(Request $request)
{
- //
+ $this->authorize('index', Consumable::class);
+ $consumables = Company::scopeCompanyables(
+ Consumable::select('consumables.*')
+ ->whereNull('consumables.deleted_at')
+ ->with('company', 'location', 'category', 'users', 'manufacturer')
+ );
+
+ if ($request->has('search')) {
+ $consumables = $consumables->TextSearch(e($request->input('search')));
+ }
+
+ $offset = request('offset', 0);
+ $limit = request('limit', 50);
+ $allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','company','category','model_number', 'item_no', 'manufacturer','location'];
+ $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
+ $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
+
+ 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;
+ }
+
+ $total = $consumables->count();
+ $consumables = $consumables->skip($offset)->take($limit)->get();
+ return (new ConsumablesTransformer)->transformConsumables($consumables, $total);
+
}
@@ -31,7 +73,14 @@ class ConsumablesController extends Controller
*/
public function store(Request $request)
{
- //
+ $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()));
}
/**
@@ -43,7 +92,9 @@ class ConsumablesController extends Controller
*/
public function show($id)
{
- //
+ $this->authorize('view', Consumable::class);
+ $consumable = Consumable::findOrFail($id);
+ return (new ConsumablesTransformer)->transformConsumable($consumable);
}
@@ -58,7 +109,15 @@ class ConsumablesController extends Controller
*/
public function update(Request $request, $id)
{
- //
+ $this->authorize('edit', Consumable::class);
+ $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()));
}
/**
@@ -71,6 +130,10 @@ class ConsumablesController extends Controller
*/
public function destroy($id)
{
- //
+ $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')));
}
}
diff --git a/app/Http/Controllers/ConsumablesController.php b/app/Http/Controllers/ConsumablesController.php
index a99992b157..29491e3b81 100644
--- a/app/Http/Controllers/ConsumablesController.php
+++ b/app/Http/Controllers/ConsumablesController.php
@@ -291,66 +291,6 @@ class ConsumablesController extends Controller
}
- /**
- * Returns the JSON response containing the the consumables data.
- *
- * @author [A. Gianotto] [<[email protected]>]
- * @see ConsumablesController::getIndex() method that returns the view that consumes the JSON.
- * @since [v1.0]
- * @return array
- */
- public function getDatatable()
- {
- $this->authorize('index', Consumable::class);
- $consumables = Company::scopeCompanyables(
- Consumable::select('consumables.*')
- ->whereNull('consumables.deleted_at')
- ->with('company', 'location', 'category', 'users', 'manufacturer')
- );
-
- if (Input::has('search')) {
- $consumables = $consumables->TextSearch(e(Input::get('search')));
- }
-
- $offset = request('offset', 0);
- $limit = request('limit', 50);
- $allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','companyName','category','model_number', 'item_no', 'manufacturer'];
- $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
- $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
-
- switch ($sort) {
- case 'category':
- $consumables = $consumables->OrderCategory($order);
- break;
- case 'location':
- $consumables = $consumables->OrderLocation($order);
- break;
- case 'manufacturer':
- $consumables = $consumables->OrderManufacturer($order);
- break;
- case 'companyName':
- $consumables = $consumables->OrderCompany($order);
- break;
- default:
- $consumables = $consumables->orderBy($sort, $order);
- break;
- }
-
- $consumCount = $consumables->count();
- $consumables = $consumables->skip($offset)->take($limit)->get();
-
- $rows = array();
-
- foreach ($consumables as $consumable) {
- $rows[] = $consumable->present()->forDataTable();
- }
-
- $data = array('total' => $consumCount, 'rows' => $rows);
-
- return $data;
-
- }
-
/**
* Returns a JSON response containing details on the users associated with this consumable.
*
diff --git a/app/Http/Transformers/ConsumablesTransformer.php b/app/Http/Transformers/ConsumablesTransformer.php
new file mode 100644
index 0000000000..691ac1c0f1
--- /dev/null
+++ b/app/Http/Transformers/ConsumablesTransformer.php
@@ -0,0 +1,54 @@
+<?php
+namespace App\Http\Transformers;
+
+use App\Models\Consumable;
+use Illuminate\Database\Eloquent\Collection;
+use App\Helpers\Helper;
+
+class ConsumablesTransformer
+{
+
+ public function transformConsumables (Collection $consumables, $total)
+ {
+ $array = array();
+ foreach ($consumables as $consumable) {
+ $array[] = self::transformConsumable($consumable);
+ }
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+ public function transformConsumable (Consumable $consumable)
+ {
+ $transformed = [
+ 'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => $consumable->category->name] : null,
+ 'company' => ($consumable->company) ? ['id' => $consumable->company->id, 'name' => $consumable->company->name] : null,
+ 'id' => $consumable->id,
+ 'item_no' => $consumable->item_no,
+ 'location' => ($consumable->location) ? ['id' => $consumable->location->id, 'name' => $consumable->location->name] : null,
+ 'manufacturer' => ($consumable->manufacturer) ? ['id' => $consumable->manufacturer->id, 'name' => $consumable->manufacturer->name] : null,
+ 'min_amt' => $consumable->min_amt,
+ 'model_number' => $consumable->model_number,
+ 'name' => $consumable->name,
+ 'numRemaining' => $consumable->numRemaining(),
+ 'order_number' => $consumable->order_number,
+ 'purchase_cost' => Helper::formatCurrencyOutput($consumable->purchase_cost),
+ 'purchase_date' => $consumable->purchase_date,
+ 'qty' => $consumable->qty,
+ ];
+ return $transformed;
+ }
+
+
+ public function transformCheckedoutConsumables (Collection $consumables_users, $total)
+ {
+
+ $array = array();
+ foreach ($consumables_users as $user) {
+ $array[] = (new UsersTransformer)->transformUser($user);
+ }
+ return (new DatatablesTransformer)->transformDatatables($array, $total);
+ }
+
+
+
+}
diff --git a/resources/views/consumables/index.blade.php b/resources/views/consumables/index.blade.php
index 5655727ebc..15eb5c4aa2 100644
--- a/resources/views/consumables/index.blade.php
+++ b/resources/views/consumables/index.blade.php
@@ -32,20 +32,20 @@
<thead>
<tr>
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
- <th data-switchable="true" data-searchable="true" data-sortable="true" data-field="companyName">{{ trans('admin/companies/table.title') }}</th>
- <th data-sortable="true" data-searchable="true" data-field="name">{{ trans('admin/consumables/table.title') }}</th>
- <th data-searchable="true" data-sortable="true" data-field="location">{{ trans('general.location') }}</th>
- <th data-searchable="true" data-sortable="true" data-field="category">{{ trans('general.category') }}</th>
+ <th data-switchable="true" data-searchable="true" data-sortable="true" data-field="company" data-formatter="companiesLinkObjFormatter">{{ trans('admin/companies/table.title') }}</th>
+ <th data-sortable="true" data-searchable="true" data-field="name" data-formatter="consumablesLinkFormatter">{{ trans('admin/consumables/table.title') }}</th>
+ <th data-searchable="true" data-sortable="true" data-field="location" data-formatter="locationsLinkObjFormatter">{{ trans('general.location') }}</th>
+ <th data-searchable="true" data-sortable="true" data-field="category" data-formatter="categoriesLinkObjFormatter">{{ trans('general.category') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="qty"> {{ trans('admin/consumables/general.total') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="numRemaining"> {{ trans('admin/consumables/general.remaining') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="true" data-field="min_amt"> {{ trans('general.min_amt') }}</th>
- <th data-sortable="true" data-field="manufacturer" data-visible="false">{{ trans('general.manufacturer') }}</th>
+ <th data-sortable="true" data-field="manufacturer" data-formatter="manufacturersLinkObjFormatter" data-visible="false">{{ trans('general.manufacturer') }}</th>
<th data-sortable="true" data-field="model_number" data-visible="false">{{ trans('general.model_no') }}</th>
<th data-sortable="true" data-field="item_no" data-visible="false">{{ trans('admin/consumables/general.item_no') }}</th>
<th data-sortable="true" data-searchable="true" data-field="order_number" data-visible="false">{{ trans('general.order_number') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_date" data-visible="false">{{ trans('general.purchase_date') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_cost" data-visible="false">{{ trans('general.purchase_cost') }}</th>
- <th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions"> {{ trans('table.actions') }}</th>
+ <th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" data-formatter="consumablesActionsFormatter"> {{ trans('table.actions') }}</th>
</tr>
</thead>
diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php
index 516073f71b..47d90aba54 100644
--- a/resources/views/partials/bootstrap-table.blade.php
+++ b/resources/views/partials/bootstrap-table.blade.php
@@ -121,6 +121,8 @@ $('.snipe-table').bootstrapTable({
var formatters = [
'hardware',
'accessories',
+ 'consumables',
+ 'components',
'locations',
'users',
'manufacturers',