Updates for company import

This commit is contained in:
David Bomba 2023-11-03 15:14:34 +11:00
parent aadbde7b07
commit 3013f64e10
7 changed files with 75 additions and 9 deletions

View File

@ -25,7 +25,10 @@ class StoreSchedulerRequest extends Request
*/ */
public function authorize(): bool public function authorize(): bool
{ {
return auth()->user()->isAdmin(); /** @var \App\Models\User $user */
$user = auth()->user();
return $user->isAdmin();
} }
public function rules() public function rules()

View File

@ -22,7 +22,10 @@ class UpdateSchedulerRequest extends Request
*/ */
public function authorize(): bool public function authorize(): bool
{ {
return auth()->user()->isAdmin() && $this->task_scheduler->company_id == auth()->user()->company()->id; /** @var \App\Models\User $user */
$user = auth()->user();
return $user->isAdmin() && $this->task_scheduler->company_id == $user->company()->id;
} }
public function rules(): array public function rules(): array

View File

@ -28,7 +28,10 @@ class ValidClientIds implements Rule
*/ */
public function passes($attribute, $value) public function passes($attribute, $value)
{ {
return Client::where('company_id', auth()->user()->company()->id)->whereIn('id', $this->transformKeys($value))->count() == count($value); /** @var \App\Models\User $user */
$user = auth()->user();
return Client::where('company_id', $user->company()->id)->whereIn('id', $this->transformKeys($value))->count() == count($value);
} }
/** /**

View File

@ -201,7 +201,7 @@ class CompanyExport implements ShouldQueue
return $document->makeVisible(['id']); return $document->makeVisible(['id']);
})->all(); })->all();
$this->export_data['expense_categories'] = $this->company->expense_categories->map(function ($expense_category) { $this->export_data['expense_categories'] = $this->company->expense_categories()->cursor()->map(function ($expense_category) {
$expense_category = $this->transformArrayOfKeys($expense_category, ['user_id', 'company_id']); $expense_category = $this->transformArrayOfKeys($expense_category, ['user_id', 'company_id']);
return $expense_category->makeVisible(['id']); return $expense_category->makeVisible(['id']);
@ -399,6 +399,12 @@ class CompanyExport implements ShouldQueue
return $bank_transaction->makeVisible(['id','user_id','company_id']); return $bank_transaction->makeVisible(['id','user_id','company_id']);
})->all(); })->all();
$this->export_data['schedulers'] = $this->company->schedulers()->orderBy('id', 'ASC')->cursor()->map(function ($scheduler) {
$scheduler = $this->transformArrayOfKeys($scheduler, ['company_id', 'user_id']);
return $scheduler->makeVisible(['id','user_id','company_id']);
})->all();
//write to tmp and email to owner(); //write to tmp and email to owner();
$this->zipAndSend(); $this->zipAndSend();

View File

@ -16,6 +16,7 @@ use App\Exceptions\NonExistingMigrationFile;
use App\Factory\ClientContactFactory; use App\Factory\ClientContactFactory;
use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Ninja\TaskScheduler;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\Import\CompanyImportFailure; use App\Mail\Import\CompanyImportFailure;
use App\Mail\Import\ImportCompleted; use App\Mail\Import\ImportCompleted;
@ -153,6 +154,7 @@ class CompanyImport implements ShouldQueue
'bank_integrations', 'bank_integrations',
'bank_transactions', 'bank_transactions',
'payments', 'payments',
'schedulers',
]; ];
private $company_properties = [ private $company_properties = [
@ -455,10 +457,12 @@ class CompanyImport implements ShouldQueue
$settings->project_number_counter = 1; $settings->project_number_counter = 1;
$settings->purchase_order_number_counter = 1; $settings->purchase_order_number_counter = 1;
$this->company->settings = $co->settings; $this->company->settings = $co->settings;
// $this->company->settings = $this->backup_file->company->settings;
$this->company->saveSettings($co->settings, $this->company);
$this->company->save(); $this->company->save();
return $this; return $this;
} }
private function purgeCompanyData() private function purgeCompanyData()
@ -472,6 +476,10 @@ class CompanyImport implements ShouldQueue
$this->company->expenses()->forceDelete(); $this->company->expenses()->forceDelete();
$this->company->subscriptions()->forceDelete(); $this->company->subscriptions()->forceDelete();
$this->company->purchase_orders()->forceDelete(); $this->company->purchase_orders()->forceDelete();
$this->company->bank_integrations()->forceDelete();
$this->company->bank_transactions()->forceDelete();
$this->company->schedulers()->forceDelete();
$this->company->system_log_relation()->forceDelete();
$this->company->save(); $this->company->save();
@ -523,6 +531,20 @@ class CompanyImport implements ShouldQueue
return $this; return $this;
} }
private function import_schedulers()
{
$this->genericNewClassImport(
\App\Models\Scheduler::class,
['company_id', 'id', 'hashed_id'],
[
['users' => 'user_id'],
],
'schedulers'
);
return $this;
}
private function import_bank_integrations() private function import_bank_integrations()
{ {
$this->genericImport( $this->genericImport(
@ -1147,7 +1169,6 @@ class CompanyImport implements ShouldQueue
$new_document->documentable_type = $document->documentable_type; $new_document->documentable_type = $document->documentable_type;
$new_document->save(['timestamps' => false]); $new_document->save(['timestamps' => false]);
$storage_url = (object)$this->getObject('storage_url', true); $storage_url = (object)$this->getObject('storage_url', true);
@ -1393,6 +1414,10 @@ class CompanyImport implements ShouldQueue
if (Ninja::isSelfHost() && $obj_array['gateway_key'] == 'd14dd26a47cecc30fdd65700bfb67b34') { if (Ninja::isSelfHost() && $obj_array['gateway_key'] == 'd14dd26a47cecc30fdd65700bfb67b34') {
$obj_array['gateway_key'] = 'd14dd26a37cecc30fdd65700bfb55b23'; $obj_array['gateway_key'] = 'd14dd26a37cecc30fdd65700bfb55b23';
} }
if(!isset($obj_array['fees_and_limits'])){
$obj_array['fees_and_limits'] = \json_encode([]);
}
} }
if (array_key_exists('deleted_at', $obj_array) && $obj_array['deleted_at'] > 1) { if (array_key_exists('deleted_at', $obj_array) && $obj_array['deleted_at'] > 1) {
@ -1430,8 +1455,29 @@ class CompanyImport implements ShouldQueue
$obj_array['config'] = encrypt($obj_array['config']); $obj_array['config'] = encrypt($obj_array['config']);
} }
if($class == 'App\Models\Scheduler') {
$parameters = $obj_array['parameters'];
if(isset($parameters->clients)){
$parameters->clients =
collect($parameters->clients)->map(function ($client_hash){
return $this->encodePrimaryKey($this->transformId('clients', $client_hash));
})->toArray();
}
if(isset($parameters->entity_id)){
$parameters->entity_id = $this->encodePrimaryKey($this->transformId($parameters->entity."s", $parameters->entity_id));
}
$obj_array['parameters'] = $parameters;
}
$new_obj = new $class(); $new_obj = new $class();
$new_obj->company_id = $this->company->id; $new_obj->company_id = $this->company->id;
$obj_array = $this->filterVersionProps($class,$obj_array);
$new_obj->fill($obj_array); $new_obj->fill($obj_array);
$new_obj->save(['timestamps' => false]); $new_obj->save(['timestamps' => false]);
@ -1475,6 +1521,8 @@ class CompanyImport implements ShouldQueue
$obj_array['recurring_product_ids'] = $this->recordProductIds($obj_array['recurring_product_ids']); $obj_array['recurring_product_ids'] = $this->recordProductIds($obj_array['recurring_product_ids']);
$obj_array['webhook_configuration'] = \json_encode($obj_array['webhook_configuration']); $obj_array['webhook_configuration'] = \json_encode($obj_array['webhook_configuration']);
} }
$obj_array = $this->filterVersionProps($class, $obj_array);
$new_obj = $class::firstOrNew( $new_obj = $class::firstOrNew(
[$match_key => $obj->{$match_key}], [$match_key => $obj->{$match_key}],
@ -1526,6 +1574,8 @@ class CompanyImport implements ShouldQueue
$obj_array['recurring_product_ids'] = ''; $obj_array['recurring_product_ids'] = '';
$obj_array['product_ids'] = ''; $obj_array['product_ids'] = '';
} }
$obj_array = $this->filterVersionProps($class, $obj_array);
/* Expenses that don't have a number will not be inserted - so need to override here*/ /* Expenses that don't have a number will not be inserted - so need to override here*/
if ($class == 'App\Models\Expense' && is_null($obj->{$match_key})) { if ($class == 'App\Models\Expense' && is_null($obj->{$match_key})) {

View File

@ -20,6 +20,7 @@ use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Carbon\Exceptions\InvalidFormatException; use Carbon\Exceptions\InvalidFormatException;
use App\Libraries\Currency\Conversion\CurrencyApi; use App\Libraries\Currency\Conversion\CurrencyApi;
use Illuminate\Database\Eloquent\Collection;
/** /**
* ExpenseRepository. * ExpenseRepository.
@ -167,7 +168,7 @@ class ExpenseRepository extends BaseRepository
* @param int $category_id * @param int $category_id
* @return void * @return void
*/ */
public function categorize($expenses, $category_id): void public function categorize(Collection $expenses, int $category_id): void
{ {
$ec = ExpenseCategory::withTrashed()->find($category_id); $ec = ExpenseCategory::withTrashed()->find($category_id);

View File

@ -27,7 +27,7 @@ class BankTransactionFactory extends Factory
'amount' => $this->faker->randomFloat(2, 10, 10000) , 'amount' => $this->faker->randomFloat(2, 10, 10000) ,
'currency_id' => '1', 'currency_id' => '1',
'account_type' => 'creditCard', 'account_type' => 'creditCard',
'category_id' => 1, 'category_id' => null,
'category_type' => 'Random' , 'category_type' => 'Random' ,
'date' => $this->faker->date('Y-m-d') , 'date' => $this->faker->date('Y-m-d') ,
'bank_account_id' => 1 , 'bank_account_id' => 1 ,