1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Company/CompanyImport.php

235 lines
6.0 KiB
PHP
Raw Normal View History

2021-05-14 07:23:00 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Company;
2021-05-27 07:57:07 +02:00
use App\Exceptions\ImportCompanyFailed;
2021-05-14 07:23:00 +02:00
use App\Exceptions\NonExistingMigrationFile;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
use App\Libraries\MultiDB;
use App\Mail\DownloadBackup;
use App\Mail\DownloadInvoices;
use App\Models\Company;
2021-05-27 07:57:07 +02:00
use App\Models\CompanyUser;
2021-05-14 07:23:00 +02:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Models\User;
use App\Models\VendorContact;
use App\Utils\Traits\MakesHash;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
2021-05-15 06:29:19 +02:00
use Illuminate\Support\Str;
2021-05-14 07:23:00 +02:00
use ZipArchive;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class CompanyImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
2021-05-15 06:29:19 +02:00
protected $current_app_version;
2021-05-14 07:23:00 +02:00
public $company;
2021-05-15 06:29:19 +02:00
private $account;
2021-05-14 07:23:00 +02:00
public $file_path;
private $backup_file;
public $import_company;
2021-05-15 06:29:19 +02:00
public $ids = [];
2021-05-14 07:23:00 +02:00
private $options = '';
2021-05-15 06:29:19 +02:00
private $importables = [
'company',
'users',
// 'payment_terms',
// 'tax_rates',
2021-05-27 13:02:03 +02:00
// 'expense_categories',
// 'task_statuses',
2021-05-15 06:29:19 +02:00
// 'clients',
2021-05-28 00:00:30 +02:00
// 'client_contacts',
// 'products',
2021-05-15 06:29:19 +02:00
// 'vendors',
// 'projects',
2021-05-28 10:37:08 +02:00
// 'company_gateways',
// 'client_gateway_tokens',
// 'group_settings',
2021-05-15 06:29:19 +02:00
// 'credits',
// 'invoices',
// 'recurring_invoices',
// 'quotes',
// 'payments',
// 'expenses',
// 'tasks',
// 'documents',
2021-05-27 13:02:03 +02:00
// 'subscriptions',
// 'webhooks',
// 'system_logs',
// 'paymentables',
// 'company_ledger',
// 'backups',
2021-05-15 06:29:19 +02:00
];
2021-05-14 07:23:00 +02:00
/**
* Create a new job instance.
*
* @param Company $company
* @param User $user
* @param string $custom_token_name
*/
public function __construct(Company $company, string $file_path, array $options)
{
$this->company = $company;
$this->file_path = $file_path;
$this->options = $options;
2021-05-15 06:29:19 +02:00
$this->current_app_version = config('ninja.app_version');
2021-05-14 07:23:00 +02:00
}
public function handle()
{
MultiDB::setDb($this->company->db);
2021-05-27 07:57:07 +02:00
$this->company = Company::where('company_key', $this->company->company_key)->firstOrFail();
2021-05-15 06:29:19 +02:00
$this->account = $this->company->account;
2021-05-14 07:23:00 +02:00
$this->unzipFile()
->preFlightChecks();
2021-05-15 06:29:19 +02:00
foreach($this->importables as $import){
$method = Str::ucfirst(Str::camel($import));
$this->{$method}();
}
2021-05-14 07:23:00 +02:00
}
//check if this is a complete company import OR if it is selective
/*
Company and settings only
Data
*/
private function preFlightChecks()
{
//check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema
2021-05-15 06:29:19 +02:00
if($this->current_app_version != $this->backup_file->app_version)
{
//perform some magic here
}
2021-05-14 07:23:00 +02:00
return $this;
}
private function unzipFile()
{
$zip = new ZipArchive();
$archive = $zip->open(public_path("storage/backups/{$this->file_path}"));
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
$zip->extractTo(public_path("storage/backups/{$filename}"));
$zip->close();
$file_location = public_path("storage/backups/$filename/backup.json");
if (! file_exists($file_location)) {
throw new NonExistingMigrationFile('Backup file does not exist, or it is corrupted.');
}
$this->backup_file = json_decode(file_get_contents($file_location));
return $this;
}
private function importCompany()
{
//$this->import_company = ..
return $this;
}
2021-05-15 06:29:19 +02:00
private function importUsers()
2021-05-14 07:23:00 +02:00
{
2021-05-15 06:29:19 +02:00
User::unguard();
2021-05-14 07:23:00 +02:00
2021-05-15 06:29:19 +02:00
foreach ($this->backup_file->users as $user)
{
2021-05-27 07:57:07 +02:00
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");
2021-05-15 06:29:19 +02:00
$new_user = User::firstOrNew(
['email' => $user->email],
(array)$user,
);
$new_user->account_id = $this->account->id;
$new_user->save(['timestamps' => false]);
2021-05-27 07:57:07 +02:00
$this->ids['users']["{$user->hashed_id}"] = $new_user->id;
}
User::reguard();
}
private function importCompanyUsers()
{
CompanyUser::unguard();
foreach($this->backup_file->company_users as $cu)
{
$user_id = $this->transformId($cu->user_id);
$new_cu = CompanyUser::firstOrNew(
['user_id' => $user_id, 'company_id', $this->company->id],
(array)$cu,
);
$new_cu->account_id = $this->account->id;
$new_cu->save(['timestamps' => false]);
2021-05-15 06:29:19 +02:00
}
2021-05-27 07:57:07 +02:00
CompanyUser::reguard();
2021-05-15 06:29:19 +02:00
}
2021-05-27 07:57:07 +02:00
public function transformId(string $resource, string $old): int
2021-05-15 06:29:19 +02:00
{
if (! array_key_exists($resource, $this->ids)) {
throw new \Exception("Resource {$resource} not available.");
}
if (! array_key_exists("{$old}", $this->ids[$resource])) {
throw new \Exception("Missing resource key: {$old}");
}
return $this->ids[$resource]["{$old}"];
2021-05-14 07:23:00 +02:00
}
}