'Purchase Order Approval', 'message' => 'You have successfully updated the Purchase order status' ]; } /** @var CreatePurchaseOrderTransactionLogic */ private $createPurchaseOrderTransactionLogic; /** * AutoPackingListFillLogic constructor. * @param CreatePurchaseOrderTransactionLogic $createPurchaseOrderTransactionLogic */ public function __construct(CreatePurchaseOrderTransactionLogic $createPurchaseOrderTransactionLogic) { $this->createPurchaseOrderTransactionLogic = $createPurchaseOrderTransactionLogic; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request) : JsonResponse { $booking = Booking::whereMonth('created_at', 7) ->whereYear('created_at', 2021) ->whereDoesntHave('transactions', function($q){ $q->where('type', TransactionType::PURCHASE_ORDER); $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); })->get(); foreach ($booking as $key => $row) { $amount = $row->fix_amount; $other_transaction = Transaction:: where('type', TransactionType::PURCHASE_ORDER) ->where('status', ApprovalStatus::APPROVED) ->where('amount', '<=', $amount) ->where('issuer', $row->company_id) ->first(); if (!$other_transaction) { $other_transaction = Transaction:: where('type', TransactionType::PURCHASE_ORDER) ->where('status', ApprovalStatus::APPROVED) ->where('amount', '<=', $amount) ->first(); } if ($other_transaction) { $other_transaction_detail = $other_transaction->transactionDetails()->get(); $other_amount = 0; foreach ($other_transaction_detail as $key_2 => $row_2) { if ($amount > $other_amount) { $other_amount += $row_2->amount; $products[] = [ 'description' => $row_2->product_name, 'quantity' => (int) $row_2->quantity, 'stockCode' => $row_2->product_code, 'total' => $row_2->amount, 'unit_price' => (int) $row_2->price, ]; } } $remain_amount = $amount - $other_amount; if ($remain_amount > 0) { $remain = [ 'description' => 'other service charge', 'quantity' => 1, 'stockCode' => 'OTHER_SERVICE_CHARGE', 'total' => $remain_amount, 'unit_price' => $remain_amount, ]; $products = array_merge([$remain], $products); } $request->request->add([ 'products' => $products ]); $create_transaction = $this->createPurchaseOrderTransactionLogic->logic($request, $row->id); } } return $this->response([]); } }