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
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. 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;
|
|
|
|
|
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;
|
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
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
public $invoices;
|
2020-02-15 10:06:30 +01:00
|
|
|
|
|
|
|
private $company;
|
|
|
|
|
2021-02-18 00:30:31 +01:00
|
|
|
private $user;
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2020-08-10 06:55:44 +02:00
|
|
|
public $settings;
|
2020-09-06 11:38:10 +02: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.
|
|
|
|
*/
|
2021-02-18 00:30:31 +01:00
|
|
|
public function __construct($invoices, Company $company, User $user)
|
2020-02-15 10:06:30 +01:00
|
|
|
{
|
|
|
|
$this->invoices = $invoices;
|
|
|
|
|
|
|
|
$this->company = $company;
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2021-02-18 00:30:31 +01:00
|
|
|
$this->user = $user;
|
2020-08-10 06:55:44 +02:00
|
|
|
|
|
|
|
$this->settings = $company->settings;
|
2020-02-15 10:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-02-15 10:06:30 +01:00
|
|
|
*/
|
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);
|
|
|
|
|
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
|
|
|
$this->invoices->each(function ($invoice) {
|
2022-07-31 23:30:04 +02:00
|
|
|
(new CreateEntityPdf($invoice->invitations()->first()))->handle();
|
2022-03-02 04:17:45 +01:00
|
|
|
});
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2022-06-21 11:57:17 +02: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();
|
|
|
|
$zip_file_name = basename($file);
|
|
|
|
$zipFile->addFromString($zip_file_name, Storage::get($file));
|
|
|
|
|
|
|
|
//$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
|
|
|
}
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2022-03-02 03:26:30 +01:00
|
|
|
Storage::put($path.$file_name, $zipFile->outputAsString());
|
2020-02-15 10:06:30 +01:00
|
|
|
|
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-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
|
|
|
}
|