mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
refactor for bulk downloads
This commit is contained in:
parent
5fd8aab4af
commit
1b256dba9b
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
115
app/Jobs/Credit/ZipCredits.php
Normal file
115
app/Jobs/Credit/ZipCredits.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?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\Credit;
|
||||
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\DownloadCredits;
|
||||
use App\Models\Company;
|
||||
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 ZipCredits implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $credits;
|
||||
|
||||
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($credits, Company $company, User $user)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
114
app/Jobs/Quote/ZipQuotes.php
Normal file
114
app/Jobs/Quote/ZipQuotes.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?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\Quote;
|
||||
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\DownloadQuotes;
|
||||
use App\Models\Company;
|
||||
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 ZipQuotes implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $quotes;
|
||||
|
||||
private $company;
|
||||
|
||||
private $user;
|
||||
|
||||
public $settings;
|
||||
|
||||
/**
|
||||
* @param $invoices
|
||||
* @param Company $company
|
||||
* @param $email
|
||||
* @deprecated confirm to be deleted
|
||||
* Create a new job instance.
|
||||
*
|
||||
*/
|
||||
public function __construct($quotes, Company $company, User $user)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
53
app/Mail/DownloadCredits.php
Normal file
53
app/Mail/DownloadCredits.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?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 DownloadCredits 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'))
|
||||
->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(),
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,4 +1,13 @@
|
||||
<?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;
|
||||
|
||||
|
53
app/Mail/DownloadQuotes.php
Normal file
53
app/Mail/DownloadQuotes.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?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 DownloadQuotes 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'))
|
||||
->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(),
|
||||
]);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user