diff --git a/app/Events/Socket/DownloadAvailable.php b/app/Events/Socket/DownloadAvailable.php new file mode 100644 index 0000000000..d2825b8066 --- /dev/null +++ b/app/Events/Socket/DownloadAvailable.php @@ -0,0 +1,53 @@ +user->account->key}-{$this->user->id}"), + ]; + } + + public function broadcastWith(): array + { + + ctrans('texts.document_download_subject'); + + return [ + 'message' => $this->message, + 'url' => $this->url, + ]; + } + +} diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 1d7da9fc1e..142b853dbb 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -11,20 +11,22 @@ 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\User; use App\Models\Company; use App\Models\Invoice; -use App\Models\User; +use App\Libraries\MultiDB; +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\Mail\DownloadInvoices; +use App\Jobs\Mail\NinjaMailerJob; +use Illuminate\Support\Facades\App; +use App\Jobs\Mail\NinjaMailerObject; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Storage; +use App\Events\Socket\DownloadAvailable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; class ZipInvoices implements ShouldQueue { @@ -55,11 +57,10 @@ class ZipInvoices implements ShouldQueue public function handle(): void { MultiDB::setDb($this->company->db); - + App::setLocale($this->company->locale()); + $settings = $this->company->settings; - nlog(count($this->invoices)); - $this->invoices = Invoice::withTrashed() ->where('company_id', $this->company->id) ->whereIn('id', $this->invoices) @@ -99,9 +100,10 @@ class ZipInvoices implements ShouldQueue } Storage::put($path.$file_name, $zipFile->outputAsString()); + $storage_url = Storage::url($path.$file_name); $nmo = new NinjaMailerObject(); - $nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company); + $nmo->mailable = new DownloadInvoices($storage_url, $this->company); $nmo->to_user = $this->user; $nmo->settings = $settings; $nmo->company = $this->company; @@ -110,6 +112,10 @@ class ZipInvoices implements ShouldQueue UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); + $message = count($this->invoices). " ". ctrans('texts.invoices'). " " . ctrans('texts.download_files'); + + broadcast(new DownloadAvailable($storage_url, $message, $this->company->locale(), $this->user)); + } catch (\PhpZip\Exception\ZipException $e) { nlog('could not make zip => '.$e->getMessage()); } finally { diff --git a/routes/channels.php b/routes/channels.php index bc3fb0d9d5..0a5e594034 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -14,3 +14,8 @@ Broadcast::channel('company-{company_key}', function (\App\Models\User $user, string $company_key) { return $user->company()->company_key === $company_key; }); + +Broadcast::channel('user-{account_key}-{user_id}', function (\App\Models\User $user, string $account_key, string $user_id) { + return $user->account->key === $account_key && $user->id === (int)$user_id; +}); +