diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 82e6c7e022f3..4268046da8d6 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -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); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 5836d2091c21..cc24d53384d0 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -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')]); diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 18fa2f0b95d3..8edf82b56719 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -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(); + } + } }