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

130 lines
3.6 KiB
PHP
Raw Normal View History

2022-03-14 23:30:19 +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-14 23:30:19 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Document;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
use App\Libraries\MultiDB;
use App\Mail\DownloadDocuments;
use App\Models\Company;
use App\Models\Document;
use App\Models\User;
2022-04-06 04:22:13 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates;
2022-03-14 23:30:19 +01:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-04-06 04:22:13 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
2022-03-14 23:30:19 +01:00
use Illuminate\Support\Facades\Storage;
class ZipDocuments implements ShouldQueue
{
2022-04-06 04:22:13 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates;
2022-03-14 23:30:19 +01:00
public $document_ids;
private $company;
private $user;
public $settings;
public $tries = 1;
/**
* @param $invoices
* @param Company $company
* @param $email
* @deprecated confirm to be deleted
* Create a new job instance.
*/
public function __construct($document_ids, Company $company, User $user)
{
$this->document_ids = $document_ids;
$this->company = $company;
$this->user = $user;
$this->settings = $company->settings;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
2022-04-06 04:22:13 +02:00
App::setLocale($this->company->locale());
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
// create new zip object
2022-03-14 23:30:19 +01:00
$zipFile = new \PhpZip\ZipFile();
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.documents')).'.zip';
$path = $this->company->file_path();
try {
2023-08-06 09:03:12 +02:00
$documents = Document::query()->whereIn('id', $this->document_ids)->get();
2022-03-14 23:30:19 +01:00
foreach ($documents as $document) {
2022-04-06 02:38:01 +02:00
$zipFile->addFromString($this->buildFileName($document), $document->getFile());
2022-03-14 23:30:19 +01:00
}
Storage::put($path.$file_name, $zipFile->outputAsString());
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadDocuments(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
2022-03-14 23:30:19 +01:00
NinjaMailerJob::dispatch($nmo);
2022-03-14 23:30:19 +01:00
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
} catch (\PhpZip\Exception\ZipException $e) {
nlog('could not make zip => '.$e->getMessage());
} finally {
2022-03-14 23:30:19 +01:00
$zipFile->close();
}
}
2022-04-06 02:38:01 +02:00
private function buildFileName($document) :string
{
$filename = $document->name;
2022-04-06 04:22:13 +02:00
$date = $this->formatDate(Carbon::createFromTimestamp($document->created_at), 'Y-m-d');
$number = '_';
2022-04-06 04:22:13 +02:00
if (isset($document->documentable->number)) {
$number = '_'.$document->documentable->number;
}
2022-04-06 04:22:13 +02:00
2022-09-14 01:48:25 +02:00
$entity = ctrans('texts.document');
2023-02-16 02:36:09 +01:00
if (isset($document->documentable)) {
2022-09-14 01:48:25 +02:00
$entity = $document->documentable->translate_entity();
}
return "{$date}_{$entity}{$number}_{$filename}";
2022-04-06 02:38:01 +02:00
}
2022-03-14 23:30:19 +01:00
}