Merge pull request #5919 from turbo124/v5-develop

Company Import
This commit is contained in:
David Bomba 2021-06-04 20:20:45 +10:00 committed by GitHub
commit 1483c8bb02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 17 deletions

View File

@ -18,6 +18,7 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use ZipArchive;
class ImportJsonController extends BaseController class ImportJsonController extends BaseController
{ {
@ -67,7 +68,7 @@ class ImportJsonController extends BaseController
Cache::put( $hash, base64_encode( $contents ), 3600 ); 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); return response()->json(['message' => 'Processing'], 200);
@ -86,7 +87,7 @@ class ImportJsonController extends BaseController
if (! file_exists($file_location)) if (! file_exists($file_location))
throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); 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_contents);
unlink($file_location); unlink($file_location);

View File

@ -116,11 +116,12 @@ class CreateAccount
private function processSettings($settings) 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']); //&& $data = unserialize(@file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $this->client_ip))
$country_code = strtolower($data['geoplugin_countryCode']); // $currency_code = strtolower($data['geoplugin_currencyCode']);
// $country_code = strtolower($data['geoplugin_countryCode']);
$currency = Cache::get('currencies')->filter(function ($item) use ($currency_code) { $currency = Cache::get('currencies')->filter(function ($item) use ($currency_code) {
return strtolower($item->code) == $currency_code; return strtolower($item->code) == $currency_code;
@ -146,7 +147,7 @@ class CreateAccount
$settings->language_id = (string)$language->id; $settings->language_id = (string)$language->id;
} }
$timezone = Timezone::where('name', $data['geoplugin_timezone'])->first(); //$timezone = Timezone::where('name', $data['geoplugin_timezone'])->first();
if($timezone) { if($timezone) {
$settings->timezone_id = (string)$timezone->id; $settings->timezone_id = (string)$timezone->id;

View File

@ -168,7 +168,7 @@ class CompanyExport implements ShouldQueue
$this->export_data['company'] = $this->company->toArray(); $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 = $this->transformArrayOfKeys($company_gateway, ['company_id', 'user_id']);
$company_gateway->config = decrypt($company_gateway->config); $company_gateway->config = decrypt($company_gateway->config);

View File

@ -40,6 +40,7 @@ use App\Models\Payment;
use App\Models\PaymentTerm; use App\Models\PaymentTerm;
use App\Models\Paymentable; use App\Models\Paymentable;
use App\Models\Product; use App\Models\Product;
use App\Models\Project;
use App\Models\Quote; use App\Models\Quote;
use App\Models\QuoteInvitation; use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice; use App\Models\RecurringInvoice;
@ -59,6 +60,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use ZipArchive; use ZipArchive;
@ -150,14 +152,15 @@ class CompanyImport implements ShouldQueue
if ( empty( $this->backup_file ) ) if ( empty( $this->backup_file ) )
throw new \Exception('No import data found, has the cache expired?'); 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(); $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() $this->preFlightChecks()
->purgeCompanyData() ->purgeCompanyData()
@ -177,7 +180,6 @@ class CompanyImport implements ShouldQueue
private function preFlightChecks() private function preFlightChecks()
{ {
//check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema //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) if($this->current_app_version != $this->backup_file->app_version)
{ {
//perform some magic here //perform some magic here
@ -328,8 +330,8 @@ class CompanyImport implements ShouldQueue
{ {
$this->genericImport(ClientContact::class, $this->genericImport(ClientContact::class,
['user_id', 'assigned_user_id', 'company_id', 'id', 'hashed_id'], ['user_id', 'company_id', 'id', 'hashed_id'],
[['users' => 'user_id'], ['users' => 'assigned_user_id']], [['users' => 'user_id'], ['clients' => 'client_id']],
'client_contacts', 'client_contacts',
'email'); 'email');
@ -802,9 +804,14 @@ class CompanyImport implements ShouldQueue
if(User::where('email', $user->email)->where('account_id', '!=', $this->account->id)->exists()) 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"); 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( $new_user = User::firstOrNew(
['email' => $user->email], ['email' => $user->email],
(array)$user, $user_array,
); );
$new_user->account_id = $this->account->id; $new_user->account_id = $this->account->id;
@ -824,11 +831,14 @@ class CompanyImport implements ShouldQueue
foreach($this->backup_file->company_users as $cu) 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( $new_cu = CompanyUser::firstOrNew(
['user_id' => $user_id, 'company_id', $this->company->id], ['user_id' => $user_id, 'company_id' => $this->company->id],
(array)$cu, $cu_array,
); );
$new_cu->account_id = $this->account->id; $new_cu->account_id = $this->account->id;
@ -1105,10 +1115,14 @@ class CompanyImport implements ShouldQueue
return null; return null;
if (! array_key_exists($resource, $this->ids)) { if (! array_key_exists($resource, $this->ids)) {
nlog($this->ids);
nlog($this->backup_file->payments);
throw new \Exception("Resource {$resource} not available."); throw new \Exception("Resource {$resource} not available.");
} }
if (! array_key_exists("{$old}", $this->ids[$resource])) { if (! array_key_exists("{$old}", $this->ids[$resource])) {
nlog($this->ids);
nlog($this->backup_file->payments);
throw new \Exception("Missing {$resource} key: {$old}"); throw new \Exception("Missing {$resource} key: {$old}");
} }

View File

@ -215,6 +215,7 @@ class HtmlEngine
$data['$quote_no'] = &$data['$quote.number']; $data['$quote_no'] = &$data['$quote.number'];
$data['$quote.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['$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_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['$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')]; $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')];