public function BahanStok()
 {
     // Bahan stok dibawah ambang batas stok
     $bahanStokWarning = Bahan::join('stok_bahans', 'bahans.id', '=', 'stok_bahans.bahan_id')->where('bahans.active', 1)->select(['bahans.*', 'stok_bahans.stok'])->get()->filter(function ($item) {
         return $item->stok < $item->qty_warning;
     });
     $data = ['data' => $bahanStokWarning, 'header' => 'Bahan'];
     return view(config('app.template') . '.dashboard.stok', $data);
 }
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     MutasiStokBahan::where('tanggal', $this->tanggal)->delete();
     $bahans = \App\Bahan::MutasiStok($this->tanggal);
     $inserts = [];
     foreach ($bahans as $bahan) {
         array_push($inserts, ['bahan_id' => $bahan['id'], 'before' => $bahan['before'], 'pembelian' => $bahan['pembelian'], 'penjualan' => $bahan['penjualan'], 'adjustment_increase' => $bahan['adjustment_increase'], 'adjustment_reduction' => $bahan['adjustment_reduction'], 'sisa' => $bahan['sisa'], 'tanggal' => $this->tanggal]);
     }
     MutasiStokBahan::insert($inserts);
 }
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $oldStok = StokBahan::all();
     $oldStokKey = array_column($oldStok->toArray(), 'bahan_id');
     $bahans = Bahan::stok()->orderBy('bahans.id')->get();
     foreach ($bahans as $bahan) {
         if (in_array($bahan->id, $oldStokKey)) {
             StokBahan::where('bahan_id', $bahan->id)->update(['stok' => $bahan->sisa_stok]);
         } else {
             StokBahan::create(['bahan_id' => $bahan->id, 'stok' => $bahan->sisa_stok]);
         }
     }
 }
 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.');
 }
 protected function _stokBahanPertahun(Request $request)
 {
     $tahun = $request->get('tahun') ? $request->get('tahun') : date('Y');
     $start = Carbon::parse('first day of January ' . $tahun);
     $end = Carbon::parse('last day of December ' . $tahun);
     $bahans = \App\Bahan::MutasiStok($start->format('Y-m-d'), $end->format('Y-m-d'));
     return ['tanggal' => Carbon::createFromFormat('Y', $tahun), 'bahans' => $bahans];
 }
 public function showTest(Request $request)
 {
     $action = $request->get('act') ? $request->get('act') : 'bahan';
     $action = strtolower($action);
     if ($action == 'produk') {
         return \App\Produk::stok()->orderBy('produks.id')->get();
     } elseif ($action == 'bahan') {
         return \App\Bahan::stok()->orderBy('bahans.id')->get();
     } else {
         abort(404);
     }
 }
 public function ajaxLoad(Request $request)
 {
     if ($request->get('id')) {
         return Bahan::where('active', 1)->where('id', $request->get('id'))->first();
     } else {
         return Bahan::where('nama', 'like', '%' . $request->get('q') . '%')->where('active', 1)->whereNotIn('id', explode('+', $request->get('except')))->limit($request->get('page'))->get();
     }
 }