From b954766e997be327c1ff7487ae8571872b2d76ad Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Sun, 11 Jun 2023 16:31:56 +0800 Subject: [PATCH] remove accounting tasks from auto generated tasks temporarily until ready --- .../CreateBookingPaymentLogic.php | 196 ++++++++++-------- 1 file changed, 115 insertions(+), 81 deletions(-) diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php index 199531f4..47095df5 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php @@ -5,6 +5,7 @@ namespace App\Classes\Modules\Bookings\ControllersLogic; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Modules\Bookings\DataTransferObjects\CalculationObject; use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding; use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation; use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit; @@ -25,11 +26,8 @@ use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\ValueObjects\Constants\TransactionType; use App\Http\Resources\TransactionResource; -use App\Classes\Modules\Transactions\Processors\CreateCashBackTransactionProcessor; - use App\Models\Booking; use App\Models\Transaction; -use App\Models\Wallet; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -38,6 +36,7 @@ use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CreateBookingPaymentLogic extends AbstractControllerLogic { + /** * @return array */ @@ -72,8 +71,6 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; - /** @var CreateCashBackTransactionProcessor */ - private $createCashBackTransactionProcessor; /** @var RecalculatesWalletBalance */ private $recalculatesWalletBalance; @@ -89,8 +86,9 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic /** @var RedeemsVoucher */ private $redeemsVoucher; + + /** - * CreateBookingPaymentLogic constructor. * @param FetchesBookingQuotation $fetchBookingQuotation * @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber @@ -99,14 +97,13 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic * @param CreatesBillplzBill $createsBillplzBill * @param UpdatesWalletBalance $updatesWalletBalance * @param UpdatesTransactionStatus $updatesTransactionStatus - * @param CreateCashBackTransactionProcessor $createCashBackTransactionProcessor * @param RecalculatesWalletBalance $recalculatesWalletBalance * @param FetchesVoucher $fetchesVoucher * @param CreatesVoucher $createsVoucher * @param CreatesVoucherRedemption $createsVoucherRedemption - * @param RedeemVoucher $redeemVoucher + * @param RedeemsVoucher $redeemsVoucher */ - public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, CreateCashBackTransactionProcessor $createCashBackTransactionProcessor, RecalculatesWalletBalance $recalculatesWalletBalance, FetchesVoucher $fetchesVoucher, CreatesVoucher $createsVoucher, CreatesVoucherRedemption $createsVoucherRedemption, RedeemsVoucher $redeemsVoucher) + public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, RecalculatesWalletBalance $recalculatesWalletBalance, FetchesVoucher $fetchesVoucher, CreatesVoucher $createsVoucher, CreatesVoucherRedemption $createsVoucherRedemption, RedeemsVoucher $redeemsVoucher) { $this->fetchBookingQuotation = $fetchBookingQuotation; $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; @@ -116,7 +113,6 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $this->createsBillplzBill = $createsBillplzBill; $this->updatesWalletBalance = $updatesWalletBalance; $this->updatesTransactionStatus = $updatesTransactionStatus; - $this->createCashBackTransactionProcessor = $createCashBackTransactionProcessor; $this->recalculatesWalletBalance = $recalculatesWalletBalance; $this->fetchesVoucher = $fetchesVoucher; $this->createsVoucher = $createsVoucher; @@ -124,101 +120,139 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $this->redeemsVoucher = $redeemsVoucher; } - /** - * @param Request $request - * @return JsonResponse - * @throws MalformedRequestException - */ public function logic(Request $request) : JsonResponse { - $voucherCode = $request->input('voucher_code'); //cief to, json_encode and json_decode ??? - // if($voucherReqInput){ - // $voucherObject = json_decode($voucherReqInput); - // $validatedVoucherObject = new ValidatedVoucherObject($voucherObject->metadata->name, $voucherObject->code, $voucherObject->discount->type, $voucherObject->discount->amount_off); - // } - $booking = Booking::find($request->route('id')); - $conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $request->input('amount'))), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')]); + $conversionObject = $this->createConversionObject($request, $booking); - $outstanding = $this->calculatesBookingOutstanding->execute($booking); + $this->validatePayment($request, $booking, $conversionObject); - if($conversionObject->getAmount() > round($outstanding, 2)) throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.'); - - $configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject, $voucherCode); - - $paymentAttemptLimit = $this->fetchesCompanyPaymentAttemptLimit->execute($booking->company); + $configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject, $request->input('voucher_code')); $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); - $paymentReference = null; + $paymentReference = $this->handlePaymentMethods($request, $booking, $configurations, $billNumber); - $amount = $configurations->getTotal(); - - if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){ - $billPlzBill = $this->createsBillplzBill->execute($booking->company->name, $request->user()->email, 'This payment is made for transfer ref. '.$booking->marking, $configurations->getTotal(), $billNumber, $request->input('bank_code')); - $paymentReference = $billPlzBill->id; - } - - if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET){ - /** @var Wallet $wallet */ - $wallet = $booking->company->wallets()->first(); - - if((float) number_format(($wallet->amount - $amount),2) < 0){ - throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.'); - } - - $transaction_object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], ''); - $transaction = $this->createsTransaction->execute($wallet, $transaction_object); - - $paymentReference = $billNumber; - - $walletBalance = $this->recalculatesWalletBalance->execute($wallet); - $this->updatesWalletBalance->execute($wallet, $walletBalance); - } - - $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); - - $object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, - $configurations->getConfigurations()->getBankId(), $configurations->getConversionObject()->getPaymentMethod(), - $configurations->getTotal(), $configurations->getForeignTotal(), 1, - $configurations->getConversionObject()->getCurrencyId(), $configurations->getConfigurations()->getRate(), - $configurations->getTax(), $configurations->getServiceCharge(), Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_SUBMISSION, [], $paymentReference); + $object = $this->createTransactionObject($booking, $configurations, $billNumber, $paymentReference); /** @var Transaction $transaction */ $transaction = $this->createsTransaction->execute($booking, $object); -// $cash_back_transaction = $this->createCashBackTransactionProcessor->execute($transaction); - if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET){ - $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED); + if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){ + $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::PENDING_VERIFICATION); } - //Voucherify + $this->handleVoucherCode($request, $booking, $configurations, $transaction); + + return $this->resourceResponse(new TransactionResource($transaction)); + } + + private function createConversionObject(Request $request, Booking $booking): CurrencyConversionObject + { + return new CurrencyConversionObject( + floatval(str_replace(',', '', $request->input('amount'))), + $booking->convertible_currency_id, + $booking->service_id, + $booking->fix_currency_id === 1 ? 0:1, + PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] + ); + } + + private function validatePayment(Request $request, Booking $booking, CurrencyConversionObject $conversionObject): void + { + $outstanding = $this->calculatesBookingOutstanding->execute($booking); + + if($conversionObject->getAmount() > round($outstanding, 2)) + throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.'); + } + + private function handlePaymentMethods(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): ?string + { + $paymentReference = null; + + if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY) + $paymentReference = $this->handlePaymentGateway($request, $booking, $configurations, $billNumber); + + if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::WALLET) + $paymentReference = $this->handleWalletPayment($request, $booking, $configurations, $billNumber); + + return $paymentReference; + } + + private function handlePaymentGateway(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): string + { + $billPlzBill = $this->createsBillplzBill->execute( + $booking->company->name, + $request->user()->email, + 'This payment is made for transfer ref. '.$booking->marking, + $configurations->getTotal(), + $billNumber, + $request->input('bank_code') + ); + + return $billPlzBill->id; + } + + private function handleWalletPayment(Request $request, Booking $booking, CalculationObject $configurations, string $billNumber): string + { + $wallet = $booking->company->wallets()->first(); + + $amount = $configurations->getTotal(); + if((float) number_format(($wallet->amount - $amount),2) < 0) + throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.'); + + $transactionObject = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], ''); + $this->createsTransaction->execute($wallet, $transactionObject); + + $walletBalance = $this->recalculatesWalletBalance->execute($wallet); + $this->updatesWalletBalance->execute($wallet, $walletBalance); + + return $billNumber; + } + + private function createTransactionObject(Booking $booking, CalculationObject $configurations, string $billNumber, ?string $paymentReference): TransactionObject + { + return new TransactionObject( + $billNumber, + TransactionType::PAYMENT, + 1, + $booking->company->id, + $configurations->getConfigurations()->getBankId(), + $configurations->getConversionObject()->getPaymentMethod(), + $configurations->getTotal(), + $configurations->getForeignTotal(), + 1, + $configurations->getConversionObject()->getCurrencyId(), + $configurations->getConfigurations()->getRate(), + $configurations->getTax(), + $configurations->getServiceCharge(), + Carbon::now()->addMinutes($this->fetchesCompanyPaymentAttemptLimit->execute($booking->company)), + ApprovalStatus::PENDING_SUBMISSION, + [], + $paymentReference + ); + } + + private function handleVoucherCode(Request $request, Booking $booking, CalculationObject $configurations, Transaction $transaction): void + { + $voucherCode = $request->input('voucher_code'); if($voucherCode){ $employee = $booking->company->employees()->first(); $result = $this->redeemsVoucher->execute($voucherCode, $configurations->getSubTotal(), $employee); $voucher = $result->voucher; $redemptionId = $result->id; - //Update Database - $voucherValue = 0.00; - if(isset($voucher->discount->amount_off)){ - $voucherValue =$voucher->discount->amount_off; - } - if($voucherValue == 0.00){ - if(isset($voucher->discount->percent_off)){ - $voucherValue =$voucher->discount->percent_off; - } - } - $voucherObject= new VoucherObject($voucher->code, isset($voucher->metadata->name) ? $voucher->metadata->name : "", $voucher->discount->type, $voucherValue); + $voucherValue = $voucher->discount->amount_off ?? ($voucher->discount->percent_off ?? 0.00); + $voucherObject = new VoucherObject($voucher->code, $voucher->metadata->name ?? "", $voucher->discount->type, $voucherValue); $voucher = $this->createsVoucher->execute($voucherObject); - if(!$voucher) $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]); - $this->createsVoucherRedemption->execute($transaction, $voucher, $redemptionId, $configurations->getVoucherDiscountAmount()); + + if(!$voucher) + $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]); + + $this->createsVoucherRedemption->execute($redemptionId, $voucher->id, $employee->id, $configurations->getVoucherDiscountAmount()); } - - return $this->resourceResponse(new TransactionResource($transaction)); } - - } +