public function ajaxLoad(Request $request)
 {
     if ($request->get('id')) {
         $produk = Produk::with(['detail' => function ($query) {
             $query->join('bahans', 'produk_details.bahan_id', '=', 'bahans.id');
         }])->where('active', 1)->where('id', $request->get('id'))->first();
         return ['id' => $produk->id, 'nama' => $produk->nama, 'satuan' => $produk->satuan, 'harga' => CountPrice($produk)];
     } else {
         $produk = Produk::leftJoin('produk_details', 'produks.id', '=', 'produk_details.produk_id')->where('nama', 'like', '%' . $request->get('q') . '%')->where('produks.active', 1)->whereNotIn('produks.id', explode('+', $request->get('except')))->select('produks.*')->groupBy('produks.id')->limit($request->get('page'));
         $produk = $request->get('without_has_bahan') == 'Ya' ? $produk->whereNull('produk_details.id') : $produk;
         $produk = $produk->get();
         return $produk;
     }
 }
 public function getProduk(Request $request)
 {
     $produks = Produk::with(['detail' => function ($query) {
         $query->join('bahans', 'produk_details.bahan_id', '=', 'bahans.id');
     }])->where('active', 1)->where('nama', 'like', '%' . $request->get('q') . '%')->get();
     $data = [];
     foreach ($produks as $produk) {
         array_push($data, ['id' => $produk->id, 'nama' => $produk->nama, 'satuan' => $produk->satuan, 'harga' => CountPrice($produk)]);
     }
     return $data;
 }
 public function changeTransaksi(Request $request)
 {
     \Debugbar::disable();
     $id = $request->get('id');
     $data_order_detail = $request->get('data_order') != "" ? json_decode($request->get('data_order'), true) : [];
     // Convert like data session
     $temp = [];
     foreach ($data_order_detail as $d) {
         $key = $d['id'];
         $temp[$key] = $d;
     }
     $data_order_detail = $temp;
     $order = \App\Order::with('place.place')->find($id);
     if (count($data_order_detail)) {
         // Order Detail
         $orderDetailOld = \App\OrderDetail::where('order_id', $id)->whereIn('produk_id', array_keys($data_order_detail))->get();
         # Update Order Detail
         foreach ($orderDetailOld as $odo) {
             $oldQty = $odo->qty;
             $updateQty = $oldQty + $data_order_detail[$odo->produk_id]['qty'];
             $updatePrice = $data_order_detail[$odo->produk_id]['harga'];
             \App\OrderDetail::find($odo->id)->update(['qty' => $updateQty, 'harga_jual' => $updatePrice]);
             unset($data_order_detail[$odo->produk_id]);
         }
         if (count($data_order_detail)) {
             # New Order Detail
             $produks = Produk::with(['detail' => function ($query) {
                 $query->join('bahans', 'produk_details.bahan_id', '=', 'bahans.id');
             }])->whereIn('id', array_keys($data_order_detail))->get();
             $orderDetailBahan = [];
             foreach ($produks as $produk) {
                 $pId = $produk->id;
                 // Order Detail
                 $orderDetail = ['order_id' => $id, 'produk_id' => $produk->id, 'hpp' => CountHpp($produk), 'harga_jual' => $data_order_detail[$pId]['harga'], 'qty' => $data_order_detail[$pId]['qty'], 'use_mark_up' => $produk->use_mark_up, 'mark_up' => $produk->mark_up, 'note' => ""];
                 //echo "<pre>", print_r($orderDetail), "</pre>";
                 $orderDetail = \App\OrderDetail::create($orderDetail);
                 if ($produk->detail->count()) {
                     // Order Detail Bahan
                     foreach ($produk->detail as $pd) {
                         array_push($orderDetailBahan, ['order_detail_id' => $orderDetail->id, 'bahan_id' => $pd->bahan_id, 'harga' => $pd->harga, 'qty' => $pd->qty, 'satuan' => $pd->satuan]);
                     }
                 }
             }
             \App\OrderDetailBahan::insert($orderDetailBahan);
         }
     }
     Artisan::call('bahan:count');
     Artisan::call('produk:count');
     return 1;
 }