/**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $oldStok = StokProduk::all();
     $oldStokKey = array_column($oldStok->toArray(), 'produk_id');
     $produks = Produk::stok()->orderBy('produks.id')->get();
     foreach ($produks as $produk) {
         if (in_array($produk->id, $oldStokKey)) {
             StokProduk::where('produk_id', $produk->id)->update(['stok' => $produk->sisa_stok]);
         } else {
             StokProduk::create(['produk_id' => $produk->id, 'stok' => $produk->sisa_stok]);
         }
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(ProdukRequest $request)
 {
     //return json_decode($request->get('produk_details'));
     $produkData = $request->only(['nama', 'satuan', 'satuan_beli', 'supplier_id', 'harga', 'use_mark_up', 'mark_up', 'produk_kategori_id', 'qty_warning']);
     $produk_details = json_decode($request->get('produk_details'));
     $produkData['hpp'] = count($produk_details) == 0 ? $request->get('hpp') : 0;
     $produkData['harga'] = $request->get('use_mark_up') == 'Tidak' ? $request->get('harga') : 0;
     $produkData['konsinyasi'] = !$request->get('konsinyasi') ? 'Tidak' : 'Ya';
     $produk = Produk::create($produkData);
     $produkDetailData = [];
     foreach ($produk_details as $pd) {
         array_push($produkDetailData, ['produk_id' => $produk->id, 'bahan_id' => $pd->bahan_id, 'qty' => $pd->qty]);
     }
     if (!count($produkDetailData)) {
         StokProduk::create(['produk_id' => $produk->id, 'stok' => 0]);
     }
     if (ProdukDetail::insert($produkDetailData)) {
         return redirect('/produk')->with('succcess', 'Sukses simpan data produk.');
     }
     return redirect()->back()->withErrors(['failed' => 'Gagal simpan data produk.']);
 }