'Regenerate Booking Invoice', 'message' => 'You have successfully regenerate booking invoice' ]; } /** @var CanFetchBooking */ private $canFetchBooking; /** @var FetchesBooking */ private $fetchesBooking; /** @var DeletesTransaction */ private $deletesTransaction; /** @var UpdatesBookingStatus */ private $updatesBookingStatus; /** @var DeletesDocument */ private $deletesDocument; /** @var CreateInvoiceTransactionProcessor */ private $createInvoiceTransactionProcessor; /** * FetchBookingLogic constructor. * @param CanFetchBooking $canFetchBooking * @param FetchesBooking $fetchesBooking * @param DeletesTransaction $deletesTransaction * @param UpdatesBookingStatus $updatesBookingStatus * @param DeletesDocument $deletesDocument * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor */ public function __construct( CanFetchBooking $canFetchBooking, FetchesBooking $fetchesBooking, DeletesTransaction $deletesTransaction, UpdatesBookingStatus $updatesBookingStatus, DeletesDocument $deletesDocument, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor ) { $this->canFetchBooking = $canFetchBooking; $this->fetchesBooking = $fetchesBooking; $this->deletesTransaction = $deletesTransaction; $this->updatesBookingStatus = $updatesBookingStatus; $this->deletesDocument = $deletesDocument; $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\RequestValidationException */ public function logic(Request $request) : JsonResponse { $this->canFetchBooking->passes(); $booking = $this->fetchesBooking->execute([ 'id' => $request->route('id'), 'status' => ApprovalStatus::COMPLETED, 'with_transactions' => true] ); $this->updatesBookingStatus->execute($booking, ApprovalStatus::APPROVED); $transaction = $booking->transactions()->whereIn('type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->get(); foreach ($transaction as $key => $row) { $this->deletesTransaction->execute($row); } $document = $booking->documents()->whereIn('document_type', [DocumentType::PURCHASE_ORDER, DocumentType::INVOICE, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->get(); foreach ($document as $key => $row) { $this->deletesDocument->execute($row); } $this->createInvoiceTransactionProcessor->execute($booking); return $this->resourceResponse(new BookingResource($booking)); } }