/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(BahanRequest $request)
 {
     $bahan = Bahan::create($request->all());
     if ($bahan) {
         StokBahan::create(['bahan_id' => $bahan->id, 'stok' => 0]);
         return redirect('/bahan-produksi')->with('succcess', 'Sukses simpan data bahan produksi.');
     }
     return redirect()->back()->withErrors(['failed' => 'Gagal simpan data bahan produksi.']);
 }
 /**
  * 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 saveProduk(Request $request)
 {
     $session = $request->session();
     // Check Stok
     $produkId = $request->get('id');
     $qty = $request->get('qty');
     $produk = Produk::with('detail')->find($produkId);
     $denied = false;
     // allow transaction with >=0 stok
     if ($produk->detail->count()) {
         $tempBahan = [];
         foreach ($produk->detail as $pd) {
             $bId = $pd['bahan_id'];
             $tempBahan[$bId] = $pd['qty'] * $qty;
         }
         $bahans = \App\StokBahan::whereIn('bahan_id', array_keys($tempBahan))->get();
         foreach ($bahans as $bahan) {
             $bId = $bahan->bahan_id;
             if ($bahan->stok < $tempBahan[$bId]) {
                 $denied = true;
             }
         }
     } else {
         $produk = \App\StokProduk::where('produk_id', $produkId)->first();
         if ($produk->stok < $qty) {
             $denied = true;
         }
     }
     if (!$denied) {
         $saveSession = $request->only(['id', 'qty', 'harga', 'note']);
         // id as produk_id
         $session->put('data_order.' . $request->get('id'), $saveSession);
         return ['num' => $request->get('num'), 'nama' => $request->get('nama'), 'harga' => number_format($request->get('harga'), 0, ',', '.'), 'qty' => $request->get('qty'), 'subtotal' => number_format($request->get('harga') * $request->get('qty'), 0, ',', '.'), 'note' => $request->get('note'), 'action' => $request->get('action')];
     } else {
         return 0;
     }
 }
 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 checkStok(Request $request)
 {
     \Debugbar::disable();
     //return 1; // allow transaction with >=0 stok
     $produkId = $request->get('id');
     $qty = $request->get('qty') ? $request->get('qty') : 1;
     $produk = Produk::with('detail')->where('active', 1)->where('id', $produkId)->first();
     $denied = false;
     if ($produk->detail->count()) {
         $tempBahan = [];
         foreach ($produk->detail as $pd) {
             $bId = $pd['bahan_id'];
             $tempBahan[$bId] = $pd['qty'] * $qty;
         }
         $bahans = \App\StokBahan::whereIn('bahan_id', array_keys($tempBahan))->get();
         foreach ($bahans as $bahan) {
             $bId = $bahan->bahan_id;
             if ($bahan->stok < $tempBahan[$bId]) {
                 $denied = true;
             }
         }
     } else {
         $produk = \App\StokProduk::where('produk_id', $produkId)->first();
         if ($produk->stok < $qty) {
             $denied = true;
         }
     }
     if (!$denied) {
         return 1;
     }
     return 0;
 }
 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.');
 }