public function store(Request $request) { try { $counter = $request->input('counter'); ItemOut::create($request->all()); $data = ItemOut::orderBy('created_at', 'desc')->first(); echo $counter; for ($i = 0; $i < $counter; $i++) { $qty = $request->input('qty' . strval($i)); $itemId = $request->input('item_id' . strval($i)); $isItemAvailable = Item::where('id', 'like', '%' . $itemId . '%')->first(); // Item::findOrFail($itemId); if (is_null($isItemAvailable)) { ItemOut::destroy($data->id); return redirect('itemout')->with('message', 'Data dengan kode barang: ' . $itemId . ', tidak ada'); } else { DetailItemOut::create(['qty' => $qty, 'item_id' => $itemId, 'item_out_id' => $data->id]); Item::decreaseStock($itemId, $qty); } } return redirect('itemout')->with('message', 'Data berhasil dibuat!'); } catch (\Illuminate\Database\QueryException $e) { return redirect('itemout')->with('message', 'Data dengan email tersebut sudah digunakan!'); } catch (\PDOException $e) { return redirect('itemout')->with('message', 'Data dengan email tersebut sudah digunakan!'); } }
public function store(Request $request) { try { $counter = $request->input('counter'); $transactionDate = $request->input('date'); Transaction::create($request->all()); $data = Transaction::orderBy('created_at', 'desc')->first(); ItemOut::create(['date' => $transactionDate, 'description' => 'kode transaksi ' . $data->id]); $dataOut = ItemOut::orderBy('created_at', 'desc')->first(); $total = 0; for ($i = 0; $i < $counter; $i++) { echo "counter: " . $counter . "/"; $qty = $request->input('qty' . strval($i)); $priceId = $request->input('price_id' . strval($i)); $subtotal = $request->input('subtotal' . strval($i)); $total = $total + $subtotal; $price = Price::where('id', '=', $priceId)->first(); $isItemAvailable = Item::where('id', '=', $price->item_id)->first(); if (is_null($isItemAvailable)) { Transaction::destroy($data->id); return redirect('transaction')->with('message', 'Data dengan kode barang: ' . $price->item_id . ', tidak ada'); } else { DetailTransaction::create(['qty' => $qty, 'price_id' => $priceId, 'transaction_id' => $data->id, 'subtotal' => $subtotal]); DetailItemOut::create(['qty' => $qty, 'item_id' => $price->item_id, 'item_out_id' => $dataOut->id]); Item::decreaseStock($price->item_id, $qty); } } $updateTransaction = Transaction::where('id', '=', $data->id)->first(); $updateTransaction->total_price = $total; $updateTransaction->save(); return redirect('transaction')->with('message', 'Data berhasil dibuat!'); } catch (\Illuminate\Database\QueryException $e) { return redirect('transaction')->with('message', 'Data dengan transaksi tersebut sudah digunakan!'); } catch (\PDOException $e) { return redirect('transaction')->with('message', 'Data dengan transaksi tersebut sudah digunakan!'); } }