Refactor for bulk downloads

This commit is contained in:
David Bomba 2022-03-02 13:26:30 +11:00
parent a07c66f044
commit 475831bf70
3 changed files with 77 additions and 27 deletions

View File

@ -26,6 +26,7 @@ use App\Http\Requests\Credit\UpdateCreditRequest;
use App\Http\Requests\Credit\UploadCreditRequest;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailCredit;
use App\Jobs\Invoice\ZipInvoices;
use App\Models\Account;
use App\Models\Client;
use App\Models\Credit;
@ -511,6 +512,24 @@ class CreditController extends BaseController
return response()->json(['message' => ctrans('texts.no_credits_found')]);
}
/*
* Download Invoice/s
*/
if ($action == 'bulk_download' && $credits->count() > 1) {
$credits->each(function ($invoice) {
if (auth()->user()->cannot('view', $credit)) {
nlog("access denied");
return response()->json(['message' => ctrans('text.access_denied')]);
}
});
ZipInvoices::dispatch($credits, $credits->first()->company, auth()->user());
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}
$credits->each(function ($credit, $key) use ($action) {
if (auth()->user()->can('edit', $credit)) {
$this->performAction($credit, $action, true);

View File

@ -527,7 +527,7 @@ class QuoteController extends BaseController
* Download Invoice/s
*/
if ($action == 'download' && $quotes->count() >= 1) {
if ($action == 'bulk_download' && $quotes->count() >= 1) {
$quotes->each(function ($quote) {
if (auth()->user()->cannot('view', $quote)) {
return response()->json(['message'=> ctrans('texts.access_denied')]);

View File

@ -70,40 +70,71 @@ class ZipInvoices implements ShouldQueue
public function handle()
{
# create new zip object
$zip = new ZipArchive();
$invitation = $this->invoices->first()->invitations->first();
$path = $this->invoices->first()->client->invoice_filepath($invitation);
$zipFile = new \PhpZip\ZipFile();
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
$tmp_file = @tempnam('.', '');
$zip->open($tmp_file , ZipArchive::OVERWRITE);
$invitation = $this->invoices->invitations->first();
$path = $this->invoices->first()->client->invoice_filepath($invitation);
# loop through each file
foreach ($this->invoices as $invoice) {
try{
$inv = $invoice->invitations->first();
foreach ($this->invoices as $invoice) {
$download_file = file_get_contents($invoice->pdf_file_path($invitation, 'url', true));
$zipFile->addFromString(basename($invoice->pdf_file_path($invitation)), $download_file);
# download file
$download_file = file_get_contents($invoice->pdf_file_path($inv, 'url', true));
}
#add it to the zip
$zip->addFromString(basename($invoice->pdf_file_path($inv)), $download_file);
Storage::put($path.$file_name, $zipFile->outputAsString());
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(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){
// handle exception
}
finally{
$zipFile->close();
}
# close zip
$zip->close();
Storage::put($path.$file_name, file_get_contents($tmp_file));
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
}
private function zipFiles()
{
$zipFile = new \PhpZip\ZipFile();
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
$invitation = $this->invoices->invitations->first();
$path = $this->invoices->first()->client->invoice_filepath($invitation);
try{
foreach ($this->invoices as $invoice) {
NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
$download_file = file_get_contents($invoice->pdf_file_path($invitation, 'url', true));
$zipFile->addFromString(basename($invoice->pdf_file_path($invitation)), $download_file);
}
Storage::put($path.$file_name, $zipFile->outputAsString());
}
catch(\PhpZip\Exception\ZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
}
}