public function saveProduk(Request $request) { $session = $request->session(); // Check Stok $produkId = $request->get('id'); $qty = $request->get('qty'); $produk = Produk::with('detail')->find($produkId); $denied = false; // allow transaction with >=0 stok if ($produk->detail->count()) { $tempBahan = []; foreach ($produk->detail as $pd) { $bId = $pd['bahan_id']; $tempBahan[$bId] = $pd['qty'] * $qty; } $bahans = \App\StokBahan::whereIn('bahan_id', array_keys($tempBahan))->get(); foreach ($bahans as $bahan) { $bId = $bahan->bahan_id; if ($bahan->stok < $tempBahan[$bId]) { $denied = true; } } } else { $produk = \App\StokProduk::where('produk_id', $produkId)->first(); if ($produk->stok < $qty) { $denied = true; } } if (!$denied) { $saveSession = $request->only(['id', 'qty', 'harga', 'note']); // id as produk_id $session->put('data_order.' . $request->get('id'), $saveSession); return ['num' => $request->get('num'), 'nama' => $request->get('nama'), 'harga' => number_format($request->get('harga'), 0, ',', '.'), 'qty' => $request->get('qty'), 'subtotal' => number_format($request->get('harga') * $request->get('qty'), 0, ',', '.'), 'note' => $request->get('note'), 'action' => $request->get('action')]; } else { return 0; } }
public function checkStok(Request $request) { \Debugbar::disable(); //return 1; // allow transaction with >=0 stok $produkId = $request->get('id'); $qty = $request->get('qty') ? $request->get('qty') : 1; $produk = Produk::with('detail')->where('active', 1)->where('id', $produkId)->first(); $denied = false; if ($produk->detail->count()) { $tempBahan = []; foreach ($produk->detail as $pd) { $bId = $pd['bahan_id']; $tempBahan[$bId] = $pd['qty'] * $qty; } $bahans = \App\StokBahan::whereIn('bahan_id', array_keys($tempBahan))->get(); foreach ($bahans as $bahan) { $bId = $bahan->bahan_id; if ($bahan->stok < $tempBahan[$bId]) { $denied = true; } } } else { $produk = \App\StokProduk::where('produk_id', $produkId)->first(); if ($produk->stok < $qty) { $denied = true; } } if (!$denied) { return 1; } return 0; }