firefly-iii/app/Console/Commands/CreateExport.php

138 lines
4.6 KiB
PHP
Raw Normal View History

2017-09-14 11:10:38 -05:00
<?php
2018-05-11 03:08:34 -05:00
2017-09-14 11:10:38 -05:00
/**
* CreateExport.php
2018-05-11 03:08:34 -05:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2017-09-14 11:10:38 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-09-14 11:10:38 -05:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2017-09-14 11:10:38 -05:00
namespace FireflyIII\Console\Commands;
use Carbon\Carbon;
use FireflyIII\Export\ProcessorInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-09-14 11:10:38 -05:00
use Illuminate\Console\Command;
use Storage;
2017-09-14 11:10:38 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class CreateExport.
2017-09-16 00:41:03 -05:00
*
* Generates export from the command line.
2017-09-14 11:10:38 -05:00
*/
class CreateExport extends Command
{
use VerifiesAccessToken;
2017-09-14 11:10:38 -05:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Use this command to create a new import. Your user ID can be found on the /profile page.';
2017-09-14 11:10:38 -05:00
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature
= 'firefly:create-export
{--user= : The user ID that the import should import for.}
{--token= : The user\'s access token.}
{--with_attachments : Include user\'s attachments?}
{--with_uploads : Include user\'s uploads?}';
2017-09-14 11:10:38 -05:00
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (!$this->verifyAccessToken()) {
$this->error('Invalid access token.');
return 1;
}
$this->line('Full export is running...');
// make repositories
/** @var UserRepositoryInterface $userRepository */
$userRepository = app(UserRepositoryInterface::class);
/** @var ExportJobRepositoryInterface $jobRepository */
$jobRepository = app(ExportJobRepositoryInterface::class);
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
/** @var JournalRepositoryInterface $journalRepository */
$journalRepository = app(JournalRepositoryInterface::class);
// set user
$user = $userRepository->findNull((int)$this->option('user'));
$jobRepository->setUser($user);
$journalRepository->setUser($user);
$accountRepository->setUser($user);
// first date
2018-04-27 23:23:13 -05:00
$firstJournal = $journalRepository->firstNull();
$first = new Carbon;
2018-04-27 23:23:13 -05:00
if (null !== $firstJournal) {
$first = $firstJournal->date;
}
// create job and settings.
$job = $jobRepository->create();
$settings = [
'accounts' => $accountRepository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]),
'startDate' => $first,
'endDate' => new Carbon,
'exportFormat' => 'csv',
'includeAttachments' => $this->option('with_attachments'),
'includeOldUploads' => $this->option('with_uploads'),
'job' => $job,
];
/** @var ProcessorInterface $processor */
$processor = app(ProcessorInterface::class);
$processor->setSettings($settings);
$processor->collectJournals();
$processor->convertJournals();
$processor->exportJournals();
if ($settings['includeAttachments']) {
$processor->collectAttachments();
}
if ($settings['includeOldUploads']) {
$processor->collectOldUploads();
}
$processor->createZipFile();
$disk = Storage::disk('export');
$fileName = sprintf('export-%s.zip', date('Y-m-d_H-i-s'));
$disk->move($job->key . '.zip', $fileName);
$this->line('The export has finished! You can find the ZIP file in this location:');
$this->line(storage_path(sprintf('export/%s', $fileName)));
2018-04-02 07:42:07 -05:00
return 0;
2017-09-14 11:10:38 -05:00
}
}