diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CancelBookingLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CancelBookingLogic.php new file mode 100644 index 00000000..6a6fedef --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/CancelBookingLogic.php @@ -0,0 +1,68 @@ + 'Cancel Booking', + 'message' => 'You have successfully canceled the Booking' + ]; + } + + + /** @var FetchesBooking */ + private $fetchesBooking; + + /** @var CancelsBooking */ + private $cancelsBooking; + + /** + * CancelBookingLogic constructor. + * @param FetchesBooking $fetchesBooking + * @param CancelsBooking $cancelsBooking + */ + public function __construct(FetchesBooking $fetchesBooking, CancelsBooking $cancelsBooking) + { + $this->fetchesBooking = $fetchesBooking; + $this->cancelsBooking = $cancelsBooking; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]); + + $bookingActivePayments = $booking->transactions()->where('type', '=', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->get(); + + if(count($bookingActivePayments)){ + throw new MalformedRequestException('You can\'t cancel an order with active payments'); + } + + $this->cancelsBooking->execute($booking); + + return $this->resourceResponse(new BookingResource($booking)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php new file mode 100644 index 00000000..9c66abc5 --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php @@ -0,0 +1,97 @@ + 'Create Refund Booking', + 'message' => 'You have successfully created a refund booking' + ]; + } + + /** @var FetchesTransaction */ + private $fetchesTransaction; + + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus; + + /** @var FetchesCompanyPaymentAttemptLimit */ + private $fetchesCompanyPaymentAttemptLimit; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** + * CreateBookingPaymentLogic constructor. + * @param FetchesTransaction $fetchesTransaction + * @param UpdatesTransactionStatus $updatesTransactionStatus + * @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + */ + public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction) + { + $this->fetchesTransaction = $fetchesTransaction; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + + $booking = Booking::find($request->route('id')); + + $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]); + + $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::REFUNDED); + + $paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company); + + $billNumber = $this->generatesTransactionBillNumber->execute('CDTN-'); + + $object = new TransactionObject($billNumber, TransactionType::CREDIT_NOTE, 1, $booking->company->id, + $booking->bank_id, $transaction->payment_method, + $transaction->amount, $transaction->original_amount, 1, + $transaction->original_currency_id, $transaction->currency_rate, + $transaction->tax, $transaction->service_charge, Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_VERIFICATION); + + $transaction = $this->createsTransaction->execute($booking, $object); + + return $this->resourceResponse(new TransactionResource($transaction)); + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php new file mode 100644 index 00000000..843ecb88 --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php @@ -0,0 +1,97 @@ + 'Create Refund Booking', + 'message' => 'You have successfully created a refund booking' + ]; + } + + /** @var FetchesTransaction */ + private $fetchesTransaction; + + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus; + + /** @var FetchesCompanyPaymentAttemptLimit */ + private $fetchesCompanyPaymentAttemptLimit; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** + * CreateBookingPaymentLogic constructor. + * @param FetchesTransaction $fetchesTransaction + * @param UpdatesTransactionStatus $updatesTransactionStatus + * @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + */ + public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction) + { + $this->fetchesTransaction = $fetchesTransaction; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + + $booking = Booking::find($request->route('id')); + + $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]); + + $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::REFUNDED); + + $paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company); + + $billNumber = $this->generatesTransactionBillNumber->execute('RFD-'); + + $object = new TransactionObject($billNumber, TransactionType::REFUND, 1, $booking->company->id, + $booking->bank_id, $transaction->payment_method, + $transaction->amount, $transaction->original_amount, 1, + $transaction->original_currency_id, $transaction->currency_rate, + $transaction->tax, $transaction->service_charge, Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_VERIFICATION); + + $transaction = $this->createsTransaction->execute($booking, $object); + + return $this->resourceResponse(new TransactionResource($transaction)); + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/ControllersLogic/RestoreBookingLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/RestoreBookingLogic.php new file mode 100644 index 00000000..2ad8fa26 --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/RestoreBookingLogic.php @@ -0,0 +1,68 @@ + 'Restore Booking', + 'message' => 'You have successfully restored the Booking' + ]; + } + + /** @var CanDeleteBooking */ + private $canDeleteBooking; + + /** @var FetchesBooking */ + private $fetchesBooking; + + /** @var RestoresBooking */ + private $restoresBooking; + + /** + * RestoreBookingLogic constructor. + * @param CanDeleteBooking $canDeleteBooking + * @param FetchesBooking $fetchesBooking + * @param RestoresBooking $restoresBooking + */ + public function __construct(CanDeleteBooking $canDeleteBooking, FetchesBooking $fetchesBooking, RestoresBooking $restoresBooking) + { + $this->canDeleteBooking = $canDeleteBooking; + $this->fetchesBooking = $fetchesBooking; + $this->restoresBooking = $restoresBooking; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]); + + $this->restoresBooking->execute($booking); + + return $this->resourceResponse(new BookingResource($booking)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php new file mode 100644 index 00000000..2dec77fa --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php @@ -0,0 +1,18 @@ +transactions()->refunds()->complete()->sum('original_amount'); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/Services/CancelsBooking.php b/app/Classes/Modules/Bookings/Services/CancelsBooking.php new file mode 100644 index 00000000..dd9b12f3 --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/CancelsBooking.php @@ -0,0 +1,24 @@ +status = ApprovalStatus::SUSPENDED; + + return $this->handler($model); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/Services/RestoresBooking.php b/app/Classes/Modules/Bookings/Services/RestoresBooking.php new file mode 100644 index 00000000..9efef208 --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/RestoresBooking.php @@ -0,0 +1,24 @@ +status = ApprovalStatus::APPROVED; + + return $this->handler($model); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/Standards/Rules/CanDeleteBooking.php b/app/Classes/Modules/Bookings/Standards/Rules/CanDeleteBooking.php index 1b5acee4..dfd307bc 100644 --- a/app/Classes/Modules/Bookings/Standards/Rules/CanDeleteBooking.php +++ b/app/Classes/Modules/Bookings/Standards/Rules/CanDeleteBooking.php @@ -34,7 +34,7 @@ class CanDeleteBooking extends AbstractRule /** - * @param BookingObject $object + * @param $object * @return bool */ protected function criteria($object): bool diff --git a/app/Classes/ValueObjects/Constants/ApprovalStatus.php b/app/Classes/ValueObjects/Constants/ApprovalStatus.php index 2f6a7a4a..154db2ca 100644 --- a/app/Classes/ValueObjects/Constants/ApprovalStatus.php +++ b/app/Classes/ValueObjects/Constants/ApprovalStatus.php @@ -18,4 +18,5 @@ final class ApprovalStatus { public const EXPIRED = 6; + public const REFUNDED = 7; } diff --git a/app/Classes/ValueObjects/Constants/TransactionType.php b/app/Classes/ValueObjects/Constants/TransactionType.php index 905c12b1..c3d9eb64 100644 --- a/app/Classes/ValueObjects/Constants/TransactionType.php +++ b/app/Classes/ValueObjects/Constants/TransactionType.php @@ -22,4 +22,5 @@ final class TransactionType { public const SUPPLIER_DELIVER = 8; + public const CREDIT_NOTE = 9; } diff --git a/app/Http/Controllers/Bookings/CancelBookingController.php b/app/Http/Controllers/Bookings/CancelBookingController.php new file mode 100644 index 00000000..f31a80a1 --- /dev/null +++ b/app/Http/Controllers/Bookings/CancelBookingController.php @@ -0,0 +1,19 @@ +execute($request); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Bookings/CreateBookingRefundController.php b/app/Http/Controllers/Bookings/CreateBookingRefundController.php new file mode 100644 index 00000000..40d4ddda --- /dev/null +++ b/app/Http/Controllers/Bookings/CreateBookingRefundController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Bookings/CreateBookingRefundCreditNoteController.php b/app/Http/Controllers/Bookings/CreateBookingRefundCreditNoteController.php new file mode 100644 index 00000000..4e54678a --- /dev/null +++ b/app/Http/Controllers/Bookings/CreateBookingRefundCreditNoteController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Bookings/RestoreBookingController.php b/app/Http/Controllers/Bookings/RestoreBookingController.php new file mode 100644 index 00000000..5b4aa809 --- /dev/null +++ b/app/Http/Controllers/Bookings/RestoreBookingController.php @@ -0,0 +1,19 @@ +execute($request); + } +} \ No newline at end of file diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 002d7611..9d1ed5b9 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -84,6 +84,15 @@ class Transaction extends AbstractModel implements Documentable return $query->where('type', TransactionType::BILL); } + /** + * @param Builder $query + * @return Builder + */ + public function scopeRefunds(Builder $query) + { + return $query->where('type', TransactionType::REFUND); + } + /** * @param Builder $query diff --git a/resources/assets/vue/components/bookings/elements/BookingComponent.vue b/resources/assets/vue/components/bookings/elements/BookingComponent.vue index febce6ba..509e9840 100644 --- a/resources/assets/vue/components/bookings/elements/BookingComponent.vue +++ b/resources/assets/vue/components/bookings/elements/BookingComponent.vue @@ -52,17 +52,26 @@ Pending... -
@@ -70,6 +79,11 @@ \ No newline at end of file diff --git a/resources/assets/vue/components/bookings/forms/CancelBookingFormComponent.vue b/resources/assets/vue/components/bookings/forms/CancelBookingFormComponent.vue new file mode 100644 index 00000000..b32275f1 --- /dev/null +++ b/resources/assets/vue/components/bookings/forms/CancelBookingFormComponent.vue @@ -0,0 +1,40 @@ + +