From 1b256dba9b36bbf85da6a483dbfe2d89bdd6500a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 2 Mar 2022 13:51:38 +1100 Subject: [PATCH] refactor for bulk downloads --- app/Http/Controllers/CreditController.php | 7 +- app/Http/Controllers/QuoteController.php | 3 +- app/Jobs/Credit/ZipCredits.php | 115 ++++++++++++++++++++++ app/Jobs/Invoice/ZipInvoices.php | 34 +------ app/Jobs/Quote/ZipQuotes.php | 114 +++++++++++++++++++++ app/Mail/DownloadCredits.php | 53 ++++++++++ app/Mail/DownloadInvoices.php | 9 ++ app/Mail/DownloadQuotes.php | 53 ++++++++++ resources/lang/en/texts.php | 3 + 9 files changed, 357 insertions(+), 34 deletions(-) create mode 100644 app/Jobs/Credit/ZipCredits.php create mode 100644 app/Jobs/Quote/ZipQuotes.php create mode 100644 app/Mail/DownloadCredits.php create mode 100644 app/Mail/DownloadQuotes.php diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 4268046da8d6..081cdde4c989 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -24,6 +24,7 @@ use App\Http\Requests\Credit\ShowCreditRequest; use App\Http\Requests\Credit\StoreCreditRequest; use App\Http\Requests\Credit\UpdateCreditRequest; use App\Http\Requests\Credit\UploadCreditRequest; +use App\Jobs\Credit\ZipCredits; use App\Jobs\Entity\EmailEntity; use App\Jobs\Invoice\EmailCredit; use App\Jobs\Invoice\ZipInvoices; @@ -506,7 +507,7 @@ class CreditController extends BaseController $ids = request()->input('ids'); - $credits = Credit::withTrashed()->whereIn('id', $this->transformKeys($ids)); + $credits = Credit::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get(); if (! $credits) { return response()->json(['message' => ctrans('texts.no_credits_found')]); @@ -517,14 +518,14 @@ class CreditController extends BaseController */ if ($action == 'bulk_download' && $credits->count() > 1) { - $credits->each(function ($invoice) { + $credits->each(function ($credit) { 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()); + ZipCredits::dispatch($credits, $credits->first()->company, auth()->user()); return response()->json(['message' => ctrans('texts.sent_message')], 200); } diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index cc24d53384d0..6f14b3a18b48 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -27,6 +27,7 @@ use App\Http\Requests\Quote\StoreQuoteRequest; use App\Http\Requests\Quote\UpdateQuoteRequest; use App\Http\Requests\Quote\UploadQuoteRequest; use App\Jobs\Invoice\ZipInvoices; +use App\Jobs\Quote\ZipQuotes; use App\Models\Account; use App\Models\Client; use App\Models\Invoice; @@ -534,7 +535,7 @@ class QuoteController extends BaseController } }); - ZipInvoices::dispatch($quotes, $quotes->first()->company, auth()->user()); + ZipQuotes::dispatch($quotes, $quotes->first()->company, auth()->user()); return response()->json(['message' => ctrans('texts.sent_message')], 200); } diff --git a/app/Jobs/Credit/ZipCredits.php b/app/Jobs/Credit/ZipCredits.php new file mode 100644 index 000000000000..79d46bfe9872 --- /dev/null +++ b/app/Jobs/Credit/ZipCredits.php @@ -0,0 +1,115 @@ +credits = $credits; + + $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.credits')).'.zip'; + $invitation = $this->credits->first()->invitations->first(); + $path = $this->credits->first()->client->quote_filepath($invitation); + + try{ + + foreach ($this->credits as $credit) { + + $download_file = file_get_contents($credit->pdf_file_path($invitation, 'url', true)); + $zipFile->addFromString(basename($credit->pdf_file_path($invitation)), $download_file); + + } + + Storage::put($path.$file_name, $zipFile->outputAsString()); + + $nmo = new NinjaMailerObject; + $nmo->mailable = new DownloadCredits(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(); + } + + + } + +} diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 8edf82b56719..77336159cd35 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -14,6 +14,7 @@ namespace App\Jobs\Invoice; use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Util\UnlinkFile; +use App\Libraries\MultiDB; use App\Mail\DownloadInvoices; use App\Models\Company; use App\Models\User; @@ -69,10 +70,12 @@ class ZipInvoices implements ShouldQueue 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.invoices')).'.zip'; - $invitation = $this->invoices->invitations->first(); + $invitation = $this->invoices->first()->invitations->first(); $path = $this->invoices->first()->client->invoice_filepath($invitation); try{ @@ -108,33 +111,4 @@ class ZipInvoices implements ShouldQueue } - 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) { - - $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(); - } - - } } diff --git a/app/Jobs/Quote/ZipQuotes.php b/app/Jobs/Quote/ZipQuotes.php new file mode 100644 index 000000000000..8c40a976231e --- /dev/null +++ b/app/Jobs/Quote/ZipQuotes.php @@ -0,0 +1,114 @@ +quotes = $quotes; + + $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.quotes')).'.zip'; + $invitation = $this->quotes->first()->invitations->first(); + $path = $this->quotes->first()->client->quote_filepath($invitation); + + try{ + + foreach ($this->quotes as $quote) { + + $download_file = file_get_contents($quote->pdf_file_path($invitation, 'url', true)); + $zipFile->addFromString(basename($quote->pdf_file_path($invitation)), $download_file); + + } + + 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; + + 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(); + } + + + } + +} diff --git a/app/Mail/DownloadCredits.php b/app/Mail/DownloadCredits.php new file mode 100644 index 000000000000..85b8b1e9c727 --- /dev/null +++ b/app/Mail/DownloadCredits.php @@ -0,0 +1,53 @@ +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')) + ->view('email.admin.download_credits', [ + '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(), + ]); + } +} diff --git a/app/Mail/DownloadInvoices.php b/app/Mail/DownloadInvoices.php index ba82d325a710..51846f667c82 100644 --- a/app/Mail/DownloadInvoices.php +++ b/app/Mail/DownloadInvoices.php @@ -1,4 +1,13 @@ 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')) + ->view('email.admin.download_quotes', [ + '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(), + ]); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index d6679f4db6db..900e67e71223 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4566,6 +4566,9 @@ $LANG = array( 'upgrade_to_add_company' => 'Upgrade your plan to add companies', 'file_saved_in_downloads_folder' => 'The file has been saved in the downloads folder', 'small' => 'Small', + 'quotes_backup_subject' => 'Your quotes are ready for download', + 'credits_backup_subject' => 'Your credits are ready for download', + ); return $LANG;