'Update Purchase Order', 'message' => 'You have successfully updated you booking\'s purchase order' ]; } /** @var FetchesBooking */ private $fetchesBooking; /** @var GeneratesTransactionBillNumber */ private $generatesTransactionBillNumber; /** @var CreatePurchaseOrderTransactionProcessor */ private $createPurchaseOrderTransactionProcessor; /** * CreatePurchaseOrderTransactionLogic constructor. * @param FetchesBooking $fetchesBooking * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber * @param CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor */ public function __construct(FetchesBooking $fetchesBooking, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor) { $this->fetchesBooking = $fetchesBooking; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createPurchaseOrderTransactionProcessor = $createPurchaseOrderTransactionProcessor; } /** * @param Request $request * @param string $id * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request, $id = '') : JsonResponse { /** @var Booking $booking */ $booking = $this->fetchesBooking->execute(['id' => $request->route('id') ?? $id]); $billNumber = $this->generatesTransactionBillNumber->execute('PO-'); $products = $request->input('products'); if ($request->input('pushToExisting')) { $transaction = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first(); if ($transaction) { foreach ($transaction->transactionDetails as $detail) { array_push($products, [ 'stockCode' => $detail->product_code, 'description' => $detail->product_name, 'quantity' => $detail->quantity, 'unit_price' => $detail->price, 'total' => $detail->amount ]); } } } $total = collect($products)->sum(function($product){ return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price'])); }); $object = new TransactionObject($billNumber, TransactionType::PURCHASE_ORDER, $booking->company->id, 1, 1, PaymentMethodType::CASH, $total, $total, $booking->fix_currency_id, $booking->fix_currency_id, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $products); $transaction = $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); return $this->resourceResponse(new TransactionResource($transaction)); } }