mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor zips
This commit is contained in:
parent
f5ee1efec0
commit
2fe595d4c5
@ -12,21 +12,22 @@
|
||||
|
||||
namespace App\Http\Controllers\ClientPortal;
|
||||
|
||||
use App\Events\Misc\InvitationWasViewed;
|
||||
use App\Utils\Ninja;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\QuoteInvitation;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Events\Quote\QuoteWasViewed;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ClientPortal\Quotes\ProcessQuotesInBulkRequest;
|
||||
use App\Jobs\Invoice\InjectSignature;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Events\Misc\InvitationWasViewed;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use App\Http\Requests\ClientPortal\Quotes\ShowQuoteRequest;
|
||||
use App\Http\Requests\ClientPortal\Quotes\ShowQuotesRequest;
|
||||
use App\Jobs\Invoice\InjectSignature;
|
||||
use App\Models\Quote;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use App\Http\Requests\ClientPortal\Quotes\ProcessQuotesInBulkRequest;
|
||||
|
||||
class QuoteController extends Controller
|
||||
{
|
||||
@ -121,37 +122,38 @@ class QuoteController extends Controller
|
||||
/** @var \App\Models\ClientContact $client_contact **/
|
||||
$client_contact = auth()->user();
|
||||
|
||||
$quotes = Quote::query()
|
||||
->whereIn('id', $ids)
|
||||
->whereClientId($client_contact->client_id)
|
||||
$quote_invitations = QuoteInvitation::query()
|
||||
->with('quote','company')
|
||||
->whereIn('quote_id', $ids)
|
||||
->where('client_contact_id', $client_contact->id)
|
||||
->withTrashed()
|
||||
->get();
|
||||
|
||||
if (! $quotes || $quotes->count() == 0) {
|
||||
if (! $quote_invitations || $quote_invitations->count() == 0) {
|
||||
return redirect()
|
||||
->route('client.quotes.index')
|
||||
->with('message', ctrans('texts.no_quotes_available_for_download'));
|
||||
}
|
||||
|
||||
if ($quotes->count() == 1) {
|
||||
$file = $quotes->first()->service()->getQuotePdf();
|
||||
// return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
|
||||
if ($quote_invitations->count() == 1) {
|
||||
$invitation = $quote_invitations->first();
|
||||
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation, $invitation->company->db))->handle();
|
||||
return response()->streamDownload(function () use ($file) {
|
||||
echo Storage::get($file);
|
||||
}, basename($file), ['Content-Type' => 'application/pdf']);
|
||||
echo $file;
|
||||
}, $invitation->quote->numberFormatter().".pdf", ['Content-Type' => 'application/pdf']);
|
||||
}
|
||||
|
||||
return $this->buildZip($quotes);
|
||||
return $this->buildZip($quote_invitations);
|
||||
}
|
||||
|
||||
private function buildZip($quotes)
|
||||
private function buildZip($quote_invitations)
|
||||
{
|
||||
// create new archive
|
||||
$zipFile = new \PhpZip\ZipFile();
|
||||
try {
|
||||
foreach ($quotes as $quote) {
|
||||
//add it to the zip
|
||||
$zipFile->addFromString(basename($quote->pdf_file_path()), file_get_contents($quote->pdf_file_path(null, 'url', true)));
|
||||
foreach ($quote_invitations as $invitation) {
|
||||
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation, $invitation->company->db))->handle();
|
||||
$zipFile->addFromString($invitation->quote->numberFormatter() . '.pdf', $file);
|
||||
}
|
||||
|
||||
$filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.quotes')).'.zip';
|
||||
@ -162,7 +164,6 @@ class QuoteController extends Controller
|
||||
|
||||
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
|
||||
} catch (\PhpZip\Exception\ZipException $e) {
|
||||
// handle exception
|
||||
} finally {
|
||||
$zipFile->close();
|
||||
}
|
||||
|
@ -542,7 +542,7 @@ class CreditController extends BaseController
|
||||
|
||||
if ($action == 'bulk_print' && $user->can('view', $credits->first())) {
|
||||
$paths = $credits->map(function ($credit) {
|
||||
return $credit->service()->getCreditPdf($credit->invitations->first());
|
||||
return (new \App\Jobs\Entity\CreateRawPdf($credit->invitations->first(), $credit->company->db))->handle();
|
||||
});
|
||||
|
||||
$merge = (new PdfMerge($paths->toArray()))->run();
|
||||
|
@ -527,7 +527,7 @@ class InvoiceController extends BaseController
|
||||
|
||||
if ($action == 'bulk_print' && $user->can('view', $invoices->first())) {
|
||||
$paths = $invoices->map(function ($invoice) {
|
||||
return $invoice->service()->getInvoicePdf();
|
||||
return (new \App\Jobs\Entity\CreateRawPdf($invoice->invitations->first(), $invoice->company->db))->handle();
|
||||
});
|
||||
|
||||
$merge = (new PdfMerge($paths->toArray()))->run();
|
||||
|
@ -475,11 +475,14 @@ class PurchaseOrderController extends BaseController
|
||||
*/
|
||||
public function bulk(BulkPurchaseOrderRequest $request)
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$action = $request->input('action');
|
||||
|
||||
$ids = $request->input('ids');
|
||||
|
||||
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && !auth()->user()->company()->account->account_sms_verified) {
|
||||
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && !$user->company()->account->account_sms_verified) {
|
||||
return response(['message' => 'Please verify your account to send emails.'], 400);
|
||||
}
|
||||
|
||||
@ -493,8 +496,8 @@ class PurchaseOrderController extends BaseController
|
||||
* Download Purchase Order/s
|
||||
*/
|
||||
if ($action == 'bulk_download' && $purchase_orders->count() >= 1) {
|
||||
$purchase_orders->each(function ($purchase_order) {
|
||||
if (auth()->user()->cannot('view', $purchase_order)) {
|
||||
$purchase_orders->each(function ($purchase_order) use ($user){
|
||||
if ($user->cannot('view', $purchase_order)) {
|
||||
return response()->json(['message' => ctrans('text.access_denied')]);
|
||||
}
|
||||
});
|
||||
@ -504,9 +507,9 @@ class PurchaseOrderController extends BaseController
|
||||
return response()->json(['message' => ctrans('texts.sent_message')], 200);
|
||||
}
|
||||
|
||||
if ($action == 'bulk_print' && auth()->user()->can('view', $purchase_orders->first())) {
|
||||
if ($action == 'bulk_print' && $user->can('view', $purchase_orders->first())) {
|
||||
$paths = $purchase_orders->map(function ($purchase_order) {
|
||||
return $purchase_order->service()->getPurchaseOrderPdf();
|
||||
return (new \App\Jobs\Vendor\CreatePurchaseOrderPdf($purchase_order->invitations->first()))->rawPdf();
|
||||
});
|
||||
|
||||
$merge = (new PdfMerge($paths->toArray()))->run();
|
||||
@ -519,8 +522,8 @@ class PurchaseOrderController extends BaseController
|
||||
/*
|
||||
* Send the other actions to the switch
|
||||
*/
|
||||
$purchase_orders->each(function ($purchase_order, $key) use ($action) {
|
||||
if (auth()->user()->can('edit', $purchase_order)) {
|
||||
$purchase_orders->each(function ($purchase_order, $key) use ($action, $user) {
|
||||
if ($user->can('edit', $purchase_order)) {
|
||||
$this->performAction($purchase_order, $action, true);
|
||||
}
|
||||
});
|
||||
|
@ -524,16 +524,15 @@ class QuoteController extends BaseController
|
||||
return response(['message' => 'Please verify your account to send emails.'], 400);
|
||||
}
|
||||
|
||||
$quotes = Quote::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
|
||||
$quotes = Quote::query()->with('invitations')->withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
|
||||
|
||||
if (! $quotes) {
|
||||
return response()->json(['message' => ctrans('texts.quote_not_found')]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Download Invoice/s
|
||||
* Download Quote/s
|
||||
*/
|
||||
|
||||
if ($action == 'bulk_download' && $quotes->count() >= 1) {
|
||||
$quotes->each(function ($quote) use($user){
|
||||
if ($user->cannot('view', $quote)) {
|
||||
@ -541,7 +540,7 @@ class QuoteController extends BaseController
|
||||
}
|
||||
});
|
||||
|
||||
ZipQuotes::dispatch($quotes, $quotes->first()->company, auth()->user());
|
||||
ZipQuotes::dispatch($quotes->pluck('id')->toArray(), $quotes->first()->company, auth()->user());
|
||||
|
||||
return response()->json(['message' => ctrans('texts.sent_message')], 200);
|
||||
}
|
||||
@ -561,7 +560,7 @@ class QuoteController extends BaseController
|
||||
|
||||
if ($action == 'bulk_print' && $user->can('view', $quotes->first())) {
|
||||
$paths = $quotes->map(function ($quote) {
|
||||
return $quote->service()->getQuotePdf();
|
||||
return (new \App\Jobs\Entity\CreateRawPdf($quote->invitations->first(), $quote->company->db))->handle();
|
||||
});
|
||||
|
||||
$merge = (new PdfMerge($paths->toArray()))->run();
|
||||
@ -720,7 +719,6 @@ class QuoteController extends BaseController
|
||||
break;
|
||||
case 'download':
|
||||
|
||||
//$file = $quote->pdf_file_path();
|
||||
$file = $quote->service()->getQuotePdf();
|
||||
|
||||
return response()->streamDownload(function () use ($file) {
|
||||
|
@ -11,51 +11,31 @@
|
||||
|
||||
namespace App\Jobs\Quote;
|
||||
|
||||
use App\Jobs\Entity\CreateEntityPdf;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use App\Models\User;
|
||||
use App\Models\Company;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\DownloadQuotes;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Models\QuoteInvitation;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class ZipQuotes implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $quotes;
|
||||
|
||||
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($quotes, Company $company, User $user)
|
||||
public function __construct(protected array $quote_ids, protected Company $company, protected User $user)
|
||||
{
|
||||
$this->quotes = $quotes;
|
||||
|
||||
$this->company = $company;
|
||||
|
||||
$this->user = $user;
|
||||
|
||||
$this->settings = $company->settings;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,27 +46,22 @@ class ZipQuotes implements ShouldQueue
|
||||
public function handle()
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
|
||||
$this->settings = $this->company->settings;
|
||||
|
||||
// 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);
|
||||
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.quotes')).'.zip';
|
||||
|
||||
$this->quotes->each(function ($quote) {
|
||||
$quote->service()->createInvitations();
|
||||
|
||||
(new CreateEntityPdf($quote->invitations()->first()))->handle();
|
||||
});
|
||||
$invitations = QuoteInvitation::query()->with('quote')->whereIn('quote_id', $this->quote_ids)->get();
|
||||
$invitation = $invitations->first();
|
||||
$path = $invitation->contact->client->quote_filepath($invitation);
|
||||
|
||||
try {
|
||||
foreach ($this->quotes as $quote) {
|
||||
$file = $quote->service()->getQuotePdf();
|
||||
$zip_file_name = basename($file);
|
||||
$zipFile->addFromString($zip_file_name, Storage::get($file));
|
||||
|
||||
// $download_file = file_get_contents($quote->pdf_file_path($invitation, 'url', true));
|
||||
// $zipFile->addFromString(basename($quote->pdf_file_path($invitation)), $download_file);
|
||||
foreach ($invitations as $invitation) {
|
||||
$file = (new \App\Jobs\Entity\CreateRawPdf($invitation, $this->company->db))->handle();
|
||||
$zipFile->addFromString($invitation->quote->numberFormatter() . '.pdf', $file);
|
||||
}
|
||||
|
||||
Storage::put($path.$file_name, $zipFile->outputAsString());
|
||||
@ -100,8 +75,9 @@ class ZipQuotes implements ShouldQueue
|
||||
NinjaMailerJob::dispatch($nmo);
|
||||
|
||||
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
|
||||
|
||||
} catch (\PhpZip\Exception\ZipException $e) {
|
||||
// handle exception
|
||||
nlog("zip build failed: ".$e->getMessage());
|
||||
} finally {
|
||||
$zipFile->close();
|
||||
}
|
||||
|
@ -87,41 +87,41 @@ class QuoteInvitation extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function quote()
|
||||
public function quote(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Quote::class)->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function entity()
|
||||
public function entity(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Quote::class)->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function contact()
|
||||
public function contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClientContact::class, 'client_contact_id', 'id')->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class)->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function company()
|
||||
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
@ -13,12 +13,18 @@
|
||||
namespace App\Services\PdfMaker;
|
||||
|
||||
use \setasign\Fpdi\Fpdi;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use setasign\Fpdi\PdfParser\StreamReader;
|
||||
|
||||
class PdfMerge
|
||||
{
|
||||
public function __construct(private array $file_paths)
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
* @param array $files
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(private array $files)
|
||||
{
|
||||
}
|
||||
|
||||
@ -26,8 +32,8 @@ class PdfMerge
|
||||
{
|
||||
$pdf = new FPDI();
|
||||
|
||||
foreach ($this->file_paths as $file) {
|
||||
$pageCount = $pdf->setSourceFile(StreamReader::createByString(Storage::get($file)));
|
||||
foreach ($this->files as $file) {
|
||||
$pageCount = $pdf->setSourceFile(StreamReader::createByString($file));
|
||||
for ($i = 0; $i < $pageCount; $i++) {
|
||||
$tpl = $pdf->importPage($i + 1, '/MediaBox');
|
||||
$pdf->addPage();
|
||||
|
Loading…
x
Reference in New Issue
Block a user