Files
exchange-2.0/app/Classes/Modules/Bookings/ControllersLogic/BatchBookingsGenerateEInvoiceLogic.php
T

102 lines
3.7 KiB
PHP

<?php
namespace App\Classes\Modules\Bookings\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Jobs\Commands\V2\ProcessBookingForEInvoiceV2CommandJob;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;
class BatchBookingsGenerateEInvoiceLogic extends AbstractControllerLogic
{
protected int $processedCount = 0;
/**
* @return array
*/
protected function notification(): array
{
return [
'title' => 'Generate Bookings E-Invoices',
'message' => sprintf(
'You have successfully submitted %d booking%s for E-Invoices processing.',
$this->processedCount,
$this->processedCount === 1 ? '' : 's'
),
];
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request): JsonResponse
{
$validated = $request->validate([
'startDate' => 'nullable|date_format:d-m-Y',
'endDate' => 'nullable|date_format:d-m-Y|after_or_equal:startDate',
]);
$startDate = null;
$endDate = null;
if (isset($validated['startDate']) && $validated['startDate']) {
$startDate = Carbon::createFromFormat('d-m-Y', $validated['startDate'])->startOfDay();
} else {
$startDate = Carbon::now()->subMonths(1)->startOfDay();
}
if (isset($validated['endDate']) && $validated['endDate']) {
$endDate = Carbon::createFromFormat('d-m-Y', $validated['endDate'])->endOfDay();
} else {
$endDate = Carbon::now()->endOfDay();
}
$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)
->whereHas('transactions', function ($query) use ($startDate, $endDate) {
$query->payments()
->complete()
->whereBetween('created_at', [$startDate, $endDate])
->latest('created_at');
})
->get();
foreach ($bookings as $booking) {
// Log::info('Booking ID: ' . $booking->marking);
ProcessBookingForEInvoiceV2CommandJob::dispatch($booking);
}
$this->processedCount = count($bookings);
return $this->resourceResponse(JsonResource::collection(collect([])));
}
}