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] [] - * @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 @@ +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 @@ {{ trans('general.id') }} - {{ trans('admin/companies/table.title') }} - {{ trans('admin/consumables/table.title') }} - {{ trans('general.location') }} - {{ trans('general.category') }} + {{ trans('admin/companies/table.title') }} + {{ trans('admin/consumables/table.title') }} + {{ trans('general.location') }} + {{ trans('general.category') }} {{ trans('admin/consumables/general.total') }} {{ trans('admin/consumables/general.remaining') }} {{ trans('general.min_amt') }} - {{ trans('general.manufacturer') }} + {{ trans('general.manufacturer') }} {{ trans('general.model_no') }} {{ trans('admin/consumables/general.item_no') }} {{ trans('general.order_number') }} {{ trans('general.purchase_date') }} {{ trans('general.purchase_cost') }} - {{ trans('table.actions') }} + {{ trans('table.actions') }} 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',