From 30884201bdd2fb1d0aeab49053c76b2105850ef4 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 19 Oct 2021 21:51:47 +0800 Subject: [PATCH] regenerate booking invoice --- .../RegenerateInvoiceBookingLogic.php | 110 ++++++++++++++++++ .../Documents/Services/DeletesDocument.php | 19 +++ .../Services/DeletesTransaction.php | 19 +++ .../RegenerateInvoiceBookingController.php | 20 ++++ routes/booking.php | 1 + 5 files changed, 169 insertions(+) create mode 100644 app/Classes/Modules/Bookings/ControllersLogic/RegenerateInvoiceBookingLogic.php create mode 100644 app/Classes/Modules/Documents/Services/DeletesDocument.php create mode 100644 app/Classes/Modules/Transactions/Services/DeletesTransaction.php create mode 100644 app/Http/Controllers/Bookings/RegenerateInvoiceBookingController.php diff --git a/app/Classes/Modules/Bookings/ControllersLogic/RegenerateInvoiceBookingLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/RegenerateInvoiceBookingLogic.php new file mode 100644 index 00000000..d1cdbada --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/RegenerateInvoiceBookingLogic.php @@ -0,0 +1,110 @@ + 'Regenerate Booking Invoice', + 'message' => 'You have successfully regenerate booking invoice' + ]; + } + + /** @var CanFetchBooking */ + private $canFetchBooking; + + /** @var FetchesBooking */ + private $fetchesBooking; + + /** @var DeletesTransaction */ + private $deletesTransaction; + + /** @var UpdatesBookingStatus */ + private $updatesBookingStatus; + + /** @var DeletesDocument */ + private $deletesDocument; + + /** @var CreateInvoiceTransactionProcessor */ + private $createInvoiceTransactionProcessor; + + /** + * FetchBookingLogic constructor. + * @param CanFetchBooking $canFetchBooking + * @param FetchesBooking $fetchesBooking + * @param DeletesTransaction $deletesTransaction + * @param UpdatesBookingStatus $updatesBookingStatus + * @param DeletesDocument $deletesDocument + * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor + */ + public function __construct( + CanFetchBooking $canFetchBooking, + FetchesBooking $fetchesBooking, + DeletesTransaction $deletesTransaction, + UpdatesBookingStatus $updatesBookingStatus, + DeletesDocument $deletesDocument, + CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor + ) + { + $this->canFetchBooking = $canFetchBooking; + $this->fetchesBooking = $fetchesBooking; + $this->deletesTransaction = $deletesTransaction; + $this->updatesBookingStatus = $updatesBookingStatus; + $this->deletesDocument = $deletesDocument; + $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; + } + + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request) : JsonResponse + { + $this->canFetchBooking->passes(); + + $booking = $this->fetchesBooking->execute([ + 'id' => $request->route('id'), + 'status' => ApprovalStatus::COMPLETED, + 'with_transactions' => true] + ); + + $this->updatesBookingStatus->execute($booking, ApprovalStatus::APPROVED); + + $transaction = $booking->transactions()->whereIn('type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->get(); + foreach ($transaction as $key => $row) { + $this->deletesTransaction->execute($row); + } + + $document = $booking->documents()->get(); + foreach ($document as $key => $row) { + $this->deletesDocument->execute($row); + } + + $this->createInvoiceTransactionProcessor->execute($booking); + + return $this->resourceResponse(new BookingResource($booking)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Documents/Services/DeletesDocument.php b/app/Classes/Modules/Documents/Services/DeletesDocument.php new file mode 100644 index 00000000..e0f02819 --- /dev/null +++ b/app/Classes/Modules/Documents/Services/DeletesDocument.php @@ -0,0 +1,19 @@ +handler($model); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/Services/DeletesTransaction.php b/app/Classes/Modules/Transactions/Services/DeletesTransaction.php new file mode 100644 index 00000000..66416d60 --- /dev/null +++ b/app/Classes/Modules/Transactions/Services/DeletesTransaction.php @@ -0,0 +1,19 @@ +handler($model); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Bookings/RegenerateInvoiceBookingController.php b/app/Http/Controllers/Bookings/RegenerateInvoiceBookingController.php new file mode 100644 index 00000000..d63c1b9c --- /dev/null +++ b/app/Http/Controllers/Bookings/RegenerateInvoiceBookingController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/routes/booking.php b/routes/booking.php index c2805443..c7ff65dd 100644 --- a/routes/booking.php +++ b/routes/booking.php @@ -10,6 +10,7 @@ Route::group(['prefix' => 'booking', 'as' => 'booking.', 'namespace' => 'Booking Route::put('/cancel/{id}', 'CancelBookingController@cancel')->name('cancel'); Route::put('/restore/{id}', 'RestoreBookingController@restore')->name('restore'); Route::delete('/delete/{id}', 'DeleteBookingController@delete')->name('delete'); + Route::post('/regenerate/invoice/{id}', 'RegenerateInvoiceBookingController@regenerate')->name('regenerate.invoice'); Route::group(['prefix' => '{id}/payment', 'as' => 'payment.'], function () { Route::post('quotation', 'FetchBookingPaymentQuotationController@fetch')->name('quotation');