<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Laporan extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        cek_login();
        $this->load->model('Menu_model', 'menu');
        $this->menu->aksesUser($this->session->userdata('login_session')['role']);

        $this->load->model('Admin_model', 'admin');
        $this->load->model('Barang_model', 'barang');
        $this->load->model('Retur_model', 'retur');
        $this->load->library('form_validation');
    }
    
    public function index()
    {
        // id merupakan id_sa
        $data['dt_user'] = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $dt_cabang = $this->admin->getDataCabangByUser($data['dt_user']['cabang_id']);

        $data['title'] = "Laporan Stok ".$dt_cabang['nama_cabang']; 
        // $data['barang'] = $this->barang->getDetailBarangMasukByIdUser($dt_user['id_user'], $dt_cabang['id_cabang']);
        $data['barang'] = $this->admin->getBarang();
        if(is_user()) {
            $this->db->where_in('a.id_user', $data['dt_user']['id_user']);
        }
        $data['user_pemilik'] = $this->admin->getUserByRole(3);

        // saldo akhir 30 hari terakhir
        $tgl_satu = date('Y-m-01', strtotime(date("Y-m-d")));
        $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
        $tgl_akhir = date('Y-m-d', strtotime('-1 days', strtotime($tgl_satu)));

        $data['tglAwal'] = $tgl_satu;
        $data['tglAkhir'] = date("Y-m-d");
        $data['status'] = 0;
        $data['id_barang'] = null;
        $data['IdPemilik'] = null;
        $data['result_mutasi'] = array();
        
        $this->form_validation->set_rules('id_barang', '', 'required');
        $this->form_validation->set_rules('id_user_pemilik', '', 'required');
        $this->form_validation->set_rules('tgl_awal', '', 'required');
        $this->form_validation->set_rules('tgl_akhir', '', 'required');
        if($this->form_validation->run() == false) {
            $this->template->load('templates/dashboard', 'laporan/index', $data);
        } else {
            $data['status'] = 1;

            $id_barang = $this->input->post('id_barang', true);
            $id_user_pemilik = $this->input->post('id_user_pemilik', true);
            $tglAwal = $this->input->post('tgl_awal', true);
            $tglAkhir = $this->input->post('tgl_akhir', true);

            $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
            $tgl_akhir = date('Y-m-d', strtotime('-1 days', strtotime($tglAwal)));
            $data['dt_barang'] = $this->admin->get('barang', ['id_barang' => $id_barang]);
            $data['dt_pemilik'] = $this->admin->get('user', ['id_user' => $id_user_pemilik]);
            $data['satuan'] = $this->admin->get('satuan', ['id_satuan' => $data['dt_barang']['satuan_id']]);
 
            $data['tglAwal'] = $tglAwal;
            $data['tglAkhir'] = $tglAkhir;
            $data['IdPemilik'] = $id_user_pemilik;
            $data['id_barang'] = $id_barang;

            $this->db->where('b.user_id_pemilik', $id_user_pemilik);
            $dt_user_pemilik = $this->barang->getUserPemilikFormBarangMasukByIdBarang($id_barang);

            $data['result_mutasi'] = array();
            foreach($dt_user_pemilik as $row) {

                    $data['result_mutasi'][] = array(
                        'id_user_pemilik' => $id_user_pemilik,
                        'nm_user_pemilik' => $row['nm_user_pemilik'],
                        'nama_barang' => $row['nama_barang'],
                        'nama_satuan' => $row['nama_satuan'],
                        'stok_awal' => $this->barang->_saldo_awal_barang_by_user_pemilik($id_user_pemilik, $id_barang, $tgl_awal, $tgl_akhir, $row['id_detail']),
                        'mutasi_barang' => $this->barang->getMutasiBarangByDateAndPemilik($id_user_pemilik, $id_barang, $dt_cabang['id_cabang'], $data['tglAwal'], $data['tglAkhir'], $row['id_detail']),
                    );
            }     
            
            
            $this->template->load('templates/dashboard', 'laporan/index', $data);
        }
    }

    function _saldo_awal_barang($id_sa_barang, $tgl_awal, $tgl_akhir)
    {
        $sa = $this->admin->get('sa_barang', ['id_sa' => $id_sa_barang]);
        $mutasi_barang = $this->barang->getMutasiBarangByDate($sa['barang_id'], $sa['cabang_id'], $tgl_awal, $tgl_akhir);

        $sisa_stok = $sa['saldo'];
        foreach ($mutasi_barang as $b) :

            switch ($b['jenis']) {
                case "in_tr":
                    $jenis = 'Mutasi Barang Masuk ke ' . $b['trs_cbg'];
                    break;
                case "out_tr":
                    $jenis = 'Mutasi Barang Keluar ke ' . $b['trs_cbg'];
                    break;
                case "in":
                    $jenis = 'Barang Masuk';
                    break;
                case "in_retur":
                    $jenis = 'Barang Retur dari ' . $b['nama_cabang'];
                    break;
                case "stkpn":
                    $jenis = 'Stok Op Name dari ' . $b['nama_cabang'];
                    break;
                default:
                    $jenis = 'Barang Keluar';
            }

            if($b['jenis'] == 'in' || $b['jenis'] == 'in_retur') {
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'in_tr' ) { 
                 $sisa_stok += $b['jml']; 
            } else if($b['jenis'] == 'stkpn' ) { 
                $sisa_stok += $b['jml']; 
            } else { 
                $sisa_stok -= $b['jml']; 
            } 
        endforeach;

        $saldo_awal = array(
            'qty' => $sisa_stok,
            'harga' => intval($sa['hpp']),
        );

        return $saldo_awal;
    }
    
    public function stok_harian()
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $dt_cabang = $this->admin->getDataCabangByUser($dt_user['cabang_id']);
        
        $data['title'] = "Laporan Stok Harian ( ".$dt_cabang['nama_cabang']." )"; // saldo akhir 30 hari terakhir
        
        $data['tglAwal'] = date('Y-m-d');
        $data['tglAkhir'] = date('Y-m-d');
        $data['stts'] = 1;
        
        $data['dt_transaksi'] = array();
        
        $this->form_validation->set_rules('tgl_awal', '', 'required');
        $this->form_validation->set_rules('tgl_akhir', '', 'required');
        if($this->form_validation->run() == false) {
            $this->template->load('templates/dashboard', 'laporan/stokharian', $data);
        } else {
            $tglAwal = $this->input->post('tgl_awal', true);
            $tglAkhir = $this->input->post('tgl_akhir', true);
            $stts = $this->input->post('stts', true);
            
            $data['tglAwal'] = $tglAwal;
            $data['tglAkhir'] = $tglAkhir;
            $data['stts'] = $stts;
            
            $tgl_Awal = date('d/m/Y', strtotime($tglAwal));
            $tgl_Akhir = date('d/m/Y', strtotime($tglAkhir));
            
            $data['title'] = "Laporan Stok Harian ( ".$tgl_Awal." sd ".$tgl_Akhir." - ". $dt_cabang['nama_cabang']." )"; // saldo akhir 30 hari terakhir
            
            $tgl_set = date('Y-m-d', strtotime('1/1/1971'));
            $tgl_sebelumtglawal = date('Y-m-d', strtotime('-1 days', strtotime($tglAwal)));
            
            // data barang by id
            $data['barang'] = $this->admin->getBarangByCabang($dt_cabang['id_cabang']); 
    
            // // perhitungan hpp
            $data['dt_transaksi'] = array();
            foreach($data['barang'] as $row){
                // mencari data transaksi terkait keluar masuk
                $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_set, $tgl_sebelumtglawal);
                $stok_awal = intval($saldo_awal['qty']);
    
                $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tglAwal, $tglAkhir); 

                $qty_in_baru = 0;
                $harga_in_baru = 0;

                $qty_out_baru = 0;
                foreach($mutasi as $row_mutasi) {
                    if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                        $qty_in_baru = intval($row_mutasi['jml']);
                        $harga_in_baru = intval($row_mutasi['harga']);
                    } else {
                        $qty_out_baru += intval($row_mutasi['jml']);
                    }
                }
                
                if($stts == 2) {
                    if($qty_in_baru != 0 || $qty_out_baru != 0) {
                        
                        $sisa_stok = $stok_awal+$qty_in_baru-$qty_out_baru;
                    
                        $data['dt_transaksi'][] = array(
                            'id_barang' => $row['id_barang'],
                            'nm_barang' => $row['nama_barang'],
                            'stok_awal' => $stok_awal,
                            'stok_masuk' => $qty_in_baru,
                            'stok_keluar' => $qty_out_baru,
                            'stok_akhir' => $sisa_stok,
                        );
                    }
                } else {
                    
                    $sisa_stok = $stok_awal+$qty_in_baru-$qty_out_baru;
                
                    $data['dt_transaksi'][] = array(
                        'id_barang' => $row['id_barang'],
                        'nm_barang' => $row['nama_barang'],
                        'stok_awal' => $stok_awal,
                        'stok_masuk' => $qty_in_baru,
                        'stok_keluar' => $qty_out_baru,
                        'stok_akhir' => $sisa_stok,
                    );
                }
            }
            
            $this->template->load('templates/dashboard', 'laporan/stokharian', $data);
        }
    }
    
    
    public function persentase_barang_keluar()
    {
        // mencari persentase barang keluar harian
        // misal barang masuk 200, barang keluar 150 __ harian
        // rumus persentase = (150/200)*100% 
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $dt_cabang = $this->admin->getDataCabangByUser($dt_user['cabang_id']);

        $data['title'] = "Laporan Persentase Barang Keluar ( ".$dt_cabang['nama_cabang']." )"; 

        // menampilkan semua barang
        $data['barang'] = $this->admin->getBarangByCabang($dt_cabang['id_cabang']); 

        date_default_timezone_set('Asia/Jakarta');
        
        $data['bulan_now'] = date("m");
        $data['tahun_now'] = date("Y");

        $data['tahun'] = array(2022,2023,2024,2025,2026);

        // memperoleh tgl pada bulan ini
        $tgl = date("d");
        $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
        $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
        $tgl_a = explode("-",$data['tglAkhir']);
        $array_tgl = array();   
        for($i=1;$i<=$tgl_a[2];$i++){
            $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
        }

        $data['array_tgl'] = $array_tgl;
        $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
        $tgl_akhir = date('Y-m-01', strtotime($data['now']));

        // // perhitungan hpp
        $data['dt_transaksi'] = array();
        foreach($data['barang'] as $row){
            // mencari data transaksi terkait keluar masuk
            $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);

            $transaksi = array();
            foreach($array_tgl as $row_tgl) {
                // mutasi stok
                $tgl_set = date('Y-m-d', strtotime($row_tgl));
                $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 

                $qty_in_baru = 0;
                $qty_out_baru = 0;
                foreach($mutasi as $row_mutasi) {
                    if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                        $qty_in_baru = intval($row_mutasi['jml']);
                    } else {
                        $qty_out_baru += intval($row_mutasi['jml']);
                    }
                }
                // die;
                $transaksi[] = array(
                    'tgl' => $row_tgl,
                    'masuk' => $qty_in_baru,
                    'keluar' => $qty_out_baru,
                    'persentase' => ($qty_in_baru == 0) ? 0 : ($qty_out_baru/$qty_in_baru)*100,
                );
            }

            $data['dt_transaksi'][] = array(
                'id_barang' => $row['id_barang'],
                'nm_barang' => $row['nama_barang'],
                'saldo_awal' => $saldo_awal,
                'transaksi' => $transaksi,
            );
        }
        
        // echo json_encode($data['dt_transaksi']);
        // die;
        
        // hasil
        $this->form_validation->set_rules('bulan', '', 'required');
        $this->form_validation->set_rules('tahun', '', 'required');
        if($this->form_validation->run() == false) {
            $data['status'] = 0;
            $this->template->load('templates/dashboard', 'laporan/persentasebk', $data);
        } else {
            $data['status'] = 1;
            $data['bulan_now'] = $this->input->post('bulan', true);
            $data['tahun_now'] = $this->input->post('tahun', true);
    
            $data['tahun'] = array(2022,2023,2024,2025,2026);
    
            // memperoleh tgl pada bulan ini
            $tgl = date("d");
            $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
            $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
            $tgl_a = explode("-",$data['tglAkhir']);
            $array_tgl = array();   
            for($i=1;$i<=$tgl_a[2];$i++){
                $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
            }
    
            $data['array_tgl'] = $array_tgl;
            $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
            $tgl_akhir = date('Y-m-01', strtotime($data['now']));
    
            // // perhitungan hpp
            $data['dt_transaksi'] = array();
            foreach($data['barang'] as $row){
                // mencari data transaksi terkait keluar masuk
                $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);
    
                $transaksi = array();
                foreach($array_tgl as $row_tgl) {
                    // mutasi stok
                    $tgl_set = date('Y-m-d', strtotime($row_tgl));
                    $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 
    
                    $qty_in_baru = 0;
    
                    $qty_out_baru = 0;
                    foreach($mutasi as $row_mutasi) {
                        if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                            $qty_in_baru = intval($row_mutasi['jml']);
                        } else {
                            $qty_out_baru += intval($row_mutasi['jml']);
                        }
                    }
                    // die;
                    $transaksi[] = array(
                        'tgl' => $row_tgl,
                        'masuk' => $qty_in_baru,
                        'keluar' => $qty_out_baru,
                        'persentase' => ($qty_in_baru == 0) ? 0 : ($qty_out_baru/$qty_in_baru)*100,
                    );
                }
    
                $data['dt_transaksi'][] = array(
                    'id_barang' => $row['id_barang'],
                    'nm_barang' => $row['nama_barang'],
                    'saldo_awal' => $saldo_awal,
                    'transaksi' => $transaksi,
                );
            }

            $this->template->load('templates/dashboard', 'laporan/persentasebk', $data);
        }
    }
    
    
    public function perhitungan_hpp()
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $dt_cabang = $this->admin->getDataCabangByUser($dt_user['cabang_id']);

        $data['title'] = "Laporan Perhitungan HPP ".$dt_cabang['nama_cabang']; 

        // menampilkan semua barang
        $data['barang'] = $this->admin->getBarangByCabang($dt_cabang['id_cabang']); 

        date_default_timezone_set('Asia/Jakarta');
        
        $data['bulan_now'] = date("m");
        $data['tahun_now'] = date("Y");

        $data['tahun'] = array(2022,2023,2024,2025,2026);

        // memperoleh tgl pada bulan ini
        $tgl = date("d");
        $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
        $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
        $tgl_a = explode("-",$data['tglAkhir']);
        $array_tgl = array();   
        for($i=1;$i<=$tgl_a[2];$i++){
            $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
        }

        $data['array_tgl'] = $array_tgl;
        $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
        $tgl_akhir = date('Y-m-01', strtotime($data['now']));

        // // perhitungan hpp
        $data['dt_transaksi'] = array();
        foreach($data['barang'] as $row){
            // mencari data transaksi terkait keluar masuk
            $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);

            $transaksi = array();
            foreach($array_tgl as $row_tgl) {
                // mutasi stok
                $tgl_set = date('Y-m-d', strtotime($row_tgl));
                $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 

                $qty_in_baru = 0;
                $harga_in_baru = 0;

                $qty_out_baru = 0;
                foreach($mutasi as $row_mutasi) {
                    if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                        $qty_in_baru = intval($row_mutasi['jml']);
                        $harga_in_baru = intval($row_mutasi['harga']);
                    } else {
                        $qty_out_baru += intval($row_mutasi['jml']);
                    }
                }
                // die;
                $stok_in = array(
                    'qty' => $qty_in_baru,
                    'harga' => $harga_in_baru,
                );

                $stok_out = array(
                    'qty' => $qty_out_baru,
                );
                $saldo_akhir = array();

                $transaksi[] = array(
                    'tgl' => $row_tgl,
                    'masuk' => $stok_in,
                    'keluar' => $stok_out,
                );
            }

            $data['dt_transaksi'][] = array(
                'id_barang' => $row['id_barang'],
                'nm_barang' => $row['nama_barang'],
                'saldo_awal' => $saldo_awal,
                'transaksi' => $transaksi,
            );
        }

        // hasil
        $this->form_validation->set_rules('bulan', '', 'required');
        $this->form_validation->set_rules('tahun', '', 'required');
        if($this->form_validation->run() == false) {
            $data['status'] = 0;
            $this->template->load('templates/dashboard', 'laporan/hpp', $data);
        } else {
            $data['status'] = 1;
            $data['bulan_now'] = $this->input->post('bulan', true);
            $data['tahun_now'] = $this->input->post('tahun', true);
    
            $data['tahun'] = array(2022,2023,2024,2025,2026);
    
            // memperoleh tgl pada bulan ini
            $tgl = date("d");
            $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
            $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
            $tgl_a = explode("-",$data['tglAkhir']);
            $array_tgl = array();   
            for($i=1;$i<=$tgl_a[2];$i++){
                $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
            }
    
            $data['array_tgl'] = $array_tgl;
            
            $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
            $tgl_akhir = date('Y-m-01', strtotime($data['now']));
    
            // // perhitungan hpp
            $data['dt_transaksi'] = array();
            foreach($data['barang'] as $row){
                // mencari data transaksi terkait keluar masuk
                $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);
    
                $transaksi = array();
                foreach($array_tgl as $row_tgl) {
                    // mutasi stok
                    $tgl_set = date('Y-m-d', strtotime($row_tgl));
                    $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 
    
                    $qty_in_baru = 0;
                    $harga_in_baru = 0;
    
                    $qty_out_baru = 0;
                    foreach($mutasi as $row_mutasi) {
                        if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                            $qty_in_baru = intval($row_mutasi['jml']);
                            $harga_in_baru = intval($row_mutasi['harga']);
                        } else {
                            $qty_out_baru += intval($row_mutasi['jml']);
                        }
                    }
                    // die;
                    $stok_in = array(
                        'qty' => $qty_in_baru,
                        'harga' => $harga_in_baru,
                    );
    
                    $stok_out = array(
                        'qty' => $qty_out_baru,
                    );
                    $saldo_akhir = array();
    
                    $transaksi[] = array(
                        'tgl' => $row_tgl,
                        'masuk' => $stok_in,
                        'keluar' => $stok_out,
                    );
                }
    
                $data['dt_transaksi'][] = array(
                    'id_barang' => $row['id_barang'],
                    'nm_barang' => $row['nama_barang'],
                    'saldo_awal' => $saldo_awal,
                    'transaksi' => $transaksi,
                );
            }

            $this->template->load('templates/dashboard', 'laporan/hpp', $data);
        }
    }

    public function perhitungan_hpp_perhari()
    {
        $dt_user = $this->admin->get('user', ['id_user' => $this->session->userdata('login_session')['user']]);
        $dt_cabang = $this->admin->getDataCabangByUser($dt_user['cabang_id']);

        $data['title'] = "Laporan Perhitungan HPP ".$dt_cabang['nama_cabang']; 

        // menampilkan semua barang
        $data['barang'] = $this->admin->getBarangByCabang($dt_cabang['id_cabang']); 

        date_default_timezone_set('Asia/Jakarta');
        
        $data['bulan_now'] = date("m");
        $data['tahun_now'] = date("Y");

        $data['tahun'] = array(2022,2023,2024,2025,2026);

        // memperoleh tgl pada bulan ini
        $tgl = date("d");
        $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
        $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
        $tgl_a = explode("-",$data['tglAkhir']);
        $array_tgl = array();   
        for($i=1;$i<=$tgl_a[2];$i++){
            $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
        }

        $data['array_tgl'] = $array_tgl;
        $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
        $tgl_akhir = date('Y-m-01', strtotime($data['now']));

        // // perhitungan hpp
        $data['dt_transaksi'] = array();
        foreach($data['barang'] as $row){
            // mencari data transaksi terkait keluar masuk
            $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);

            $transaksi = array();
            foreach($array_tgl as $row_tgl) {
                // mutasi stok
                $tgl_set = date('Y-m-d', strtotime($row_tgl));
                $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 

                $qty_in_baru = 0;
                $harga_in_baru = 0;

                $qty_out_baru = 0;
                foreach($mutasi as $row_mutasi) {
                    if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                        $qty_in_baru = intval($row_mutasi['jml']);
                        $harga_in_baru = intval($row_mutasi['harga']);
                    } else {
                        $qty_out_baru += intval($row_mutasi['jml']);
                    }
                }
                // die;
                $stok_in = array(
                    'qty' => $qty_in_baru,
                    'harga' => $harga_in_baru,
                );

                $stok_out = array(
                    'qty' => $qty_out_baru,
                );
                $saldo_akhir = array();

                $transaksi[] = array(
                    'tgl' => $row_tgl,
                    'masuk' => $stok_in,
                    'keluar' => $stok_out,
                );
            }

            $data['dt_transaksi'][] = array(
                'id_barang' => $row['id_barang'],
                'nm_barang' => $row['nama_barang'],
                'saldo_awal' => $saldo_awal,
                'transaksi' => $transaksi,
            );
        }

        // hasil
        $this->form_validation->set_rules('bulan', '', 'required');
        $this->form_validation->set_rules('tahun', '', 'required');
        if($this->form_validation->run() == false) {
            $data['status'] = 0;
            $this->template->load('templates/dashboard', 'laporan/hppperhari', $data);
        } else {
            $data['status'] = 1;
            $data['bulan_now'] = $this->input->post('bulan', true);
            $data['tahun_now'] = $this->input->post('tahun', true);
    
            $data['tahun'] = array(2022,2023,2024,2025,2026);
    
            // memperoleh tgl pada bulan ini
            $tgl = date("d");
            $data['now'] = $data['tahun_now'].'-'.$data['bulan_now'].'-'.$tgl;
            $data['tglAkhir'] = date("Y-m-t", strtotime( $data['now'] ));
            $tgl_a = explode("-",$data['tglAkhir']);
            $array_tgl = array();   
            for($i=1;$i<=$tgl_a[2];$i++){
                $array_tgl[] = $tgl_a[0].'-'.sprintf("%02d", $tgl_a[1]).'-'.sprintf("%02d", $i);
            }
    
            $data['array_tgl'] = $array_tgl;
            $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
            $tgl_akhir = date('Y-m-01', strtotime($data['now']));
    
            // // perhitungan hpp
            $data['dt_transaksi'] = array();
            foreach($data['barang'] as $row){
                // mencari data transaksi terkait keluar masuk
                $saldo_awal = $this->_saldo_awal_barang($row['id_sa'], $tgl_awal, $tgl_akhir);
    
                $transaksi = array();
                foreach($array_tgl as $row_tgl) {
                    // mutasi stok
                    $tgl_set = date('Y-m-d', strtotime($row_tgl));
                    $mutasi = $this->barang->getMutasiBarangByDate($row['id_barang'], $dt_user['cabang_id'], $tgl_set, $tgl_set); 
    
                    $qty_in_baru = 0;
                    $harga_in_baru = 0;
    
                    $qty_out_baru = 0;
                    foreach($mutasi as $row_mutasi) {
                        if($row_mutasi['jenis'] == 'in_tr' || $row_mutasi['jenis'] == 'in') {
                            $qty_in_baru = intval($row_mutasi['jml']);
                            $harga_in_baru = intval($row_mutasi['harga']);
                        } else {
                            $qty_out_baru += intval($row_mutasi['jml']);
                        }
                    }
                    // die;
                    $stok_in = array(
                        'qty' => $qty_in_baru,
                        'harga' => $harga_in_baru,
                    );
    
                    $stok_out = array(
                        'qty' => $qty_out_baru,
                    );
                    $saldo_akhir = array();
    
                    $transaksi[] = array(
                        'tgl' => $row_tgl,
                        'masuk' => $stok_in,
                        'keluar' => $stok_out,
                    );
                }
    
                $data['dt_transaksi'][] = array(
                    'id_barang' => $row['id_barang'],
                    'nm_barang' => $row['nama_barang'],
                    'saldo_awal' => $saldo_awal,
                    'transaksi' => $transaksi,
                );
            }

            $this->template->load('templates/dashboard', 'laporan/hppperhari', $data);
        }
    }

    // perhitungan hpp
    private function saldo_awal_hpp()
    {
         $tgl_awal = date('Y-m-d', strtotime('1/1/1971'));
         $tgl_akhir = date('Y-m-d', strtotime('-1 days', strtotime($tglAwal)));
         $saldo_awal = $this->db->get_where('p_item', ['item_id' => $item_id])->row_array();
         $data_persediaan = $this->Stok_m->getData($item_id, $tgl_awal, $tgl_akhir);

         $qty = $saldo_awal['stok'];
         $harga = $saldo_awal['harga'];
         if ($data_persediaan != null) {
            foreach ($data_persediaan as $p) :
               if ($p['stts'] == 0) {
                  $qty -= $p['qty'];
                  $harga_rata_rata = $harga;
               } else {
                  $harga_rata_rata = (($qty * $harga) + ($p['qty'] * $p['harga'])) / ($qty + $p['qty']);
                  $qty += $p['qty'];
               }

               $harga = $harga_rata_rata;
            endforeach;
         } else {
            $harga_rata_rata = $harga;
         }
    }


}
