E-Invoice - Enhancement to allow EInvoice to be generated for cases with refund

This commit is contained in:
Dillon Ngo
2025-09-12 12:59:34 +08:00
parent f4dfadfb29
commit d215e5558d
10 changed files with 630 additions and 47 deletions
@@ -44,7 +44,7 @@ class CreateInvoiceDocumentProcessor
* @return void
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function execute($transaction, $purchaseOrder, $supplier, $document_type, $voucherRedemption = null, $isAllowNormalInvoice = false)
public function execute($transaction, $purchaseOrder, $supplier, $document_type, $voucherRedemption = null, $generateEInvoiceWithNormalInvoiceTemplate = false, $generateEInvoiceRefund = false)
{
// calculate current Paid Amount
$booking = $transaction->owner_type == Booking::class ? $transaction->owner : null;
@@ -61,7 +61,13 @@ class CreateInvoiceDocumentProcessor
if ($booking) {
$bookingCreatedDate = Carbon::parse($booking->created_at);
if ($bookingCreatedDate->isAfter($eInvoiceStartDate)) {
$lastPaymentTransaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->latest()->first();
if($generateEInvoiceRefund){
$lastPaymentTransaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::REFUNDED])->latest()->first();
}
else{
$lastPaymentTransaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->latest()->first();
}
$documentDate = $lastPaymentTransaction->created_at;
}
@@ -83,7 +89,14 @@ class CreateInvoiceDocumentProcessor
$documentDate = $lastDayOfMonth;
}
}
$payment = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->first();
if($generateEInvoiceRefund){
$payment = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::REFUNDED])->first();
}
else{
$payment = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->first();
}
$refundAmount = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->sum('amount');
$paymentAmount = $payment->amount;
$currentPaidAmount = $paymentAmount - $refundAmount;
@@ -92,7 +105,7 @@ class CreateInvoiceDocumentProcessor
$lowercaseDocumentType = strtolower($document_type);
if($document_type === DocumentType::EINVOICE){ //July 2025 workaround generate normal invoice instead of E-Invoice
if($isAllowNormalInvoice){
if($generateEInvoiceWithNormalInvoiceTemplate){
$lowercaseDocumentType = strtolower(DocumentType::INVOICE);
}
}
@@ -169,7 +182,7 @@ class CreateInvoiceDocumentProcessor
$document = $this->createsDocument->execute($transaction, $document_object);
}
else{
$document = $this->createsDocument->execute($purchaseOrder->booking, $document_object);
$document = $this->createsDocument->execute($purchaseOrder->booking ?? $booking, $document_object);
}
$this->createsFile->execute($document, $document_object);
}