1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Jobs/Company/CompanyExport.php

494 lines
20 KiB
PHP
Raw Normal View History

2021-05-13 00:13:33 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-05-13 00:13:33 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-05-13 00:13:33 +02:00
*/
namespace App\Jobs\Company;
use App\Models\User;
use App\Utils\Ninja;
use App\Models\Company;
2021-05-13 00:13:33 +02:00
use App\Libraries\MultiDB;
use Illuminate\Support\Str;
2021-05-13 08:01:12 +02:00
use App\Mail\DownloadBackup;
use App\Jobs\Util\UnlinkFile;
use App\Models\VendorContact;
use Illuminate\Bus\Queueable;
use App\Models\QuoteInvitation;
use App\Utils\Traits\MakesHash;
2021-05-13 04:12:18 +02:00
use App\Models\CreditInvitation;
use App\Jobs\Mail\NinjaMailerJob;
2021-05-13 04:12:18 +02:00
use App\Models\InvoiceInvitation;
use Illuminate\Support\Facades\App;
use App\Jobs\Mail\NinjaMailerObject;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use App\Models\PurchaseOrderInvitation;
use Illuminate\Support\Facades\Storage;
use Illuminate\Queue\InteractsWithQueue;
2021-05-13 05:32:36 +02:00
use App\Models\RecurringInvoiceInvitation;
2021-05-13 00:13:33 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class CompanyExport implements ShouldQueue
{
2021-05-13 03:25:26 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
2021-05-13 00:13:33 +02:00
private $export_format = 'json';
2021-05-13 03:25:26 +02:00
private $export_data = [];
2021-05-13 08:01:12 +02:00
2021-05-13 00:13:33 +02:00
/**
* Create a new job instance.
*
* @param Company $company
* @param User $user
* @param string $custom_token_name
*/
public function __construct(public Company $company, private User $user, public string $hash)
2021-05-13 00:13:33 +02:00
{
}
/**
* Execute the job.
*
* @return CompanyToken|null
*/
2021-05-14 05:32:37 +02:00
public function handle()
2021-05-13 00:13:33 +02:00
{
MultiDB::setDb($this->company->db);
2021-05-15 08:54:27 +02:00
$this->company = Company::where('company_key', $this->company->company_key)->first();
2021-05-13 00:13:33 +02:00
set_time_limit(0);
2021-05-13 05:32:36 +02:00
$this->export_data['app_version'] = config('ninja.app_version');
$this->export_data['storage_url'] = Storage::url('');
2021-05-13 05:32:36 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['activities'] = $this->company->all_activities->map(function ($activity) {
2021-05-13 05:32:36 +02:00
$activity = $this->transformArrayOfKeys($activity, [
'user_id',
'company_id',
'client_id',
'client_contact_id',
'account_id',
'project_id',
'vendor_id',
'payment_id',
'invoice_id',
'credit_id',
'invitation_id',
'task_id',
'expense_id',
'token_id',
'quote_id',
'subscription_id',
'recurring_invoice_id'
]);
return $activity;
2021-05-27 07:57:07 +02:00
})->makeHidden(['id'])->all();
2021-05-13 05:32:36 +02:00
2021-12-13 21:50:36 +01:00
// $this->export_data['backups'] = $this->company->all_activities()->with('backup')->cursor()->map(function ($activity){
2021-05-13 05:32:36 +02:00
2021-12-13 21:50:36 +01:00
// $backup = $activity->backup;
2021-05-13 08:01:12 +02:00
2021-12-13 21:50:36 +01:00
// if(!$backup)
// return;
2021-05-13 08:01:12 +02:00
2021-12-13 21:50:36 +01:00
// $backup->activity_id = $this->encodePrimaryKey($backup->activity_id);
2021-05-13 05:32:36 +02:00
2021-12-13 21:50:36 +01:00
// return $backup;
2021-05-13 05:32:36 +02:00
2021-12-13 21:50:36 +01:00
// })->all();
2021-05-13 05:32:36 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['users'] = $this->company->users()->withTrashed()->cursor()->map(function ($user) {
2021-05-15 08:54:27 +02:00
$user->account_id = $this->encodePrimaryKey($user->account_id);
2021-05-27 07:57:07 +02:00
// $user->id = $this->encodePrimaryKey($user->id);
2021-05-15 08:54:27 +02:00
2021-05-28 10:37:08 +02:00
return $user->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-15 08:54:27 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['client_contacts'] = $this->company->client_contacts->map(function ($client_contact) {
2021-05-28 00:00:30 +02:00
$client_contact = $this->transformArrayOfKeys($client_contact, ['company_id', 'user_id', 'client_id']);
2023-02-16 02:36:09 +01:00
return $client_contact->makeVisible([
'password',
'remember_token',
'user_id',
'company_id',
'client_id',
'google_2fa_secret',
'id',
'oauth_provider_id',
'oauth_user_id',
'token',
'hashed_id',
]);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['client_gateway_tokens'] = $this->company->client_gateway_tokens->map(function ($client_gateway_token) {
$client_gateway_token = $this->transformArrayOfKeys($client_gateway_token, ['company_id', 'client_id', 'company_gateway_id']);
2021-05-13 05:32:36 +02:00
2021-05-28 00:00:30 +02:00
return $client_gateway_token->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['clients'] = $this->company->clients()->orderBy('number', 'DESC')->cursor()->map(function ($client) {
2021-05-27 13:02:03 +02:00
$client = $this->transformArrayOfKeys($client, ['company_id', 'user_id', 'assigned_user_id', 'group_settings_id']);
2023-04-11 04:23:09 +02:00
$client->tax_data = '';
return $client->makeVisible(['id','private_notes','user_id','company_id','last_login','hashed_id'])->makeHidden(['is_tax_exempt','has_valid_vat_number']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2021-05-27 13:02:03 +02:00
2021-05-15 08:54:27 +02:00
$this->export_data['company'] = $this->company->toArray();
$this->export_data['company']['company_key'] = $this->createHash();
2023-02-16 02:36:09 +01:00
$this->export_data['company_gateways'] = $this->company->company_gateways()->withTrashed()->cursor()->map(function ($company_gateway) {
2021-05-13 04:12:18 +02:00
$company_gateway = $this->transformArrayOfKeys($company_gateway, ['company_id', 'user_id']);
2021-05-14 08:00:25 +02:00
$company_gateway->config = decrypt($company_gateway->config);
2021-05-28 00:00:30 +02:00
return $company_gateway->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['company_tokens'] = $this->company->tokens->map(function ($token) {
2021-05-13 04:12:18 +02:00
$token = $this->transformArrayOfKeys($token, ['company_id', 'account_id', 'user_id']);
return $token;
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['company_ledger'] = $this->company->ledger->map(function ($ledger) {
2021-05-13 04:12:18 +02:00
$ledger = $this->transformArrayOfKeys($ledger, ['activity_id', 'client_id', 'company_id', 'account_id', 'user_id','company_ledgerable_id']);
return $ledger;
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['company_users'] = $this->company->company_users()->without(['user','account'])->cursor()->map(function ($company_user) {
2021-05-13 04:12:18 +02:00
$company_user = $this->transformArrayOfKeys($company_user, ['company_id', 'account_id', 'user_id']);
2023-03-05 21:51:39 +01:00
// return $company_user->makeHidden(['react_settings']);
2021-05-13 04:12:18 +02:00
return $company_user;
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['credits'] = $this->company->credits()->orderBy('number', 'DESC')->cursor()->map(function ($credit) {
2021-05-13 04:12:18 +02:00
$credit = $this->transformBasicEntities($credit);
$credit = $this->transformArrayOfKeys($credit, ['recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id','invoice_id']);
2021-05-28 10:37:08 +02:00
return $credit->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['credit_invitations'] = CreditInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($credit) {
$credit = $this->transformArrayOfKeys($credit, ['company_id', 'user_id', 'client_contact_id', 'credit_id']);
2021-05-13 04:12:18 +02:00
2021-05-28 10:37:08 +02:00
return $credit->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2021-05-27 07:57:07 +02:00
$this->export_data['designs'] = $this->company->user_designs->makeHidden(['id'])->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['documents'] = $this->company->all_documents->map(function ($document) {
2021-05-30 13:26:43 +02:00
$document = $this->transformArrayOfKeys($document, ['user_id', 'assigned_user_id', 'company_id', 'project_id', 'vendor_id','documentable_id']);
$document->hashed_id = $this->encodePrimaryKey($document->id);
2021-05-13 04:12:18 +02:00
2021-05-28 10:37:08 +02:00
return $document->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['expense_categories'] = $this->company->expense_categories->map(function ($expense_category) {
2021-05-13 04:12:18 +02:00
$expense_category = $this->transformArrayOfKeys($expense_category, ['user_id', 'company_id']);
2021-05-28 10:37:08 +02:00
return $expense_category->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['expenses'] = $this->company->expenses()->orderBy('number', 'DESC')->cursor()->map(function ($expense) {
2021-05-13 04:12:18 +02:00
$expense = $this->transformBasicEntities($expense);
$expense = $this->transformArrayOfKeys($expense, ['vendor_id', 'invoice_id', 'client_id', 'category_id', 'recurring_expense_id','project_id']);
2021-05-28 10:37:08 +02:00
return $expense->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['group_settings'] = $this->company->group_settings->map(function ($gs) {
2021-05-13 04:12:18 +02:00
$gs = $this->transformArrayOfKeys($gs, ['user_id', 'company_id']);
2021-05-28 10:37:08 +02:00
return $gs->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['invoices'] = $this->company->invoices()->orderBy('number', 'DESC')->cursor()->map(function ($invoice) {
2021-05-13 04:12:18 +02:00
$invoice = $this->transformBasicEntities($invoice);
$invoice = $this->transformArrayOfKeys($invoice, ['recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id','project_id']);
2023-04-11 04:23:09 +02:00
$invoice->tax_data = '';
2021-05-13 04:12:18 +02:00
2021-05-29 00:27:06 +02:00
return $invoice->makeVisible(['id',
'private_notes',
'user_id',
'client_id',
'company_id',]);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['invoice_invitations'] = InvoiceInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($invoice) {
$invoice = $this->transformArrayOfKeys($invoice, ['company_id', 'user_id', 'client_contact_id', 'invoice_id']);
2021-05-13 04:12:18 +02:00
2021-05-28 10:37:08 +02:00
return $invoice->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['payment_terms'] = $this->company->user_payment_terms->map(function ($term) {
2021-05-13 03:25:26 +02:00
$term = $this->transformArrayOfKeys($term, ['user_id', 'company_id']);
return $term;
2021-05-27 07:57:07 +02:00
})->makeHidden(['id'])->all();
2021-05-13 03:25:26 +02:00
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['payments'] = $this->company->payments()->orderBy('number', 'DESC')->cursor()->map(function ($payment) {
2021-05-13 04:12:18 +02:00
$payment = $this->transformBasicEntities($payment);
$payment = $this->transformArrayOfKeys($payment, ['client_id','project_id', 'vendor_id', 'client_contact_id', 'invitation_id', 'company_gateway_id', 'transaction_id']);
2021-05-13 04:12:18 +02:00
2021-05-30 10:03:31 +02:00
$payment->paymentables = $this->transformPaymentable($payment);
2021-05-28 00:00:30 +02:00
return $payment->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['products'] = $this->company->products->map(function ($product) {
2021-05-28 00:00:30 +02:00
$product = $this->transformBasicEntities($product);
$product = $this->transformArrayOfKeys($product, ['vendor_id','project_id']);
return $product->makeVisible(['id']);
})->all();
2021-05-13 04:12:18 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['projects'] = $this->company->projects()->orderBy('number', 'DESC')->cursor()->map(function ($project) {
2021-05-13 03:25:26 +02:00
$project = $this->transformBasicEntities($project);
$project = $this->transformArrayOfKeys($project, ['client_id']);
2021-05-28 10:37:08 +02:00
return $project->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['quotes'] = $this->company->quotes()->orderBy('number', 'DESC')->cursor()->map(function ($quote) {
2021-05-13 03:25:26 +02:00
$quote = $this->transformBasicEntities($quote);
$quote = $this->transformArrayOfKeys($quote, ['invoice_id','recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id']);
2021-05-28 10:37:08 +02:00
return $quote->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['quote_invitations'] = QuoteInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($quote) {
$quote = $this->transformArrayOfKeys($quote, ['company_id', 'user_id', 'client_contact_id', 'quote_id']);
2021-05-13 03:25:26 +02:00
2021-05-28 11:57:56 +02:00
return $quote->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['recurring_expenses'] = $this->company->recurring_expenses()->orderBy('number', 'DESC')->cursor()->map(function ($expense) {
$expense = $this->transformBasicEntities($expense);
$expense = $this->transformArrayOfKeys($expense, ['vendor_id', 'invoice_id', 'client_id', 'category_id', 'project_id']);
return $expense->makeVisible(['id']);
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['recurring_invoices'] = $this->company->recurring_invoices()->orderBy('number', 'DESC')->cursor()->map(function ($ri) {
2021-05-13 03:25:26 +02:00
$ri = $this->transformBasicEntities($ri);
2021-05-13 05:32:36 +02:00
$ri = $this->transformArrayOfKeys($ri, ['client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id']);
2021-05-28 10:37:08 +02:00
return $ri->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['recurring_invoice_invitations'] = RecurringInvoiceInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($ri) {
2021-05-13 03:25:26 +02:00
$ri = $this->transformArrayOfKeys($ri, ['company_id', 'user_id', 'client_contact_id', 'recurring_invoice_id']);
return $ri;
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['subscriptions'] = $this->company->subscriptions->map(function ($subscription) {
2021-05-13 03:25:26 +02:00
$subscription = $this->transformBasicEntities($subscription);
2021-05-13 08:01:12 +02:00
$subscription->group_id = $this->encodePrimaryKey($subscription->group_id);
2021-05-13 03:25:26 +02:00
2021-05-28 10:37:08 +02:00
return $subscription->makeVisible([ 'id',
'user_id',
'assigned_user_id',
'company_id',
'product_ids',
'recurring_product_ids',
'group_id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['system_logs'] = $this->company->system_logs->map(function ($log) {
2021-05-13 03:25:26 +02:00
$log->client_id = $this->encodePrimaryKey($log->client_id);
$log->company_id = $this->encodePrimaryKey($log->company_id);
return $log;
2021-05-27 07:57:07 +02:00
})->makeHidden(['id'])->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['tasks'] = $this->company->tasks()->orderBy('number', 'DESC')->cursor()->map(function ($task) {
2021-05-13 03:25:26 +02:00
$task = $this->transformBasicEntities($task);
2021-05-13 08:01:12 +02:00
$task = $this->transformArrayOfKeys($task, ['client_id', 'invoice_id', 'project_id', 'status_id']);
2021-05-13 03:25:26 +02:00
2021-05-28 10:37:08 +02:00
return $task->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['task_statuses'] = $this->company->task_statuses->map(function ($status) {
2021-05-13 03:25:26 +02:00
$status->id = $this->encodePrimaryKey($status->id);
$status->user_id = $this->encodePrimaryKey($status->user_id);
$status->company_id = $this->encodePrimaryKey($status->company_id);
return $status;
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['tax_rates'] = $this->company->tax_rates->map(function ($rate) {
2021-05-13 03:25:26 +02:00
$rate->company_id = $this->encodePrimaryKey($rate->company_id);
$rate->user_id = $this->encodePrimaryKey($rate->user_id);
2021-05-13 05:32:36 +02:00
return $rate;
2021-05-27 07:57:07 +02:00
})->makeHidden(['id'])->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['vendors'] = $this->company->vendors()->orderBy('number', 'DESC')->cursor()->map(function ($vendor) {
2021-05-28 10:37:08 +02:00
return $this->transformBasicEntities($vendor)->makeVisible(['id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['vendor_contacts'] = VendorContact::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($vendor) {
2021-05-13 03:25:26 +02:00
$vendor = $this->transformBasicEntities($vendor);
$vendor = $this->transformArrayOfKeys($vendor, ['vendor_id']);
2021-05-13 03:25:26 +02:00
return $vendor->makeVisible(['id','user_id']);
2021-05-27 07:57:07 +02:00
})->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['webhooks'] = $this->company->webhooks->map(function ($hook) {
2021-05-13 03:25:26 +02:00
$hook->user_id = $this->encodePrimaryKey($hook->user_id);
$hook->company_id = $this->encodePrimaryKey($hook->company_id);
return $hook;
2021-05-27 07:57:07 +02:00
})->makeHidden(['id'])->all();
2021-05-13 03:25:26 +02:00
2023-02-16 02:36:09 +01:00
$this->export_data['purchase_orders'] = $this->company->purchase_orders()->orderBy('number', 'DESC')->cursor()->map(function ($purchase_order) {
$purchase_order = $this->transformBasicEntities($purchase_order);
$purchase_order = $this->transformArrayOfKeys($purchase_order, ['expense_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id','project_id']);
return $purchase_order->makeVisible(['id',
'private_notes',
'user_id',
'client_id',
'vendor_id',
'company_id',]);
})->all();
2023-02-16 02:36:09 +01:00
$this->export_data['purchase_order_invitations'] = PurchaseOrderInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($purchase_order) {
$purchase_order = $this->transformArrayOfKeys($purchase_order, ['company_id', 'user_id', 'vendor_contact_id', 'purchase_order_id']);
return $purchase_order->makeVisible(['id']);
})->all();
2023-02-16 02:36:09 +01:00
$this->export_data['bank_integrations'] = $this->company->bank_integrations()->orderBy('id', 'ASC')->cursor()->map(function ($bank_integration) {
$bank_integration = $this->transformArrayOfKeys($bank_integration, ['account_id','company_id', 'user_id']);
return $bank_integration->makeVisible(['id','user_id','company_id','account_id']);
})->all();
2023-02-16 02:36:09 +01:00
$this->export_data['bank_transactions'] = $this->company->bank_transactions()->orderBy('id', 'ASC')->cursor()->map(function ($bank_transaction) {
$bank_transaction = $this->transformArrayOfKeys($bank_transaction, ['company_id', 'user_id','bank_integration_id','expense_id','category_id','ninja_category_id','vendor_id']);
return $bank_transaction->makeVisible(['id','user_id','company_id']);
})->all();
2021-05-13 08:01:12 +02:00
//write to tmp and email to owner();
2021-05-30 13:26:43 +02:00
2023-02-16 02:36:09 +01:00
$this->zipAndSend();
2021-05-14 05:32:37 +02:00
2023-02-16 02:36:09 +01:00
return true;
2021-05-13 03:25:26 +02:00
}
private function transformBasicEntities($model)
{
2021-05-29 00:09:47 +02:00
return $this->transformArrayOfKeys($model, ['user_id', 'assigned_user_id', 'company_id']);
2021-05-13 03:25:26 +02:00
}
private function transformArrayOfKeys($model, $keys)
{
2023-02-16 02:36:09 +01:00
foreach ($keys as $key) {
2021-05-13 03:25:26 +02:00
$model->{$key} = $this->encodePrimaryKey($model->{$key});
}
return $model;
2021-05-13 00:13:33 +02:00
}
2021-05-30 10:03:31 +02:00
private function transformPaymentable($payment)
{
$new_arr = [];
2023-02-16 02:36:09 +01:00
foreach ($payment->paymentables as $paymentable) {
2021-05-30 10:03:31 +02:00
$paymentable->payment_id = $this->encodePrimaryKey($paymentable->payment_id);
$paymentable->paymentable_id = $this->encodePrimaryKey($paymentable->paymentable_id);
$new_arr[] = $paymentable;
}
return $new_arr;
}
2021-05-13 08:01:12 +02:00
private function zipAndSend()
{
2023-02-16 02:36:09 +01:00
$file_name = date('Y-m-d').'_'.str_replace([" ", "/"], ["_",""], $this->company->present()->name() . '_' . $this->company->company_key .'.zip');
2021-05-13 08:01:12 +02:00
2021-06-15 15:11:45 +02:00
$path = 'backups';
Storage::makeDirectory(public_path('storage/backups/'));
try {
mkdir(public_path('storage/backups/'));
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
nlog("could not create directory");
}
2021-05-26 10:47:10 +02:00
$zip_path = public_path('storage/backups/'.$file_name);
$zip = new \ZipArchive();
2021-05-13 08:01:12 +02:00
2023-02-16 02:36:09 +01:00
if ($zip->open($zip_path, \ZipArchive::CREATE)!==true) {
2021-05-26 10:47:10 +02:00
nlog("cannot open {$zip_path}");
}
2021-05-13 08:01:12 +02:00
2021-05-26 10:47:10 +02:00
$zip->addFromString("backup.json", json_encode($this->export_data));
$zip->close();
2021-05-13 08:01:12 +02:00
2023-02-16 02:36:09 +01:00
if (Ninja::isHosted()) {
2021-05-31 12:47:12 +02:00
Storage::disk(config('filesystems.default'))->put('backups/'.$file_name, file_get_contents($zip_path));
}
2021-09-18 06:50:15 +02:00
$storage_file_path = Storage::disk(config('filesystems.default'))->url('backups/'.$file_name);
$storage_path = Storage::disk(config('filesystems.default'))->path('backups/'.$file_name);
2021-09-18 06:48:13 +02:00
$url = Cache::get($this->hash);
Cache::put($this->hash, $storage_path, now()->addHour());
2023-07-07 07:05:08 +02:00
2021-08-23 12:14:04 +02:00
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
2023-02-16 02:36:09 +01:00
$company_reference = Company::find($this->company->id);
2021-05-13 15:37:25 +02:00
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadBackup($url, $company_reference);
2021-05-13 15:37:25 +02:00
$nmo->to_user = $this->user;
$nmo->company = $company_reference;
2021-05-13 15:37:25 +02:00
$nmo->settings = $this->company->settings;
2021-05-13 08:01:12 +02:00
2021-09-20 12:10:07 +02:00
NinjaMailerJob::dispatch($nmo, true);
UnlinkFile::dispatch(config('filesystems.default'), $storage_path)->delay(now()->addHours(1));
2021-05-13 15:37:25 +02:00
2023-02-16 02:36:09 +01:00
if (Ninja::isHosted()) {
2021-09-18 06:49:43 +02:00
sleep(3);
unlink($zip_path);
}
2021-05-13 08:01:12 +02:00
}
2021-05-13 00:13:33 +02:00
}