2022-06-10 10:00:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2022-06-10 10:00:07 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\PurchaseOrder;
|
|
|
|
|
2023-10-26 08:50:07 +02:00
|
|
|
use App\Jobs\Entity\CreateRawPdf;
|
2022-06-10 10:00:07 +02:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Jobs\Util\UnlinkFile;
|
|
|
|
use App\Libraries\MultiDB;
|
2022-06-10 10:29:15 +02:00
|
|
|
use App\Mail\DownloadPurchaseOrders;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Models\Company;
|
2023-09-05 03:16:47 +02:00
|
|
|
use App\Models\PurchaseOrderInvitation;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2022-06-10 10:00:07 +02:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2022-06-10 10:00:07 +02:00
|
|
|
|
|
|
|
class ZipPurchaseOrders implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2022-06-10 10:00:07 +02:00
|
|
|
|
|
|
|
public $settings;
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
2023-09-05 03:16:47 +02:00
|
|
|
public function __construct(protected array $purchase_order_ids, protected Company $company, protected User $user)
|
2022-06-10 10:00:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2023-09-05 03:16:47 +02:00
|
|
|
$this->settings = $this->company->settings;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
// create new zip object
|
2022-06-10 10:00:07 +02:00
|
|
|
$zipFile = new \PhpZip\ZipFile();
|
2023-09-05 03:16:47 +02:00
|
|
|
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
|
2022-06-10 10:00:07 +02:00
|
|
|
|
2023-09-05 03:16:47 +02:00
|
|
|
$invitations = PurchaseOrderInvitation::query()
|
|
|
|
->with('purchase_order')
|
|
|
|
->whereIn('purchase_order_id', $this->purchase_order_ids)
|
|
|
|
->get();
|
|
|
|
$invitation = $invitations->first();
|
|
|
|
$path = $invitation->contact->vendor->purchase_order_filepath($invitation);
|
2022-06-10 10:00:07 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2023-09-05 03:16:47 +02:00
|
|
|
foreach ($invitations as $invitation) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-03-19 15:35:03 +01:00
|
|
|
if ($invitation->purchase_order->vendor->getSetting("enable_e_invoice")) {
|
2024-03-10 15:27:37 +01:00
|
|
|
$xml = $invitation->purchase_order->service()->getEInvoice();
|
|
|
|
$zipFile->addFromString($invitation->purchase_order->getFileName("xml"), $xml);
|
|
|
|
}
|
|
|
|
|
2023-10-26 08:50:07 +02:00
|
|
|
$file = (new CreateRawPdf($invitation))->handle();
|
|
|
|
|
2023-09-05 03:16:47 +02:00
|
|
|
$zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file);
|
2022-06-10 10:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage::put($path.$file_name, $zipFile->outputAsString());
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$nmo = new NinjaMailerObject();
|
2022-06-10 10:29:15 +02:00
|
|
|
$nmo->mailable = new DownloadPurchaseOrders(Storage::url($path.$file_name), $this->company);
|
2022-06-10 10:00:07 +02:00
|
|
|
$nmo->to_user = $this->user;
|
|
|
|
$nmo->settings = $this->settings;
|
|
|
|
$nmo->company = $this->company;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-06-10 10:00:07 +02: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-06-10 10:00:07 +02:00
|
|
|
$zipFile->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|