Skip to content

Commit

Permalink
Merge pull request #14587 from Godmartinz/License-export-button
Browse files Browse the repository at this point in the history
Added a License Export function and button
  • Loading branch information
snipe committed Apr 24, 2024
2 parents 685f1cb + 9e7bbc9 commit 2439758
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
103 changes: 103 additions & 0 deletions app/Http/Controllers/Licenses/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* This controller handles all actions related to Licenses for
Expand Down Expand Up @@ -289,4 +290,106 @@ public function getClone($licenseId = null)
->with('item', $license)
->with('maintained_list', $maintained_list);
}

/**
* Exports Licenses to CSV
*
* @author [G. Martinez]
* @since [v6.3]
* @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');
$licenses= License::with('company',
'manufacturer',
'category',
'supplier',
'adminuser',
'assignedusers')
->orderBy('created_at', 'DESC');
Company::scopeCompanyables($licenses)
->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('general.company'),
trans('general.name'),
trans('general.serial_number'),
trans('general.purchase_date'),
trans('general.purchase_cost'),
trans('general.order_number'),
trans('general.licenses_available'),
trans('admin/licenses/table.seats'),
trans('general.created_by'),
trans('general.depreciation'),
trans('general.updated_at'),
trans('admin/licenses/table.deleted_at'),
trans('general.email'),
trans('admin/hardware/form.fully_depreciated'),
trans('general.supplier'),
trans('admin/licenses/form.expiration'),
trans('admin/licenses/form.purchase_order'),
trans('admin/licenses/form.termination_date'),
trans('admin/licenses/form.maintained'),
trans('general.manufacturer'),
trans('general.category'),
trans('general.min_amt'),
trans('admin/licenses/form.reassignable'),
trans('general.notes'),
trans('general.created_at'),
];

fputcsv($handle, $headers);

foreach ($licenses as $license) {
// Add a new row with data
$values = [
$license->id,
$license->company ? $license->company->name: '',
$license->name,
$license->serial,
$license->purchase_date,
$license->purchase_cost,
$license->order_number,
$license->free_seat_count,
$license->seats,
$license->adminuser->present()->fullName(),
$license->depreciation ? $license->depreciation->name: '',
$license->updated_at,
$license->deleted_at,
$license->email,
( $license->depreciate == '1') ? trans('general.yes') : trans('general.no'),
($license->supplier) ? $license->supplier->name: '',
$license->expiration_date,
$license->purchase_order,
$license->termination_date,
( $license->maintained == '1') ? trans('general.yes') : trans('general.no'),
$license->manufacturer ? $license->manufacturer->name: '',
$license->category ? $license->category->name: '',
$license->min_amt,
( $license->reassignable == '1') ? trans('general.yes') : trans('general.no'),
$license->notes,
$license->created_at,
];

fputcsv($handle, $values);
}
});

// Close the output stream
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv; charset=UTF-8',
'Content-Disposition' => 'attachment; filename="licenses-'.date('Y-m-d-his').'.csv"',
]);

return $response;
}
}
22 changes: 20 additions & 2 deletions app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class License extends Depreciable
'serial',
'supplier_id',
'termination_date',
'free_seat_count',
'user_id',
'min_amt',
];
Expand Down Expand Up @@ -114,6 +115,7 @@ class License extends Depreciable
'category' => ['name'],
'depreciation' => ['name'],
];
protected $appends = ['free_seat_count'];

/**
* Update seat counts when the license is updated
Expand Down Expand Up @@ -280,6 +282,16 @@ public function setTerminationDateAttribute($value)
}
$this->attributes['termination_date'] = $value;
}
/**
* Sets free_seat_count attribute
*
* @author G. Martinez
* @since [v6.3]
* @return mixed
*/
public function getFreeSeatCountAttribute(){
return $this->attributes['free_seat_count'] = $this->remaincount();
}

/**
* Establishes the license -> company relationship
Expand Down Expand Up @@ -502,7 +514,13 @@ public static function availassetcount()
->whereNull('deleted_at')
->count();
}

/**
* Returns the available seats remaining
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v2.0]
* @return int
*/

/**
* Returns the number of total available seats for this license
Expand Down Expand Up @@ -579,7 +597,7 @@ public function remaincount()
$taken = $this->assigned_seats_count;
$diff = ($total - $taken);

return $diff;
return (int) $diff;
}

/**
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/licenses/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'assigned_to' => 'Assigned To',
'checkout' => 'In/Out',
'deleted_at' => 'Deleted at',
'id' => 'ID',
'license_email' => 'License Email',
'license_name' => 'Licensed To',
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en-US/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
'last_name' => 'Last Name',
'license' => 'License',
'license_report' => 'License Report',
'licenses_available' => 'licenses available',
'licenses_available' => 'Licenses available',
'licenses' => 'Licenses',
'list_all' => 'List All',
'loading' => 'Loading... please wait....',
Expand Down
3 changes: 3 additions & 0 deletions resources/views/licenses/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
{{ trans('general.create') }}
</a>
@endcan
@can('view', \App\Models\License::class)
<a class="btn btn-default pull-right" href="{{ route('licenses.export') }}" style="margin-right: 5px;">{{ trans('general.export') }}</a>
@endcan
@stop

{{-- Page content --}}
Expand Down
7 changes: 7 additions & 0 deletions routes/web/licenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
'{licenseId}/showfile/{fileId}/{download?}',
[Licenses\LicenseFilesController::class, 'show']
)->name('show.licensefile');
Route::get(
'export',
[
Licenses\LicensesController::class,
'getExportLicensesCsv'
]
)->name('licenses.export');
});

Route::resource('licenses', Licenses\LicensesController::class, [
Expand Down

0 comments on commit 2439758

Please sign in to comment.