Files
exchange-2.0/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingRefundLogic.php
T
2021-09-05 22:52:33 +08:00

125 lines
5.9 KiB
PHP

<?php
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\Transactions\Services\CalculateTransactionRefundOriginalAmount;
use App\Classes\Modules\Transactions\Services\CalculateTransactionRefundAmount;
use App\Classes\Modules\Transactions\Services\CalculateTransactionRefundServiceCharge;
use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;
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;
class CreateBookingRefundLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Create Refund Booking',
'message' => 'You have successfully created a refund booking'
];
}
/** @var FetchesBookingQuotation */
private $fetchBookingQuotation;
/** @var FetchesTransaction */
private $fetchesTransaction;
/** @var UpdatesTransactionStatus */
private $updatesTransactionStatus;
/** @var CalculateTransactionRefundOriginalAmount */
private $calculateTransactionRefundOriginalAmount;
/** @var CalculateTransactionRefundAmount */
private $calculateTransactionRefundAmount;
/** @var CalculateTransactionRefundServiceCharge */
private $calculateTransactionRefundServiceCharge;
/** @var FetchesCompanyPaymentAttemptLimit */
private $fetchesCompanyPaymentAttemptLimit;
/** @var GeneratesTransactionBillNumber */
private $generatesTransactionBillNumber;
/** @var CreatesTransaction */
private $createsTransaction;
/**
* CreateBookingPaymentLogic constructor.
* @param FetchesBookingQuotation $fetchBookingQuotation
* @param FetchesTransaction $fetchesTransaction
* @param UpdatesTransactionStatus $updatesTransactionStatus
* @param CalculateTransactionRefundOriginalAmount $calculateTransactionRefundOriginalAmount
* @param CalculateTransactionRefundServiceCharge $calculateTransactionRefundServiceCharge
* @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit
* @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
* @param CreatesTransaction $createsTransaction
*/
public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, CalculateTransactionRefundOriginalAmount $calculateTransactionRefundOriginalAmount, CalculateTransactionRefundAmount $calculateTransactionRefundAmount, CalculateTransactionRefundServiceCharge $calculateTransactionRefundServiceCharge, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction)
{
$this->fetchBookingQuotation = $fetchBookingQuotation;
$this->fetchesTransaction = $fetchesTransaction;
$this->updatesTransactionStatus = $updatesTransactionStatus;
$this->calculateTransactionRefundOriginalAmount = $calculateTransactionRefundOriginalAmount;
$this->calculateTransactionRefundAmount = $calculateTransactionRefundAmount;
$this->calculateTransactionRefundServiceCharge = $calculateTransactionRefundServiceCharge;
$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')]);
$billNumber = $this->generatesTransactionBillNumber->execute('RFD-');
$unrequestedAmount = $this->calculateTransactionRefundOriginalAmount->execute($transaction, $request->input('amount'));
$conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $unrequestedAmount)), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1);
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject);
$totalRefundAmount = $this->calculateTransactionRefundAmount->execute($transaction, $configurations->getTotal());
$object = new TransactionObject($billNumber, TransactionType::REFUND, 1, $booking->company->id,
$configurations->getConfigurations()->getBankId(), $configurations->getConversionObject()->getPaymentMethod(),
$totalRefundAmount, $request->input('amount'), 1,
$configurations->getConversionObject()->getCurrencyId(), ((int) $transaction->type === TransactionType::BILL) ? $transaction->currency_rate : 0,
0, 0, null, ApprovalStatus::PENDING_VERIFICATION);
$transaction = $this->createsTransaction->execute($booking, $object);
return $this->resourceResponse(new TransactionResource($transaction));
}
}