diff --git a/app/Http/Controllers/ImportJsonController.php b/app/Http/Controllers/ImportJsonController.php index 73dcac41f943..f10b5f7e4f1b 100644 --- a/app/Http/Controllers/ImportJsonController.php +++ b/app/Http/Controllers/ImportJsonController.php @@ -18,6 +18,7 @@ use App\Utils\Traits\MakesHash; use Illuminate\Http\Response; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; +use ZipArchive; class ImportJsonController extends BaseController { @@ -67,7 +68,7 @@ class ImportJsonController extends BaseController Cache::put( $hash, base64_encode( $contents ), 3600 ); - CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $hash, $request->all()); + CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $hash, $request->except('files')); return response()->json(['message' => 'Processing'], 200); @@ -86,7 +87,7 @@ class ImportJsonController extends BaseController if (! file_exists($file_location)) throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); - $data = json_decode(file_get_contents($file_location)); + $data = file_get_contents($file_location); unlink($file_contents); unlink($file_location); diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 6b41005b229b..4f3f23ae851e 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -116,11 +116,12 @@ class CreateAccount private function processSettings($settings) { - if(Ninja::isHosted() && Cache::get('currencies') && $data = unserialize(@file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $this->client_ip))) + if(Ninja::isHosted() && Cache::get('currencies')) { - $currency_code = strtolower($data['geoplugin_currencyCode']); - $country_code = strtolower($data['geoplugin_countryCode']); + //&& $data = unserialize(@file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $this->client_ip)) + // $currency_code = strtolower($data['geoplugin_currencyCode']); + // $country_code = strtolower($data['geoplugin_countryCode']); $currency = Cache::get('currencies')->filter(function ($item) use ($currency_code) { return strtolower($item->code) == $currency_code; @@ -146,7 +147,7 @@ class CreateAccount $settings->language_id = (string)$language->id; } - $timezone = Timezone::where('name', $data['geoplugin_timezone'])->first(); + //$timezone = Timezone::where('name', $data['geoplugin_timezone'])->first(); if($timezone) { $settings->timezone_id = (string)$timezone->id; diff --git a/app/Jobs/Company/CompanyExport.php b/app/Jobs/Company/CompanyExport.php index 256448b97dd9..2b7a942fd73d 100644 --- a/app/Jobs/Company/CompanyExport.php +++ b/app/Jobs/Company/CompanyExport.php @@ -168,7 +168,7 @@ class CompanyExport implements ShouldQueue $this->export_data['company'] = $this->company->toArray(); - $this->export_data['company_gateways'] = $this->company->company_gateways->map(function ($company_gateway){ + $this->export_data['company_gateways'] = $this->company->company_gateways->withTrashed()->cursor()->map(function ($company_gateway){ $company_gateway = $this->transformArrayOfKeys($company_gateway, ['company_id', 'user_id']); $company_gateway->config = decrypt($company_gateway->config); diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index c79822444304..1efb528d8e2d 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -40,6 +40,7 @@ use App\Models\Payment; use App\Models\PaymentTerm; use App\Models\Paymentable; use App\Models\Product; +use App\Models\Project; use App\Models\Quote; use App\Models\QuoteInvitation; use App\Models\RecurringInvoice; @@ -59,6 +60,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use ZipArchive; @@ -150,14 +152,15 @@ class CompanyImport implements ShouldQueue if ( empty( $this->backup_file ) ) throw new \Exception('No import data found, has the cache expired?'); - $this->backup_file = base64_decode($this->backup_file); + $this->backup_file = json_decode(base64_decode($this->backup_file)); + // nlog($this->backup_file); - if(array_key_exists('import_settings', $request) && $request['import_settings'] == 'true') { + if(array_key_exists('import_settings', $this->request_array) && $this->request_array['import_settings'] == 'true') { $this->preFlightChecks()->importSettings(); } - if(array_key_exists('import_data', $request) && $request['import_data'] == 'true') { + if(array_key_exists('import_data', $this->request_array) && $this->request_array['import_data'] == 'true') { $this->preFlightChecks() ->purgeCompanyData() @@ -177,7 +180,6 @@ class CompanyImport implements ShouldQueue private function preFlightChecks() { //check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema - if($this->current_app_version != $this->backup_file->app_version) { //perform some magic here @@ -328,8 +330,8 @@ class CompanyImport implements ShouldQueue { $this->genericImport(ClientContact::class, - ['user_id', 'assigned_user_id', 'company_id', 'id', 'hashed_id'], - [['users' => 'user_id'], ['users' => 'assigned_user_id']], + ['user_id', 'company_id', 'id', 'hashed_id'], + [['users' => 'user_id'], ['clients' => 'client_id']], 'client_contacts', 'email'); @@ -802,9 +804,14 @@ class CompanyImport implements ShouldQueue if(User::where('email', $user->email)->where('account_id', '!=', $this->account->id)->exists()) throw new ImportCompanyFailed("{$user->email} is already in the system attached to a different account"); + $user_array = (array)$user; + unset($user_array['laravel_through_key']); + unset($user_array['hashed_id']); + unset($user_array['id']); + $new_user = User::firstOrNew( ['email' => $user->email], - (array)$user, + $user_array, ); $new_user->account_id = $this->account->id; @@ -824,11 +831,14 @@ class CompanyImport implements ShouldQueue foreach($this->backup_file->company_users as $cu) { - $user_id = $this->transformId($cu->user_id); + $user_id = $this->transformId('users', $cu->user_id); + + $cu_array = (array)$cu; + unset($cu_array['id']); $new_cu = CompanyUser::firstOrNew( - ['user_id' => $user_id, 'company_id', $this->company->id], - (array)$cu, + ['user_id' => $user_id, 'company_id' => $this->company->id], + $cu_array, ); $new_cu->account_id = $this->account->id; @@ -1105,10 +1115,14 @@ class CompanyImport implements ShouldQueue return null; if (! array_key_exists($resource, $this->ids)) { + nlog($this->ids); + nlog($this->backup_file->payments); throw new \Exception("Resource {$resource} not available."); } if (! array_key_exists("{$old}", $this->ids[$resource])) { + nlog($this->ids); + nlog($this->backup_file->payments); throw new \Exception("Missing {$resource} key: {$old}"); } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 976ff0783fb5..7173e5eeca0e 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -215,6 +215,7 @@ class HtmlEngine $data['$quote_no'] = &$data['$quote.number']; $data['$quote.quote_no'] = &$data['$quote.number']; $data['$quote.valid_until'] = ['value' => $this->translateDate($this->entity->due_date, $this->client->date_format(), $this->entity->client->locale()), 'label' => ctrans('texts.valid_until')]; + $data['$valid_until'] = &$data['$quote.valid_until']; $data['$credit_amount'] = ['value' => Number::formatMoney($this->entity_calc->getTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.credit_amount')]; $data['$credit_balance'] = ['value' => Number::formatMoney($this->entity->balance, $this->client) ?: ' ', 'label' => ctrans('texts.credit_balance')]; $data['$quote.custom1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice1', $this->entity->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice1')];