'Update Purchase Order With Booking Amount Update', 'message' => 'You have successfully updated booking amount' ]; } /** @var FetchesBooking */ private $fetchesBooking; /** @var UpdatesBookingFixedAmount */ private $updatesBookingFixedAmount; /** * UpdateBookingAmountWithPOLogic constructor. * @param FetchesBooking $fetchesBooking * @param UpdatesBookingFixedAmount $updatesBookingFixedAmount */ public function __construct(FetchesBooking $fetchesBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount) { $this->fetchesBooking = $fetchesBooking; $this->updatesBookingFixedAmount = $updatesBookingFixedAmount; } /** * @param Request $request * @param string $id * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\CriteriaNotFulfilledException */ public function logic(Request $request, $id = '') : JsonResponse { /** @var Booking $booking */ $booking = $this->fetchesBooking->execute(['id' => $request->route('id') ?? $id]); $bookingAttribute = $booking->attributesKVP()->where('key', "BOOKING_AMOUNT_UPDATE")->first(); if($bookingAttribute){ $total = collect($request->input('products'))->sum(function($product){ return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price'])); }); $bookingAmountUpdate = (float)$bookingAttribute->value; $isTally = $total === $bookingAmountUpdate ? true : false; if(!$isTally){ throw new MalformedRequestException('Purchase Order total not tally with updated booking amount of ' . $bookingAmountUpdate); } $booking->transactions()->where('type', TransactionType::PROFORMA)->delete(); $booking = $this->updatesBookingFixedAmount->execute($booking, $bookingAmountUpdate); $request->merge(['is_privilleged_update' => true]); $bookingAttribute->delete(); } return $this->resourceResponse(new BookingResource($booking)); } }