diff --git a/app/Classes/General/Eloquent/Filters/BillNo.php b/app/Classes/General/Eloquent/Filters/BillNo.php new file mode 100644 index 00000000..167dcd51 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/BillNo.php @@ -0,0 +1,20 @@ +where('bill_no', $value); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php index 9c66abc5..e78a9690 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundCreditNoteLogic.php @@ -7,15 +7,14 @@ use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; -use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; +use App\Classes\Modules\Transactions\DataTransferObjects\TransactionRefundCalculationObject; use App\Classes\Modules\Transactions\Services\CreatesTransaction; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; use App\Http\Resources\TransactionResource; use App\Models\Booking; -use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -38,9 +37,6 @@ class CreateBookingRefundCreditNoteLogic extends AbstractControllerLogic /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; - /** @var FetchesCompanyPaymentAttemptLimit */ - private $fetchesCompanyPaymentAttemptLimit; - /** @var GeneratesTransactionBillNumber */ private $generatesTransactionBillNumber; @@ -51,15 +47,13 @@ class CreateBookingRefundCreditNoteLogic extends AbstractControllerLogic * 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) + public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction) { $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; - $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; } @@ -76,19 +70,24 @@ class CreateBookingRefundCreditNoteLogic extends AbstractControllerLogic $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]); - $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::REFUNDED); + $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED); - $paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company); + $paymentReferenceTransaction = $this->fetchesTransaction->execute(['bill_no' => $transaction->payment_reference]); - $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); + if((int) $paymentReferenceTransaction->type === TransactionType::BILL){ + $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, $transaction, $transaction->original_amount, $paymentReferenceTransaction->currency_rate); + $transactionRefundCalculationObject->setRefundConversionAmount(); - $transaction = $this->createsTransaction->execute($booking, $object); + $billNumber = $this->generatesTransactionBillNumber->execute('CRDNT-'); + + $object = new TransactionObject($billNumber, TransactionType::CREDIT_NOTE, 1, $booking->company->id, + $booking->bank_id, $paymentReferenceTransaction->payment_method, + $transactionRefundCalculationObject->getRefundConversionAmount(), $transaction->original_amount, 1, + $paymentReferenceTransaction->original_currency_id, $paymentReferenceTransaction->currency_rate, + $paymentReferenceTransaction->tax, $paymentReferenceTransaction->service_charge, null, ApprovalStatus::PENDING_VERIFICATION, [], $paymentReferenceTransaction->bill_no); + + $transaction = $this->createsTransaction->execute($booking, $object); + } return $this->resourceResponse(new TransactionResource($transaction)); } diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php index 843ecb88..90de18f4 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php @@ -5,17 +5,17 @@ namespace App\Classes\Modules\Bookings\ControllersLogic; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation; use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; -use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; +use App\Classes\Modules\Transactions\DataTransferObjects\TransactionRefundCalculationObject; use App\Classes\Modules\Transactions\Services\CreatesTransaction; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; use App\Http\Resources\TransactionResource; use App\Models\Booking; -use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -32,15 +32,15 @@ class CreateBookingRefundLogic extends AbstractControllerLogic ]; } + /** @var FetchesBookingQuotation */ + private $fetchBookingQuotation; + /** @var FetchesTransaction */ private $fetchesTransaction; /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; - /** @var FetchesCompanyPaymentAttemptLimit */ - private $fetchesCompanyPaymentAttemptLimit; - /** @var GeneratesTransactionBillNumber */ private $generatesTransactionBillNumber; @@ -49,17 +49,17 @@ class CreateBookingRefundLogic extends AbstractControllerLogic /** * CreateBookingPaymentLogic constructor. + * @param FetchesBookingQuotation $fetchBookingQuotation * @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) + public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction) { + $this->fetchBookingQuotation = $fetchBookingQuotation; $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; - $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; } @@ -75,18 +75,21 @@ class CreateBookingRefundLogic extends AbstractControllerLogic $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-'); - + + if((int) $transaction->type === TransactionType::BILL){ + $customerBooking = $booking->transactions()->payments()->complete()->where('original_amount', '=', $transaction->original_amount)->where('id', '<', $transaction->id)->orderByDesc('id')->first(); + } + + $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, (int) $transaction->type === TransactionType::BILL ? $customerBooking : $transaction, $request->input('amount')); + $transactionRefundCalculationObject->init(); + $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); + $transactionRefundCalculationObject->getConfigurations()->getConfigurations()->getBankId(), $transactionRefundCalculationObject->getConversionObject()->getPaymentMethod(), + $transactionRefundCalculationObject->getRefundTotalAmount(), $transactionRefundCalculationObject->getAmount(), 1, + $transactionRefundCalculationObject->getConversionObject()->getCurrencyId(), $transactionRefundCalculationObject->getTransaction()->currency_rate, + $transactionRefundCalculationObject->getRefundTax(), $transactionRefundCalculationObject->getRefundServiceCharge(), null, ApprovalStatus::PENDING_VERIFICATION, [], $transaction->bill_no); $transaction = $this->createsTransaction->execute($booking, $object); diff --git a/app/Classes/Modules/Transactions/DataTransferObjects/TransactionObject.php b/app/Classes/Modules/Transactions/DataTransferObjects/TransactionObject.php index 8fcf4ece..780615a0 100644 --- a/app/Classes/Modules/Transactions/DataTransferObjects/TransactionObject.php +++ b/app/Classes/Modules/Transactions/DataTransferObjects/TransactionObject.php @@ -57,6 +57,9 @@ class TransactionObject implements DataTransferObject /** @var array|null */ private $details; + /** @var string */ + private $paymentReference; + /** * TransactionObject constructor. * @param string $billNo @@ -75,8 +78,9 @@ class TransactionObject implements DataTransferObject * @param Carbon|null $expiresOn * @param int|null $status * @param array|null $details + * @param string $paymentReference */ - public function __construct(string $billNo, string $transactionType, int $issuer, int $receiver, int $recipientBankAccountId, int $paymentMethod, float $amount, float $originalAmount, int $currencyId, int $originalCurrencyId, float $currencyRate, float $tax, float $serviceCharge, ?Carbon $expiresOn, ?int $status = ApprovalStatus::PENDING_SUBMISSION, ?array $details = []) + public function __construct(string $billNo, string $transactionType, int $issuer, int $receiver, int $recipientBankAccountId, int $paymentMethod, float $amount, float $originalAmount, int $currencyId, int $originalCurrencyId, float $currencyRate, float $tax, float $serviceCharge, ?Carbon $expiresOn, ?int $status = ApprovalStatus::PENDING_SUBMISSION, ?array $details = [], ?string $paymentReference = null) { $this->billNo = $billNo; $this->transactionType = $transactionType; @@ -94,6 +98,7 @@ class TransactionObject implements DataTransferObject $this->expiresOn = $expiresOn; $this->status = $status; $this->details = $details; + $this->paymentReference = $paymentReference; } /** @@ -226,6 +231,11 @@ class TransactionObject implements DataTransferObject }, $this->details); } + public function getPaymentReference(): string + { + return $this->paymentReference; + } + diff --git a/app/Classes/Modules/Transactions/DataTransferObjects/TransactionRefundCalculationObject.php b/app/Classes/Modules/Transactions/DataTransferObjects/TransactionRefundCalculationObject.php new file mode 100644 index 00000000..6da9a37d --- /dev/null +++ b/app/Classes/Modules/Transactions/DataTransferObjects/TransactionRefundCalculationObject.php @@ -0,0 +1,176 @@ +booking = $booking; + $this->transaction = $transaction; + $this->amount = $amount; + $this->currency_rate = $currency_rate; + } + + /** + * @return Booking + */ + public function getBooking(): Booking + { + return $this->booking; + } + + /** + * @return Transaction + */ + public function getTransaction(): Transaction + { + return $this->transaction; + } + + public function getAmount(): float + { + return $this->amount; + } + + public function getCurrencyRate(): float + { + return $this->currency_rate; + } + + /** + * @return float + */ + public function setUnrequestedAmount() + { + $this->unrequested_amount = $this->transaction->original_amount == $this->amount ? $this->transaction->original_amount : $this->transaction->original_amount - $this->amount; + return $this; + } + + public function getUnrequestedAmount() + { + return $this->unrequested_amount; + } + + public function setConversionObject() + { + $this->conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $this->getUnrequestedAmount())), $this->booking->convertible_currency_id, $this->booking->service_id, $this->booking->fix_currency_id === 1 ? 0 : 1); + return $this; + } + + public function getConversionObject() + { + return $this->conversionObject; + } + + public function setConfigurations() + { + $this->configurations = app('App\Classes\Modules\Bookings\Services\FetchesBookingQuotation')->execute($this->booking->company, $this->getConversionObject()); + return $this; + } + + public function getConfigurations() + { + return $this->configurations; + } + + public function setRefundAmount() + { + $this->refund_amount = $this->transaction->amount == $this->getConfigurations()->getLocalTotal() ? $this->transaction->amount : $this->transaction->amount - $this->getConfigurations()->getLocalTotal(); + return $this; + } + + public function getRefundAmount() + { + return $this->refund_amount; + } + + public function setRefundServiceCharge() + { + $this->refund_service_charge = $this->transaction->service_charge == $this->getConfigurations()->getServiceCharge() ? $this->transaction->service_charge : $this->transaction->service_charge - $this->getConfigurations()->getServiceCharge(); + return $this; + } + + public function getRefundServiceCharge() + { + return $this->refund_service_charge; + } + + public function setRefundTax() + { + $this->refund_tax = $this->transaction->tax == $this->getConfigurations()->getTax() ? $this->transaction->tax : $this->transaction->tax - $this->getConfigurations()->getTax(); + return $this; + } + + public function getRefundTax() + { + return $this->refund_tax; + } + + public function setRefundTotalAmount() + { + $this->refund_total_amount = $this->getRefundAmount() + $this->getRefundServiceCharge() + $this->getRefundTax(); + return $this; + } + + public function getRefundTotalAmount() + { + return $this->refund_total_amount; + } + + public function setRefundConversionAmount() + { + $this->refund_conversion_amount = $this->getCurrencyRate() ? $this->transaction->original_amount * (1 / $this->getCurrencyRate()) : $this->transaction->original_amount; + return $this; + } + + public function getRefundConversionAmount() + { + return $this->refund_conversion_amount; + } + + public function init(){ + $this->setUnrequestedAmount(); + $this->setConversionObject(); + $this->setConfigurations(); + $this->setRefundAmount(); + $this->setRefundServiceCharge(); + $this->setRefundTax(); + $this->setRefundTotalAmount(); + $this->setRefundConversionAmount(); + } +} diff --git a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php index ddebf0d5..debd5854 100644 --- a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php +++ b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php @@ -32,6 +32,7 @@ class CreatesTransaction extends AbstractUpdateRelationshipRecord $model->service_charge = $object->getServiceCharge(); $model->expires_on = $object->getExpiresOn(); $model->status = $object->getStatus(); + $model->payment_reference = $object->getPaymentReference(); return $this->handler($booking->transactions(), $model); diff --git a/app/Http/Resources/TransactionResource.php b/app/Http/Resources/TransactionResource.php index fc0dff73..9caa6687 100644 --- a/app/Http/Resources/TransactionResource.php +++ b/app/Http/Resources/TransactionResource.php @@ -3,6 +3,7 @@ namespace App\Http\Resources; use App\Classes\ValueObjects\Constants\TransactionType; +use App\Models\Transaction; use Carbon\Carbon; use Illuminate\Http\Resources\Json\JsonResource; diff --git a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue index 6e9ea19f..3abccf8b 100644 --- a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue +++ b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue @@ -2,7 +2,7 @@
-
+
Status
@@ -42,7 +42,7 @@
-
+
@@ -116,6 +116,106 @@
+
+
+ +
+
+
+
+
+
+
+
+
Refund Type:
+
+
+
+ {{refundMethod.name}} +
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
Fully Refund
+
+
+
+
+
+
+
+
+
Partially Refund
+
+
+
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+
{{''}}
+
+
+
+
+
+
+ + + + +
+
+
+
+
{{''}}
+
+
+
+
+
+
+
+ + +
+
+
+
+
+ +
+
+ +
+
@@ -127,7 +227,13 @@ export default { data(){ return { - expanded: false + expandPaymentDetails: false, + expandRefund: false, + refundMethod: { + name: 'Fully Refund', + status: false + }, + amount: (Math.round(1000 * 100) / 100).toFixed(2), } }, computed: { @@ -135,6 +241,26 @@ return this.item.type === 1 ? this.item : this.item.customer_booking; } }, + methods: { + requestRefund(){ + this.expandRefund = !this.expandRefund; + this.expandPaymentDetails = !this.expandPaymentDetails; + }, + clickExpand(){ + if (this.expandPaymentDetails == false && this.expandRefund == false) { + this.expandPaymentDetails = !this.expandPaymentDetails; + } else { + this.expandRefund = false; + this.expandPaymentDetails = false; + } + }, + updateRefundType(refund){ + this.refundMethod = { + name: refund.name, + status: !this.refundMethod.status + } + }, + }, mixins: [componentHandler] }