Skip to content

Commit

Permalink
Use league/csv for export
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Jul 9, 2015
1 parent 4a50660 commit 2d10931
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/config/version.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
return array (
'app_version' => 'v1.2.8-149',
'hash_version' => 'v1.2.8-149-g74429d9',
'app_version' => 'v1.2.8-150',
'hash_version' => 'v1.2.8-150-g84ffc1a',
);
36 changes: 21 additions & 15 deletions app/controllers/admin/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Redirect;
use Response;
use Actionlog;
use Setting;
use League\Csv\Writer;
use League\Csv\Reader;

class ReportsController extends AdminController
{
Expand Down Expand Up @@ -147,6 +150,9 @@ 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 = array();

// Create the header row
Expand All @@ -161,8 +167,9 @@ public function exportDeprecationReport()
Lang::get('admin/hardware/table.book_value'),
Lang::get('admin/hardware/table.diff')
);
$header = array_map('trim', $header);
$rows[] = implode($header, ',');

//we insert the CSV header
$csv->insertOne($header);

// Create a row per asset
foreach ($assets as $asset) {
Expand All @@ -182,7 +189,7 @@ public function exportDeprecationReport()
if (($asset->assigned_to > 0) && ($asset->assigneduser->location_id > 0)) {
$location = Location::find($asset->assigneduser->location_id);
if ($location->city) {
$row[] = '"'.$location->city . ', ' . $location->state.'"';
$row[] = $location->city . ', ' . $location->state;
} elseif ($location->name) {
$row[] = $location->name;
} else {
Expand All @@ -194,23 +201,22 @@ public function exportDeprecationReport()




if ($asset->assetloc) {
$currency = $asset->assetloc->currency;
} else {
$currency = Setting::first()->default_currency;
}

$row[] = $asset->purchase_date;
$row[] = '"'.$asset->assetloc->currency.number_format($asset->purchase_cost).'"';
$row[] = '"'.$asset->assetloc->currency.number_format($asset->getDepreciatedValue()).'"';
$row[] = '"-'.$asset->assetloc->currency.number_format(($asset->purchase_cost - $asset->getDepreciatedValue())).'"';
$rows[] = implode($row, ',');
$row[] = $currency.number_format($asset->purchase_cost);
$row[] = $currency.number_format($asset->getDepreciatedValue());
$row[] = $currency.number_format(($asset->purchase_cost - $asset->getDepreciatedValue()));
$csv->insertOne($row);
}

// spit out a csv
$csv = implode($rows, "\n");
$csv->output('depreciation-report-'.date('Y-m-d').'.csv');
die;

$response = Response::make($csv, 200);
$response->header('Content-Type', 'text/csv');
$response->header('Content-disposition', 'attachment;filename=report.csv');

return $response;
}

/**
Expand Down

0 comments on commit 2d10931

Please sign in to comment.