protected function _adjustmentPertahun(Request $request)
 {
     $tahun = $request->get('tahun') ? $request->get('tahun') : date('Y');
     // Produk
     # Increase
     $produk_increase = \App\AdjustmentDetail::join('adjustments', 'adjustment_details.adjustment_id', '=', 'adjustments.id')->join('produks', 'adjustment_details.relation_id', '=', 'produks.id')->where(DB::raw('SUBSTRING(adjustments.tanggal, 1, 4)'), $tahun)->where('adjustment_details.type', 'produk')->where('adjustment_details.state', 'increase')->groupBy('produks.id')->select(['produks.nama', 'adjustment_details.type', DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty)/SUM(adjustment_details.qty))harga_avg'), DB::raw('SUM(adjustment_details.qty)qty_stok'), DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty))subtotal'), 'produks.satuan'])->get();
     # Reduction
     $produk_reduction = \App\AdjustmentDetail::join('adjustments', 'adjustment_details.adjustment_id', '=', 'adjustments.id')->join('produks', 'adjustment_details.relation_id', '=', 'produks.id')->where(DB::raw('SUBSTRING(adjustments.tanggal, 1, 4)'), $tahun)->where('adjustment_details.type', 'produk')->where('adjustment_details.state', 'reduction')->groupBy('produks.id')->select(['produks.nama', 'adjustment_details.type', DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty)/SUM(adjustment_details.qty))harga_avg'), DB::raw('SUM(adjustment_details.qty)qty_stok'), DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty))subtotal'), 'produks.satuan'])->get();
     // Bahan
     # Increase
     $bahan_increase = \App\AdjustmentDetail::join('adjustments', 'adjustment_details.adjustment_id', '=', 'adjustments.id')->join('bahans', 'adjustment_details.relation_id', '=', 'bahans.id')->where(DB::raw('SUBSTRING(adjustments.tanggal, 1, 4)'), $tahun)->where('adjustment_details.type', 'bahan')->where('adjustment_details.state', 'increase')->groupBy('bahans.id')->select(['bahans.nama', 'adjustment_details.type', DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty)/SUM(adjustment_details.qty))harga_avg'), DB::raw('SUM(adjustment_details.qty)qty_stok'), DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty))subtotal'), 'bahans.satuan'])->get();
     # Reduction
     $bahan_reduction = \App\AdjustmentDetail::join('adjustments', 'adjustment_details.adjustment_id', '=', 'adjustments.id')->join('bahans', 'adjustment_details.relation_id', '=', 'bahans.id')->where(DB::raw('SUBSTRING(adjustments.tanggal, 1, 4)'), $tahun)->where('adjustment_details.type', 'bahan')->where('adjustment_details.state', 'reduction')->groupBy('bahans.id')->select(['bahans.nama', 'adjustment_details.type', DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty)/SUM(adjustment_details.qty))harga_avg'), DB::raw('SUM(adjustment_details.qty)qty_stok'), DB::raw('(SUM(adjustment_details.harga*adjustment_details.qty))subtotal'), 'bahans.satuan'])->get();
     return ['tanggal' => Carbon::createFromFormat('Y', $tahun), 'adjustments' => ['increase' => array_merge($produk_increase->toArray(), $bahan_increase->toArray()), 'reduction' => array_merge($produk_reduction->toArray(), $bahan_reduction->toArray())]];
 }
 protected static function _adjustmentReduction_Stok($tanggal1, $tanggal2)
 {
     return \App\AdjustmentDetail::join('produks', 'adjustment_details.relation_id', '=', 'produks.id')->join('adjustments', 'adjustment_details.adjustment_id', '=', 'adjustments.id')->whereBetween('adjustments.tanggal', [$tanggal1, $tanggal2])->where('type', 'produk')->where('state', 'reduction')->groupBy('produks.id')->select(['produks.id', 'produks.nama', DB::raw('SUM(adjustment_details.qty)qty')])->get()->toArray();
 }
 public function store(Request $request)
 {
     $denied = false;
     if (!$request->session()->has('data_adjustment')) {
         $denied = true;
     } else {
         // Reduction ( Pengurangan )
         $data_adjustment_reduction = $request->session()->get('data_adjustment.reduction');
         $data_adjustment_reduction_bahan = isset($data_adjustment_reduction['bahan']) ? $data_adjustment_reduction['bahan'] : [];
         $data_adjustment_reduction_produk = isset($data_adjustment_reduction['produk']) ? $data_adjustment_reduction['produk'] : [];
         // Increase ( Penambahan )
         $data_adjustment_increase = $request->session()->get('data_adjustment.increase');
         $data_adjustment_increase_bahan = isset($data_adjustment_increase['bahan']) ? $data_adjustment_increase['bahan'] : [];
         $data_adjustment_increase_produk = isset($data_adjustment_increase['produk']) ? $data_adjustment_increase['produk'] : [];
         if (empty($data_adjustment_reduction_bahan) && empty($data_adjustment_reduction_produk) && empty($data_adjustment_increase_bahan) && empty($data_adjustment_increase_produk)) {
             $denied = true;
         }
     }
     if ($denied) {
         return redirect('adjustment/add')->withInput()->withErrors(['no_details' => 'Tidak ada barang yang di adjustment.']);
     }
     // Adjustment
     $karyawan_id = Auth::check() ? Auth::user()->karyawan->id : '1';
     $input = $request->only(['keterangan', 'tanggal']) + ['karyawan_id' => $karyawan_id];
     $adjustment = Adjustment::create($input);
     # Update Data [Bahan => Harga, Produk => HPP]
     // Bahan
     if (!empty($data_adjustment_increase_bahan)) {
         $keys = array_keys($data_adjustment_increase_bahan);
         $bahans = \App\StokBahan::with(['bahan'])->whereIn('bahan_id', $keys)->orderBy('bahan_id')->get();
         foreach ($bahans as $bahan) {
             $bId = $bahan->bahan_id;
             $inStok = $data_adjustment_increase_bahan[$bId]['qty'];
             $inHarga = $data_adjustment_increase_bahan[$bId]['harga'];
             if ($bahan->bahan->harga != $inHarga) {
                 $oldTtl = $bahan->stok > 0 ? $bahan->bahan->harga * $bahan->stok : 0;
                 $inTtl = $inStok > 0 ? $inHarga * $inStok : 0;
                 $sumInOld = $oldTtl + $inTtl;
                 $qtyTotal = $bahan->stok + $inStok;
                 $harga = $sumInOld > 0 && $qtyTotal > 0 ? $sumInOld / $qtyTotal : 0;
                 if ($harga != $bahan->bahan->harga) {
                     \App\Bahan::find($bId)->update(['harga' => $harga]);
                     \App\AveragePriceAction::create(['type' => 'bahan', 'relation_id' => $bId, 'old_price' => $bahan->bahan->harga, 'old_stok' => $bahan->stok, 'input_price' => $inHarga, 'input_stok' => $inStok, 'average_with_round' => $harga, 'action' => "Adjustment Increase #" . $adjustment->id]);
                 }
             }
         }
     }
     // Produk, Harga => HPP
     if (!empty($data_adjustment_increase_produk)) {
         $keys = array_keys($data_adjustment_increase_produk);
         $produks = \App\StokProduk::with(['produk'])->whereIn('produk_id', $keys)->orderBy('produk_id')->get();
         foreach ($produks as $produk) {
             $pId = $produk->produk_id;
             $inStok = $data_adjustment_increase_produk[$pId]['qty'];
             $inHarga = $data_adjustment_increase_produk[$pId]['harga'];
             if ($produk->produk->hpp != $inHarga) {
                 $oldTtl = $produk->stok > 0 ? $produk->produk->hpp * $produk->stok : 0;
                 $inTtl = $inStok > 0 ? $inHarga * $inStok : 0;
                 $sumInOld = $oldTtl + $inTtl;
                 $qtyTotal = $produk->stok + $inStok;
                 $harga = $sumInOld > 0 && $qtyTotal > 0 ? $sumInOld / $qtyTotal : 0;
                 // HPP
                 if ($harga != $produk->produk->hpp) {
                     \App\Produk::find($pId)->update(['hpp' => $harga]);
                     \App\AveragePriceAction::create(['type' => 'produk', 'relation_id' => $pId, 'old_price' => $produk->produk->hpp, 'old_stok' => $produk->stok, 'input_price' => $inHarga, 'input_stok' => $inStok, 'average_with_round' => $harga, 'action' => "Adjustment Increase #" . $adjustment->id]);
                 }
             }
         }
     }
     // Adjustment Detail
     $data_adjustment = array_merge($data_adjustment_reduction_bahan, $data_adjustment_reduction_produk);
     $data_adjustment = array_merge($data_adjustment_increase_bahan, $data_adjustment);
     $data_adjustment = array_merge($data_adjustment_increase_produk, $data_adjustment);
     $details = [];
     foreach ($data_adjustment as $da) {
         $temp = $da;
         $temp['adjustment_id'] = $adjustment->id;
         array_push($details, $temp);
     }
     AdjustmentDetail::insert($details);
     Artisan::call('bahan:count');
     Artisan::call('produk:count');
     $request->session()->forget('data_adjustment');
     $request->session()->forget('info_adjustment');
     return redirect('/adjustment')->with('succcess', 'Sukses simpan adjustment bahan / produk.');
 }