'Update Purchase Order With Booking Amount Update', 'message' => 'You have successfully updated booking amount' ]; } /** @var FetchesBooking */ private $fetchesBooking; /** @var UpdatesBookingFixedAmount */ private $updatesBookingFixedAmount; /** @var CalculatesBookingOutstanding */ private $calculatesBookingOutstanding; /** @var CalculatesBookingPayableAmount */ private $calculatesBookingPayableAmount; /** @var CalculatesBookingRefundAmount */ private $calculatesBookingRefundAmount; /** * UpdateBookingAmountWithPOLogic constructor. * @param FetchesBooking $fetchesBooking * @param UpdatesBookingFixedAmount $updatesBookingFixedAmount * @param CalculatesBookingOutstanding $calculatesBookingOutstanding * @param CalculatesBookingPayableAmount $calculatesBookingPayableAmount * @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount */ public function __construct(FetchesBooking $fetchesBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount, CalculatesBookingOutstanding $calculatesBookingOutstanding, CalculatesBookingPayableAmount $calculatesBookingPayableAmount, CalculatesBookingRefundAmount $calculatesBookingRefundAmount) { $this->fetchesBooking = $fetchesBooking; $this->updatesBookingFixedAmount = $updatesBookingFixedAmount; $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; $this->calculatesBookingPayableAmount = $calculatesBookingPayableAmount; $this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount; } /** * @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]); $outstandingAmount = $this->calculatesBookingOutstanding->execute($booking); $bookingAttribute = $booking->attributesKVP()->where('key', "BOOKING_AMOUNT_UPDATE")->first(); // cief todo: 90 - BEFORE REVERT $refundAmount = $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id); $paidAmount = floatval($this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id)) - floatval($refundAmount); if($paidAmount > 0 && $outstandingAmount > 0 && !$bookingAttribute){ if($refundAmount != $outstandingAmount){ throw new MalformedRequestException('Purchase Order at this point can only be edited after editing the booking amount'); } } // // cief todo: 90 - REVERTED STARTS // $paidAmount = floatval($this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id)) - floatval($this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id)); // if($paidAmount > 0 && $outstandingAmount > 0 && !$bookingAttribute){ // throw new MalformedRequestException('Purchase Order at this point can only be edited after editing the booking amount'); // } // cief todo: 90 - REVERTED ENDS 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; $isTally = bccomp($total, $bookingAmountUpdate, 3) === 0; 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)); } }