/** * Generates the JSON response for asset maintenances listing view. * * @see AssetMaintenancesController::getIndex() method that generates view * @author Vincent Sposato <*****@*****.**> * @version v1.0 * @since [v1.8] * @return String JSON */ public function getDatatable() { $maintenances = AssetMaintenance::with('asset', 'supplier', 'asset.company', 'admin')->withTrashed(); if (Input::has('search')) { $maintenances = $maintenances->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', 'title', 'asset_maintenance_time', 'asset_maintenance_type', 'cost', 'start_date', 'completion_date', 'notes', 'user_id']; $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at'; switch ($sort) { case 'user_id': $maintenances = $maintenances->OrderAdmin($order); break; default: $maintenances = $maintenances->orderBy($sort, $order); break; } $maintenancesCount = $maintenances->count(); $maintenances = $maintenances->skip($offset)->take($limit)->get(); $rows = array(); $settings = Setting::getSettings(); foreach ($maintenances as $maintenance) { $actions = '<nobr><a href="' . route('update/asset_maintenance', $maintenance->id) . '" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a><a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="' . route('delete/asset_maintenance', $maintenance->id) . '" data-content="' . trans('admin/asset_maintenances/message.delete.confirm') . '" data-title="' . trans('general.delete') . ' ' . htmlspecialchars($maintenance->title) . '?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>'; if ($maintenance->cost && $maintenance->asset->assetloc && $maintenance->asset->assetloc->currency != '') { $maintenance_cost = $maintenance->asset->assetloc->currency . $maintenance->cost; } else { $maintenance_cost = $settings->default_currency . $maintenance->cost; } $company = $maintenance->asset->company; $rows[] = array('id' => $maintenance->id, 'asset_name' => (string) link_to('/hardware/' . $maintenance->asset->id . '/view', $maintenance->asset->showAssetName()), 'title' => $maintenance->title, 'notes' => $maintenance->notes, 'supplier' => $maintenance->supplier->name, 'cost' => $maintenance_cost, 'asset_maintenance_type' => e($maintenance->asset_maintenance_type), 'start_date' => $maintenance->start_date, 'asset_maintenance_time' => $maintenance->asset_maintenance_time, 'completion_date' => $maintenance->completion_date, 'user_id' => $maintenance->admin ? (string) link_to('/admin/users/' . $maintenance->admin->id . '/view', $maintenance->admin->fullName()) : '', 'actions' => $actions, 'companyName' => is_null($company) ? '' : $company->name); } $data = array('total' => $maintenancesCount, 'rows' => $rows); return $data; }
/** * exportImprovementsReport * * @return \Illuminate\Http\Response * @author Vincent Sposato <*****@*****.**> * @version v1.0 */ public function exportAssetMaintenancesReport() { // Grab all the improvements $assetMaintenances = AssetMaintenance::with('asset', 'supplier')->orderBy('created_at', 'DESC')->get(); $rows = []; $header = [trans('admin/asset_maintenances/table.asset_name'), trans('admin/asset_maintenances/table.supplier_name'), trans('admin/asset_maintenances/form.asset_maintenance_type'), trans('admin/asset_maintenances/form.title'), trans('admin/asset_maintenances/form.start_date'), trans('admin/asset_maintenances/form.completion_date'), trans('admin/asset_maintenances/form.asset_maintenance_time'), trans('admin/asset_maintenances/form.cost')]; $header = array_map('trim', $header); $rows[] = implode($header, ','); foreach ($assetMaintenances as $assetMaintenance) { $row = []; $row[] = str_replace(',', '', e($assetMaintenance->asset->name)); $row[] = str_replace(',', '', e($assetMaintenance->supplier->name)); $row[] = e($assetMaintenance->improvement_type); $row[] = e($assetMaintenance->title); $row[] = e($assetMaintenance->start_date); $row[] = e($assetMaintenance->completion_date); if (is_null($assetMaintenance->asset_maintenance_time)) { $improvementTime = intval(Carbon::now()->diffInDays(Carbon::parse($assetMaintenance->start_date))); } else { $improvementTime = intval($assetMaintenance->asset_maintenance_time); } $row[] = $improvementTime; $row[] = trans('general.currency') . Helper::formatCurrencyOutput($assetMaintenance->cost); $rows[] = implode($row, ','); } // spit out a csv $csv = implode($rows, "\n"); $response = Response::make($csv, 200); $response->header('Content-Type', 'text/csv'); $response->header('Content-disposition', 'attachment;filename=report.csv'); return $response; }