1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Jobs/Quote/ZipQuotes.php

86 lines
2.6 KiB
PHP
Raw Normal View History

2022-03-02 03:51:38 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-03-02 03:51:38 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Quote;
2023-10-26 04:57:44 +02:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
2022-03-02 03:51:38 +01:00
use App\Libraries\MultiDB;
use App\Mail\DownloadQuotes;
2023-10-26 04:57:44 +02:00
use App\Models\Company;
2023-09-05 02:49:16 +02:00
use App\Models\QuoteInvitation;
2023-10-26 04:57:44 +02:00
use App\Models\User;
use Illuminate\Bus\Queueable;
2023-09-05 02:49:16 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
2022-03-02 03:51:38 +01:00
class ZipQuotes implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $settings;
2022-03-02 04:17:45 +01:00
public $tries = 1;
2023-09-05 02:49:16 +02:00
public function __construct(protected array $quote_ids, protected Company $company, protected User $user)
2022-03-02 03:51:38 +01:00
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
2023-09-05 02:49:16 +02:00
$this->settings = $this->company->settings;
2022-03-02 03:51:38 +01:00
// create new zip object
2022-03-02 03:51:38 +01:00
$zipFile = new \PhpZip\ZipFile();
2023-09-05 02:49:16 +02:00
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.quotes')).'.zip';
2023-02-04 00:17:19 +01:00
2023-09-05 02:49:16 +02:00
$invitations = QuoteInvitation::query()->with('quote')->whereIn('quote_id', $this->quote_ids)->get();
$invitation = $invitations->first();
$path = $invitation->contact->client->quote_filepath($invitation);
2022-03-02 04:17:45 +01:00
try {
2022-03-06 10:41:43 +01:00
2023-09-05 02:49:16 +02:00
foreach ($invitations as $invitation) {
2023-10-26 03:25:56 +02:00
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation))->handle();
2023-09-05 02:49:16 +02:00
$zipFile->addFromString($invitation->quote->numberFormatter() . '.pdf', $file);
2022-03-02 03:51:38 +01:00
}
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;
2022-03-02 03:51:38 +01:00
NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
2023-09-05 02:49:16 +02:00
} catch (\PhpZip\Exception\ZipException $e) {
2023-09-05 02:49:16 +02:00
nlog("zip build failed: ".$e->getMessage());
} finally {
2022-03-02 03:51:38 +01:00
$zipFile->close();
}
}
}