'Delete Group Transaction', 'message' => 'You have successfully deleted this Group Transaction' ]; } /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; /** @var FetchesGroup */ private $fetchesGroup; /** @var DeletesTransaction */ private $deletesTransaction; /** @var UpdateRefundTransactionStatusProcessor */ private $updateRefundTransactionStatusProcessor; /** * DeleteGroupLogic constructor. * @param updatesTransactionStatus $updatesTransactionStatus * @param FetchesGroup $fetchesGroup * @param DeletesTransaction $deletesTransaction * @param UpdateRefundTransactionStatusProcessor $updateRefundTransactionStatusProcessor */ public function __construct(updatesTransactionStatus $updatesTransactionStatus, FetchesGroup $fetchesGroup, DeletesTransaction $deletesTransaction, UpdateRefundTransactionStatusProcessor $updateRefundTransactionStatusProcessor) { $this->updatesTransactionStatus = $updatesTransactionStatus; $this->fetchesGroup = $fetchesGroup; $this->deletesTransaction = $deletesTransaction; $this->updateRefundTransactionStatusProcessor = $updateRefundTransactionStatusProcessor; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request) : JsonResponse { $group = $this->fetchesGroup->execute(['id' => $request->route('id')]); $items = $group->transactions()->get(); foreach($items as $item) { $bill = $item; $payment = $bill->owner; $group->transactions()->detach($bill->id); $trns = $payment->transactions()->where('type', TransactionType::BILL)->with('groupTransaction')->get(); if(count($trns) <= 1){ $this->updatesTransactionStatus->execute($payment, ApprovalStatus::APPROVED); } $this->deletesTransaction->execute($bill); $this->checkForRefund($payment); } $group->delete(); return $this->resourceResponse(new GroupResource($group)); } //Check for refund if it is still pending, auto reject immediately private function checkForRefund($payment){ $refunds = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION])->get(); foreach($refunds as $refund) { $this->updateRefundTransactionStatusProcessor->execute($refund->id, ApprovalStatus::REJECTED); } } }