snipe-it/app/Http/Controllers/ReportsController.php

956 lines
34 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
2016-03-25 01:18:05 -07:00
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\AssetMaintenance;
use App\Models\AssetModel;
2016-03-25 01:18:05 -07:00
use App\Models\Company;
use App\Models\CustomField;
use App\Models\License;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
2016-03-25 01:18:05 -07:00
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;
use Input;
use League\Csv\Reader;
use Redirect;
2016-09-12 14:06:55 -07:00
use Symfony\Component\HttpFoundation\StreamedResponse;
2016-03-25 01:18:05 -07:00
2016-04-07 13:21:09 -07:00
/**
* This controller handles all actions related to Reports for
* the Snipe-IT Asset Management application.
*
* @version v1.0
*/
2016-03-25 01:18:05 -07:00
class ReportsController extends Controller
{
/**
2016-09-28 22:57:19 -07:00
* Returns a view that displays the accessories report.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function getAccessoryReport()
{
$accessories = Accessory::orderBy('created_at', 'DESC')->with('company')->get();
return View::make('reports/accessories', compact('accessories'));
}
/**
* Exports the accessories to CSV
*
* @deprecated Server-side exports have been replaced by datatables export since v2.
* @author [A. Gianotto] [<snipe@snipe.net>]
* @see ManufacturersController::getDatatable() method that generates the JSON response
* @since [v1.0]
2016-04-07 17:08:38 -07:00
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function exportAccessoryReport()
{
$accessories = Accessory::orderBy('created_at', 'DESC')->get();
$rows = array();
$header = array(
trans('admin/accessories/table.title'),
trans('admin/accessories/general.accessory_category'),
trans('admin/accessories/general.total'),
trans('admin/accessories/general.remaining')
2016-03-25 01:18:05 -07:00
);
$header = array_map('trim', $header);
$rows[] = implode($header, ', ');
// Row per accessory
foreach ($accessories as $accessory) {
$row = array();
2016-03-25 15:24:12 -07:00
$row[] = e($accessory->accessory_name);
$row[] = e($accessory->accessory_category);
$row[] = e($accessory->total);
$row[] = e($accessory->remaining);
2016-03-25 01:18:05 -07:00
$rows[] = implode($row, ',');
}
$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;
}
/**
* Display asset report view.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function getAssetsReport()
{
$settings = \App\Models\Setting::first();
2016-06-22 12:27:41 -07:00
return View::make('reports/asset', compact('assets'))->with('settings', $settings);
2016-03-25 01:18:05 -07:00
}
2016-03-25 01:18:05 -07:00
/**
* Exports the assets to CSV
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
2016-04-07 17:08:38 -07:00
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function exportAssetReport()
{
2016-09-12 14:06:55 -07:00
\Debugbar::disable();
2016-09-27 15:09:47 -07:00
$customfields = CustomField::get();
2016-09-28 19:18:01 -07:00
$response = new StreamedResponse(function() use ($customfields) {
2016-09-12 14:06:55 -07:00
// Open output stream
$handle = fopen('php://output', 'w');
2016-09-28 19:18:01 -07:00
Asset::with('assigneduser', 'assetloc','defaultLoc','assigneduser.userloc','model','supplier','assetstatus','model.manufacturer')->orderBy('created_at', 'DESC')->chunk(500, function($assets) use($handle, $customfields) {
2016-09-27 15:09:47 -07:00
$headers=[
trans('general.company'),
trans('admin/hardware/table.asset_tag'),
trans('admin/hardware/form.manufacturer'),
trans('admin/hardware/form.model'),
trans('general.model_no'),
trans('general.name'),
trans('admin/hardware/table.serial'),
trans('general.status'),
trans('admin/hardware/table.purchase_date'),
trans('admin/hardware/table.purchase_cost'),
trans('admin/hardware/form.order'),
trans('admin/hardware/form.supplier'),
trans('admin/hardware/table.checkoutto'),
trans('admin/hardware/table.checkout_date'),
trans('admin/hardware/table.location'),
trans('general.notes'),
];
foreach($customfields as $field) {
$headers[]=$field->name;
}
fputcsv($handle, $headers);
foreach ($assets as $asset) {
// Add a new row with data
$values=[
($asset->company) ? $asset->company->name : '',
$asset->asset_tag,
($asset->model->manufacturer) ? $asset->model->manufacturer->name : '',
($asset->model) ? $asset->model->name : '',
Partialize forms (#2884) * Consolidate edit form elements into reusable partials. This is a large code change that doesn't do much immediately. It refactors all of the various edit.blade.php files to reference standardized partials, so that they all reference the same base html layout. This has the side effect of moving everything to the new fancy "required" indicators, and making things look consistent. In addition, I've gone ahead and renamed a few database fields. We had Assetmodel::modelno and Consumable::model_no, I've renamed both to model_number. We had items using ::note and ::notes, I've standardized on ::notes. Component used total_qty where consumables and accessories used qty, so I've moved everything to qty (And fixed a few bugs in the helper file in the process. TODO includes looking at how/where to place the modal javascripts to allow for on the fly creation from all places, rather than just the asset page. Rename assetmodel::modelno to model_number for clarity and consistency Rename consumable::model_no to model_number for clarity and consistency Rename assetmodel::note to notes for clarity and consistency Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying. * Share a settings variable with all views. * Allow editing the per_page setting. We showed the value, but we never showed it on the edit page.. * use snipeSettings in all views instead of the long ugly path. * War on partials. Centralize all bootstrap table javascript * Use model_number instead of modelno in importer * Codacy fix. * More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read. * Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs * Fix DB seeder. * Base sql dump and csv's to import data from for tests. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name * Use a .env.tests file for testing functional and unit to allow a separate database. * Add functional tests for compoents, groups, and licenses. * Now that the config is in the functional.yml, this just confuses things. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * update db dump * Update tests to new reality * env for the test setup * only load the database at beginning of tests, not between each Functional test. * Fix a miss from renaming note to notes. * Set Termination date when creating an asset. It was only set on edit. * Rename serial_number to serial in components for consistency. * Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB. * Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
($asset->model->model_number) ? $asset->model->model_number : '',
2016-09-27 15:09:47 -07:00
($asset->name) ? $asset->name : '',
($asset->serial) ? $asset->serial : '',
($asset->assetstatus) ? e($asset->assetstatus->name) : '',
($asset->purchase_date) ? e($asset->purchase_date) : '',
($asset->purchase_cost > 0) ? Helper::formatCurrencyOutput($asset->purchase_cost) : '',
($asset->order_number) ? e($asset->order_number) : '',
($asset->supplier) ? e($asset->supplier->name) : '',
($asset->assigneduser) ? e($asset->assigneduser->present()->fullName()) : '',
2016-09-27 15:09:47 -07:00
($asset->last_checkout!='') ? e($asset->last_checkout) : '',
($asset->assigneduser && $asset->assigneduser->userloc!='') ?
e($asset->assigneduser->userloc->name) : ( ($asset->defaultLoc!='') ? e($asset->defaultLoc->name) : ''),
($asset->notes) ? e($asset->notes) : '',
];
foreach($customfields as $field) {
$values[]=$asset->{$field->db_column_name()};
}
fputcsv($handle, $values);
}
});
2016-03-25 01:18:05 -07:00
2016-09-12 14:06:55 -07:00
// Close the output stream
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="assets-'.date('Y-m-d-his').'.csv"',
]);
2016-03-25 01:18:05 -07:00
return $response;
2016-09-12 14:06:55 -07:00
2016-03-25 01:18:05 -07:00
}
/**
2016-04-07 17:08:38 -07:00
* Show depreciation report for assets.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function getDeprecationReport()
{
// Grab all the assets
$assets = Asset::with('model', 'assigneduser', 'assetstatus', 'defaultLoc', 'assetlog', 'company')
2016-03-25 01:18:05 -07:00
->orderBy('created_at', 'DESC')->get();
return View::make('reports/depreciation', compact('assets'));
}
/**
2016-04-07 17:08:38 -07:00
* Exports the depreciations to CSV
*
* @deprecated Server-side exports have been replaced by datatables export since v2.
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function exportDeprecationReport()
{
// Grab all the assets
$assets = Asset::with('model', 'assigneduser', 'assetstatus', 'defaultLoc', 'assetlog')
->orderBy('created_at', 'DESC')->get();
$csv = \League\Csv\Writer::createFromFileObject(new \SplTempFileObject());
$csv->setOutputBOM(Reader::BOM_UTF16_BE);
$rows = [ ];
// Create the header row
$header = [
trans('admin/hardware/table.asset_tag'),
trans('admin/hardware/table.title'),
trans('admin/hardware/table.serial'),
trans('admin/hardware/table.checkoutto'),
trans('admin/hardware/table.location'),
trans('admin/hardware/table.purchase_date'),
trans('admin/hardware/table.purchase_cost'),
trans('admin/hardware/table.book_value'),
trans('admin/hardware/table.diff')
2016-03-25 01:18:05 -07:00
];
//we insert the CSV header
$csv->insertOne($header);
// Create a row per asset
foreach ($assets as $asset) {
$row = [ ];
2016-03-25 15:24:12 -07:00
$row[] = e($asset->asset_tag);
$row[] = e($asset->name);
$row[] = e($asset->serial);
2016-03-25 01:18:05 -07:00
if ($asset->assigned_to > 0) {
$user = User::find($asset->assigned_to);
$row[] = e($user->present()->fullName());
2016-03-25 01:18:05 -07:00
} else {
$row[] = ''; // Empty string if unassigned
}
if (( $asset->assigned_to > 0 ) && ( $asset->assigneduser->location_id > 0 )) {
$location = Location::find($asset->assigneduser->location_id);
if ($location->city) {
2016-03-25 15:24:12 -07:00
$row[] = e($location->city) . ', ' . e($location->state);
2016-03-25 01:18:05 -07:00
} elseif ($location->name) {
2016-03-25 15:24:12 -07:00
$row[] = e($location->name);
2016-03-25 01:18:05 -07:00
} else {
$row[] = '';
}
} else {
$row[] = ''; // Empty string if location is not set
}
if ($asset->assetloc) {
2016-03-25 15:24:12 -07:00
$currency = e($asset->assetloc->currency);
2016-03-25 01:18:05 -07:00
} else {
2016-03-25 15:24:12 -07:00
$currency = e(Setting::first()->default_currency);
2016-03-25 01:18:05 -07:00
}
$row[] = $asset->purchase_date;
$row[] = $currency . Helper::formatCurrencyOutput($asset->purchase_cost);
$row[] = $currency . Helper::formatCurrencyOutput($asset->getDepreciatedValue());
$row[] = $currency . Helper::formatCurrencyOutput(( $asset->purchase_cost - $asset->getDepreciatedValue() ));
2016-03-25 01:18:05 -07:00
$csv->insertOne($row);
}
$csv->output('depreciation-report-' . date('Y-m-d') . '.csv');
die;
}
/**
2016-04-07 17:08:38 -07:00
* Displays activity report.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function getActivityReport()
{
$log_actions = Actionlog::orderBy('created_at', 'DESC')
2016-09-28 22:57:19 -07:00
->with('item')
2016-03-25 01:18:05 -07:00
->orderBy('created_at', 'DESC')
->get();
return View::make('reports/activity', compact('log_actions'));
}
/**
2016-09-28 22:57:19 -07:00
* Returns Activity Report JSON.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
public function getActivityReportDataTable()
{
$activitylogs = Company::scopeCompanyables(Actionlog::with('item', 'user', 'target'))->orderBy('created_at', 'DESC');
2016-09-28 22:57:19 -07:00
if (Input::has('search')) {
$activitylogs = $activitylogs->TextSearch(e(Input::get('search')));
2016-09-28 22:57:19 -07:00
}
if (Input::has('offset')) {
$offset = e(Input::get('offset'));
} else {
$offset = 0;
}
if (Input::has('limit')) {
$limit = e(Input::get('limit'));
} else {
$limit = 50;
}
$allowed_columns = ['created_at'];
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
$activityCount = $activitylogs->count();
$activitylogs = $activitylogs->offset($offset)->limit($limit)->get();
2016-09-28 22:57:19 -07:00
$rows = array();
foreach ($activitylogs as $activity) {
if (($activity->item) && ($activity->itemType()=="asset")) {
$activity_item = '<a href="'.route('hardware.show', $activity->item_id).'">'.e($activity->item->asset_tag).' - '. e($activity->item->present()->name()).'</a>';
2016-09-28 22:57:19 -07:00
$item_type = 'asset';
2016-10-26 11:27:37 -07:00
} elseif ($activity->item) {
2016-12-15 20:52:39 -08:00
$activity_item = '<a href="' . route($activity->parseItemRoute().'.show', $activity->item_id) . '">' . e($activity->item->name) . '</a>';
2016-09-28 22:57:19 -07:00
$item_type = $activity->itemType();
} else {
$activity_item = "unkonwn";
$item_type = "null";
2016-09-28 22:57:19 -07:00
}
2016-12-15 19:59:42 -08:00
2016-09-28 22:57:19 -07:00
if (($activity->user) && ($activity->action_type=="uploaded") && ($activity->itemType()=="user")) {
$activity_target = '<a href="'.route('users.show', $activity->target_id).'">'.$activity->user->present()->fullName().'</a>';
} elseif ($activity->target_type === "App\Models\Asset") {
if($activity->target) {
$activity_target = '<a href="'.route('hardware.show', $activity->target_id).'">'.$activity->target->present()->name().'</a>';
} else {
$activity_target = "";
}
} elseif ( $activity->target_type === "App\Models\User") {
2016-10-25 12:53:07 -07:00
if($activity->target) {
$activity_target = '<a href="'.route('users.show', $activity->target_id).'">'.$activity->target->present()->fullName().'</a>';
} else {
$activity_target = '';
}
} elseif (($activity->action_type=='accepted') || ($activity->action_type=='declined')) {
$activity_target = '<a href="' . route('users.show', $activity->item->assigneduser->id) . '">' . e($activity->item->assigneduser->present()->fullName()) . '</a>';
2016-09-28 22:57:19 -07:00
} elseif ($activity->action_type=='requested') {
if ($activity->user) {
$activity_target = '<a href="'.route('users.show', $activity->user_id).'">'.$activity->user->present()->fullName().'</a>';
} else {
$activity_target = '';
}
2016-09-28 22:57:19 -07:00
} else {
if($activity->target) {
$activity_target = $activity->target->id;
} else {
$activity_target = "";
}
2016-09-28 22:57:19 -07:00
}
2016-12-15 19:59:42 -08:00
2016-09-28 22:57:19 -07:00
$rows[] = array(
2016-12-15 20:52:39 -08:00
'icon' => '<i class="'.$activity->parseItemIcon().'"></i>',
2016-09-28 22:57:19 -07:00
'created_at' => date("M d, Y g:iA", strtotime($activity->created_at)),
'action_type' => strtolower(trans('general.'.str_replace(' ','_',$activity->action_type))),
'admin' => $activity->user ? (string) link_to_route('users.show', $activity->user->present()->fullName(), [$activity->user_id]) : '',
2016-09-28 22:57:19 -07:00
'target' => $activity_target,
'item' => $activity_item,
2016-09-28 22:57:19 -07:00
'item_type' => $item_type,
'note' => e($activity->note),
);
}
$data = array('total'=>$activityCount, 'rows'=>$rows);
return $data;
}
/**
* Displays license report
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function getLicenseReport()
{
2016-09-28 22:57:19 -07:00
$licenses = License::with('depreciation')->orderBy('created_at', 'DESC')
2016-03-25 01:18:05 -07:00
->with('company')
->get();
return View::make('reports/licenses', compact('licenses'));
}
/**
2016-04-07 17:08:38 -07:00
* Exports the licenses to CSV
*
* @deprecated Server-side exports have been replaced by datatables export since v2.
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function exportLicenseReport()
{
$licenses = License::orderBy('created_at', 'DESC')->get();
$rows = [ ];
$header = [
trans('admin/licenses/table.title'),
trans('admin/licenses/table.serial'),
trans('admin/licenses/form.seats'),
trans('admin/licenses/form.remaining_seats'),
trans('admin/licenses/form.expiration'),
2016-11-29 08:46:33 -08:00
trans('general.purchase_date'),
trans('general.depreciation'),
trans('general.purchase_cost')
2016-03-25 01:18:05 -07:00
];
$header = array_map('trim', $header);
$rows[] = implode($header, ', ');
// Row per license
foreach ($licenses as $license) {
$row = [ ];
2016-03-25 15:24:12 -07:00
$row[] = e($license->name);
$row[] = e($license->serial);
$row[] = e($license->seats);
2016-03-25 01:18:05 -07:00
$row[] = $license->remaincount();
$row[] = $license->expiration_date;
$row[] = $license->purchase_date;
2016-09-28 22:57:19 -07:00
$row[] = ($license->depreciation!='') ? '' : e($license->depreciation->name);
$row[] = '"' . Helper::formatCurrencyOutput($license->purchase_cost) . '"';
2016-03-25 01:18:05 -07:00
$rows[] = implode($row, ',');
}
$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;
}
2016-04-07 17:08:38 -07:00
/**
* Returns a form that allows the user to generate a custom CSV report.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @see ReportsController::postCustomReport() method that generates the CSV
* @since [v1.0]
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function getCustomReport()
{
$customfields = CustomField::get();
return View::make('reports/custom')->with('customfields', $customfields);
2016-03-25 01:18:05 -07:00
}
2016-04-07 17:08:38 -07:00
/**
* Exports the custom report to CSV
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @see ReportsController::getCustomReport() method that generates form view
* @since [v1.0]
* @return \Illuminate\Http\Response
*/
2016-03-25 01:18:05 -07:00
public function postCustom()
{
$assets = Asset::orderBy('created_at', 'DESC')->with('company','assigneduser', 'assetloc','defaultLoc','assigneduser.userloc','model','supplier','assetstatus','model.manufacturer')->get();
$customfields = CustomField::get();
2016-03-25 01:18:05 -07:00
$rows = [ ];
$header = [ ];
if (e(Input::get('company')) == '1') {
$header[] = 'Company Name';
}
2016-03-25 01:18:05 -07:00
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';
}
2016-03-25 01:18:05 -07:00
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';
}
2016-08-02 15:16:01 -07:00
if (e(Input::get('username')) == '1') {
$header[] = 'Username';
}
if (e(Input::get('employee_num')) == '1') {
$header[] = 'Employee No.';
}
2016-03-25 01:18:05 -07:00
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');
}
2016-03-25 01:18:05 -07:00
foreach ($customfields as $customfield) {
if (e(Input::get($customfield->db_column_name())) == '1') {
$header[] = $customfield->name;
}
}
2016-03-25 01:18:05 -07:00
$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.'"';
}
2016-03-25 01:18:05 -07:00
if (e(Input::get('asset_name')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = '"' .e($asset->name) . '"';
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('asset_tag')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = e($asset->asset_tag);
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('manufacturer')) == '1') {
if ($asset->model->manufacturer) {
2016-03-25 15:24:12 -07:00
$row[] = '"' .e($asset->model->manufacturer->name) . '"';
2016-03-25 01:18:05 -07:00
} else {
$row[] = '';
}
}
if (e(Input::get('model')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = '"' . e($asset->model->name) . '"';
Partialize forms (#2884) * Consolidate edit form elements into reusable partials. This is a large code change that doesn't do much immediately. It refactors all of the various edit.blade.php files to reference standardized partials, so that they all reference the same base html layout. This has the side effect of moving everything to the new fancy "required" indicators, and making things look consistent. In addition, I've gone ahead and renamed a few database fields. We had Assetmodel::modelno and Consumable::model_no, I've renamed both to model_number. We had items using ::note and ::notes, I've standardized on ::notes. Component used total_qty where consumables and accessories used qty, so I've moved everything to qty (And fixed a few bugs in the helper file in the process. TODO includes looking at how/where to place the modal javascripts to allow for on the fly creation from all places, rather than just the asset page. Rename assetmodel::modelno to model_number for clarity and consistency Rename consumable::model_no to model_number for clarity and consistency Rename assetmodel::note to notes for clarity and consistency Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying. * Share a settings variable with all views. * Allow editing the per_page setting. We showed the value, but we never showed it on the edit page.. * use snipeSettings in all views instead of the long ugly path. * War on partials. Centralize all bootstrap table javascript * Use model_number instead of modelno in importer * Codacy fix. * More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read. * Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs * Fix DB seeder. * Base sql dump and csv's to import data from for tests. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name * Use a .env.tests file for testing functional and unit to allow a separate database. * Add functional tests for compoents, groups, and licenses. * Now that the config is in the functional.yml, this just confuses things. * Start some functional tests for creating items. * Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things * Improvements to functional tests. Use the built in DB seeding mechanism instead of doing it ourselves. Break the tests into multiple units, rather than testing everything in each function. * Some improvements to acceptance tests. Make sure we're only looking at the "trs" within the bootstrap table. Creation of assets is now tested at the functional level (and is faster) so ignore it here. I'm testing acceptance tests with the IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder imported. * update db dump * Update tests to new reality * env for the test setup * only load the database at beginning of tests, not between each Functional test. * Fix a miss from renaming note to notes. * Set Termination date when creating an asset. It was only set on edit. * Rename serial_number to serial in components for consistency. * Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB. * Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
$row[] = '"' . e($asset->model->model_number) . '"';
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('category')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = '"' .e($asset->model->category->name) . '"';
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('serial')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = e($asset->serial);
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('purchase_date')) == '1') {
2016-03-25 15:24:12 -07:00
$row[] = e($asset->purchase_date);
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('purchase_cost')) == '1' && ( e(Input::get('depreciation')) != '1' )) {
$row[] = '"' . Helper::formatCurrencyOutput($asset->purchase_cost) . '"';
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('eol')) == '1') {
$row[] = '"' .($asset->present()->eol_date()) ? $asset->present()->eol_date() : ''. '"';
}
2016-03-25 01:18:05 -07:00
if (e(Input::get('order')) == '1') {
if ($asset->order_number) {
2016-03-25 15:24:12 -07:00
$row[] = e($asset->order_number);
2016-03-25 01:18:05 -07:00
} else {
$row[] = '';
}
}
if (e(Input::get('supplier')) == '1') {
if ($asset->supplier) {
2016-03-25 15:24:12 -07:00
$row[] = '"' .e($asset->supplier->name) . '"';
2016-03-25 01:18:05 -07:00
} else {
$row[] = '';
}
}
2016-03-25 01:18:05 -07:00
if (e(Input::get('location')) == '1') {
$show_loc = '';
if (($asset->assigned_to > 0) && ($asset->assigneduser) && ($asset->assigneduser->location)) {
$show_loc .= '"' .e($asset->assigneduser->location->name). '"';
2016-03-25 01:18:05 -07:00
} elseif ($asset->rtd_location_id!='') {
$location = Location::find($asset->rtd_location_id);
if ($location) {
2016-03-25 15:24:12 -07:00
$show_loc .= '"' .e($location->name). '"';
2016-03-25 01:18:05 -07:00
} else {
$show_loc .= 'Default location '.$asset->rtd_location_id.' is invalid';
}
}
$row[] = $show_loc;
}
2016-03-25 01:18:05 -07:00
if (e(Input::get('assigned_to')) == '1') {
if ($asset->assigneduser) {
$row[] = '"' .e($asset->assigneduser->present()->fullName()). '"';
2016-03-25 01:18:05 -07:00
} else {
$row[] = ''; // Empty string if unassigned
}
}
if (e(Input::get('username')) == '1') {
if ($asset->assigneduser) {
$row[] = '"' .e($asset->assigneduser->username). '"';
} else {
$row[] = ''; // Empty string if unassigned
}
}
if (e(Input::get('employee_num')) == '1') {
if ($asset->assigneduser) {
$row[] = '"' .e($asset->assigneduser->employee_num). '"';
} else {
$row[] = ''; // Empty string if unassigned
}
}
2016-03-25 01:18:05 -07:00
if (e(Input::get('status')) == '1') {
if (( $asset->status_id == '0' ) && ( $asset->assigned_to == '0' )) {
$row[] = trans('general.ready_to_deploy');
2016-03-25 01:18:05 -07:00
} elseif (( $asset->status_id == '' ) && ( $asset->assigned_to == '0' )) {
$row[] = trans('general.pending');
2016-03-25 01:18:05 -07:00
} elseif ($asset->assetstatus) {
2016-03-25 15:24:12 -07:00
$row[] = '"' .e($asset->assetstatus->name). '"';
2016-03-25 01:18:05 -07:00
} else {
$row[] = '';
}
}
if (e(Input::get('warranty')) == '1') {
if ($asset->warranty_months) {
$row[] = $asset->warranty_months;
$row[] = $asset->present()->warrantee_expires();
2016-03-25 01:18:05 -07:00
} 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) . '"';
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('expected_checkin')) == '1') {
if ($asset->expected_checkin) {
$row[] = '"' .e($asset->expected_checkin). '"';
} else {
$row[] = ''; // Empty string if blankd
}
}
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);
}
}
2016-03-25 01:18:05 -07:00
$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='.date('Y-m-d-His').'-custom-asset-report.csv');
2016-03-25 01:18:05 -07:00
return $response;
} else {
2016-04-28 21:06:41 -07:00
return redirect()->to("reports/custom")
->with('error', trans('admin/reports/message.error'));
2016-03-25 01:18:05 -07:00
}
}
2016-12-15 19:59:42 -08:00
2016-03-25 01:18:05 -07:00
/**
* getImprovementsReport
*
2016-04-07 17:08:38 -07:00
* @return View
2016-03-25 01:18:05 -07:00
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
public function getAssetMaintenancesReport()
{
// Grab all the improvements
2016-03-25 19:26:22 -07:00
$assetMaintenances = AssetMaintenance::with('asset', 'supplier', 'asset.company')
2016-03-25 01:18:05 -07:00
->orderBy('created_at', 'DESC')
->get();
return View::make('reports/asset_maintenances', compact('assetMaintenances'));
}
/**
* exportImprovementsReport
*
* @return \Illuminate\Http\Response
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
public function exportAssetMaintenancesReport()
{
// Grab all the improvements
$assetMaintenances = AssetMaintenance::with('asset', 'supplier')
->orderBy('created_at', 'DESC')
->get();
$rows = [ ];
$header = [
trans('admin/hardware/table.asset_tag'),
trans('admin/asset_maintenances/table.asset_name'),
2016-11-29 12:48:00 -08:00
trans('general.supplier'),
trans('admin/asset_maintenances/form.asset_maintenance_type'),
trans('admin/asset_maintenances/form.title'),
trans('admin/asset_maintenances/form.start_date'),
trans('admin/asset_maintenances/form.completion_date'),
trans('admin/asset_maintenances/form.asset_maintenance_time'),
trans('admin/asset_maintenances/form.cost')
2016-03-25 01:18:05 -07:00
];
$header = array_map('trim', $header);
$rows[] = implode($header, ',');
foreach ($assetMaintenances as $assetMaintenance) {
$row = [ ];
$row[] = str_replace(',', '', e($assetMaintenance->asset->asset_tag));
2016-03-25 15:24:12 -07:00
$row[] = str_replace(',', '', e($assetMaintenance->asset->name));
$row[] = str_replace(',', '', e($assetMaintenance->supplier->name));
$row[] = e($assetMaintenance->improvement_type);
$row[] = e($assetMaintenance->title);
$row[] = e($assetMaintenance->start_date);
2016-03-25 18:51:44 -07:00
$row[] = e($assetMaintenance->completion_date);
2016-03-25 01:18:05 -07:00
if (is_null($assetMaintenance->asset_maintenance_time)) {
$improvementTime = intval(Carbon::now()
->diffInDays(Carbon::parse($assetMaintenance->start_date)));
} else {
$improvementTime = intval($assetMaintenance->asset_maintenance_time);
}
$row[] = $improvementTime;
$row[] = trans('general.currency') . Helper::formatCurrencyOutput($assetMaintenance->cost);
2016-03-25 01:18:05 -07:00
$rows[] = implode($row, ',');
}
// spit out a csv
$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;
}
/**
* getAssetAcceptanceReport
*
* @return mixed
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
public function getAssetAcceptanceReport()
{
$assetsForReport = Asset::notYetAccepted()->with('company')->get();
return View::make('reports/unaccepted_assets', compact('assetsForReport'));
}
/**
* exportAssetAcceptanceReport
*
* @return \Illuminate\Http\Response
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
public function exportAssetAcceptanceReport()
{
// Grab all the improvements
$assetsForReport = Actionlog::whereIn('id', $this->getAssetsNotAcceptedYet())
->get();
$rows = [ ];
$header = [
trans('general.category'),
trans('admin/hardware/form.model'),
trans('admin/hardware/form.name'),
trans('admin/hardware/table.asset_tag'),
trans('admin/hardware/table.checkoutto'),
2016-03-25 01:18:05 -07:00
];
$header = array_map('trim', $header);
$rows[] = implode($header, ',');
foreach ($assetsForReport as $assetItem) {
$row = [ ];
2016-03-25 15:24:12 -07:00
$row[] = str_replace(',', '', e($assetItem->assetlog->model->category->name));
$row[] = str_replace(',', '', e($assetItem->assetlog->model->name));
$row[] = str_replace(',', '', e($assetItem->assetlog->present()->name()));
2016-03-25 15:24:12 -07:00
$row[] = str_replace(',', '', e($assetItem->assetlog->asset_tag));
$row[] = str_replace(',', '', e($assetItem->assetlog->assigneduser->present()->fullName()));
2016-03-25 01:18:05 -07:00
$rows[] = implode($row, ',');
}
// spit out a csv
$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;
}
/**
* getCheckedOutAssetsRequiringAcceptance
*
* @param $modelsInCategoriesThatRequireAcceptance
*
* @return array
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
protected function getCheckedOutAssetsRequiringAcceptance($modelsInCategoriesThatRequireAcceptance)
{
$assets = Asset::deployed()
->inModelList($modelsInCategoriesThatRequireAcceptance)
->select('id')
->get()
->toArray();
return array_pluck($assets, 'id');
}
/**
* getModelsInCategoriesThatRequireAcceptance
*
* @param $assetCategoriesRequiringAcceptance
* @return array
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
protected function getModelsInCategoriesThatRequireAcceptance($assetCategoriesRequiringAcceptance)
{
return array_pluck(Model::inCategory($assetCategoriesRequiringAcceptance)
->select('id')
->get()
->toArray(), 'id');
}
/**
* getCategoriesThatRequireAcceptance
*
* @return array
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
protected function getCategoriesThatRequireAcceptance()
{
return array_pluck(Category::requiresAcceptance()
->select('id')
->get()
->toArray(), 'id');
}
/**
* getAssetsCheckedOutRequiringAcceptance
*
* @return array
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
protected function getAssetsCheckedOutRequiringAcceptance()
{
return $this->getCheckedOutAssetsRequiringAcceptance(
$this->getModelsInCategoriesThatRequireAcceptance($this->getCategoriesThatRequireAcceptance())
);
}
/**
* getAssetsNotAcceptedYet
*
* @return array
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
*/
protected function getAssetsNotAcceptedYet()
{
return Asset::unaccepted();
}
}