Updates for NordigenController

This commit is contained in:
David Bomba 2025-01-22 17:22:31 +11:00
parent 07c57e1361
commit 8ffbb54e3b
8 changed files with 189 additions and 20 deletions

View File

@ -165,6 +165,7 @@ class NordigenController extends BaseController
return $this->failed('requisition-no-accounts', $context, $company); return $this->failed('requisition-no-accounts', $context, $company);
} }
// connect new accounts // connect new accounts
$bank_integration_ids = []; $bank_integration_ids = [];
foreach ($requisition['accounts'] as $nordigenAccountId) { foreach ($requisition['accounts'] as $nordigenAccountId) {
@ -213,10 +214,6 @@ class NordigenController extends BaseController
} }
} }
if(!$bank_integration)
continue;
// perform update in background // perform update in background
$company->account->bank_integrations $company->account->bank_integrations
->where('integration_type', BankIntegration::INTEGRATION_TYPE_NORDIGEN) ->where('integration_type', BankIntegration::INTEGRATION_TYPE_NORDIGEN)

View File

@ -544,13 +544,44 @@ class InvoiceController extends BaseController
} }
if ($action == 'bulk_print' && $user->can('view', $invoices->first())) { if ($action == 'bulk_print' && $user->can('view', $invoices->first())) {
$paths = $invoices->map(function ($invoice) { $start = microtime(true);
return (new \App\Jobs\Entity\CreateRawPdf($invoice->invitations->first()))->handle();
});
return response()->streamDownload(function () use ($paths) { // 2025-01-22 Legacy implementation of bulk print
echo $merge = (new PdfMerge($paths->toArray()))->run(); // $paths = $invoices->map(function ($invoice) {
}, 'print.pdf', ['Content-Type' => 'application/pdf']); // return (new \App\Jobs\Entity\CreateRawPdf($invoice->invitations->first()))->handle();
// });
// return response()->streamDownload(function () use ($paths) {
// echo $merge = (new PdfMerge($paths->toArray()))->run();
// }, 'print.pdf', ['Content-Type' => 'application/pdf']);
$batch_id = (new \App\Jobs\Invoice\PrintInvoiceBatch($invoices->pluck('id')->toArray(), $user->company()->db))->handle();
$batch = \Illuminate\Support\Facades\Bus::findBatch($batch_id);
$batch_key = $batch->name;
$finished = false;
do{
usleep(500000);
$batch = \Illuminate\Support\Facades\Bus::findBatch($batch_id);
$finished = $batch->finished();
}while(!$finished);
$paths = $invoices->map(function ($invoice) use($batch_key){
return \Illuminate\Support\Facades\Cache::pull("{$batch_key}-{$invoice->id}");
})->filter(function ($value) {
return !is_null($value);
})->toArray();
$mergedPdf = (new PdfMerge($paths))->run();
return response()->streamDownload(function () use ($mergedPdf) {
echo $mergedPdf;
}, 'print.pdf', [
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
'Server-Timing' => (string)(microtime(true) - $start)
]);
} }
if ($action == 'template' && $user->can('view', $invoices->first())) { if ($action == 'template' && $user->can('view', $invoices->first())) {
@ -592,13 +623,6 @@ class InvoiceController extends BaseController
$invoice->service()->sendEmail(email_type: $request->input('email_type', $invoice->calculateTemplate('invoice'))); $invoice->service()->sendEmail(email_type: $request->input('email_type', $invoice->calculateTemplate('invoice')));
}); });
// if ($user->can('edit', $invoice)) {
// $template = $request->input('email_type', $invoice->calculateTemplate('invoice'));
// BulkInvoiceJob::dispatch($invoices->pluck('id')->toArray(), $user->company()->db, $template);
// }
return $this->listResponse(Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()); return $this->listResponse(Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
} }

View File

@ -100,7 +100,7 @@ class PreviewController extends BaseController
'Content-Disposition' => 'inline', 'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf', 'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache', 'Cache-Control:' => 'no-cache',
'Server-Timing' => microtime(true) - $start 'Server-Timing' => (string)(microtime(true) - $start)
]); ]);
} }

View File

@ -215,7 +215,7 @@ class PreviewPurchaseOrderController extends BaseController
'Content-Disposition' => 'inline', 'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf', 'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache', 'Cache-Control:' => 'no-cache',
'Server-Timing' => microtime(true) - $start 'Server-Timing' => (string)(microtime(true) - $start)
]); ]);

View File

@ -0,0 +1,51 @@
<?php
/**
* Entity Ninja (https://entityninja.com).
*
* @link https://github.com/entityninja/entityninja source repository
*
* @copyright Copyright (c) 2022. Entity Ninja LLC (https://entityninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Entity;
use Illuminate\Bus\Batchable;
use App\Jobs\Entity\CreateRawPdf;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class CreateBatchablePdf implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, SerializesModels;
private $batch_key;
private $invitation;
/**
* @param $invitation
*/
public function __construct($invitation, $batch_key)
{
$this->invitation = $invitation;
$this->batch_key = $batch_key;
}
public function handle()
{
$pdf = (new CreateRawPdf($this->invitation))->handle();
Cache::put($this->batch_key, $pdf);
}
public function failed($e)
{
nlog("CreateBatchablePdf failed: {$e->getMessage()}");
}
}

View File

@ -0,0 +1,82 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Invoice;
use Throwable;
use App\Models\Invoice;
use App\Models\Webhook;
use Illuminate\Bus\Batch;
use App\Libraries\MultiDB;
use Illuminate\Support\Str;
use App\Services\Email\Email;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use App\Jobs\Entity\EmailEntity;
use App\Services\Email\EmailObject;
use App\Services\PdfMaker\PdfMerge;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use App\Jobs\Entity\CreateBatchablePdf;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class PrintInvoiceBatch implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use Batchable;
public function __construct(private array $invoice_ids, private string $db){}
public function handle()
{
MultiDB::setDb($this->db);
$batch_key = Str::uuid();
$invites = Invoice::with('invitations')->withTrashed()
->whereIn('id', $this->invoice_ids)
->get()
->map(function ($invoice) use ($batch_key){
return new CreateBatchablePdf($invoice->invitations->first(), "{$batch_key}-{$invoice->id}");
})->toArray();
$mergedPdf = null;
$batch = Bus::batch($invites)
->before(function (Batch $batch) {
// The batch has been created but no jobs have been added...
// nlog("before");
})->progress(function (Batch $batch) {
// A single job has completed successfully...
// nlog("Batch {$batch->id} is {$batch->progress()}% complete");
})->then(function (Batch $batch) {
// All jobs completed successfully...
// nlog("job finished");
})->catch(function (Batch $batch, Throwable $e) {
// First batch job failure detected...
// nlog("PrintInvoiceBatch failed: {$e->getMessage()}");
})->finally(function (Batch $batch) {
// The batch has finished executing...
// nlog("I have finished");
})->name($batch_key)->dispatch();
return $batch->id;
}
}

View File

@ -73,6 +73,10 @@ return [
], ],
'batching' => [
'database' => env('DB_BATCH', 'mysql'),
'table' => 'job_batches',
],
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Failed Queue Jobs | Failed Queue Jobs

View File

@ -12,7 +12,18 @@ return new class extends Migration
public function up(): void public function up(): void
{ {
// "verifyBankAccount":false, Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
\App\Models\CompanyGateway::withTrashed()->where('gateway_key','b9886f9257f0c6ee7c302f1c74475f6c') \App\Models\CompanyGateway::withTrashed()->where('gateway_key','b9886f9257f0c6ee7c302f1c74475f6c')
->cursor() ->cursor()