LaporanStokController.php

7 maret 2023
 avatar
FIKRI
php
a year ago
78 kB
1
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Exports\ExportNewLaporanStok;
use App\LaporanStok;
use App\Product;
use App\Transaction;
use App\Supply;
use App\TransactionNonPPN;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class LaporanStokController extends Controller
{
    public function index()
    {
        return view('report/report_laporan_stok');
    }

    public function newLaporanStok(
        $kode_barang, 
        $kode_aktivitas, 
        $jenis_kemasan, 
        $stok_awal_besar,
        $stok_awal_kecil,
        $stok_input_besar,
        $stok_input_kecil,
        $stok_akhir_besar,
        $stok_akhir_kecil, 
        $status_pajak,
        $status_aktivitas,
        $status
    )
    {
            $pp = Product::where('kode_barang', $kode_barang)->first();

            if($status == 1){
                $total_input_besar = $stok_input_besar * $pp->harga_modal_dos;
                $total_input_kecil = $stok_input_kecil * $pp->harga_modal_pack;
            }else{
                $total_input_besar = $stok_input_besar * $pp->harga_jual_dos;
                $total_input_kecil = $stok_input_kecil * $pp->harga_jual_pack;
            }

            $ls = new LaporanStok;
            $ls->kode_aktivitas = $kode_aktivitas;
            $ls->kode_barang = $pp->kode_barang;
            $ls->nama_barang = $pp->nama_barang;
            $ls->jenis_kemasan = $jenis_kemasan;
            $ls->stok_awal_besar = $stok_awal_besar;
            $ls->stok_awal_kecil = $stok_awal_kecil;
            $ls->stok_input_besar = $stok_input_besar;
            $ls->stok_input_kecil = $stok_input_kecil;
            $ls->stok_akhir_besar = $stok_akhir_besar;
            $ls->stok_akhir_kecil = $stok_akhir_kecil;
            $ls->harga_beli_besar = $pp->harga_modal_dos;
            $ls->harga_beli_kecil = $pp->harga_modal_pack;
            $ls->harga_jual_besar = $pp->harga_jual_dos;
            $ls->harga_jual_kecil = $pp->harga_jual_pack;
            $ls->total_harga_input_besar = $total_input_besar;
            $ls->total_harga_input_kecil = $total_input_kecil;
            //ppn,non ppn
            $ls->status_pajak = $status_pajak;
            $ls->status_aktivitas = $status_aktivitas;
            //masuk 1, keluar 2
            $ls->status = $status;
            $ls->save();
    }

    public function getLaporanStok()
    {
        $product = Product::orderBy('nama_barang')->get();

        $tgl = Carbon::today();

        $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) {
            $data_awal = LaporanStok::whereDate('created_at', $tgl)->where('kode_barang',$value->kode_barang)
            ->whereIn('status_aktivitas', ['lunas','retur'])->first();
            // if($data_awal != null){
                $stok_awal_b = ($data_awal == null) ? 0 : $data_awal->stok_awal_besar;
                $sb_awal += $stok_awal_b;
                $stok_awal_k = ($data_awal == null) ? 0 : $data_awal->stok_awal_kecil;
                $sk_awal += $stok_awal_k;
                $data_akhir = LaporanStok::whereDate('created_at', $tgl)->where('kode_barang',$value->kode_barang)
                ->whereIn('status_aktivitas', ['lunas','retur'])->latest()->first();
                $stok_akhir_b = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_besar;
                $sb_akhir += $stok_akhir_b;
                $stok_akhir_k = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_kecil;
                $sk_akhir += $stok_akhir_k;

                $harga_beli_besar = ($data_awal == null) ? 0 : $data_awal->harga_beli_besar;
                $harga_beli_kecil = ($data_awal == null) ? 0 : $data_awal->harga_beli_kecil;
                $harga_jual_besar = ($data_awal == null) ? 0 : $data_awal->harga_jual_besar;
                $harga_jual_kecil = ($data_awal == null) ? 0 : $data_awal->harga_jual_kecil;

                $summ_stok_besar = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',1)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
                $summ_stok_kecil = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',1)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
                $summ_total_harga_besar = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',1)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
                $summ_total_harga_kecil = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',1)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

                $sum_stok_besar = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',2)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
                $sum_stok_kecil = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',2)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
                $sum_total_harga_besar = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',2)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
                $sum_total_harga_kecil = LaporanStok::whereDate('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('status',2)
                ->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

                $sb_masuk += $summ_stok_besar;
                $sk_masuk += $summ_stok_kecil;
                $sb_keluar += $sum_stok_besar;
                $sk_keluar += $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++;
            // }
        }

        $tgl = Carbon::parse(now())->isoFormat('D MMMM Y');

        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 getLaporanStokFilter(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{
            $get_start = LaporanStok::first();
            $get_end = LaporanStok::latest()->first();
            $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) {
            $data_awal = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->where('kode_barang',$value->kode_barang)
            ->whereIn('status_aktivitas', ['lunas','retur'])->first();
            // if($data_awal != null){
                $stok_awal_b = ($data_awal == null) ? 0 : $data_awal->stok_awal_besar;
                $sb_awal += $stok_awal_b;
                $stok_awal_k = ($data_awal == null) ? 0 : $data_awal->stok_awal_kecil;
                $sk_awal += $stok_awal_k;
                $data_akhir = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->where('kode_barang',$value->kode_barang)
                ->whereIn('status_aktivitas', ['lunas','retur'])->latest()->first();
                $stok_akhir_b = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_besar;
                $sb_akhir += $stok_akhir_b;
                $stok_akhir_k = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_kecil;
                $sk_akhir += $stok_akhir_k;

                $harga_beli_besar = ($data_awal == null) ? 0 : $data_awal->harga_beli_besar;
                $harga_beli_kecil = ($data_awal == null) ? 0 : $data_awal->harga_beli_kecil;
                $harga_jual_besar = ($data_awal == null) ? 0 : $data_awal->harga_jual_besar;
                $harga_jual_kecil = ($data_awal == null) ? 0 : $data_awal->harga_jual_kecil;

                $summ_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
                $summ_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
                $summ_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
                $summ_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

                $sum_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
                $sum_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',2)->sum('stok_input_kecil');
                $sum_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
                $sum_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
                ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

                $sb_masuk += $summ_stok_besar;
                $sk_masuk += $summ_stok_kecil;
                $sb_keluar += $sum_stok_besar;
                $sk_keluar += $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 exportLaporanStok(Request $req)
    {
        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{
            $get_start = LaporanStok::first();
            $get_end = LaporanStok::latest()->first();
            $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) {
            $data_awal = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->whereIn('status_aktivitas', ['lunas','retur'])->where('kode_barang',$value->kode_barang)->first();
            $stok_awal_b = ($data_awal == null) ? 0 : $data_awal->stok_awal_besar;
            $sb_awal += $stok_awal_b;
            $stok_awal_k = ($data_awal == null) ? 0 : $data_awal->stok_awal_kecil;
            $sk_awal += $stok_awal_k;
            $data_akhir = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->whereIn('status_aktivitas', ['lunas','retur'])->where('kode_barang',$value->kode_barang)->latest()->first();
            $stok_akhir_b = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_besar;
            $sb_akhir += $stok_akhir_b;
            $stok_akhir_k = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_kecil;
            $sk_akhir += $stok_akhir_k;

            $harga_beli_besar = ($data_awal == null) ? 0 : $data_awal->harga_beli_besar;
            $harga_beli_kecil = ($data_awal == null) ? 0 : $data_awal->harga_beli_kecil;
            $harga_jual_besar = ($data_awal == null) ? 0 : $data_awal->harga_jual_besar;
            $harga_jual_kecil = ($data_awal == null) ? 0 : $data_awal->harga_jual_kecil;

            $summ_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
            $summ_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
            $summ_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
            $summ_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

            $sum_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
            $sum_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
            $sum_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
            $sum_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');
            
            $sb_masuk += $summ_stok_besar;
            $sk_masuk += $summ_stok_kecil;
            $sb_keluar += $sum_stok_besar;
            $sk_keluar += $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 printLaporanStok(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{
            $get_start = LaporanStok::first();
            $get_end = LaporanStok::latest()->first();
            $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) {
            $data_awal = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->where('kode_barang',$value->kode_barang)->whereIn('status_aktivitas', ['lunas','retur'])->first();
            $stok_awal_b = ($data_awal == null) ? 0 : $data_awal->stok_awal_besar;
            $sb_awal += $stok_awal_b;
            $stok_awal_k = ($data_awal == null) ? 0 : $data_awal->stok_awal_kecil;
            $sk_awal += $stok_awal_k;
            $data_akhir = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])->where('kode_barang',$value->kode_barang)->whereIn('status_aktivitas', ['lunas','retur'])->latest()->first();
            $stok_akhir_b = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_besar;
            $sb_akhir += $stok_akhir_b;
            $stok_akhir_k = ($data_akhir == null) ? 0 : $data_akhir->stok_akhir_kecil;
            $sk_akhir += $stok_akhir_k;

            $harga_beli_besar = ($data_awal == null) ? 0 : $data_awal->harga_beli_besar;
            $harga_beli_kecil = ($data_awal == null) ? 0 : $data_awal->harga_beli_kecil;
            $harga_jual_besar = ($data_awal == null) ? 0 : $data_awal->harga_jual_besar;
            $harga_jual_kecil = ($data_awal == null) ? 0 : $data_awal->harga_jual_kecil;

            $summ_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
            $summ_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
            $summ_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
            $summ_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',1)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');

            $sum_stok_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_besar');
            $sum_stok_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('stok_input_kecil');
            $sum_total_harga_besar = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_besar');
            $sum_total_harga_kecil = LaporanStok::whereBetween('created_at', [$start_date2, $end_date2])
            ->where('kode_barang', $value->kode_barang)->where('status',2)->whereIn('status_aktivitas', ['lunas','retur'])->sum('total_harga_input_kecil');
            
            $sb_masuk += $summ_stok_besar;
            $sk_masuk += $summ_stok_kecil;
            $sb_keluar += $sum_stok_besar;
            $sk_keluar += $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
        ]);
    }

    public function indexAwal()
    {
        return view('report/report_laporan_stok2');
    }

    public function getLaporanStokAwal(Request $request)
    {
        $limit = 500;

        $offset = $request->input('offset') ?? 0;

        $product = Product::orderBy('nama_barang')->offset($offset)->limit($limit)->get();

        $tgl = Carbon::today();

        $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', $tgl)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            if($value->status_pajak == 'ppn'){
                $transaksi_awal = Transaction::where('created_at', $tgl)->where('kode_barang',$value->kode_barang)
                        ->where('status', 'lunas')->get();
            }            
            else{
                $transaksi_awal = TransactionNonPPN::where('created_at', $tgl)->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_pertama = DB::table('stok_awal')->where('kode_barang', $value->kode_barang)->first();
            if (isset($stok_pertama)) {
                $stok_awal_b = $stok_pertama->jumlah_besar + $pasok_awal_besar - $transaksi_awal_besar;
                $stok_awal_k = $stok_pertama->jumlah_kecil + $pasok_awal_kecil - $transaksi_awal_kecil;
            }else{
                $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::where('created_at', $tgl)
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('jumlah')/$value->pack;
            $summ_stok_kecil = Supply::where('created_at', $tgl)
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('jumlah');
            $summ_total_harga_besar = Supply::where('created_at', $tgl)
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', '!=', 'pack')->sum('subtotal');
            $summ_total_harga_kecil = Supply::where('created_at', $tgl)
            ->where('kode_barang', $value->kode_barang)->where('status', 'lunas')->where('jenis_kemasan', 'pack')->sum('subtotal');

            if($value->status_pajak == 'ppn'){
                $sum_stok_besar = Transaction::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = Transaction::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = Transaction::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=' , 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = Transaction::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('total_barang');
            }
            else{
                $sum_stok_besar = TransactionNonPPN::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('jumlah')/$value->pack;
                $sum_stok_kecil = TransactionNonPPN::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', 'Pack')->where('status', 'lunas')->sum('jumlah');
                $sum_total_harga_besar = TransactionNonPPN::where('created_at', $tgl)
                ->where('kode_barang', $value->kode_barang)->where('jenis_kemasan', '!=', 'Pack')->where('status', 'lunas')->sum('total_barang');
                $sum_total_harga_kecil = TransactionNonPPN::where('created_at', $tgl)
                ->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 datatables()->of($data)
        // ->make(true);

        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,',','.'),
            'nilai_akhir_besar' => $tb_nilai,
            'nilai_akhir_kecil' => $tk_nilai,
            'total_barang' => $total_barang
        ]);
    }
    
    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_pertama = DB::table('stok_awal')->where('kode_barang', $value->kode_barang)->first();
            if (isset($stok_pertama)) {
                $stok_awal_b = $stok_pertama->jumlah_besar + $pasok_awal_besar - $transaksi_awal_besar;
                $stok_awal_k = $stok_pertama->jumlah_kecil + $pasok_awal_kecil - $transaksi_awal_kecil;
            } else {
                $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_pertama = DB::table('stok_awal')->where('kode_barang', $value->kode_barang)->first();

            if (isset($stok_pertama)) {
                $stok_awal_b = $stok_pertama->jumlah_besar + $pasok_awal_besar - $transaksi_awal_besar;
                $stok_awal_k = $stok_pertama->jumlah_kecil + $pasok_awal_kecil - $transaksi_awal_kecil;
            }else{
                $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;
            
            // $stok_awal_b = $stok_pertama->jumlah_besar + $pasok_awal_besar - $transaksi_awal_besar;
            // $stok_awal_k = $stok_pertama->jumlah_kecil + $pasok_awal_kecil - $transaksi_awal_kecil;
            // $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_pertama = DB::table('stok_awal')->where('kode_barang', $value->kode_barang)->first();
            
            $stok_awal_b = $stok_pertama->jumlah_besar + $pasok_awal_besar - $transaksi_awal_besar;
            $stok_awal_k = $stok_pertama->jumlah_kecil + $pasok_awal_kecil - $transaksi_awal_kecil;
            // $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
        ]);
    }
}