Merge branch 'dillon/90-e-invoice-e' into vapor/staging

This commit is contained in:
Dillon Ngo
2025-10-07 15:32:51 +08:00
3 changed files with 51 additions and 18 deletions
@@ -8,6 +8,7 @@ use App\Classes\Jobs\Commands\V2\ProcessBookingForEInvoiceV2CommandJob;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Classes\ValueObjects\Constants\TransactionType;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
@@ -66,29 +67,50 @@ class BatchBookingsGenerateEInvoiceLogic extends AbstractControllerLogic
$startDate = $startDate ? Carbon::parse($startDate)->startOfDay() : Carbon::now()->subMonths(1);
$endDate = $endDate ? Carbon::parse($endDate)->endOfDay() : Carbon::now();
// $bookings = Booking::where('status', ApprovalStatus::COMPLETED)
// // ->whereBetween('created_at', [$startDate, $endDate])
// ->whereHas('transactions', function ($query) use ($startDate, $endDate) {
// $query->payments()
// ->complete()
// ->whereBetween('created_at', [$startDate, $endDate])
// ->latest('created_at');
// })
// ->whereHas('attributesKVP', function (Builder $query) {
// $query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE);
// })
// ->with(['attributesKVP' => function ($query) {
// $query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE);
// }])
// ->get();
$bookings = Booking::where('status', ApprovalStatus::COMPLETED)
// ->whereBetween('created_at', [$startDate, $endDate])
->whereHas('transactions', function ($query) use ($startDate, $endDate) {
$query->payments()
->complete()
->whereBetween('created_at', [$startDate, $endDate])
->latest('created_at');
->complete()
->whereBetween('created_at', [$startDate, $endDate])
->latest('created_at');
})
->whereHas('attributesKVP', function (Builder $query) {
$query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE);
})
->with(['attributesKVP' => function ($query) {
$query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE);
}])
->get();
$count = 0;
foreach ($bookings as $booking) {
//$autocountValue = optional($booking->attributesKVP->first())->value;
//Log::info('Booking ID: ' . $booking->marking . ' | AUTOCOUNT_DOCNO_INVOICE: ' . $autocountValue);
// $this->regenerateInvoiceBookingV2Processor->execute($booking);
ProcessBookingForEInvoiceV2CommandJob::dispatch($booking);
$temp = $booking->transactions()
->whereIn('transactions.type', [TransactionType::INVOICE])
->whereHas('attributesKVP', function ($q) {
$q->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE);
})
->whereHas('attributesKVP', function ($q) {
$q->where('key', KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK);
})
->first();
Log::info('Booking ID: ' . $booking->marking);
if($temp){
ProcessBookingForEInvoiceV2CommandJob::dispatch($booking);
Log::info('Booking ID with AUTOCOUNT_DOCNO_INVOICE: ' . $booking->marking);
$count = $count + 1;
}
}
$this->processedCount = count($bookings);
$this->processedCount = $count; //count($bookings);
return $this->resourceResponse(JsonResource::collection(collect([])));
}
@@ -99,7 +99,11 @@ class RegenerateInvoiceBookingV2Processor
$this->deletesDocument->execute($row);
}
$this->createInvoiceTransactionProcessor->execute($booking, $firstBillNo, [
$kvpCopies = $firstInvoice->attributesKVP->map(function ($kvp) {
return $kvp->replicate();
});
$this->createInvoiceTransactionProcessor->execute($booking, $firstBillNo, $kvpCopies, [
'generateEInvoice' => true,
'generateEInvoiceWithNormalInvoiceTemplate' => $eInvoiceWithNormalInvoiceTemplate,
'generateEInvoiceRefund' => $eInvoiceWithRefund,
@@ -107,7 +111,7 @@ class RegenerateInvoiceBookingV2Processor
]);
}
else {
$this->createInvoiceTransactionProcessor->execute($booking, "", [
$this->createInvoiceTransactionProcessor->execute($booking, "", null, [
'generateEInvoice' => false,
'generateEInvoiceWithNormalInvoiceTemplate' => $eInvoiceWithNormalInvoiceTemplate,
'generateEInvoiceRefund' => $eInvoiceWithRefund,
@@ -88,11 +88,12 @@ class CreateInvoiceTransactionV2Processor
/**
* @param Booking $booking
* @param String $invoiceNo
* @param Illuminate\Support\Collection<\App\Models\KeyValuePair> $kvpCopies
* @param array $options
* @return void
* @throws MalformedRequestException
*/
public function execute(Booking $booking, String $invoiceNo= "", array $options = [])
public function execute(Booking $booking, String $invoiceNo= "", $kvpCopies = null, array $options = [])
{
$generateEInvoice = $options['generateEInvoice'] ?? false;
$generateEInvoiceWithNormalInvoiceTemplate = $options['generateEInvoiceWithNormalInvoiceTemplate'] ?? false;
@@ -220,6 +221,12 @@ class CreateInvoiceTransactionV2Processor
);
$invoice_transaction = $this->createsTransaction->execute($purchaseOrder->booking ?? $booking, $transaction_object);
if ($kvpCopies) {
foreach ($kvpCopies as $kvp) {
$invoice_transaction->attributesKVP()->save($kvp);
}
}
$voucherRedemption = $transaction->voucherRedemption;
// purchase order