/**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     if (Gate::denies('produk.delete')) {
         return view(config('app.template') . '.error.403');
     }
     $produk = Produk::find($id);
     if ($produk && $produk->update(['active' => 0])) {
         ProdukDetail::where('produk_id', $id)->delete();
         return redirect()->back()->with('succcess', 'Sukses hapus data produk "' . $produk->nama . '".');
     }
     return redirect()->back()->withErrors(['failed' => 'Gagal hapus data produk.']);
 }
 public function store(Request $request)
 {
     $denied = false;
     if (!$request->session()->has('data_pembelian')) {
         $denied = true;
     } else {
         $beliBahan = $request->session()->has('data_pembelian.bahan') ? $request->session()->get('data_pembelian.bahan') : [];
         $beliProduk = $request->session()->has('data_pembelian.produk') ? $request->session()->get('data_pembelian.produk') : [];
         if (empty($beliBahan) && empty($beliProduk)) {
             $denied = true;
         }
     }
     if ($denied) {
         return redirect('pembelian/add')->withInput()->withErrors(['no_details' => 'Tidak ada barang yang dibeli.']);
     }
     // Pembelian
     $karyawan_id = Auth::check() ? Auth::user()->karyawan->id : '1';
     $input = $request->only(['supplier_id', 'tanggal']) + ['karyawan_id' => $karyawan_id];
     $pembelian = Pembelian::create($input);
     // Pembelian Bayar
     if ($request->get('bayar') != "0") {
         $input = ['pembelian_id' => $pembelian->id, 'nominal' => $request->get('bayar'), 'karyawan_id' => $karyawan_id, 'tanggal' => $request->get('tanggal')];
         PembelianBayar::create($input);
         // Update Purchase Account
         Artisan::call('purchase:count', ['tanggal' => $pembelian->tanggal->format('Y-m-d')]);
     }
     # Update [Bahan => Harga, Produk => HPP]
     // Bahan
     if (!empty($beliBahan)) {
         $keys = array_keys($beliBahan);
         $bahans = \App\StokBahan::with(['bahan'])->whereIn('bahan_id', $keys)->orderBy('bahan_id')->get();
         foreach ($bahans as $bahan) {
             $bId = $bahan->bahan_id;
             $inStok = $beliBahan[$bId]['stok'];
             $inHarga = $beliBahan[$bId]['harga'] / $inStok;
             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) {
                     //echo "<pre>", print_r(['id' => $bId, 'nama' => $bahan->nama, 'harga' => $harga]), "</pre>";
                     \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' => "Pembelian #" . $pembelian->id]);
                 }
             }
         }
         Artisan::call('bahan:count');
     }
     // Produk, Harga => HPP
     if (!empty($beliProduk)) {
         $keys = array_keys($beliProduk);
         $produks = \App\StokProduk::with(['produk'])->whereIn('produk_id', $keys)->orderBy('produk_id')->get();
         foreach ($produks as $produk) {
             $pId = $produk->produk_id;
             $inStok = $beliProduk[$pId]['stok'];
             $inHarga = $beliProduk[$pId]['harga'] / $inStok;
             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) {
                     //echo "<pre>", print_r(['id' => $pId, 'nama' => $produk->nama, 'harga' => $harga]), "</pre>";
                     \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' => "Pembelian #" . $pembelian->id]);
                 }
             }
         }
         Artisan::call('produk:count');
     }
     // Pembelian Detail
     $details = array_merge($beliBahan, $beliProduk);
     $temp = [];
     foreach ($details as $detail) {
         array_push($temp, $detail + ['pembelian_id' => $pembelian->id]);
     }
     PembelianDetail::insert($temp);
     $request->session()->forget('data_pembelian');
     $request->session()->forget('info_pembelian');
     return redirect('/pembelian')->with('succcess', 'Sukses simpan data pembelian.');
 }
 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.');
 }