1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00
invoiceninja/app/Jobs/HostedMigration.php

191 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2021-07-17 07:26:47 +02:00
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Libraries\Utils;
use App\Models\Account;
use App\Models\User;
use App\Services\Migration\CompleteService;
use App\Traits\GenerateMigrationResources;
2021-09-29 10:21:46 +02:00
use GuzzleHttp\RequestOptions;
2021-07-17 07:26:47 +02:00
use Illuminate\Support\Facades\Storage;
2021-09-29 10:21:46 +02:00
// use Unirest\Request;
2021-07-17 07:26:47 +02:00
class HostedMigration extends Job
{
use GenerateMigrationResources;
public $db;
public $data;
public $user;
private $v4_secret;
2021-07-17 10:58:42 +02:00
public $migration_token;
2021-07-17 07:26:47 +02:00
2021-07-23 01:37:08 +02:00
private $forced;
2022-06-22 04:28:01 +02:00
public $tries = 1;
public $timeout = 0;
2021-07-23 01:37:08 +02:00
public function __construct(User $user, array $data, $db, $forced = false)
2021-07-17 07:26:47 +02:00
{
$this->user = $user;
$this->data = $data;
$this->db = $db;
2021-07-23 01:37:08 +02:00
$this->forced = $forced;
2021-07-17 07:26:47 +02:00
$this->v4_secret = config('ninja.ninja_hosted_secret');
}
/**
* Execute the job.
*/
public function handle()
{
2021-07-17 10:58:42 +02:00
2021-07-17 07:26:47 +02:00
config(['database.default' => $this->db]);
//Create or get a token
$this->getToken();
$completeService = (new CompleteService($this->migration_token));
2021-07-17 07:59:08 +02:00
2021-07-17 10:58:42 +02:00
$migrationData = $this->generateMigrationData($this->data);
2021-07-17 07:26:47 +02:00
$completeService->data($migrationData)
->endpoint('https://v5-app1.invoicing.co')
2021-11-21 03:49:01 +01:00
// ->endpoint('http://devhosted.test:8000')
2021-07-17 07:26:47 +02:00
->start();
}
private function getToken()
{
2021-12-05 10:31:39 +01:00
$url = 'https://invoicing.co/api/v1/get_migration_account';
// $url = 'http://devhosted.test:8000/api/v1/get_migration_account';
2021-07-17 07:26:47 +02:00
$headers = [
'X-API-HOSTED-SECRET' => $this->v4_secret,
'X-Requested-With' => 'XMLHttpRequest',
'Content-Type' => 'application/json',
];
$body = [
2021-07-17 07:59:08 +02:00
'first_name' => $this->user->first_name,
'last_name' => $this->user->last_name,
2021-07-17 07:26:47 +02:00
'email' => $this->user->email,
2021-07-17 07:59:08 +02:00
'privacy_policy' => true,
'terms_of_service' => true,
'password' => '',
2021-07-17 07:26:47 +02:00
];
2021-09-29 10:21:46 +02:00
$client = new \GuzzleHttp\Client([
'headers' => $headers,
]);
2021-07-17 10:58:42 +02:00
2021-09-29 10:21:46 +02:00
$response = $client->post($url,[
RequestOptions::JSON => $body,
RequestOptions::ALLOW_REDIRECTS => false
]);
2021-07-17 07:26:47 +02:00
2021-09-29 10:21:46 +02:00
if($response->getStatusCode() == 401){
info($response->getBody());
} elseif ($response->getStatusCode() == 200) {
$message_body = json_decode($response->getBody(), true);
$this->migration_token = $message_body['token'];
2021-07-17 07:26:47 +02:00
} else {
2021-11-21 03:49:01 +01:00
// info(json_decode($response->getBody()->getContents()));
2021-09-29 10:21:46 +02:00
}
2021-07-17 10:58:42 +02:00
return $this;
2021-07-17 07:26:47 +02:00
}
public function generateMigrationData(array $data): array
{
set_time_limit(0);
$migrationData = [];
foreach ($data['companies'] as $company) {
$account = Account::where('account_key', $company['id'])->firstOrFail();
$this->account = $account;
2021-07-23 01:37:08 +02:00
if($this->forced){
//forced migration - we need to set this v4 account as inactive.
//set activate URL
$account_email_settings = $this->account->account_email_settings;
2021-07-23 06:38:55 +02:00
$account_email_settings->account_email_settings->forward_url_for_v5 = "https://invoiceninja-{$this->account->id}.invoicing.co";
2021-07-23 01:37:08 +02:00
$account_email_settings->save();
2021-07-23 06:38:55 +02:00
$this->account->subdomain = "invoiceninja-{$this->account->id}";
2021-07-23 01:37:08 +02:00
}
2021-07-17 07:26:47 +02:00
$date = date('Y-m-d');
$accountKey = $this->account->account_key;
$output = fopen('php://output', 'w') or Utils::fatalError();
$fileName = "{$accountKey}-{$date}-invoiceninja";
$localMigrationData['data'] = [
'account' => $this->getAccount(),
'company' => $this->getCompany(),
'users' => $this->getUsers(),
'tax_rates' => $this->getTaxRates(),
'payment_terms' => $this->getPaymentTerms(),
'clients' => $this->getClients(),
'company_gateways' => $this->getCompanyGateways(),
'client_gateway_tokens' => $this->getClientGatewayTokens(),
'vendors' => $this->getVendors(),
'projects' => $this->getProjects(),
'products' => $this->getProducts(),
'credits' => $this->getCreditsNotes(),
'invoices' => $this->getInvoices(),
2021-08-29 14:40:09 +02:00
'recurring_expenses' => $this->getRecurringExpenses(),
2021-07-17 07:26:47 +02:00
'recurring_invoices' => $this->getRecurringInvoices(),
'quotes' => $this->getQuotes(),
2022-03-19 10:55:52 +01:00
'payments' => $this->getPayments(),
2021-07-17 07:26:47 +02:00
'documents' => $this->getDocuments(),
'expense_categories' => $this->getExpenseCategories(),
'task_statuses' => $this->getTaskStatuses(),
'expenses' => $this->getExpenses(),
'tasks' => $this->getTasks(),
'documents' => $this->getDocuments(),
'ninja_tokens' => $this->getNinjaToken(),
];
$localMigrationData['force'] = array_key_exists('force', $company);
Storage::makeDirectory('migrations');
2022-03-30 01:54:20 +02:00
$file = Storage::path("app/migrations/{$fileName}.zip");
2021-07-17 07:26:47 +02:00
//$file = storage_path("migrations/{$fileName}.zip");
ksort($localMigrationData);
$zip = new \ZipArchive();
$zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$zip->addFromString('migration.json', json_encode($localMigrationData, JSON_PRETTY_PRINT));
$zip->close();
$localMigrationData['file'] = $file;
$migrationData[] = $localMigrationData;
}
return $migrationData;
}
}