Company Import

This commit is contained in:
= 2021-06-04 20:07:45 +10:00
parent b75c01d6d4
commit a2e994be82
2 changed files with 22 additions and 12 deletions

View File

@ -68,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);
@ -87,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

@ -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;