1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Quote/ZipQuotes.php
2023-09-05 10:49:16 +10:00

86 lines
2.6 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Quote;
use App\Models\User;
use App\Models\Company;
use App\Libraries\MultiDB;
use App\Mail\DownloadQuotes;
use App\Jobs\Util\UnlinkFile;
use Illuminate\Bus\Queueable;
use App\Models\QuoteInvitation;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class ZipQuotes implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $settings;
public $tries = 1;
public function __construct(protected array $quote_ids, protected Company $company, protected User $user)
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
$this->settings = $this->company->settings;
// create new zip object
$zipFile = new \PhpZip\ZipFile();
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.quotes')).'.zip';
$invitations = QuoteInvitation::query()->with('quote')->whereIn('quote_id', $this->quote_ids)->get();
$invitation = $invitations->first();
$path = $invitation->contact->client->quote_filepath($invitation);
try {
foreach ($invitations as $invitation) {
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation, $this->company->db))->handle();
$zipFile->addFromString($invitation->quote->numberFormatter() . '.pdf', $file);
}
Storage::put($path.$file_name, $zipFile->outputAsString());
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadQuotes(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
} catch (\PhpZip\Exception\ZipException $e) {
nlog("zip build failed: ".$e->getMessage());
} finally {
$zipFile->close();
}
}
}