1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00

Bulk download documents

This commit is contained in:
David Bomba 2022-03-15 09:30:19 +11:00
parent bc0f81b55a
commit c6b4721d61
9 changed files with 208 additions and 2 deletions

View File

@ -173,10 +173,16 @@ class DocumentController extends BaseController
$documents = Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
if (! $invoices) {
if (! $documents) {
return response()->json(['message' => ctrans('texts.no_documents_found')]);
}
if($action == 'download'){
ZipDocuments::dispatch($documents->pluck('id'), auth()->user()->company(), auth()->user());
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}
/*
* Send the other actions to the switch
*/

View File

@ -0,0 +1,118 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Document;
use App\Jobs\Entity\CreateEntityPdf;
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\Mail\DownloadInvoices;
use App\Models\Company;
use App\Models\Document;
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 ZipArchive;
class ZipDocuments implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
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
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
public function handle()
{
MultiDB::setDb($this->company->db);
# create new zip object
$zipFile = new \PhpZip\ZipFile();
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.documents')).'.zip';
$path = $this->company->file_path();
try{
$documents = Document::whereIn('id', $this->document_ids)->get();
foreach ($documents as $document) {
$zipFile->addFromString($document->name, $document->getFile());
}
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;
NinjaMailerJob::dispatch($nmo);
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{
$zipFile->close();
}
}
}

View File

@ -0,0 +1,56 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Mail;
use App\Models\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
class DownloadDocuments extends Mailable
{
// use Queueable, SerializesModels;
public $file_path;
public $company;
public function __construct($file_path, Company $company)
{
$this->file_path = $file_path;
$this->company = $company;
}
/**
* Build the message.
*/
public function build()
{
App::setLocale($this->company->getLocale());
return $this->from(config('mail.from.address'), config('mail.from.name'))
->subject(ctrans('texts.download_files'))
->text('email.admin.download_documents_text', [
'url' => $this->file_path,
])
->view('email.admin.download_documents', [
'url' => $this->file_path,
'logo' => $this->company->present()->logo,
'whitelabel' => $this->company->account->isPaid() ? true : false,
'settings' => $this->company->settings,
'greeting' => $this->company->present()->name(),
]);
}
}

View File

@ -508,6 +508,11 @@ class Company extends BaseModel
return $this->slack_webhook_url;
}
public function file_path()
{
return $this->company_key.'/';
}
public function rBits()
{
$user = $this->owner();

View File

@ -135,4 +135,9 @@ class Document extends BaseModel
{
return Storage::disk($this->disk)->path($this->url);
}
public function getFile()
{
return Storage::get($this->filePath());
}
}

View File

@ -4568,6 +4568,7 @@ $LANG = array(
'small' => 'Small',
'quotes_backup_subject' => 'Your quotes are ready for download',
'credits_backup_subject' => 'Your credits are ready for download',
'document_download_subject' => 'Your documents are ready for download',
);

View File

@ -0,0 +1,10 @@
@component('email.template.admin', ['logo' => $logo, 'settings' => $settings])
<div class="center">
<h1>{{ ctrans('texts.document_download_subject') }}</h1>
<p>{{ ctrans('texts.download_timeframe') }}</p>
<a target="_blank" class="button" href="{{ $url }}">
{{ ctrans('texts.download') }}
</a>
</div>
@endcomponent

View File

@ -0,0 +1,5 @@
{!! ctrans('texts.invoices_backup_subject') !!}
{!! ctrans('texts.download_timeframe') !!}
{!! $url !!}

View File

@ -1,4 +1,4 @@
{!! ctrans('texts.invoices_backup_subject') !!}
{!! ctrans('texts.document_download_subject') !!}
{!! ctrans('texts.download_timeframe') !!}