1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Jobs/Invoice/ZipInvoices.php

110 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Invoice;
2021-02-18 00:30:31 +01:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
use App\Mail\DownloadInvoices;
use App\Models\Company;
2021-02-18 00:30:31 +01:00
use App\Models\User;
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;
2021-02-18 00:30:31 +01:00
class ZipInvoices implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoices;
private $company;
2021-02-18 00:30:31 +01:00
private $user;
2020-08-10 06:55:44 +02:00
public $settings;
/**
2020-10-28 11:10:49 +01:00
* @param $invoices
* @param Company $company
* @param $email
* @deprecated confirm to be deleted
* Create a new job instance.
*
*/
2021-02-18 00:30:31 +01:00
public function __construct($invoices, Company $company, User $user)
{
$this->invoices = $invoices;
$this->company = $company;
2021-02-18 00:30:31 +01:00
$this->user = $user;
2020-08-10 06:55:44 +02:00
$this->settings = $company->settings;
}
/**
* Execute the job.
*
*
* @return void
2020-10-28 11:10:49 +01:00
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
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';
2021-06-12 13:50:01 +02:00
$invoice = $this->invoices->first();
$invitation = $invoice->invitations->first();
$path = $invoice->client->invoice_filepath($invitation);
$zip = new ZipStream($file_name, $options);
foreach ($this->invoices as $invoice) {
2021-05-15 04:19:36 +02:00
//$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
2021-06-12 13:50:01 +02:00
$zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path());
}
$zip->finish();
2021-05-15 04:19:36 +02:00
Storage::disk('public')->put($path.$file_name, $tempStream);
fclose($tempStream);
2021-02-18 00:30:31 +01:00
$nmo = new NinjaMailerObject;
2021-05-15 04:19:36 +02:00
$nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company);
2021-02-18 00:30:31 +01:00
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo);
2020-11-12 10:41:19 +01:00
2021-05-15 04:19:36 +02:00
UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1));
}
}