/**
  * 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]
  * @param int $consumableId
  * @return View
  */
 public function getDatatable()
 {
     $consumables = Company::scopeCompanyables(Consumable::select('consumables.*')->whereNull('consumables.deleted_at')->with('company', 'location', 'category', 'users'));
     if (Input::has('search')) {
         $consumables = $consumables->TextSearch(e(Input::get('search')));
     }
     if (Input::has('offset')) {
         $offset = e(Input::get('offset'));
     } else {
         $offset = 0;
     }
     if (Input::has('limit')) {
         $limit = e(Input::get('limit'));
     } else {
         $limit = 50;
     }
     $allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'companyName', 'category', 'model_no', '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) {
         $actions = '<nobr>';
         if (Gate::allows('consumables.checkout')) {
             $actions .= '<a href="' . route('checkout/consumable', $consumable->id) . '" style="margin-right:5px;" class="btn btn-info btn-sm" ' . ($consumable->numRemaining() > 0 ? '' : ' disabled') . '>' . trans('general.checkout') . '</a>';
         }
         if (Gate::allows('consumables.edit')) {
             $actions .= '<a href="' . route('update/consumable', $consumable->id) . '" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a>';
         }
         if (Gate::allows('consumables.delete')) {
             $actions .= '<a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="' . route('delete/consumable', $consumable->id) . '" data-content="' . trans('admin/consumables/message.delete.confirm') . '" data-title="' . trans('general.delete') . ' ' . htmlspecialchars($consumable->name) . '?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
         }
         $actions .= '</nobr>';
         $company = $consumable->company;
         $rows[] = array('id' => $consumable->id, 'name' => (string) link_to('admin/consumables/' . $consumable->id . '/view', e($consumable->name)), 'location' => $consumable->location ? e($consumable->location->name) : '', 'min_amt' => e($consumable->min_amt), 'qty' => e($consumable->qty), 'manufacturer' => $consumable->manufacturer ? e($consumable->manufacturer->name) : '', 'model_no' => e($consumable->model_no), 'item_no' => e($consumable->item_no), 'category' => $consumable->category ? e($consumable->category->name) : 'Missing category', 'order_number' => e($consumable->order_number), 'purchase_date' => e($consumable->purchase_date), 'purchase_cost' => $consumable->purchase_cost != '' ? number_format($consumable->purchase_cost, 2) : '', 'numRemaining' => $consumable->numRemaining(), 'actions' => $actions, 'companyName' => is_null($company) ? '' : e($company->name));
     }
     $data = array('total' => $consumCount, 'rows' => $rows);
     return $data;
 }