|
11 | 11 | use Illuminate\Http\Request; |
12 | 12 | use Illuminate\Support\Facades\Auth; |
13 | 13 | use Illuminate\Support\Facades\DB; |
| 14 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
14 | 15 |
|
15 | 16 | /** |
16 | 17 | * This controller handles all actions related to Licenses for |
@@ -289,4 +290,106 @@ public function getClone($licenseId = null) |
289 | 290 | ->with('item', $license) |
290 | 291 | ->with('maintained_list', $maintained_list); |
291 | 292 | } |
| 293 | + |
| 294 | + /** |
| 295 | + * Exports Licenses to CSV |
| 296 | + * |
| 297 | + * @author [G. Martinez] |
| 298 | + * @since [v6.3] |
| 299 | + * @return StreamedResponse |
| 300 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 301 | + */ |
| 302 | + public function getExportLicensesCsv() |
| 303 | + { |
| 304 | + $this->authorize('view', License::class); |
| 305 | + \Debugbar::disable(); |
| 306 | + |
| 307 | + $response = new StreamedResponse(function () { |
| 308 | + // Open output stream |
| 309 | + $handle = fopen('php://output', 'w'); |
| 310 | + $licenses= License::with('company', |
| 311 | + 'manufacturer', |
| 312 | + 'category', |
| 313 | + 'supplier', |
| 314 | + 'adminuser', |
| 315 | + 'assignedusers') |
| 316 | + ->orderBy('created_at', 'DESC'); |
| 317 | + Company::scopeCompanyables($licenses) |
| 318 | + ->chunk(500, function ($licenses) use ($handle) { |
| 319 | + $headers = [ |
| 320 | + // strtolower to prevent Excel from trying to open it as a SYLK file |
| 321 | + strtolower(trans('general.id')), |
| 322 | + trans('general.company'), |
| 323 | + trans('general.name'), |
| 324 | + trans('general.serial_number'), |
| 325 | + trans('general.purchase_date'), |
| 326 | + trans('general.purchase_cost'), |
| 327 | + trans('general.order_number'), |
| 328 | + trans('general.licenses_available'), |
| 329 | + trans('admin/licenses/table.seats'), |
| 330 | + trans('general.created_by'), |
| 331 | + trans('general.depreciation'), |
| 332 | + trans('general.updated_at'), |
| 333 | + trans('admin/licenses/table.deleted_at'), |
| 334 | + trans('general.email'), |
| 335 | + trans('admin/hardware/form.fully_depreciated'), |
| 336 | + trans('general.supplier'), |
| 337 | + trans('admin/licenses/form.expiration'), |
| 338 | + trans('admin/licenses/form.purchase_order'), |
| 339 | + trans('admin/licenses/form.termination_date'), |
| 340 | + trans('admin/licenses/form.maintained'), |
| 341 | + trans('general.manufacturer'), |
| 342 | + trans('general.category'), |
| 343 | + trans('general.min_amt'), |
| 344 | + trans('admin/licenses/form.reassignable'), |
| 345 | + trans('general.notes'), |
| 346 | + trans('general.created_at'), |
| 347 | + ]; |
| 348 | + |
| 349 | + fputcsv($handle, $headers); |
| 350 | + |
| 351 | + foreach ($licenses as $license) { |
| 352 | + // Add a new row with data |
| 353 | + $values = [ |
| 354 | + $license->id, |
| 355 | + $license->company ? $license->company->name: '', |
| 356 | + $license->name, |
| 357 | + $license->serial, |
| 358 | + $license->purchase_date, |
| 359 | + $license->purchase_cost, |
| 360 | + $license->order_number, |
| 361 | + $license->free_seat_count, |
| 362 | + $license->seats, |
| 363 | + $license->adminuser->present()->fullName(), |
| 364 | + $license->depreciation ? $license->depreciation->name: '', |
| 365 | + $license->updated_at, |
| 366 | + $license->deleted_at, |
| 367 | + $license->email, |
| 368 | + ( $license->depreciate == '1') ? trans('general.yes') : trans('general.no'), |
| 369 | + ($license->supplier) ? $license->supplier->name: '', |
| 370 | + $license->expiration_date, |
| 371 | + $license->purchase_order, |
| 372 | + $license->termination_date, |
| 373 | + ( $license->maintained == '1') ? trans('general.yes') : trans('general.no'), |
| 374 | + $license->manufacturer ? $license->manufacturer->name: '', |
| 375 | + $license->category ? $license->category->name: '', |
| 376 | + $license->min_amt, |
| 377 | + ( $license->reassignable == '1') ? trans('general.yes') : trans('general.no'), |
| 378 | + $license->notes, |
| 379 | + $license->created_at, |
| 380 | + ]; |
| 381 | + |
| 382 | + fputcsv($handle, $values); |
| 383 | + } |
| 384 | + }); |
| 385 | + |
| 386 | + // Close the output stream |
| 387 | + fclose($handle); |
| 388 | + }, 200, [ |
| 389 | + 'Content-Type' => 'text/csv; charset=UTF-8', |
| 390 | + 'Content-Disposition' => 'attachment; filename="licenses-'.date('Y-m-d-his').'.csv"', |
| 391 | + ]); |
| 392 | + |
| 393 | + return $response; |
| 394 | + } |
292 | 395 | } |
0 commit comments