示例#1
0
 public function report()
 {
     $from = \Request::get('from');
     $to = \Request::get('to');
     $itemOuts = ItemOut::whereDate('date', '>=', date($from))->whereDate('date', '<=', date($to))->orderBy('id', 'DESC');
     return view('itemout.report', compact('itemOuts', 'from', 'to'));
 }
 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!');
     }
 }