1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Jobs/Invoice/ZipInvoices.php

97 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Invoice;
2021-02-18 00:30:31 +01:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
2022-03-02 03:51:38 +01:00
use App\Libraries\MultiDB;
use App\Mail\DownloadInvoices;
use App\Models\Company;
2021-02-18 00:30:31 +01:00
use App\Models\User;
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\Storage;
2021-02-18 00:30:31 +01:00
class ZipInvoices implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2022-03-02 04:17:45 +01:00
public $tries = 1;
/**
2020-10-28 11:10:49 +01:00
* @param $invoices
* @param Company $company
* @param $email
* @deprecated confirm to be deleted
* Create a new job instance.
*/
public function __construct(public mixed $invoices, public Company $company, public User $user)
{
}
/**
* Execute the job.
*
* @return void
*/
2023-04-24 00:22:49 +02:00
public function handle(): void
2021-11-09 11:59:52 +01:00
{
2022-03-02 03:51:38 +01:00
MultiDB::setDb($this->company->db);
$settings = $this->company->settings;
2022-03-02 03:51:38 +01:00
// create new zip object
2022-03-02 03:26:30 +01:00
$zipFile = new \PhpZip\ZipFile();
2021-06-29 22:23:23 +02:00
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
2022-03-02 03:51:38 +01:00
$invitation = $this->invoices->first()->invitations->first();
2022-03-02 03:26:30 +01:00
$path = $this->invoices->first()->client->invoice_filepath($invitation);
2022-03-02 04:17:45 +01:00
try {
2023-04-24 00:22:49 +02:00
2022-03-02 03:26:30 +01:00
foreach ($this->invoices as $invoice) {
2024-01-14 05:05:00 +01:00
if ($invoice->client->getSetting('enable_e_invoice')) {
$xml = $invoice->service()->getEInvoice();
$zipFile->addFromString($invoice->getFileName("xml"), $xml);
}
2023-08-16 11:55:35 +02:00
$file = $invoice->service()->getRawInvoicePdf();
2023-08-17 01:07:49 +02:00
$zip_file_name = $invoice->getFileName();
2023-08-16 11:55:35 +02:00
$zipFile->addFromString($zip_file_name, $file);
2023-04-24 00:22:49 +02:00
}
2022-03-02 03:26:30 +01:00
Storage::put($path.$file_name, $zipFile->outputAsString());
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
2022-03-02 03:26:30 +01:00
$nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $settings;
2022-03-02 03:26:30 +01:00
$nmo->company = $this->company;
2022-03-02 03:26:30 +01:00
NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
} catch (\PhpZip\Exception\ZipException $e) {
nlog('could not make zip => '.$e->getMessage());
} finally {
2022-03-02 03:26:30 +01:00
$zipFile->close();
2021-06-29 22:23:23 +02:00
}
2022-03-02 03:26:30 +01:00
}
}