1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Invoice/ZipInvoices.php
David Bomba e5a230e0c7
Performance improvements (#3634)
* Adjustments for tests

* Implement handling of temp downloading resources

* Refactor paths

* Refactors for file paths

* Refactor paths

* Add in S3 adapter

* Refactor company Documment URL

* Refactor for entity pdf performance

* Refactors for invoice generation

* Enhancements for emails invoices

* Emails

* Fixes for client portal queries
2020-04-16 18:41:25 +10:00

91 lines
2.3 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Jobs\Util\UnlinkFile;
use App\Libraries\MultiDB;
use App\Mail\DownloadInvoices;
use App\Models\Company;
use App\Models\Invoice;
use App\Utils\TempFile;
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\Mail;
use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class ZipInvoices implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoices;
private $company;
private $email;
/**
* @deprecated confirm to be deleted
* Create a new job instance.
*
* @return void
*/
public function __construct($invoices, Company $company, $email)
{
$this->invoices = $invoices;
$this->company = $company;
$this->email = $email;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
$tempStream = fopen('php://memory', 'w+');
$options = new Archive();
$options->setOutputStream($tempStream);
# create a new zipstream object
$file_name = date('Y-m-d') . '_' . str_replace(' ', '_', trans('texts.invoices')).".zip";
$path = $this->invoices->first()->client->invoice_filepath();
$zip = new ZipStream($file_name, $options);
foreach ($this->invoices as $invoice) {
$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
}
$zip->finish();
Storage::disk(config('filesystems.default'))->put($path . $file_name, $tempStream);
fclose($tempStream);
Mail::to($this->email)
->send(new DownloadInvoices(Storage::disk(config('filesystems.default'))->url($path . $file_name), $this->company));
UnlinkFile::dispatch(config('filesystems.default'), $path . $file_name)->delay(now()->addHours(1));
}
}