public function determineWorkOrders(Request $request) { if ($request->input('productsToFulfill') && is_array($request->input('productsToFulfill'))) { try { \DB::beginTransaction(); // Determine work orders (if any for these PO products $workOrdersToCreate = $this->workOrderSchedulerService->determineWorkOrdersForPo($request->input('productsToFulfill')); if ($request->input('purchaseOrderId')) { // This is on the detail page, instead of just previewing // the PO products and the workorders actually create them now. $po = PurchaseOrder::where('id', $request->input('purchaseOrderId'))->first(); foreach ($request->input('productsToFulfill') as $p) { PurchaseOrderProduct::create(['purchase_order_id' => $po->id, 'product_id' => $p['product_id'], 'quantity' => $p['quantity']]); // Deduct the quantity ordered from the current stock for the product $this->workOrderSchedulerService->deductStockFromProducts([$p]); } if ($workOrdersToCreate['workOrdersToCreate'] > 0) { if (isset($po)) { // Create any work orders for the PO product $this->workOrderSchedulerService->generateWorkOrdersForPo($po, $workOrdersToCreate['workOrders']); } } } \DB::commit(); return response()->json($workOrdersToCreate); } catch (\Exception $ex) { \DB::rollBack(); throw $ex; } } }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $purchaseOrder = PurchaseOrder::where('id', $id)->first(); if (isset($purchaseOrder)) { try { $workOrderScheduleService = new WorkOrderSchedulerService(); \DB::beginTransaction(); // Restore stock for any non workorder quantities $workOrderScheduleService->restoreStockForProducts($purchaseOrder->id); // Delete any work orders for this PO $workOrderScheduleService->deleteWorkOrdersForPo($purchaseOrder->id); // Now delete the PO itself $purchaseOrder->delete(); \DB::commit(); } catch (\Exception $ex) { \DB::rollBack(); throw $ex; } } }