Example #1
0
 /**
  * Exports the custom report to CSV
  *
  * @author [A. Gianotto] [<*****@*****.**>]
  * @see ReportsController::getCustomReport() method that generates form view
  * @since [v1.0]
  * @return \Illuminate\Http\Response
  */
 public function postCustom()
 {
     $assets = Asset::orderBy('created_at', 'DESC')->get();
     $customfields = CustomField::get();
     $rows = [];
     $header = [];
     if (e(Input::get('company_name')) == '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('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('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';
     }
     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_name')) == '1') {
             $row[] = is_null($asset->company) ? '' : e($asset->company->name);
         }
         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->modelno) . '"';
         }
         if (e(Input::get('category')) == '1') {
             $row[] = '"' . e($asset->model->category->name) . '"';
         }
         if (e(Input::get('serial')) == '1') {
             $row[] = e($asset->serial);
         }
         if (e(Input::get('purchase_date')) == '1') {
             $row[] = e($asset->purchase_date);
         }
         if (e(Input::get('purchase_cost')) == '1' && e(Input::get('depreciation')) != '1') {
             $row[] = '"' . Helper::formatCurrencyOutput($asset->purchase_cost) . '"';
         }
         if (e(Input::get('order')) == '1') {
             if ($asset->order_number) {
                 $row[] = e($asset->order_number);
             } else {
                 $row[] = '';
             }
         }
         if (e(Input::get('supplier')) == '1') {
             if ($asset->supplier_id) {
                 $row[] = '"' . e($asset->supplier->name) . '"';
             } else {
                 $row[] = '';
             }
         }
         if (e(Input::get('location')) == '1') {
             $show_loc = '';
             if ($asset->assigned_to > 0 && $asset->assigneduser->location_id != '') {
                 $location = Location::find($asset->assigneduser->location_id);
                 if ($location) {
                     $show_loc .= '"' . e($location->name) . '"';
                 } else {
                     $show_loc .= 'User location ' . $asset->assigneduser->location_id . ' is invalid';
                 }
             } elseif ($asset->rtd_location_id != '') {
                 $location = Location::find($asset->rtd_location_id);
                 if ($location) {
                     $show_loc .= '"' . e($location->name) . '"';
                 } else {
                     $show_loc .= 'Default location ' . $asset->rtd_location_id . ' is invalid';
                 }
             }
             $row[] = $show_loc;
         }
         if (e(Input::get('assigned_to')) == '1') {
             if ($asset->assigned_to > 0) {
                 $user = User::find($asset->assigned_to);
                 $row[] = '"' . e($user->fullName()) . '"';
             } else {
                 $row[] = '';
                 // Empty string if unassigned
             }
         }
         if (e(Input::get('username')) == '1') {
             if ($asset->assigned_to > 0) {
                 $user = User::find($asset->assigned_to);
                 $row[] = '"' . e($user->username) . '"';
             } 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->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) . '"';
         }
         foreach ($customfields as $customfield) {
             $column_name = $customfield->db_column_name();
             if (e(Input::get($customfield->db_column_name())) == '1') {
                 $row[] = $asset->{$column_name};
             }
         }
         $rows[] = implode($row, ',');
     }
     // 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=report.csv');
         return $response;
     } else {
         return redirect()->to("reports/custom")->with('error', trans('admin/reports/message.error'));
     }
 }
Example #2
0
 public static function assetsList()
 {
     $assets_list = array('' => trans('general.select_asset')) + Asset::orderBy('name', 'asc')->whereNull('deleted_at')->pluck('name', 'id')->toArray();
     return $assets_list;
 }