LaporanStokController.php

mail@pastecode.io avatar
unknown
php_laravel_blade
2 years ago
34 kB
1
Indexable
Never
public function indexAwal()
    {
        return view('report/report_laporan_stok2');
    }
public function getLaporanStokFilterAwal(Request $req)
    {
       $cari = $req->cari;
       if ($req->pajak != "") {
           $product = Product::where('status_pajak', $req->pajak)
           ->where(function ($query)use($cari){
            $query->where('nama_barang','like', '%'.$cari.'%')
                  ->orWhere('kode_barang', '%'.$cari.'%');
           })->orderBy('nama_barang')->get();
       }else{
            $product = Product::where('nama_barang','like', '%'.$cari.'%')
            ->orWhere('kode_barang','like','%'.$cari.'%')
            ->orderBy('nama_barang')->get();
       }

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $supply_start = Supply::first();
            $transppn_start = Transaction::first();
            $trans_nonppn_start = TransactionNonPPN::first();
            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start > $transppn_start){
                    $get_start = $transppn_start;
                }else{
                    $get_start = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_start = $transppn_start;
            }else if($trans_nonppn_start){
                $get_start = $trans_nonppn_start;
            }
            if($get_start && $supply_start){
                if($get_start > $supply_start){
                    $get_start = $supply_start;
                }
            }else if($supply_start){
                $get_start = $supply_start;
            }

            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start < $transppn_start){
                    $get_end = $transppn_start;
                }else{
                    $get_end = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_end = $transppn_start;
            }else if($trans_nonppn_start){
                $get_end = $trans_nonppn_start;
            }
            if($get_end && $supply_start){
                if($get_end < $supply_start){
                    $get_end = $supply_start;
                }
            }else if($supply_start){
                $get_end = $supply_start;
            }
            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        $data = [];
        $total_barang = 0;
        $ppn = 0;
        $nonppn = 0;
        $sb_awal = 0;
        $sk_awal = 0;
        $sb_masuk = 0;
        $sk_masuk = 0;
        $sb_keluar = 0;
        $sk_keluar = 0;
        $sb_akhir = 0;
        $sk_akhir = 0;
        $tb_nilai = 0;
        $tk_nilai = 0;

        foreach ($product as $key => $value) {
            // ===================versi sblm bulan 8 =======================//
            $pasok_awal = Supply::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            if($value->status_pajak == 'ppn'){
                $transaksi_awal = Transaction::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }            
            else{
                $transaksi_awal = TransactionNonPPN::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }
            $data_awal = $pasok_awal->concat($transaksi_awal);
            $pasok_awal_besar = $pasok_awal->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $transaksi_awal_besar = $transaksi_awal->where('jenis_kemasan', '!=', 'Pack')->sum('jumlah')/$value->pack;
            $pasok_awal_kecil = $pasok_awal->where('jenis_kemasan', 'pack')->sum('jumlah');
            $transaksi_awal_kecil = $transaksi_awal->where('jenis_kemasan', 'Pack')->sum('jumlah');
            $stok_awal_b = $pasok_awal_besar - $transaksi_awal_besar;
            $stok_awal_k = $pasok_awal_kecil - $transaksi_awal_kecil;
            $sb_awal += $stok_awal_b;
            $sk_awal += $stok_awal_k;
            ;
            $harga_beli_besar = ($data_awal == null) ? 0 : $value->harga_modal_dos;
            $harga_beli_kecil = ($data_awal == null) ? 0 : $value->harga_modal_pack;
            $harga_jual_besar = ($data_awal == null) ? 0 : $value->harga_jual_dos;
            $harga_jual_kecil = ($data_awal == null) ? 0 : $value->harga_jual_pack;

            $summ_stok_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $summ_stok_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('jumlah');
            $summ_total_harga_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('subtotal');
            $summ_total_harga_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('subtotal');

            if($value->status_pajak == 'ppn'){
                $sum_stok_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=' , 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }
            else{
                $sum_stok_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }

            $sb_masuk += $summ_stok_besar;
            $sk_masuk += $summ_stok_kecil;
            $sb_keluar += $sum_stok_besar;
            $sk_keluar += $sum_stok_kecil;

            $stok_akhir_b = $stok_awal_b + $summ_stok_besar - $sum_stok_besar;
            $stok_akhir_k = $stok_awal_k + $summ_stok_kecil - $sum_stok_kecil;

            $nn = $stok_akhir_b * $harga_beli_besar;
            $tb_nilai += $nn;

            $nk = $stok_akhir_k * $harga_beli_kecil;
            $tk_nilai += $nk;

            array_push($data, [
                'kode_barang' => $value->kode_barang,
                'nama_barang' => $value->nama_barang,
                'stok_awal_besar' => $stok_awal_b.' '.$value->master_kemasan,
                'stok_awal_kecil' => $stok_awal_k.' Pack',

                'stok_masuk_besar' => $summ_stok_besar.' '.$value->master_kemasan,
                'harga_beli_besar' => number_format($harga_beli_besar,2,',','.'),
                'total_nilai_besar' => number_format($summ_total_harga_besar,2,',','.'),
                'stok_masuk_kecil' => $summ_stok_kecil.' Pack',
                'harga_beli_kecil' => number_format($harga_beli_kecil,2,',','.'),
                'total_nilai_kecil' => number_format($summ_total_harga_kecil,2,',','.'),

                'stok_keluar_besar' => $sum_stok_besar.' '.$value->master_kemasan,
                'harga_jual_besar' => number_format($harga_jual_besar,2,',','.'),
                'total_nilai_jual_besar' => number_format($sum_total_harga_besar,2,',','.'),
                'stok_keluar_kecil' => $sum_stok_kecil.' Pack',
                'harga_jual_kecil' => number_format($harga_jual_kecil,2,',','.'),
                'total_nilai_jual_kecil' => number_format($sum_total_harga_kecil,2,',','.'),

                'stok_akhir_besar' => $stok_akhir_b.' '.$value->master_kemasan,
                'stok_akhir_kecil' => $stok_akhir_k.' Pack',

                'stok_rupiah_besar' => number_format($nn,2,',','.'),
                'stok_rupiah_kecil' => number_format($nk,2,',','.'),

                'status_pajak' => ($value->status_pajak == 'ppn') ? 'PPN' : 'Non PPN',
            ]);

            if($value->status_pajak == 'ppn'){
                $ppn++;
            }else{
                $nonppn++;
            }

            $total_barang++;    
        }

        return request()->json(200,[
            'tgl' => $tgl,
            'data' => $data,
            'ppn' => $ppn,
            'nonppn' => $nonppn,
            'stok_awal_besar' => $sb_awal,
            'stok_awal_kecil' => $sk_awal,
            'stok_masuk_besar' => $sb_masuk,
            'stok_masuk_kecil' => $sk_masuk,
            'stok_keluar_besar' => $sb_keluar,
            'stok_keluar_kecil' => $sk_keluar,
            'stok_akhir_besar' => $sb_akhir,
            'stok_akhir_kecil' => $sk_akhir,
            'nilai_akhir_besar' => number_format($tb_nilai,2,',','.'),
            'nilai_akhir_kecil' => number_format($tk_nilai,2,',','.'),
            'total_barang' => $total_barang
        ]);
    }
public function exportLaporanStokAwal(Request $req)
    {
        $cari = $req->cari;
       if ($req->pajak != "") {
           $product = Product::where('status_pajak', $req->pajak)
           ->where(function ($query)use($cari){
            $query->where('nama_barang','like', '%'.$cari.'%')
                  ->orWhere('kode_barang', '%'.$cari.'%');
           })->orderBy('nama_barang')->get();
       }else{
            $product = Product::where('nama_barang','like', '%'.$cari.'%')
            ->orWhere('kode_barang','like','%'.$cari.'%')
            ->orderBy('nama_barang')->get();
       }

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $supply_start = Supply::first();
            $transppn_start = Transaction::first();
            $trans_nonppn_start = TransactionNonPPN::first();
            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start > $transppn_start){
                    $get_start = $transppn_start;
                }else{
                    $get_start = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_start = $transppn_start;
            }else if($trans_nonppn_start){
                $get_start = $trans_nonppn_start;
            }
            if($get_start && $supply_start){
                if($get_start > $supply_start){
                    $get_start = $supply_start;
                }
            }else if($supply_start){
                $get_start = $supply_start;
            }

            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start < $transppn_start){
                    $get_end = $transppn_start;
                }else{
                    $get_end = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_end = $transppn_start;
            }else if($trans_nonppn_start){
                $get_end = $trans_nonppn_start;
            }
            if($get_end && $supply_start){
                if($get_end < $supply_start){
                    $get_end = $supply_start;
                }
            }else if($supply_start){
                $get_end = $supply_start;
            }
            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        $data = [];
        $total_barang = 0;
        $ppn = 0;
        $nonppn = 0;
        $sb_awal = 0;
        $sk_awal = 0;
        $sb_masuk = 0;
        $sk_masuk = 0;
        $sb_keluar = 0;
        $sk_keluar = 0;
        $sb_akhir = 0;
        $sk_akhir = 0;
        $tb_nilai = 0;
        $tk_nilai = 0;

        foreach ($product as $key => $value) {
            // ===================versi sblm bulan 8 =======================//
            $pasok_awal = Supply::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            if($value->status_pajak == 'ppn'){
                $transaksi_awal = Transaction::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }            
            else{
                $transaksi_awal = TransactionNonPPN::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }
            $data_awal = $pasok_awal->concat($transaksi_awal);
            $pasok_awal_besar = $pasok_awal->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $transaksi_awal_besar = $transaksi_awal->where('jenis_kemasan', '!=', 'Pack')->sum('jumlah')/$value->pack;
            $pasok_awal_kecil = $pasok_awal->where('jenis_kemasan', 'pack')->sum('jumlah');
            $transaksi_awal_kecil = $transaksi_awal->where('jenis_kemasan', 'Pack')->sum('jumlah');
            $stok_awal_b = $pasok_awal_besar - $transaksi_awal_besar;
            $stok_awal_k = $pasok_awal_kecil - $transaksi_awal_kecil;
            $sb_awal += $stok_awal_b;
            $sk_awal += $stok_awal_k;
            ;
            $harga_beli_besar = ($data_awal == null) ? 0 : $value->harga_modal_dos;
            $harga_beli_kecil = ($data_awal == null) ? 0 : $value->harga_modal_pack;
            $harga_jual_besar = ($data_awal == null) ? 0 : $value->harga_jual_dos;
            $harga_jual_kecil = ($data_awal == null) ? 0 : $value->harga_jual_pack;

            $summ_stok_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $summ_stok_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('jumlah');
            $summ_total_harga_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('subtotal');
            $summ_total_harga_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('subtotal');

            if($value->status_pajak == 'ppn'){
                $sum_stok_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=' , 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }
            else{
                $sum_stok_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }

            $sb_masuk += $summ_stok_besar;
            $sk_masuk += $summ_stok_kecil;
            $sb_keluar += $sum_stok_besar;
            $sk_keluar += $sum_stok_kecil;

            $stok_akhir_b = $stok_awal_b + $summ_stok_besar - $sum_stok_besar;
            $stok_akhir_k = $stok_awal_k + $summ_stok_kecil - $sum_stok_kecil;

            $nn = $stok_akhir_b * $harga_beli_besar;
            $tb_nilai += $nn;

            $nk = $stok_akhir_k * $harga_beli_kecil;
            $tk_nilai += $nk;

            array_push($data, [
                'kode_barang' => $value->kode_barang,
                'nama_barang' => $value->nama_barang,
                'stok_awal_besar' => $stok_awal_b,
                'stok_awal_kecil' => $stok_awal_k,
                'master_kemasan' => $value->master_kemasan,

                'stok_masuk_besar' => $summ_stok_besar,
                'harga_beli_besar' => $harga_beli_besar,
                'total_nilai_besar' => $summ_total_harga_besar,
                'stok_masuk_kecil' => $summ_stok_kecil,
                'harga_beli_kecil' => $harga_beli_kecil,
                'total_nilai_kecil' => $summ_total_harga_kecil,

                'stok_keluar_besar' => $sum_stok_besar,
                'harga_jual_besar' => $harga_jual_besar,
                'total_nilai_jual_besar' => $sum_total_harga_besar,
                'stok_keluar_kecil' => $sum_stok_kecil,
                'harga_jual_kecil' => $harga_jual_kecil,
                'total_nilai_jual_kecil' => $sum_total_harga_kecil,

                'stok_akhir_besar' => $stok_akhir_b,
                'stok_akhir_kecil' => $stok_akhir_k,

                'stok_rupiah_besar' => $nn,
                'stok_rupiah_kecil' => $nk,

                'status_pajak' => ($value->status_pajak == 'ppn') ? 'PPN' : 'Non PPN',
            ]);

            if($value->status_pajak == 'ppn'){
                $ppn++;
            }else{
                $nonppn++;
            }

            $total_barang++;    
        }

        return Excel::download(new ExportNewLaporanStok(
            $tgl, 
            $data,
            $ppn,
            $nonppn,
            $sb_awal,
            $sk_awal,
            $sb_masuk,
            $sk_masuk,
            $sb_keluar,
            $sk_keluar,
            $sb_akhir,
            $sk_akhir,
            $tb_nilai,
            $tk_nilai,
            $total_barang, 
        ), 'Laporan_Stok- '.now().'.xlsx');
    }

    public function printLaporanStokAwal(Request $req)
    {
        $cari = $req->cari;
       if ($req->pajak != "") {
           $product = Product::where('status_pajak', $req->pajak)
           ->where(function ($query)use($cari){
            $query->where('nama_barang','like', '%'.$cari.'%')
                  ->orWhere('kode_barang', '%'.$cari.'%');
           })->orderBy('nama_barang')->get();
       }else{
            $product = Product::where('nama_barang','like', '%'.$cari.'%')
            ->orWhere('kode_barang','like','%'.$cari.'%')
            ->orderBy('nama_barang')->get();
       }

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $supply_start = Supply::first();
            $transppn_start = Transaction::first();
            $trans_nonppn_start = TransactionNonPPN::first();
            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start > $transppn_start){
                    $get_start = $transppn_start;
                }else{
                    $get_start = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_start = $transppn_start;
            }else if($trans_nonppn_start){
                $get_start = $trans_nonppn_start;
            }
            if($get_start && $supply_start){
                if($get_start > $supply_start){
                    $get_start = $supply_start;
                }
            }else if($supply_start){
                $get_start = $supply_start;
            }

            if($trans_nonppn_start && $transppn_start){
                if($trans_nonppn_start < $transppn_start){
                    $get_end = $transppn_start;
                }else{
                    $get_end = $trans_nonppn_start;
                }
            }else if($transppn_start){
                $get_end = $transppn_start;
            }else if($trans_nonppn_start){
                $get_end = $trans_nonppn_start;
            }
            if($get_end && $supply_start){
                if($get_end < $supply_start){
                    $get_end = $supply_start;
                }
            }else if($supply_start){
                $get_end = $supply_start;
            }
            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        $data = [];
        $total_barang = 0;
        $ppn = 0;
        $nonppn = 0;
        $sb_awal = 0;
        $sk_awal = 0;
        $sb_masuk = 0;
        $sk_masuk = 0;
        $sb_keluar = 0;
        $sk_keluar = 0;
        $sb_akhir = 0;
        $sk_akhir = 0;
        $tb_nilai = 0;
        $tk_nilai = 0;

        foreach ($product as $key => $value) {
            // ===================versi sblm bulan 8 =======================//
            $pasok_awal = Supply::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            if($value->status_pajak == 'ppn'){
                $transaksi_awal = Transaction::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }            
            else{
                $transaksi_awal = TransactionNonPPN::where('created_at', '<', $start_date2)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }
            $data_awal = $pasok_awal->concat($transaksi_awal);
            $pasok_awal_besar = $pasok_awal->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $transaksi_awal_besar = $transaksi_awal->where('jenis_kemasan', '!=', 'Pack')->sum('jumlah')/$value->pack;
            $pasok_awal_kecil = $pasok_awal->where('jenis_kemasan', 'pack')->sum('jumlah');
            $transaksi_awal_kecil = $transaksi_awal->where('jenis_kemasan', 'Pack')->sum('jumlah');
            $stok_awal_b = $pasok_awal_besar - $transaksi_awal_besar;
            $stok_awal_k = $pasok_awal_kecil - $transaksi_awal_kecil;
            $sb_awal += $stok_awal_b;
            $sk_awal += $stok_awal_k;
            ;
            $harga_beli_besar = ($data_awal == null) ? 0 : $value->harga_modal_dos;
            $harga_beli_kecil = ($data_awal == null) ? 0 : $value->harga_modal_pack;
            $harga_jual_besar = ($data_awal == null) ? 0 : $value->harga_jual_dos;
            $harga_jual_kecil = ($data_awal == null) ? 0 : $value->harga_jual_pack;

            $summ_stok_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $summ_stok_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('jumlah');
            $summ_total_harga_besar = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('subtotal');
            $summ_total_harga_kecil = Supply::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('subtotal');

            if($value->status_pajak == 'ppn'){
                $sum_stok_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=' , 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }
            else{
                $sum_stok_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }

            $sb_masuk += $summ_stok_besar;
            $sk_masuk += $summ_stok_kecil;
            $sb_keluar += $sum_stok_besar;
            $sk_keluar += $sum_stok_kecil;

            $stok_akhir_b = $stok_awal_b + $summ_stok_besar - $sum_stok_besar;
            $stok_akhir_k = $stok_awal_k + $summ_stok_kecil - $sum_stok_kecil;

            $nn = $stok_akhir_b * $harga_beli_besar;
            $tb_nilai += $nn;

            $nk = $stok_akhir_k * $harga_beli_kecil;
            $tk_nilai += $nk;

            array_push($data, [
                'kode_barang' => $value->kode_barang,
                'nama_barang' => $value->nama_barang,
                'stok_awal_besar' => $stok_awal_b,
                'stok_awal_kecil' => $stok_awal_k,
                'master_kemasan' => $value->master_kemasan,

                'stok_masuk_besar' => $summ_stok_besar,
                'harga_beli_besar' => number_format($harga_beli_besar,2,',','.'),
                'total_nilai_besar' => number_format($summ_total_harga_besar,2,',','.'),
                'stok_masuk_kecil' => $summ_stok_kecil,
                'harga_beli_kecil' => number_format($harga_beli_kecil,2,',','.'),
                'total_nilai_kecil' => number_format($summ_total_harga_kecil,2,',','.'),

                'stok_keluar_besar' => $sum_stok_besar,
                'harga_jual_besar' => number_format($harga_jual_besar,2,',','.'),
                'total_nilai_jual_besar' => number_format($sum_total_harga_besar,2,',','.'),
                'stok_keluar_kecil' => $sum_stok_kecil,
                'harga_jual_kecil' => number_format($harga_jual_kecil,2,',','.'),
                'total_nilai_jual_kecil' => number_format($sum_total_harga_kecil,2,',','.'),

                'stok_akhir_besar' => $stok_akhir_b,
                'stok_akhir_kecil' => $stok_akhir_k,

                'stok_rupiah_besar' => number_format($nn,2,',','.'),
                'stok_rupiah_kecil' => number_format($nk,2,',','.'),

                'status_pajak' => ($value->status_pajak == 'ppn') ? 'PPN' : 'Non PPN',
            ]);

            if($value->status_pajak == 'ppn'){
                $ppn++;
            }else{
                $nonppn++;
            }

            $total_barang++;    
        }

        return view('report.print_new_laporan_stok',[
            'tgl' => $tgl,
            'data' => $data,
            'ppn' => $ppn,
            'nonppn' => $nonppn,
            'stok_awal_besar' => $sb_awal,
            'stok_awal_kecil' => $sk_awal,
            'stok_masuk_besar' => $sb_masuk,
            'stok_masuk_kecil' => $sk_masuk,
            'stok_keluar_besar' => $sb_keluar,
            'stok_keluar_kecil' => $sk_keluar,
            'stok_akhir_besar' => $sb_akhir,
            'stok_akhir_kecil' => $sk_akhir,
            'nilai_akhir_besar' => number_format($tb_nilai,2,',','.'),
            'nilai_akhir_kecil' => number_format($tk_nilai,2,',','.'),
            'total_barang' => $total_barang
        ]);
    }