Some enhancements to export.

This commit is contained in:
James Cole
2016-02-07 09:11:46 +01:00
parent e478238d77
commit 9a1710eb27
6 changed files with 65 additions and 19 deletions

View File

@@ -10,10 +10,13 @@ declare(strict_types = 1);
namespace FireflyIII\Export\Collector; namespace FireflyIII\Export\Collector;
use Amount;
use Auth; use Auth;
use Crypt; use Crypt;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Contracts\Encryption\DecryptException;
use Log; use Log;
/** /**
@@ -23,6 +26,9 @@ use Log;
*/ */
class AttachmentCollector extends BasicCollector implements CollectorInterface class AttachmentCollector extends BasicCollector implements CollectorInterface
{ {
/** @var string */
private $explanationString = '';
/** /**
* AttachmentCollector constructor. * AttachmentCollector constructor.
* *
@@ -48,12 +54,39 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface
$originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data'; $originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';
if (file_exists($originalFile)) { if (file_exists($originalFile)) {
Log::debug('Stored 1 attachment'); Log::debug('Stored 1 attachment');
try {
$decrypted = Crypt::decrypt(file_get_contents($originalFile)); $decrypted = Crypt::decrypt(file_get_contents($originalFile));
$newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - ' $newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - '
. $attachment->filename; . $attachment->filename;
file_put_contents($newFile, $decrypted); file_put_contents($newFile, $decrypted);
$this->getFiles()->push($newFile); $this->getFiles()->push($newFile);
// explain:
$this->explain($attachment);
} catch (DecryptException $e) {
Log::error('Catchable error: could not decrypt attachment #' . $attachment->id);
}
} }
} }
// put the explanation string in a file and attach it as well.
$explanationFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Source of all your attachments explained.txt';
file_put_contents($explanationFile, $this->explanationString);
$this->getFiles()->push($explanationFile);
}
/**
* @param Attachment $attachment
*/
private function explain(Attachment $attachment)
{
/** @var TransactionJournal $journal */
$journal = $attachment->attachable;
$string = 'Attachment #' . $attachment->id . ' is part of ' . strtolower($journal->transactionType->type) . ' #' . $journal->id
. ', with description "' . $journal->description . '". This transaction was for ' . Amount::formatJournal($journal, false) .
' and occured on ' . $journal->date->formatLocalized(config('config.month_and_day')) . "\n\n";
$this->explanationString .= $string;
} }
} }

View File

@@ -13,7 +13,8 @@ namespace FireflyIII\Export\Collector;
use Auth; use Auth;
use Crypt; use Crypt;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use Illuminate\Contracts\Encryption\DecryptException;
use Log;
/** /**
* Class UploadCollector * Class UploadCollector
* *
@@ -45,6 +46,7 @@ class UploadCollector extends BasicCollector implements CollectorInterface
$len = strlen($expected); $len = strlen($expected);
foreach ($files as $entry) { foreach ($files as $entry) {
if (substr($entry, 0, $len) === $expected) { if (substr($entry, 0, $len) === $expected) {
try {
// this is an original upload. // this is an original upload.
$parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry)); $parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry));
$originalUpload = intval($parts[1]); $originalUpload = intval($parts[1]);
@@ -58,6 +60,9 @@ class UploadCollector extends BasicCollector implements CollectorInterface
// add entry to set: // add entry to set:
$this->getFiles()->push($fullPath); $this->getFiles()->push($fullPath);
} catch (DecryptException $e) {
Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped.');
}
} }
} }
} }

View File

@@ -54,7 +54,7 @@ class ReportController extends Controller
public function index(ARI $repository) public function index(ARI $repository)
{ {
/** @var Carbon $start */ /** @var Carbon $start */
$start = session('first'); $start = clone session('first');
$months = $this->helper->listOfMonths($start); $months = $this->helper->listOfMonths($start);
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data; $customFiscalYear = Preferences::get('customFiscalYear', 0)->data;

View File

@@ -35,7 +35,9 @@ class ExportFormRequest extends Request
*/ */
public function rules() public function rules()
{ {
$first = session('first')->subDay()->format('Y-m-d'); $sessionFirst = clone session('first');
$first = $sessionFirst->subDay()->format('Y-m-d');
$today = Carbon::create()->addDay()->format('Y-m-d'); $today = Carbon::create()->addDay()->format('Y-m-d');
$formats = join(',', array_keys(config('firefly.export_formats'))); $formats = join(',', array_keys(config('firefly.export_formats')));

View File

@@ -27,6 +27,7 @@ function startExport() {
console.log('Start export...'); console.log('Start export...');
hideForm(); hideForm();
showLoading(); showLoading();
hideError();
// do export // do export
callExport(); callExport();
@@ -34,6 +35,10 @@ function startExport() {
return false; return false;
} }
function hideError() {
"use strict";
$('#export-error').hide();
}
function hideForm() { function hideForm() {
"use strict"; "use strict";

View File

@@ -73,6 +73,7 @@ return [
'export_status_creating_zip_file' => 'Creating a zip file...', 'export_status_creating_zip_file' => 'Creating a zip file...',
'export_status_created_zip_file' => 'Created a zip file!', 'export_status_created_zip_file' => 'Created a zip file!',
'export_status_finished' => 'Export has succesfully finished! Yay!', 'export_status_finished' => 'Export has succesfully finished! Yay!',
'export_data_please_wait' => 'Please wait...',
// rules // rules
'rules' => 'Rules', 'rules' => 'Rules',