2020-02-15 10:06:30 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-15 10:06:30 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-15 10:06:30 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-15 10:06:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
2021-02-18 00:30:31 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2020-02-24 11:15:30 +01:00
|
|
|
use App\Jobs\Util\UnlinkFile;
|
2022-03-02 03:51:38 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Mail\DownloadInvoices;
|
2020-02-15 10:06:30 +01:00
|
|
|
use App\Models\Company;
|
2021-02-18 00:30:31 +01:00
|
|
|
use App\Models\User;
|
2020-02-15 10:06:30 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-02-17 10:37:44 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2021-02-18 00:30:31 +01:00
|
|
|
class ZipInvoices implements ShouldQueue
|
2020-02-15 10:06:30 +01:00
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2022-03-02 04:17:45 +01:00
|
|
|
public $tries = 1;
|
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $invoices
|
|
|
|
* @param Company $company
|
|
|
|
* @param $email
|
2020-02-15 10:06:30 +01:00
|
|
|
* @deprecated confirm to be deleted
|
|
|
|
* Create a new job instance.
|
|
|
|
*/
|
2023-10-26 01:49:09 +02:00
|
|
|
public function __construct(public mixed $invoices, public Company $company, public User $user)
|
2020-02-15 10:06:30 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2023-10-26 01:49:09 +02:00
|
|
|
$settings = $this->company->settings;
|
2022-03-02 03:51:38 +01:00
|
|
|
|
2022-06-21 11:57:17 +02: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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2023-04-24 00:22:49 +02:00
|
|
|
|
2023-08-16 14:05:35 +02:00
|
|
|
|
2022-03-02 03:26:30 +01:00
|
|
|
foreach ($this->invoices as $invoice) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-16 14:05:35 +02: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());
|
2020-02-15 10:06:30 +01:00
|
|
|
|
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;
|
2023-10-26 01:49:09 +02:00
|
|
|
$nmo->settings = $settings;
|
2022-03-02 03:26:30 +01:00
|
|
|
$nmo->company = $this->company;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-02 03:26:30 +01:00
|
|
|
NinjaMailerJob::dispatch($nmo);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
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
|
|
|
}
|
2020-02-15 10:06:30 +01:00
|
|
|
}
|