1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/app/Jobs/Invoice/ZipInvoices.php

117 lines
3.6 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
2020-10-28 11:10:49 +01:00
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
2021-06-29 22:23:23 +02:00
public function handle()
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
$this->invoices->each(function ($invoice) {
(new CreateEntityPdf($invoice->invitations()->first()))->handle();
if ($this->company->use_xinvoice){
2023-04-21 07:44:11 +02:00
(new CreateEInvoice($invoice, false))->handle();
}
2022-03-02 04:17:45 +01:00
});
try {
2022-03-02 03:26:30 +01:00
foreach ($this->invoices as $invoice) {
2022-03-06 10:41:43 +01:00
$file = $invoice->service()->getInvoicePdf();
$xinvoice = $invoice->service()->getXInvoice();
2022-03-06 10:41:43 +01:00
$zip_file_name = basename($file);
$xinvoice_zip_file_name = basename($xinvoice);
$zipFile->addFromString($zip_file_name, Storage::get($file))
->addDir($xinvoice_zip_file_name, Storage::get($xinvoice));
2022-03-06 10:41:43 +01:00
//$download_file = file_get_contents($invoice->pdf_file_path($invitation, 'url', true));
//$zipFile->addFromString(basename($invoice->pdf_file_path($invitation)), $download_file);
2022-03-02 03:26:30 +01: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
}
}