diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index c3bc7b4136..d8c2ca6793 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -422,261 +422,275 @@ class ReportsController extends Controller * @since [v1.0] * @return \Illuminate\Http\Response */ - public function postCustom() + public function postCustom(Request $request) { - $assets = Asset::orderBy('created_at', 'DESC')->with('company', 'assignedTo', 'location', 'defaultLoc', 'model', 'supplier', 'assetstatus', 'model.manufacturer')->get(); + + \Debugbar::disable(); $customfields = CustomField::get(); + $response = new StreamedResponse(function () use ($customfields, $request) { - $rows = [ ]; - $header = [ ]; - - if (e(Input::get('company')) == '1') { - $header[] = 'Company Name'; - } - - if (e(Input::get('asset_name')) == '1') { - $header[] = 'Asset Name'; - } - if (e(Input::get('asset_tag')) == '1') { - $header[] = 'Asset Tag'; - } - if (e(Input::get('manufacturer')) == '1') { - $header[] = 'Manufacturer'; - } - if (e(Input::get('model')) == '1') { - $header[] = 'Model'; - $header[] = 'Model Number'; - } - if (e(Input::get('category')) == '1') { - $header[] = 'Category'; - } - if (e(Input::get('serial')) == '1') { - $header[] = 'Serial'; - } - if (e(Input::get('purchase_date')) == '1') { - $header[] = 'Purchase Date'; - } - if (( e(Input::get('purchase_cost')) == '1' ) && ( e(Input::get('depreciation')) != '1' )) { - $header[] = 'Purchase Cost'; - } - if (e(Input::get('eol')) == '1') { - $header[] = 'EOL'; - } - if (e(Input::get('order')) == '1') { - $header[] = 'Order Number'; - } - if (e(Input::get('supplier')) == '1') { - $header[] = 'Supplier'; - } - if (e(Input::get('location')) == '1') { - $header[] = 'Location'; - } - if (e(Input::get('assigned_to')) == '1') { - $header[] = 'Assigned To'; - } - if (e(Input::get('username')) == '1') { - $header[] = 'Username'; - } - if (e(Input::get('employee_num')) == '1') { - $header[] = 'Employee No.'; - } - if (e(Input::get('status')) == '1') { - $header[] = 'Status'; - } - if (e(Input::get('warranty')) == '1') { - $header[] = 'Warranty'; - $header[] = 'Warranty Expires'; - } - if (e(Input::get('depreciation')) == '1') { - $header[] = 'Purchase Cost'; - $header[] = 'Value'; - $header[] = 'Diff'; - } - if (e(Input::get('expected_checkin')) == '1') { - $header[] = trans('admin/hardware/form.expected_checkin'); - } - - if (e(Input::get('notes')) == '1') { - $header[] = trans('general.notes'); - } - - - foreach ($customfields as $customfield) { - if (e(Input::get($customfield->db_column_name())) == '1') { - $header[] = $customfield->name; - } - } - - - $header = array_map('trim', $header); - $rows[] = implode($header, ','); - - foreach ($assets as $asset) { - $row = [ ]; - - if (e(Input::get('company')) == '1') { - $row[] = is_null($asset->company) ? '' : '"'.$asset->company->name.'"'; + // Open output stream + $handle = fopen('php://output', 'w'); + + if ($request->has('use_bom')) { + fprintf($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); } - if (e(Input::get('asset_name')) == '1') { - $row[] = '"' .e($asset->name) . '"'; - } - if (e(Input::get('asset_tag')) == '1') { - $row[] = e($asset->asset_tag); - } - if (e(Input::get('manufacturer')) == '1') { - if ($asset->model->manufacturer) { - $row[] = '"' .e($asset->model->manufacturer->name) . '"'; - } else { - $row[] = ''; - } - } - if (e(Input::get('model')) == '1') { - $row[] = '"' . e($asset->model->name) . '"'; - $row[] = '"' . e($asset->model->model_number) . '"'; - } - if (e(Input::get('category')) == '1') { - $row[] = '"' .e($asset->model->category->name) . '"'; + $header = []; + + if ($request->has('company')) { + $header[] = 'Company Name'; } - if (e(Input::get('serial')) == '1') { - $row[] = e($asset->serial); + if ($request->has('asset_name')) { + $header[] = 'Asset Name'; } - if (e(Input::get('purchase_date')) == '1') { - $row[] = e($asset->purchase_date); + if ($request->has('asset_tag')) { + $header[] = 'Asset Tag'; } - if (e(Input::get('purchase_cost')) == '1' && ( e(Input::get('depreciation')) != '1' )) { - $row[] = '"' . Helper::formatCurrencyOutput($asset->purchase_cost) . '"'; + if ($request->has('manufacturer')) { + $header[] = 'Manufacturer'; } - if (e(Input::get('eol')) == '1') { - $row[] = '"' .($asset->present()->eol_date()) ? $asset->present()->eol_date() : ''. '"'; + if ($request->has('model')) { + $header[] = 'Model'; + $header[] = 'Model Number'; } - if (e(Input::get('order')) == '1') { - if ($asset->order_number) { - $row[] = e($asset->order_number); - } else { - $row[] = ''; - } + if ($request->has('category')) { + $header[] = 'Category'; } - if (e(Input::get('supplier')) == '1') { - if ($asset->supplier) { - $row[] = '"' .e($asset->supplier->name) . '"'; - } else { - $row[] = ''; - } + if ($request->has('serial')) { + $header[] = 'Serial'; + } + if ($request->has('purchase_date')) { + $header[] = 'Purchase Date'; + } + if ($request->has('purchase_cost')) { + $header[] = 'Purchase Cost'; + } + if ($request->has('eol')) { + $header[] = 'EOL'; + } + if ($request->has('order')) { + $header[] = 'Order Number'; + } + if ($request->has('supplier')) { + $header[] = 'Supplier'; + } + if ($request->has('location')) { + $header[] = 'Location'; + } + if ($request->has('assigned_to')) { + $header[] = 'Assigned To'; + $header[] = 'Assigned Type'; + } + if ($request->has('username')) { + $header[] = 'Username'; + } + if ($request->has('employee_num')) { + $header[] = 'Employee No.'; + } + if ($request->has('status')) { + $header[] = 'Status'; + } + if ($request->has('warranty')) { + $header[] = 'Warranty'; + $header[] = 'Warranty Expires'; + } + if ($request->has('depreciation')) { + $header[] = 'Purchase Cost'; + $header[] = 'Value'; + $header[] = 'Diff'; + } + if ($request->has('expected_checkin')) { + $header[] = trans('admin/hardware/form.expected_checkin'); } - if (e(Input::get('location')) == '1') { - if($asset->location) { - $show_loc = $asset->location->present()->name(); - } else { - $show_loc = 'Default location '.$asset->rtd_location_id.' is invalid'; - } - $row[] = $show_loc; + if ($request->has('notes')) { + $header[] = trans('general.notes'); } - if (e(Input::get('assigned_to')) == '1') { - if ($asset->assignedto) { - $row[] = '"' .e($asset->assignedto->present()->name()). '"'; - } else { - $row[] = ''; // Empty string if unassigned - } - } - - if (e(Input::get('username')) == '1') { - // Only works if we're checked out to a user, not anything else. - if ($asset->checkedOutToUser()) { - if ($asset->assignedto) { - $row[] = '"' .e($asset->assignedto->username). '"'; - } else { - $row[] = ''; // Empty string if unassigned - } - - } else { - $row[] = ''; // Empty string if unassigned - } - } - - if (e(Input::get('employee_num')) == '1') { - // Only works if we're checked out to a user, not anything else. - if ($asset->checkedOutToUser()) { - if ($asset->assignedto) { - $row[] = '"' .e($asset->assignedto->employee_num). '"'; - } else { - $row[] = ''; // Empty string if unassigned - } - - } else { - $row[] = ''; // Empty string if unassigned - } - } - - if (e(Input::get('status')) == '1') { - if (( $asset->status_id == '0' ) && ( $asset->assigned_to == '0' )) { - $row[] = trans('general.ready_to_deploy'); - } elseif (( $asset->status_id == '' ) && ( $asset->assigned_to == '0' )) { - $row[] = trans('general.pending'); - } elseif ($asset->assetstatus) { - $row[] = '"' .e($asset->assetstatus->name). '"'; - } else { - $row[] = ''; - } - } - if (e(Input::get('warranty')) == '1') { - if ($asset->warranty_months) { - $row[] = $asset->warranty_months; - $row[] = $asset->present()->warrantee_expires(); - } else { - $row[] = ''; - $row[] = ''; - } - } - if (e(Input::get('depreciation')) == '1') { - $depreciation = $asset->getDepreciatedValue(); - $row[] = '"' . Helper::formatCurrencyOutput($asset->purchase_cost) . '"'; - $row[] = '"' . Helper::formatCurrencyOutput($depreciation) . '"'; - $row[] = '"' . Helper::formatCurrencyOutput($asset->purchase_cost) . '"'; - } - if (e(Input::get('expected_checkin')) == '1') { - if ($asset->expected_checkin) { - $row[] = '"' .e($asset->expected_checkin). '"'; - } else { - $row[] = ''; // Empty string if blankd - } - } - - if (e(Input::get('notes')) == '1') { - if ($asset->notes) { - $row[] = '"' .$asset->notes . '"'; - } else { - $row[] = ''; - } - } - foreach ($customfields as $customfield) { - $column_name = $customfield->db_column_name(); if (e(Input::get($customfield->db_column_name())) == '1') { - $row[] = str_replace(",", "\,", $asset->$column_name); + $header[] = $customfield->name; } } - $rows[] = implode($row, ','); - } + fputcsv($handle, $header); + + $assets = Asset::orderBy('created_at', 'DESC')->with('company', 'assignedTo', 'location', 'defaultLoc', 'model', 'supplier', 'assetstatus', 'model.manufacturer'); + + if ($request->has('by_location_id')) { + $assets->where('assets.location_id', $request->input('by_location_id')); + } + + if ($request->has('by_supplier_id')) { + $assets->where('assets.supplier_id', $request->input('by_supplier_id')); + } + + if ($request->has('by_company_id')) { + $assets->where('assets.company_id', $request->input('by_company_id')); + } + + if ($request->has('by_model_id')) { + $assets->where('assets.model_id', $request->input('by_model_id')); + } + + if ($request->has('by_category_id')) { + $assets->InCategory($request->input('by_category_id')); + } + + if ($request->has('by_order_number')) { + $assets->where('assets.order_number', $request->input('by_order_number')); + } + + if (($request->has('start')) && ($request->has('end'))) { + $assets->whereBetween('assets.purchase_date', [$request->input('start'), $request->input('end')]); + } + + $assets->orderBy('assets.created_at', 'DESC')->chunk(500, function($assets) use($handle, $customfields, $request) { + + foreach ($assets as $asset) { + $row = []; + + if ($request->has('company')) { + $row[] = ($asset->company) ? $asset->company->name : ''; + } + + if ($request->has('asset_name')) { + $row[] = ($asset->name) ? $asset->name : ''; + } + + if ($request->has('asset_tag')) { + $row[] = ($asset->asset_tag) ? $asset->asset_tag : ''; + } + + if ($request->has('manufacturer')) { + $row[] = ($asset->model && $asset->model->manufacturer) ? $asset->model->manufacturer->name : ''; + } + + if ($request->has('model')) { + $row[] = ($asset->model) ? $asset->model->name : ''; + $row[] = ($asset->model) ? $asset->model->model_number : ''; + } + + if ($request->has('category')) { + $row[] = ($asset->model->category) ? $asset->model->category->name : ''; + } + + if ($request->has('serial')) { + $row[] = ($asset->serial) ? $asset->serial : ''; + } + + if ($request->has('purchase_date')) { + $row[] = ($asset->purchase_date) ? $asset->purchase_date : ''; + } + + if ($request->has('purchase_cost')) { + $row[] = ($asset->purchase_cost) ? Helper::formatCurrencyOutput($asset->purchase_cost) : ''; + } + + if ($request->has('eol')) { + $row[] = ($asset->eol) ? $asset->present()->eol_date() : ''; + } + + if ($request->has('order')) { + $row[] = ($asset->order_number) ? $asset->order_number : ''; + } + + if ($request->has('supplier')) { + $row[] = ($asset->location) ? $asset->supplier->name : ''; + } + + + if ($request->has('location')) { + $row[] = ($asset->location) ? $asset->location->present()->name() : ''; + } + + if ($request->has('assigned_to')) { + $row[] = ($asset->checkedOutToUser() && $asset->assigned) ? e($asset->assigned->getFullNameAttribute()) : ($asset->assigned ? e($asset->assigned->display_name) : ''); + $row[] = ($asset->checkedOutToUser() && $asset->assigned) ? 'user' : e($asset->assignedType()); + } + + if ($request->has('username')) { + // Only works if we're checked out to a user, not anything else. + if ($asset->checkedOutToUser()) { + $row[] = ($asset->assignedto) ? $asset->assignedto->username : ''; + } else { + $row[] = ''; // Empty string if unassigned + } + } + + if ($request->has('employee_num')) { + // Only works if we're checked out to a user, not anything else. + if ($asset->checkedOutToUser()) { + $row[] = ($asset->assignedto) ? $asset->assignedto->employee_num : ''; + } else { + $row[] = ''; // Empty string if unassigned + } + } + + if ($request->has('status')) { + if (( $asset->status_id == '0' ) && ( $asset->assigned_to == '0' )) { + $row[] = trans('general.ready_to_deploy'); + } elseif (( $asset->status_id == '' ) && ( $asset->assigned_to == '0' )) { + $row[] = trans('general.pending'); + } elseif ($asset->assetstatus) { + $row[] = $asset->assetstatus->name; + } else { + $row[] = ''; + } + } + + + if ($request->has('warranty')) { + $row[] = ($asset->warranty_months) ? $asset->warranty_months : ''; + $row[] = $asset->present()->warrantee_expires(); + } + + if ($request->has('depreciation')) { + if ($asset->purchase_cost!='') { + $depreciation = $asset->getDepreciatedValue(); + $diff = ($asset->purchase_cost - $depreciation); + $row[] = Helper::formatCurrencyOutput($asset->purchase_cost); + $row[] = Helper::formatCurrencyOutput($depreciation); + $row[] = Helper::formatCurrencyOutput($diff); + } else { + $row[] = ''; + } + + } + + if ($request->has('expected_checkin')) { + $row[] = ($asset->expected_checkin) ? $asset->expected_checkin : ''; + } + + if ($request->has('notes')) { + $row[] = ($asset->notes) ? $asset->notes : ''; + } + + foreach ($customfields as $customfield) { + $column_name = $customfield->db_column_name(); + if ($request->has($customfield->db_column_name())) { + $row[] = $asset->$column_name; + } + } + fputcsv($handle, $row); + } + }); + + // Close the output stream + fclose($handle); + }, 200, [ + 'Content-Type' => 'text/csv', + 'Content-Disposition' + => 'attachment; filename="custom-assets-report-'.date('Y-m-d-his').'.csv"', + ]); + + return $response; - // spit out a csv - if (array_filter($rows)) { - $csv = implode($rows, "\n"); - $response = Response::make($csv, 200); - $response->header('Content-Type', 'text/csv'); - $response->header('Content-disposition', 'attachment;filename='.date('Y-m-d-His').'-custom-asset-report.csv'); - return $response; - } else { - return redirect()->to("reports/custom") - ->with('error', trans('admin/reports/message.error')); - } } diff --git a/resources/views/partials/forms/edit/location-select.blade.php b/resources/views/partials/forms/edit/location-select.blade.php index 2214130d2b..ba67bb5166 100644 --- a/resources/views/partials/forms/edit/location-select.blade.php +++ b/resources/views/partials/forms/edit/location-select.blade.php @@ -16,7 +16,9 @@