added export button, half the logic for export method

This commit is contained in:
Godfrey M 2024-04-10 11:31:30 -07:00
parent d0f171ebc6
commit e2679852ce
3 changed files with 108 additions and 0 deletions

View file

@ -11,6 +11,7 @@ use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\StreamedResponse;
/** /**
* This controller handles all actions related to Licenses for * This controller handles all actions related to Licenses for
@ -289,4 +290,101 @@ class LicensesController extends Controller
->with('item', $license) ->with('item', $license)
->with('maintained_list', $maintained_list); ->with('maintained_list', $maintained_list);
} }
/**
* Exports users to CSV
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.5]
* @return StreamedResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function getExportLicensesCsv()
{
$this->authorize('view', License::class);
\Debugbar::disable();
$response = new StreamedResponse(function () {
// Open output stream
$handle = fopen('php://output', 'w');
License::all()
->orderBy('created_at', 'DESC')->get()
->chunk(500, function ($licenses) use ($handle) {
$headers = [
// strtolower to prevent Excel from trying to open it as a SYLK file
strtolower(trans('general.id')),
trans('admin/companies/table.title'),
trans('admin/users/table.title'),
trans('admin/users/table.name'),
trans('admin/users/table.username'),
trans('admin/users/table.email'),
trans('admin/users/table.manager'),
trans('admin/users/table.location'),
trans('general.department'),
trans('general.assets'),
trans('general.licenses'),
trans('general.accessories'),
trans('general.consumables'),
trans('admin/users/table.groups'),
trans('general.notes'),
trans('admin/users/table.activated'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
trans('general.created_at'),
];
fputcsv($handle, $headers);
foreach ($licenses as $license) {
// Add a new row with data
$values = [
$license->id,
$license->company->name,
$license->name,
$license->serial,
$license->purchase_date,
$license->purchase_cost,
$license->order_number,
$license->seats,
$license->notes,
$license->user->id,
$license->depreciation->name,
$license->updated_at,
$license->deleted_at,
$license->license_name,
$license->email,
$license->depreciate,
$license->supplier->name,
$license->expiration_date,
$license->purchase_order,
$license->termination_date,
$license->maintained,
$license->reassignable,
$license->manufacturer->name,
$license->category->name,
$license->min_amt,
( $license->reassignable == '1') ? trans('general.yes') : trans('general.no'),
$license->created_at,
];
fputcsv($handle, $values);
}
});
// Close the output stream
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv; charset=UTF-8',
'Content-Disposition' => 'attachment; filename="users-'.date('Y-m-d-his').'.csv"',
]);
return $response;
}
} }

View file

@ -13,6 +13,9 @@
{{ trans('general.create') }} {{ trans('general.create') }}
</a> </a>
@endcan @endcan
@can('view', \App\Models\User::class)
<a class="btn btn-default pull-right" href="{{ route('licenses.export') }}" style="margin-right: 5px;">{{ trans('general.export') }}</a>
@endcan
@stop @stop
{{-- Page content --}} {{-- Page content --}}

View file

@ -48,6 +48,13 @@ Route::group(['prefix' => 'licenses', 'middleware' => ['auth']], function () {
'{licenseId}/showfile/{fileId}/{download?}', '{licenseId}/showfile/{fileId}/{download?}',
[Licenses\LicenseFilesController::class, 'show'] [Licenses\LicenseFilesController::class, 'show']
)->name('show.licensefile'); )->name('show.licensefile');
Route::get(
'export',
[
Licenses\LicensesController::class,
'getExportLicensesCsv'
]
)->name('licenses.export');
}); });
Route::resource('licenses', Licenses\LicensesController::class, [ Route::resource('licenses', Licenses\LicensesController::class, [