1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Invoice/ZipInvoices.php

109 lines
3.0 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;
2022-03-02 04:17:45 +01:00
use App\Jobs\Entity\CreateEntityPdf;
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
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoices;
private $company;
2021-02-18 00:30:31 +01:00
private $user;
2020-08-10 06:55:44 +02:00
public $settings;
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.
*/
2021-02-18 00:30:31 +01:00
public function __construct($invoices, Company $company, User $user)
{
$this->invoices = $invoices;
$this->company = $company;
2021-02-18 00:30:31 +01:00
$this->user = $user;
2020-08-10 06:55:44 +02:00
$this->settings = $company->settings;
}
/**
* 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);
// 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) {
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());
2022-03-02 03:26:30 +01:00
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$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
}
}