diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index 858a8d029703..9fe58ea40161 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -2,22 +2,9 @@ namespace App\Http\Controllers\Migration; -use App\Models\User; -use App\Models\Credit; -use App\Models\Contact; -use App\Models\Invoice; -use App\Models\Payment; -use App\Models\Product; -use App\Models\TaxRate; use App\Libraries\Utils; -use App\Models\Document; -use App\Models\PaymentMethod; -use App\Models\AccountGateway; -use App\Models\AccountGatewayToken; use App\Traits\GenerateMigrationResources; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Crypt; -use App\Models\AccountGatewaySettings; use App\Services\Migration\AuthService; use App\Http\Controllers\BaseController; use App\Services\Migration\CompanyService; @@ -26,6 +13,7 @@ use App\Http\Requests\MigrationTypeRequest; use App\Services\Migration\CompleteService; use App\Http\Requests\MigrationEndpointRequest; use App\Http\Requests\MigrationCompaniesRequest; +use App\Models\Account; class StepsController extends BaseController { @@ -159,10 +147,9 @@ class StepsController extends BaseController ); } - $companyService = (new CompanyService(session('MIGRATION_ACCOUNT_TOKEN'))) - ->endpoint(session('MIGRATION_ENDPOINT')) + $companyService = (new CompanyService()) ->start(); - + if ($companyService->isSuccessful()) { return view('migration.companies', ['companies' => $companyService->getCompanies()]); } @@ -180,15 +167,12 @@ class StepsController extends BaseController ); } - foreach ($request->companies as $company) { - (new CompleteService(session('MIGRATION_ACCOUNT_TOKEN'))) - ->file($this->getMigrationFile()) - ->force(array_key_exists('force', $company)) - ->company($company['id']) - ->endpoint(session('MIGRATION_ENDPOINT')) - ->companyKey($request->account_key) - ->start(); - } + $migrationData = $this->generateMigrationData($request->all()); + + $completeService = (new CompleteService(session('MIGRATION_ACCOUNT_TOKEN'))) + ->data($migrationData) + ->endpoint(session('MIGRATION_ENDPOINT')) + ->start(); return view('migration.completed'); } @@ -224,51 +208,61 @@ class StepsController extends BaseController * * @return string */ - public function getMigrationFile() + public function generateMigrationData(array $data): array { - $this->account = Auth::user()->account; + $migrationData = []; - $date = date('Y-m-d'); - $accountKey = $this->account->account_key; + foreach ($data['companies'] as $company) { + $account = Account::where('account_key', $company['id'])->firstOrFail(); - $output = fopen('php://output', 'w') or Utils::fatalError(); + $this->account = $account; - $fileName = "{$accountKey}-{$date}-invoiceninja"; + $date = date('Y-m-d'); + $accountKey = $this->account->account_key; - $data = [ - 'company' => $this->getCompany(), - 'users' => $this->getUsers(), - 'tax_rates' => $this->getTaxRates(), - 'payment_terms' => $this->getPaymentTerms(), - 'clients' => $this->getClients(), - 'vendors' => $this->getVendors(), - 'projects' => $this->getProjects(), - 'products' => $this->getProducts(), - 'invoices' => $this->getInvoices(), - 'recurring_invoices' => $this->getRecurringInvoices(), - 'quotes' => $this->getQuotes(), - 'credits' => $this->getCreditsNotes(), - 'payments' => array_merge($this->getPayments(), $this->getCredits()), - 'documents' => $this->getDocuments(), - 'company_gateways' => $this->getCompanyGateways(), - 'client_gateway_tokens' => $this->getClientGatewayTokens(), - 'expense_categories' => $this->getExpenseCategories(), - 'task_statuses' => $this->getTaskStatuses(), - 'expenses' => $this->getExpenses(), - 'tasks' => $this->getTasks(), - ]; + $output = fopen('php://output', 'w') or Utils::fatalError(); - $file = storage_path("migrations/{$fileName}.zip"); + $fileName = "{$accountKey}-{$date}-invoiceninja"; - $zip = new \ZipArchive(); - $zip->open($file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); - $zip->addFromString('migration.json', json_encode($data, JSON_PRETTY_PRINT)); - $zip->close(); + $migrationData[$company['id']]['data'] = [ + 'company' => $this->getCompany(), + 'users' => $this->getUsers(), + 'tax_rates' => $this->getTaxRates(), + 'payment_terms' => $this->getPaymentTerms(), + 'clients' => $this->getClients(), + 'vendors' => $this->getVendors(), + 'projects' => $this->getProjects(), + 'products' => $this->getProducts(), + 'invoices' => $this->getInvoices(), + 'recurring_invoices' => $this->getRecurringInvoices(), + 'quotes' => $this->getQuotes(), + 'credits' => $this->getCreditsNotes(), + 'payments' => array_merge($this->getPayments(), $this->getCredits()), + 'documents' => $this->getDocuments(), + 'company_gateways' => $this->getCompanyGateways(), + 'client_gateway_tokens' => $this->getClientGatewayTokens(), + 'expense_categories' => $this->getExpenseCategories(), + 'task_statuses' => $this->getTaskStatuses(), + 'expenses' => $this->getExpenses(), + 'tasks' => $this->getTasks(), + ]; + + $migrationData[$company['id']]['force'] = array_key_exists('force', $company) ? true : false; + + $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(); + + $migrationData[$company['id']]['file'] = $file; + } + + return $migrationData; // header('Content-Type: application/zip'); // header('Content-Length: ' . filesize($file)); // header("Content-Disposition: attachment; filename={$fileName}.zip"); - - return $file; } } diff --git a/app/Services/Migration/CompanyService.php b/app/Services/Migration/CompanyService.php index ac655af9bfd5..ff3a3d635fd6 100644 --- a/app/Services/Migration/CompanyService.php +++ b/app/Services/Migration/CompanyService.php @@ -2,46 +2,34 @@ namespace App\Services\Migration; +use App\Models\Account; use Unirest\Request; use Unirest\Request\Body; class CompanyService { - protected $token; - protected $endpoint = 'https://app.invoiceninja.com'; - protected $uri = '/api/v1/companies'; - protected $errors = []; protected $isSuccessful; protected $companies = []; - - public function __construct(string $token) - { - $this->token = $token; - } - - public function endpoint(string $endpoint) - { - $this->endpoint = $endpoint; - - return $this; - } - public function start() { - $response = Request::get($this->getUrl(), $this->getHeaders()); + try { + foreach (session(SESSION_USER_ACCOUNTS) as $company) { + $account = Account::find($company->account_id); - if ($response->code == 200) { - $this->isSuccessful = true; - - foreach($response->body->data as $company) { - $this->companies[] = $company; + if ($account) { + $this->companies[] = [ + 'id' => $company->account_id, + 'name' => $account->name, + 'company_key' => $account->account_key, + ]; + } } - } - if (in_array($response->code, [401, 422, 500])) { + $this->isSuccessful = true; + } catch (\Exception $th) { $this->isSuccessful = false; - $this->processErrors($response->body); + $this->errors = []; } return $this; @@ -61,29 +49,8 @@ class CompanyService return []; } - public function getErrors() { return $this->errors; } - - private function getHeaders() - { - return [ - 'X-Requested-With' => 'XMLHttpRequest', - 'X-Api-Token' => $this->token, - ]; - } - - private function getUrl() - { - return $this->endpoint . $this->uri; - } - - private function processErrors($errors) - { - $array = (array) $errors; - - $this->errors = $array; - } } diff --git a/app/Services/Migration/CompleteService.php b/app/Services/Migration/CompleteService.php index 4b2e315ab362..5e8bffe1def3 100644 --- a/app/Services/Migration/CompleteService.php +++ b/app/Services/Migration/CompleteService.php @@ -9,37 +9,25 @@ use Unirest\Request\Body; class CompleteService { protected $token; - protected $company; - protected $file; + protected $endpoint = 'https://app.invoiceninja.com'; + protected $uri = '/api/v1/migration/start/'; + protected $errors = []; + protected $isSuccessful; - protected $force = false; - protected $companyKey; + + protected $data; public function __construct(string $token) { $this->token = $token; } - public function file($file) + public function data(array $data) { - $this->file = $file; - - return $this; - } - - public function force($option) - { - $this->force = $option; - - return $this; - } - - public function company($company) - { - $this->company = $company; + $this->data = $data; return $this; } @@ -51,26 +39,30 @@ class CompleteService return $this; } - public function companyKey(string $key) - { - $this->companyKey = $key; - - return $this; - } - public function start() { $body = [ - 'migration' => \Unirest\Request\Body::file($this->file, 'application/zip'), - 'force' => $this->force, - 'company_key' => $this->companyKey, + 'companies' => [], ]; - $response = Request::post($this->getUrl(), $this->getHeaders(), $body); + foreach ($this->data as $companyKey => $companyData) { + $body['companies'][] = [ + 'company_key' => $companyKey, + 'migration' => \Unirest\Request\Body::file($companyData['file'], 'application/zip'), + 'force' => $companyData['force'], + ]; + } + + try { + $response = Request::post($this->getUrl(), $this->getHeaders(), json_encode($body)); + + dd($response); + } catch (\Exception $e) { + dd($e->getMessage()); + } if ($response->code == 200) { $this->isSuccessful = true; - $this->deleteFile(); } if (in_array($response->code, [401, 422, 500])) { @@ -88,7 +80,6 @@ class CompleteService return $this->isSuccessful; } - public function getErrors() { return $this->errors; @@ -105,11 +96,11 @@ class CompleteService private function getUrl() { - return $this->endpoint . $this->uri . $this->company; + return "{$this->endpoint}/{$this->uri}"; } - public function deleteFile() + public function deleteFile(string $path) { - Storage::delete($this->file); + Storage::delete($path); } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index e58a64e1dfc7..1fc731e2dcdd 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3790,7 +3790,9 @@ $LANG = array( 'tax_name1' => 'Tax Name 1', 'tax_name2' => 'Tax Name 2', 'transaction_id' => 'Transaction ID', -); + 'migration_select_company_label' => 'Awesome! Please select the companies you would like to migrate.', + 'force_migration' => 'Force migration', +); return $LANG; diff --git a/resources/views/migration/companies.blade.php b/resources/views/migration/companies.blade.php index ac73dc852014..d69439db8064 100644 --- a/resources/views/migration/companies.blade.php +++ b/resources/views/migration/companies.blade.php @@ -11,22 +11,23 @@

{!! trans('texts.welcome_to_the_new_version') !!}

-

Awesome! Please select the company you would like to apply migration.

+

{!! trans('texts.migration_select_company_label') !!}

{{ csrf_field() }} - + @foreach($companies as $company)
- -
- - - * All current company data will be wiped. +
@endforeach