2020-03-05 21:30:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Libraries\Utils;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Traits\GenerateMigrationResources;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class ExportMigrations extends Command
|
|
|
|
{
|
|
|
|
use GenerateMigrationResources;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-11-25 01:23:51 +01:00
|
|
|
protected $signature = 'migrations:export {--user=} {--random=}';
|
2020-03-05 21:30:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-03-10 22:11:31 +01:00
|
|
|
protected $description = 'Export account migrations to folder.';
|
2020-03-05 21:30:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-03-10 22:11:31 +01:00
|
|
|
$this->info('Note: Migrations will be stored inside of (storage/migrations) folder.');
|
|
|
|
|
|
|
|
if($this->option('user')) {
|
|
|
|
$record = User::findOrFail($this->option('user'));
|
|
|
|
return $this->export($record);
|
|
|
|
}
|
|
|
|
|
2020-11-25 01:23:51 +01:00
|
|
|
if($this->option('random')){
|
|
|
|
|
2020-11-25 10:06:37 +01:00
|
|
|
User::all()->random(200)->each(function ($user){
|
2020-11-25 01:23:51 +01:00
|
|
|
$this->export($user);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:11:31 +01:00
|
|
|
$users = User::all();
|
|
|
|
|
|
|
|
foreach($users as $user) {
|
2021-02-09 23:40:55 +01:00
|
|
|
Auth::login($user);
|
2020-03-10 22:11:31 +01:00
|
|
|
$this->export($user);
|
2020-03-05 21:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 22:11:31 +01:00
|
|
|
|
|
|
|
private function export($user)
|
|
|
|
{
|
|
|
|
$this->account = $user->account;
|
|
|
|
|
|
|
|
$date = date('Y-m-d');
|
|
|
|
$accountKey = $this->account->account_key;
|
|
|
|
|
|
|
|
$output = fopen('php://output', 'w') or Utils::fatalError();
|
|
|
|
|
|
|
|
$fileName = "{$accountKey}-{$date}-invoiceninja";
|
|
|
|
|
2020-11-23 12:04:58 +01:00
|
|
|
$data['data'] = [
|
2021-02-09 23:40:55 +01:00
|
|
|
'account' => $this->getAccount(),
|
2020-03-10 22:11:31 +01:00
|
|
|
'company' => $this->getCompany(),
|
|
|
|
'users' => $this->getUsers(),
|
|
|
|
'tax_rates' => $this->getTaxRates(),
|
2021-02-09 23:40:55 +01:00
|
|
|
'payment_terms' => $this->getPaymentTerms(),
|
2020-03-10 22:11:31 +01:00
|
|
|
'clients' => $this->getClients(),
|
2021-02-09 23:40:55 +01:00
|
|
|
'company_gateways' => $this->getCompanyGateways(),
|
|
|
|
'client_gateway_tokens' => $this->getClientGatewayTokens(),
|
|
|
|
'vendors' => $this->getVendors(),
|
|
|
|
'projects' => $this->getProjects(),
|
2020-03-10 22:11:31 +01:00
|
|
|
'products' => $this->getProducts(),
|
2021-02-09 23:40:55 +01:00
|
|
|
'credits' => $this->getCreditsNotes(),
|
2020-03-10 22:11:31 +01:00
|
|
|
'invoices' => $this->getInvoices(),
|
2021-02-09 23:40:55 +01:00
|
|
|
'recurring_invoices' => $this->getRecurringInvoices(),
|
2020-03-10 22:11:31 +01:00
|
|
|
'quotes' => $this->getQuotes(),
|
|
|
|
'payments' => array_merge($this->getPayments(), $this->getCredits()),
|
|
|
|
'documents' => $this->getDocuments(),
|
2021-02-09 23:40:55 +01:00
|
|
|
'expense_categories' => $this->getExpenseCategories(),
|
|
|
|
'task_statuses' => $this->getTaskStatuses(),
|
|
|
|
'expenses' => $this->getExpenses(),
|
|
|
|
'tasks' => $this->getTasks(),
|
|
|
|
'documents' => $this->getDocuments(),
|
2020-03-10 22:11:31 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$file = storage_path("migrations/{$fileName}.zip");
|
|
|
|
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
$zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|
|
|
$zip->addFromString('migration.json', json_encode($data, JSON_PRETTY_PRINT));
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
$this->info('User with id #' . $user->id . ' exported.');
|
|
|
|
}
|
2020-03-05 21:30:48 +01:00
|
|
|
}
|