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

470 lines
15 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Company;
2021-05-13 08:01:12 +02:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
2021-05-13 00:13:33 +02:00
use App\Libraries\MultiDB;
2021-05-13 08:01:12 +02:00
use App\Mail\DownloadBackup;
use App\Mail\DownloadInvoices;
2021-05-13 00:13:33 +02:00
use App\Models\Company;
2021-05-13 04:12:18 +02:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
2021-05-13 03:25:26 +02:00
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
2021-05-13 05:32:36 +02:00
use App\Models\RecurringInvoiceInvitation;
2021-05-13 08:01:12 +02:00
use App\Models\User;
2021-05-13 05:32:36 +02:00
use App\Models\VendorContact;
2021-05-13 03:25:26 +02:00
use App\Utils\Traits\MakesHash;
2021-05-13 00:13:33 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2021-05-13 08:01:12 +02:00
use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
2021-05-13 00:13:33 +02:00
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
2021-05-13 08:01:12 +02:00
public $company;
2021-05-13 00:13:33 +02:00
private $export_format;
2021-05-13 03:25:26 +02:00
private $export_data = [];
2021-05-13 08:01:12 +02:00
public $user;
2021-05-13 00:13:33 +02:00
/**
* Create a new job instance.
*
* @param Company $company
* @param User $user
* @param string $custom_token_name
*/
2021-05-13 08:01:12 +02:00
public function __construct(Company $company, User $user, $export_format = 'json')
2021-05-13 00:13:33 +02:00
{
$this->company = $company;
2021-05-13 08:01:12 +02:00
$this->user = $user;
2021-05-13 00:13:33 +02:00
$this->export_format = $export_format;
}
/**
* Execute the job.
*
* @return CompanyToken|null
*/
public function handle() : void
{
MultiDB::setDb($this->company->db);
set_time_limit(0);
2021-05-13 05:32:36 +02:00
$this->export_data['app_version'] = config('ninja.app_version');
$this->export_data['activities'] = $this->company->all_activities->map(function ($activity){
$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-13 03:25:26 +02:00
2021-05-13 08:01:12 +02:00
})->makeHidden(['id'])->toArray();
2021-05-13 05:32:36 +02:00
$this->export_data['backups'] = $this->company->all_activities()->with('backup')->cursor()->map(function ($activity){
$backup = $activity->backup;
2021-05-13 08:01:12 +02:00
if(!$backup)
return;
2021-05-13 05:32:36 +02:00
$backup->activity_id = $this->encodePrimaryKey($backup->activity_id);
return $backup;
})->toArray();
$this->export_data['client_contacts'] = $this->company->client_contacts->map(function ($client_contact){
$client_contact = $this->transformArrayOfKeys($client_contact, ['id', 'company_id', 'user_id',' client_id']);
return $client_contact;
})->toArray();
2021-05-13 04:12:18 +02:00
2021-05-13 05:32:36 +02: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, ['id', 'company_id', 'client_id']);
return $client_gateway_token;
2021-05-13 04:12:18 +02:00
})->toArray();
2021-05-13 05:32:36 +02:00
$this->export_data['clients'] = $this->company->clients->map(function ($client){
$client = $this->transformArrayOfKeys($client, ['id', 'company_id', 'user_id',' assigned_user_id', 'group_settings_id']);
2021-05-13 04:12:18 +02:00
2021-05-13 05:32:36 +02:00
return $client;
2021-05-13 04:12:18 +02:00
})->toArray();
2021-05-13 05:32:36 +02:00
$temp_co = $this->company;
$temp_co->id = $this->encodePrimaryKey($temp_co->id);
$temp_co->account_id = $this->encodePrimaryKey($temp_co->account_id);
$this->export_data['company'] = $temp_co->toArray();
2021-05-13 04:12:18 +02:00
$this->export_data['company_gateways'] = $this->company->company_gateways->map(function ($company_gateway){
$company_gateway = $this->transformArrayOfKeys($company_gateway, ['company_id', 'user_id']);
return $company_gateway;
})->toArray();
$this->export_data['company_tokens'] = $this->company->tokens->map(function ($token){
$token = $this->transformArrayOfKeys($token, ['company_id', 'account_id', 'user_id']);
return $token;
})->toArray();
$this->export_data['company_ledger'] = $this->company->ledger->map(function ($ledger){
$ledger = $this->transformArrayOfKeys($ledger, ['activity_id', 'client_id', 'company_id', 'account_id', 'user_id','company_ledgerable_id']);
return $ledger;
})->toArray();
$this->export_data['company_users'] = $this->company->company_users->map(function ($company_user){
$company_user = $this->transformArrayOfKeys($company_user, ['company_id', 'account_id', 'user_id']);
return $company_user;
})->toArray();
$this->export_data['credits'] = $this->company->credits->map(function ($credit){
$credit = $this->transformBasicEntities($credit);
$credit = $this->transformArrayOfKeys($credit, ['recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id','invoice_id']);
return $credit;
})->toArray();
2021-05-13 05:32:36 +02:00
$this->export_data['credit_invitations'] = CreditInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($credit){
2021-05-13 04:12:18 +02:00
$credit = $this->transformArrayOfKeys($credit, ['company_id', 'user_id', 'client_contact_id', 'recurring_invoice_id']);
return $credit;
})->toArray();
$this->export_data['designs'] = $this->company->user_designs->makeHidden(['id'])->toArray();
$this->export_data['documents'] = $this->company->documents->map(function ($document){
$document = $this->transformArrayOfKeys($document, ['user_id', 'assigned_user_id', 'company_id', 'project_id', 'vendor_id']);
return $document;
})->toArray();
$this->export_data['expense_categories'] = $this->company->expenses->map(function ($expense_category){
$expense_category = $this->transformArrayOfKeys($expense_category, ['user_id', 'company_id']);
return $expense_category;
})->toArray();
$this->export_data['expenses'] = $this->company->expenses->map(function ($expense){
$expense = $this->transformBasicEntities($expense);
$expense = $this->transformArrayOfKeys($expense, ['vendor_id', 'invoice_id', 'client_id', 'category_id', 'recurring_expense_id','project_id']);
return $expense;
})->toArray();
$this->export_data['group_settings'] = $this->company->group_settings->map(function ($gs){
$gs = $this->transformArrayOfKeys($gs, ['user_id', 'company_id']);
return $gs;
})->toArray();
$this->export_data['invoices'] = $this->company->invoices->map(function ($invoice){
$invoice = $this->transformBasicEntities($invoice);
$invoice = $this->transformArrayOfKeys($invoice, ['recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id']);
return $invoice;
})->toArray();
2021-05-13 05:32:36 +02:00
$this->export_data['invoice_invitations'] = InvoiceInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($invoice){
2021-05-13 04:12:18 +02:00
$invoice = $this->transformArrayOfKeys($invoice, ['company_id', 'user_id', 'client_contact_id', 'recurring_invoice_id']);
return $invoice;
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['payment_terms'] = $this->company->user_payment_terms->map(function ($term){
$term = $this->transformArrayOfKeys($term, ['user_id', 'company_id']);
return $term;
})->makeHidden(['id'])->toArray();
2021-05-13 04:12:18 +02:00
$this->export_data['paymentables'] = $this->company->payments()->with('paymentables')->cursor()->map(function ($paymentable){
$paymentable = $this->transformArrayOfKeys($paymentable, ['payment_id','paymentable_id']);
return $paymentable;
})->toArray();
$this->export_data['payments'] = $this->company->payments->map(function ($payment){
$payment = $this->transformBasicEntities($payment);
$payment = $this->transformArrayOfKeys($payment, ['client_id','project_id', 'vendor_id', 'client_contact_id', 'invitation_id', 'company_gateway_id']);
return $project;
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['projects'] = $this->company->projects->map(function ($project){
$project = $this->transformBasicEntities($project);
$project = $this->transformArrayOfKeys($project, ['client_id']);
return $project;
})->toArray();
$this->export_data['quotes'] = $this->company->quotes->map(function ($quote){
$quote = $this->transformBasicEntities($quote);
$quote = $this->transformArrayOfKeys($quote, ['invoice_id','recurring_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id']);
return $quote;
})->toArray();
2021-05-13 05:32:36 +02:00
$this->export_data['quote_invitations'] = QuoteInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($quote){
2021-05-13 03:25:26 +02:00
$quote = $this->transformArrayOfKeys($quote, ['company_id', 'user_id', 'client_contact_id', 'recurring_invoice_id']);
return $quote;
})->toArray();
$this->export_data['recurring_invoices'] = $this->company->recurring_invoices->map(function ($ri){
$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-13 03:25:26 +02:00
return $ri;
})->toArray();
2021-05-13 05:32:36 +02: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;
})->toArray();
$this->export_data['subscriptions'] = $this->company->subscriptions->map(function ($subscription){
$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
return $subscription;
2021-05-13 05:32:36 +02:00
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['system_logs'] = $this->company->system_logs->map(function ($log){
$log->client_id = $this->encodePrimaryKey($log->client_id);
$log->company_id = $this->encodePrimaryKey($log->company_id);
return $log;
})->makeHidden(['id'])->toArray();
$this->export_data['tasks'] = $this->company->tasks->map(function ($task){
$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-13 05:32:36 +02:00
return $task;
2021-05-13 03:25:26 +02:00
2021-05-13 05:32:36 +02:00
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['task_statuses'] = $this->company->task_statuses->map(function ($status){
$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-13 05:32:36 +02:00
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['tax_rates'] = $this->company->tax_rates->map(function ($rate){
$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-13 03:25:26 +02:00
})->makeHidden(['id'])->toArray();
$this->export_data['users'] = $this->company->users->map(function ($user){
$user->account_id = $this->encodePrimaryKey($user->account_id);
$user->id = $this->encodePrimaryKey($user->id);
return $user;
})->makeHidden(['ip'])->toArray();
$this->export_data['vendors'] = $this->company->vendors->map(function ($vendor){
return $this->transformBasicEntities($vendor);
2021-05-13 05:32:36 +02:00
})->toArray();
2021-05-13 03:25:26 +02:00
2021-05-13 05:32:36 +02: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->vendor_id = $this->encodePrimaryKey($vendor->vendor_id);
return $vendor;
2021-05-13 05:32:36 +02:00
})->toArray();
2021-05-13 03:25:26 +02:00
$this->export_data['webhooks'] = $this->company->webhooks->map(function ($hook){
$hook->user_id = $this->encodePrimaryKey($hook->user_id);
$hook->company_id = $this->encodePrimaryKey($hook->company_id);
return $hook;
})->makeHidden(['id'])->toArray();
2021-05-13 08:01:12 +02:00
//write to tmp and email to owner();
$this->zipAndSend();
2021-05-13 03:25:26 +02:00
}
private function transformBasicEntities($model)
{
return $this->transformArrayOfKeys($model, ['id', 'user_id', 'assigned_user_id', 'company_id']);
}
private function transformArrayOfKeys($model, $keys)
{
foreach($keys as $key){
$model->{$key} = $this->encodePrimaryKey($model->{$key});
}
return $model;
2021-05-13 00:13:33 +02:00
}
2021-05-13 08:01:12 +02:00
private function zipAndSend()
{
nlog("zipping");
$tempStream = fopen('php://memory', 'w+');
$options = new Archive();
$options->setOutputStream($tempStream);
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', $this->company->present()->name() . '_' . $this->company->company_key .'.zip');
$zip = new ZipStream($file_name, $options);
$fp = tmpfile();
fwrite($fp, json_encode($this->export_data));
rewind($fp);
$zip->addFileFromStream('backup.json', $fp);
$zip->finish();
$path = 'backups/';
nlog($path.$file_name);
Storage::disk(config('filesystems.default'))->put($path.$file_name, $tempStream);
// fclose($fp);
fclose($tempStream);
2021-05-13 15:37:25 +02:00
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadBackup(Storage::disk(config('filesystems.default'))->url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->company = $this->company;
$nmo->settings = $this->company->settings;
2021-05-13 08:01:12 +02:00
2021-05-13 15:37:25 +02:00
NinjaMailerJob::dispatch($nmo);
2021-05-13 08:01:12 +02:00
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
}
2021-05-13 00:13:33 +02:00
}