1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Merge pull request #6287 from turbo124/master

Fixes for hosted migration workflow.
This commit is contained in:
David Bomba 2021-07-17 22:01:43 +10:00 committed by GitHub
commit b02bf18989
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 224 additions and 24 deletions

View File

@ -8,16 +8,17 @@ use App\Http\Requests\MigrationCompaniesRequest;
use App\Http\Requests\MigrationEndpointRequest; use App\Http\Requests\MigrationEndpointRequest;
use App\Http\Requests\MigrationForwardRequest; use App\Http\Requests\MigrationForwardRequest;
use App\Http\Requests\MigrationTypeRequest; use App\Http\Requests\MigrationTypeRequest;
use App\Jobs\HostedMigration;
use App\Libraries\Utils; use App\Libraries\Utils;
use App\Models\Account; use App\Models\Account;
use App\Services\Migration\AuthService; use App\Services\Migration\AuthService;
use App\Services\Migration\CompanyService; use App\Services\Migration\CompanyService;
use App\Services\Migration\CompleteService; use App\Services\Migration\CompleteService;
use App\Traits\GenerateMigrationResources; use App\Traits\GenerateMigrationResources;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Validator; use Validator;
use Illuminate\Http\Request;
class StepsController extends BaseController class StepsController extends BaseController
{ {
@ -48,6 +49,17 @@ class StepsController extends BaseController
*/ */
public function start() public function start()
{ {
if(Utils::isNinja()){
session()->put('MIGRATION_ENDPOINT', 'https://v5-app1.invoicing.co');
// session()->put('MIGRATION_ENDPOINT', 'http://ninja.test:8000');
session()->put('MIGRATION_ACCOUNT_TOKEN','');
session()->put('MIGRAITON_API_SECRET', null);
return $this->companies();
}
return view('migration.start'); return view('migration.start');
} }
@ -68,14 +80,17 @@ class StepsController extends BaseController
{ {
session()->put('MIGRATION_TYPE', $request->option); session()->put('MIGRATION_TYPE', $request->option);
if ($request->option == 0) { if ($request->option == 0 || $request->option == '0') {
session()->put('MIGRATION_ENDPOINT', 'https://invoicing.co');
return redirect( return redirect(
url('/migration/auth') url('/migration/companies?hosted=true')
); );
//old
// return redirect(
// url('/migration/auth')
// );
// return redirect( // return redirect(
// url('/migration/endpoint') // url('/migration/endpoint')
// ); // );
@ -118,6 +133,7 @@ class StepsController extends BaseController
public function endpoint() public function endpoint()
{ {
if ($this->shouldGoBack('endpoint')) { if ($this->shouldGoBack('endpoint')) {
return redirect( return redirect(
url($this->access['endpoint']['redirect']) url($this->access['endpoint']['redirect'])
@ -208,6 +224,16 @@ class StepsController extends BaseController
url($this->access['companies']['redirect']) url($this->access['companies']['redirect'])
); );
} }
$bool = true;
if(Utils::isNinja())
{
$this->dispatch(new HostedMigration(auth()->user(), $request->all(), config('database.default')));
return view('migration.completed');
}
$completeService = (new CompleteService(session('MIGRATION_ACCOUNT_TOKEN'))); $completeService = (new CompleteService(session('MIGRATION_ACCOUNT_TOKEN')));

View File

@ -0,0 +1,164 @@
<?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;
use Illuminate\Support\Facades\Storage;
use Unirest\Request;
class HostedMigration extends Job
{
use GenerateMigrationResources;
public $db;
public $data;
public $user;
private $v4_secret;
public $migration_token;
public function __construct(User $user, array $data, $db)
{
$this->user = $user;
$this->data = $data;
$this->db = $db;
$this->v4_secret = config('ninja.ninja_hosted_secret');
}
/**
* Execute the job.
*/
public function handle()
{
config(['database.default' => $this->db]);
//Create or get a token
$this->getToken();
$completeService = (new CompleteService($this->migration_token));
$migrationData = $this->generateMigrationData($this->data);
$completeService->data($migrationData)
->endpoint('https://v5-app1.invoicing.co')
// ->endpoint('http://ninja.test:8000')
->start();
}
private function getToken()
{
$url = 'https://invoicing.co/api/v1/get_migration_account';
// $url = 'http://ninja.test:8000/api/v1/get_migration_account';
$headers = [
'X-API-HOSTED-SECRET' => $this->v4_secret,
'X-Requested-With' => 'XMLHttpRequest',
'Content-Type' => 'application/json',
];
$body = [
'first_name' => $this->user->first_name,
'last_name' => $this->user->last_name,
'email' => $this->user->email,
'privacy_policy' => true,
'terms_of_service' => true,
'password' => '',
];
$body = \Unirest\Request\Body::json($body);
$response = Request::post($url, $headers, $body);
if (in_array($response->code, [200])) {
$data = $response->body;
info(print_r($data,1));
$this->migration_token = $data->token;
} else {
info("getting token failed");
info($response->raw_body);
}
return $this;
}
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;
$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(),
'recurring_invoices' => $this->getRecurringInvoices(),
'quotes' => $this->getQuotes(),
'payments' => array_merge($this->getPayments(), $this->getCredits()),
'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');
$file = Storage::path("migrations/{$fileName}.zip");
//$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;
}
}

View File

@ -40,6 +40,7 @@ class CompleteService
public function start() public function start()
{ {
$files = []; $files = [];
foreach ($this->data as $companyKey => $companyData) { foreach ($this->data as $companyKey => $companyData) {

View File

@ -73,9 +73,19 @@ trait GenerateMigrationResources
{ {
info("get company"); info("get company");
$financial_year_start = null;
if($this->account->financial_year_start)
{
//2000-02-01 format
$exploded_date = explode("-", $this->account->financial_year_start);
$financial_year_start = (int)$exploded_date[1];
}
return [ return [
'first_day_of_week' => $this->account->start_of_week, 'first_day_of_week' => $this->account->start_of_week,
'first_month_of_year' => $this->account->financial_year_start, 'first_month_of_year' => $financial_year_start,
'version' => NINJA_VERSION, 'version' => NINJA_VERSION,
'referral_code' => $this->account->referral_code ?: '', 'referral_code' => $this->account->referral_code ?: '',
'account_id' => $this->account->id, 'account_id' => $this->account->id,
@ -130,10 +140,15 @@ info("get company");
{ {
info("get co settings"); info("get co settings");
$timezone_id = $this->account->timezone_id ? $this->account->timezone_id : 15;
if($timezone_id > 57)
$timezone_id = (string)($timezone_id - 1);
return [ return [
'auto_bill' => $this->transformAutoBill($this->account->token_billing_id), 'auto_bill' => $this->transformAutoBill($this->account->token_billing_id),
'payment_terms' => $this->account->payment_terms ? (string) $this->account->payment_terms : '', 'payment_terms' => $this->account->payment_terms ? (string) $this->account->payment_terms : '',
'timezone_id' => $this->account->timezone_id ? (string) $this->account->timezone_id : '15', 'timezone_id' => $timezone_id,
'date_format_id' => $this->account->date_format_id ? (string) $this->account->date_format_id : '1', 'date_format_id' => $this->account->date_format_id ? (string) $this->account->date_format_id : '1',
'currency_id' => $this->account->currency_id ? (string) $this->account->currency_id : '1', 'currency_id' => $this->account->currency_id ? (string) $this->account->currency_id : '1',
'name' => $this->account->name ?: trans('texts.untitled'), 'name' => $this->account->name ?: trans('texts.untitled'),
@ -192,7 +207,7 @@ info("get company");
'payment_terms' => $this->account->payment_terms ?: '', 'payment_terms' => $this->account->payment_terms ?: '',
'reset_counter_frequency_id' => $this->account->reset_counter_frequency_id ? (string) $this->transformFrequencyId 'reset_counter_frequency_id' => $this->account->reset_counter_frequency_id ? (string) $this->transformFrequencyId
($this->account->reset_counter_frequency_id) : '0', ($this->account->reset_counter_frequency_id) : '0',
'payment_type_id' => $this->account->payment_type_id ? (string) $this->account->payment_type_id : '1', 'payment_type_id' => $this->account->payment_type_id ? (string) $this->transformPaymentType($this->account->payment_type_id) : '1',
'reset_counter_date' => $this->account->reset_counter_date ?: '', 'reset_counter_date' => $this->account->reset_counter_date ?: '',
'tax_name1' => $this->account->tax_name1 ?: '', 'tax_name1' => $this->account->tax_name1 ?: '',
'tax_rate1' => $this->account->tax_rate1 ?: 0, 'tax_rate1' => $this->account->tax_rate1 ?: 0,
@ -363,8 +378,6 @@ info("get company");
private function getClientSettings($client) private function getClientSettings($client)
{ {
info("get client settings");
$settings = new \stdClass(); $settings = new \stdClass();
$settings->currency_id = $client->currency_id ? (string) $client->currency_id : (string) $client->account->currency_id; $settings->currency_id = $client->currency_id ? (string) $client->currency_id : (string) $client->account->currency_id;
@ -381,7 +394,6 @@ info("get company");
protected function getClientContacts($client) protected function getClientContacts($client)
{ {
info("get client contacts");
$contacts = Contact::where('client_id', $client->id)->withTrashed()->get(); $contacts = Contact::where('client_id', $client->id)->withTrashed()->get();
@ -431,10 +443,6 @@ info("get company");
foreach($agts as $agt) { foreach($agts as $agt) {
$payment_method = $agt->default_payment_method; $payment_method = $agt->default_payment_method;
if(!$payment_method)
continue;
$contact = Contact::where('id', $payment_method->contact_id)->withTrashed()->first(); $contact = Contact::where('id', $payment_method->contact_id)->withTrashed()->first();
$transformed[] = [ $transformed[] = [
@ -999,7 +1007,7 @@ info("get company");
public function getResourceInvitations($items, $resourceKeyId) public function getResourceInvitations($items, $resourceKeyId)
{ {
info("get resource {$resourceKeyId} invitations"); // info("get resource {$resourceKeyId} invitations");
$transformed = []; $transformed = [];
@ -1072,7 +1080,7 @@ info("get company");
public function getInvoiceItems($items) public function getInvoiceItems($items)
{ {
info("get invoice items"); // info("get invoice items");
$transformed = []; $transformed = [];
@ -1262,7 +1270,7 @@ info("get company");
{ {
switch ($payment_type_id) { switch ($payment_type_id) {
case PAYMENT_TYPE_CREDIT: case PAYMENT_TYPE_CREDIT:
return 1; return 32;
case PAYMENT_TYPE_ACH: case PAYMENT_TYPE_ACH:
return 4; return 4;
case PAYMENT_TYPE_VISA: case PAYMENT_TYPE_VISA:
@ -1283,6 +1291,8 @@ info("get company");
return 12; return 12;
case PAYMENT_TYPE_PAYPAL: case PAYMENT_TYPE_PAYPAL:
return 13; return 13;
case 16:
return 15;
case PAYMENT_TYPE_CARTE_BLANCHE: case PAYMENT_TYPE_CARTE_BLANCHE:
return 16; return 16;
case PAYMENT_TYPE_UNIONPAY: case PAYMENT_TYPE_UNIONPAY:
@ -1396,12 +1406,9 @@ info("get company");
$fees_and_limits = $this->transformFeesAndLimits($gateway_type); $fees_and_limits = $this->transformFeesAndLimits($gateway_type);
info("generated fees and limits = ");
info(print_r($fees_and_limits,1));
$translated_gateway_type = $this->translateGatewayTypeId($gateway_type); $translated_gateway_type = $this->translateGatewayTypeId($gateway_type);
info("translated gateway_type = {$translated_gateway_type}");
$fees->{$translated_gateway_type} = $fees_and_limits; $fees->{$translated_gateway_type} = $fees_and_limits;
} }
@ -1774,7 +1781,7 @@ info("translated gateway_type = {$translated_gateway_type}");
'invoice_documents' => $expense->invoice_documents, 'invoice_documents' => $expense->invoice_documents,
'invoice_id' => $expense->invoice_id, 'invoice_id' => $expense->invoice_id,
'payment_date' => $expense->payment_date, 'payment_date' => $expense->payment_date,
'payment_type_id' => $expense->payment_type_id, 'payment_type_id' => $this->transformPaymentType($expense->payment_type_id),
'private_notes' => $expense->private_notes, 'private_notes' => $expense->private_notes,
'public_notes' => $expense->public_notes, 'public_notes' => $expense->public_notes,
'recurring_expense_id' => $expense->recurring_expense_id, 'recurring_expense_id' => $expense->recurring_expense_id,

View File

@ -47,4 +47,6 @@ return [
'subscription_key' => env('MSBOT_LUIS_SUBSCRIPTION_KEY'), 'subscription_key' => env('MSBOT_LUIS_SUBSCRIPTION_KEY'),
], ],
'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', false),
]; ];