mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
E-Invoice - Partial completion of => Doc Delivery - E-INVOICE, E-CN, E-RN
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Classes\Modules\Transactions\ControllersLogic;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
|
||||
use App\Classes\Modules\Companies\Services\FetchesCompany;
|
||||
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Classes\General\AWSS3Helper;
|
||||
use App\Classes\ValueObjects\Constants\DocumentType;
|
||||
use App\Models\KeyValuePair;
|
||||
use App\Models\Transaction;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class GenerateCreditNotePdfV2Logic
|
||||
{
|
||||
|
||||
/** @var FetchesTransaction */
|
||||
private $fetchesTransaction;
|
||||
|
||||
/** @var FetchesCompany */
|
||||
private $fetchesCompany;
|
||||
|
||||
/**
|
||||
* GenerateCreditNotePdfV2Logic constructor.
|
||||
* @param FetchesTransaction $fetchesTransaction
|
||||
* @param FetchesCompany $fetchesCompany
|
||||
*/
|
||||
public function __construct(FetchesTransaction $fetchesTransaction, FetchesCompany $fetchesCompany)
|
||||
{
|
||||
$this->fetchesTransaction = $fetchesTransaction;
|
||||
$this->fetchesCompany = $fetchesCompany;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return string|\Symfony\Component\HttpFoundation\Response
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function execute(Request $request)
|
||||
{
|
||||
$pdfTemplateName = 'pages.pdfs.credit_note_v2'; //default since e-invoice implementation
|
||||
$transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);
|
||||
|
||||
if($transaction->type === TransactionType::REFUND){
|
||||
//Retrieve TransactionType::CREDIT_NOTE
|
||||
$booking = $transaction->owner->booking;
|
||||
$kvp = $transaction->attributesKVP()->latest()->first();
|
||||
if($kvp){
|
||||
if($kvp->key === 'App\Models\Transaction'){
|
||||
$transaction = $this->fetchesTransaction->execute(['id' => $kvp->value ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
//For Old Cases
|
||||
$booking = $transaction->booking;
|
||||
$pdfTemplateName = 'pages.pdfs.credit_note'; //default
|
||||
|
||||
//For New Cases with e-invoice: Retrieve the refund transaction for this credit note
|
||||
$kvp = KeyValuePair::where('key', 'App\Models\Transaction')->where('value', $transaction->id)->first();
|
||||
if($kvp){
|
||||
$kvpOwner = $kvp->owner;
|
||||
if($kvpOwner && $kvpOwner instanceof Transaction && $kvpOwner->type === TransactionType::REFUND){
|
||||
$booking = $kvpOwner->owner->booking;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$date = $transaction->created_at;
|
||||
$supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]);
|
||||
$brn = $supplier->documents->where('document_type', DocumentType::SSM_REGISTRATION)->first();
|
||||
|
||||
$eInvoiceStarted = false;
|
||||
$eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00'));
|
||||
$bookingCreatedDate = Carbon::parse($booking->created_at);
|
||||
|
||||
if ($bookingCreatedDate->isAfter($eInvoiceStartDate)) {
|
||||
$eInvoiceStarted = true;
|
||||
}
|
||||
// $eInvoiceStarted = false; //cief todo: 90 - for testing
|
||||
|
||||
if($eInvoiceStarted)
|
||||
{
|
||||
if($supplier->e_invoice === 1){
|
||||
Log::info('Based on booking created date, E-Credit Note started and company wants e-invoice ' . json_encode($booking));
|
||||
$date = $booking->updated_at->copy()->endOfMonth();
|
||||
$pdfTemplateName = 'pages.pdfs.e_credit_note';
|
||||
}
|
||||
else{
|
||||
Log::info('Based on booking created date, E-Credit Note started and company do not wants e-invoice');
|
||||
$pdfTemplateName = 'pages.pdfs.credit_note_v2';
|
||||
}
|
||||
}
|
||||
else{
|
||||
Log::info('Based on booking created date, E-Credit Note not yet started');
|
||||
}
|
||||
|
||||
$pdf = LaravelMpdf::loadView($pdfTemplateName, ['transaction' => $transaction, 'booking' => $booking, 'supplier' => $supplier, 'date' => $date, 'brn' => $brn,]);
|
||||
|
||||
$exportFileName = 'CreditNote.pdf';
|
||||
$filesystemDriver = Storage::getDefaultDriver();
|
||||
if($filesystemDriver === 's3'){
|
||||
$pdfContent = $pdf->output();
|
||||
return response([ 'src' => AWSS3Helper::S3PDF($exportFileName, $pdfContent) ]);
|
||||
}
|
||||
else{
|
||||
return $pdf->stream($exportFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user