mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
108 lines
4.1 KiB
PHP
108 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Transactions\Processors;
|
|
|
|
use App\Classes\Modules\Documents\Services\CreatesDocument;
|
|
use App\Classes\Modules\Documents\Services\CreatesFiles;
|
|
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Models\Document;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
|
|
use Webklex\PDFMerger\Facades\PDFMergerFacade as PDFMerger;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class CreateInvoiceDocumentProcessor
|
|
{
|
|
/** @var CreatesDocument */
|
|
private $createsDocument;
|
|
|
|
/** @var CreatesFiles */
|
|
private $createsFile;
|
|
|
|
/**
|
|
* CreateInvoiceDocumentProcessor constructor.
|
|
* @param CreatesDocument $createsDocument
|
|
* @param CreatesFiles $createsFile
|
|
*/
|
|
public function __construct(CreatesDocument $createsDocument, CreatesFiles $createsFile)
|
|
{
|
|
$this->createsDocument = $createsDocument;
|
|
$this->createsFile = $createsFile;
|
|
}
|
|
|
|
/**
|
|
* @param $transaction
|
|
* @param $purchaseOrder
|
|
* @param $supplier
|
|
* @param $document_type
|
|
* @return void
|
|
* @throws \App\Classes\Exceptions\MalformedRequestException
|
|
*/
|
|
public function execute($transaction, $purchaseOrder, $supplier, $document_type, $voucherRedemption = null)
|
|
{
|
|
$lowercaseDocumentType = strtolower($document_type);
|
|
|
|
$order_pdf = LaravelMpdf::loadView('pages.pdfs.' . $lowercaseDocumentType, ['transaction' => $transaction, 'po_order_transaction' => $purchaseOrder, 'supplier' => $supplier, 'voucher_redemption' => $voucherRedemption]);
|
|
|
|
if($purchaseOrder->booking->service_id === 4) {
|
|
$purchaseOrderDocuments = $purchaseOrder->booking->documents()->where('document_type', DocumentType::ECOMMERCE_PURCHASE_ORDER)->get();
|
|
|
|
|
|
$oMerger = PDFMerger::init();
|
|
|
|
$saveDirectory = storage_path('app/documents');
|
|
if (!file_exists($saveDirectory)) {
|
|
mkdir($saveDirectory, 0755, true);
|
|
}
|
|
|
|
$order_pdf->save(storage_path('app/documents/temp.pdf'));
|
|
|
|
$oMerger->addPDF(storage_path('app/documents/temp.pdf'), 'all');
|
|
$filesystemDriver = Storage::getDefaultDriver();
|
|
foreach ($purchaseOrderDocuments as $document){
|
|
$currentFile = $document->files()->first()->file->file_info->original->file;
|
|
if($filesystemDriver === 's3'){
|
|
Log::info('CreateInvoiceDocumentProcessor 1: ' . $currentFile);
|
|
$fileContent = Storage::disk('s3')->get($currentFile);
|
|
$filepath = storage_path('app/documents/' . $currentFile);
|
|
$directoryPath = dirname($filepath);
|
|
if (!file_exists($directoryPath)) {
|
|
mkdir($directoryPath, 0755, true);
|
|
}
|
|
file_put_contents($filepath, $fileContent);
|
|
}
|
|
|
|
$localFilePath = storage_path('app/documents/' . $currentFile);
|
|
$oMerger->addPDF($localFilePath, 'all');
|
|
}
|
|
|
|
$oMerger->merge();
|
|
|
|
$order_pdf = $oMerger;
|
|
|
|
if($filesystemDriver === 's3'){
|
|
$finalTempPdfPath = storage_path('app/documents/temp.pdf');
|
|
$filePathForS3 = 'temp/temp.pdf';
|
|
$fileContent = file_get_contents($finalTempPdfPath);
|
|
Log::info('CreateInvoiceDocumentProcessor 2 filePathForS3: ' . $filePathForS3);
|
|
Storage::disk('s3')->put($filePathForS3, $fileContent);
|
|
}
|
|
}
|
|
|
|
$document_object = new DocumentObject(
|
|
$document_type,
|
|
[chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))],
|
|
'',
|
|
ApprovalStatus::COMPLETED,
|
|
$lowercaseDocumentType . 's'
|
|
);
|
|
|
|
/** @var Document $document */
|
|
$document = $this->createsDocument->execute($purchaseOrder->booking, $document_object);
|
|
$this->createsFile->execute($document, $document_object);
|
|
|
|
}
|
|
}
|