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

112 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-06-29 22:23:23 +02:00
use ZipArchive;
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
*/
2021-06-29 22:23:23 +02:00
public function handle()
{
# create new zip object
$zip = new ZipArchive();
2021-06-29 22:23:23 +02:00
$invitation = $this->invoices->first()->invitations->first();
$path = $this->invoices->first()->client->invoice_filepath($invitation);
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
$tmp_file = @tempnam('.', '');
$zip->open($tmp_file , ZipArchive::OVERWRITE);
2021-06-29 22:23:23 +02:00
# loop through each file
foreach ($this->invoices as $invoice) {
2021-06-29 22:23:23 +02:00
$inv = $invoice->invitations->first();
2021-06-29 22:23:23 +02:00
# download file
$download_file = file_get_contents($invoice->pdf_file_path($inv, 'url', true));
2021-06-29 22:23:23 +02:00
#add it to the zip
$zip->addFromString(basename($invoice->pdf_file_path($inv)), $download_file);
}
2021-06-29 22:23:23 +02:00
# close zip
$zip->close();
Storage::put($path.$file_name, file_get_contents($tmp_file));
2021-02-18 00:30:31 +01:00
$nmo = new NinjaMailerObject;
2021-06-29 22:23:23 +02:00
$nmo->mailable = new DownloadInvoices(Storage::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-06-29 22:23:23 +02:00
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
}
}