'Recorded Booking Amount', 'message' => 'You have successfully recorded the Booking Amount to be updated' ]; } /** @var CanUpdateBooking */ private $canUpdateBooking; /** @var UpdatesBookingFixedAmount */ private $updatesBookingFixedAmount; /** @var FetchesBooking */ private $fetchesBooking; /** @var CalculatesBookingOutstanding */ private $calculatesBookingOutstanding; /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; /** @var CreatesKeyValuePair */ private $createsKeyValuePair; /** @var UpdatesKeyValuePair */ private $updatesKeyValuePair; /** * UpdateBookingAmountLogic constructor. * @param CanUpdateBooking $canUpdateBooking * @param UpdatesBookingFixedAmount $updatesBookingFixedAmount * @param FetchesBooking $fetchesBooking * @param CalculatesBookingOutstanding $calculatesBookingOutstanding * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreatesKeyValuePair $createsKeyValuePair * @param UpdatesKeyValuePair $updatesKeyValuePair */ public function __construct(CanUpdateBooking $canUpdateBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount, FetchesBooking $fetchesBooking, CalculatesBookingOutstanding $calculatesBookingOutstanding, UpdatesTransactionStatus $updatesTransactionStatus, CreatesKeyValuePair $createsKeyValuePair, UpdatesKeyValuePair $updatesKeyValuePair) { $this->canUpdateBooking = $canUpdateBooking; $this->updatesBookingFixedAmount = $updatesBookingFixedAmount; $this->fetchesBooking = $fetchesBooking; $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createsKeyValuePair = $createsKeyValuePair; $this->updatesKeyValuePair = $updatesKeyValuePair; } /** * @param Request $request * @return JsonResponse * @throws MalformedRequestException */ public function logic(Request $request) : JsonResponse { $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]); $input_amount = number_format( floatval(str_replace(',', '', $request->input('amount_to_edit', $booking->fix_amount))), 5, '.', ''); $minimum_amount = $booking->fix_amount - $this->calculatesBookingOutstanding->execute($booking); if (((float)$input_amount + 0.01) < (float)$minimum_amount) { throw new MalformedRequestException('Booking Amount cannot be less than '. $minimum_amount .'.'); } $key = "BOOKING_AMOUNT_UPDATE"; $keyValuePairObject = new KeyValuePairObject($key, $input_amount); $metadata = $booking->attributesKVP()->where('key', $key)->first(); if($metadata){ $this->updatesKeyValuePair->execute($metadata, $keyValuePairObject); } else{ $this->createsKeyValuePair->execute($booking, $keyValuePairObject); } $poTransaction = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first(); // $booking->transactions()->where('type', TransactionType::PROFORMA)->delete(); if($poTransaction) { $this->updatesTransactionStatus->execute($poTransaction, ApprovalStatus::PENDING_SUBMISSION); } // $booking = $this->updatesBookingFixedAmount->execute($booking, $input_amount); return $this->resourceResponse(new BookingResource($booking)); } }