<?php

function cek_login()
{
    $ci = get_instance();
    if (!$ci->session->has_userdata('login_session')) {
        set_pesan('silahkan login.');
        redirect('auth');
    }
}

function is_admin()
{
    $ci = get_instance();
    $role = $ci->session->userdata('login_session')['role'];

    $status = true;

    if ($role != '1') {
        $status = false;
    }

    return $status;
}

function is_petugas()
{
    $ci = get_instance();
    $role = $ci->session->userdata('login_session')['role'];

    $status = true;

    if ($role != '2') {
        $status = false;
    }

    return $status;
}

function is_user()
{
    $ci = get_instance();
    $role = $ci->session->userdata('login_session')['role'];

    $status = true;

    if ($role != '3') {
        $status = false;
    }

    return $status;
}

function set_pesan($pesan, $tipe = true)
{
    $ci = get_instance();
    if ($tipe) {
        $ci->session->set_flashdata('pesan', "<div class='alert alert-success'><strong>SUCCESS!</strong> {$pesan} <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div>");
    } else {
        $ci->session->set_flashdata('pesan', "<div class='alert alert-danger'><strong>ERROR!</strong> {$pesan} <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div>");
    }
}

function userdata($field)
{
    $ci = get_instance();
    $ci->load->model('Admin_model', 'admin');

    $userId = $ci->session->userdata('login_session')['user'];
    return $ci->admin->get('user', ['id_user' => $userId])[$field];
}

function output_json($data)
{
    $ci = get_instance();
    $data = json_encode($data);
    $ci->output->set_content_type('application/json')->set_output($data);
}

function mutasi_stok($barang_id,$table,$operasi,$jumlah,$id_cabang = '') {
    $ci = get_instance();
    $ci->load->model('Barang_model','barang');
    $ci->barang->mutasi_stok($barang_id,$table,$operasi,$jumlah,$id_cabang);
}

function array_except($array, $keys) {
    return array_diff_key($array, array_flip((array) $keys));   
  } 
  


// terkait tanggal

function convertIndoDate($tanggal){
    $bulan = array (
    1 =>   'Januari',
    'Februari',
    'Maret',
    'April',
    'Mei',
    'Juni',
    'Juli',
    'Agustus',
    'September',
    'Oktober',
    'November',
    'Desember'
    );
    
    $pecahkan = explode('-', $tanggal);

    return $pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0];
}

 
function  getBulan($bln){
    switch  ($bln){
        case  1:
        return  "Januari";
        break;
        case  2:
        return  "Februari";
        break;
        case  3:
        return  "Maret";
        break;
        case  4:
        return  "April";
        break;
        case  5:
        return  "Mei";
        break;
        case  6:
        return  "Juni";
        break;
        case  7:
        return  "Juli";
        break;
        case  8:
        return  "Agustus";
        break;
        case  9:
        return  "September";
        break;
        case  10:
        return  "Oktober";
        break;
        case  11:
        return  "November";
        break;
        case  12:
        return  "Desember";
        break;
    }
}


function get_stts_impor($id_stts)
{        
    $data['dt_stts'] = array(
        array('id_stts' => 1, "nm_stts" => "Barang Masuk"),
        array('id_stts' => 2, "nm_stts" => "Barang Keluar"),
        array('id_stts' => 3, "nm_stts" => "Barang Transfer"),
        array('id_stts' => 4, "nm_stts" => "Stock Opname"),
    );
    
    $nm_stts = '';
    foreach($data['dt_stts'] as $row) {
        if($row['id_stts'] == $id_stts) {
            $nm_stts = $row['nm_stts'];
        }
    }

    return $nm_stts;
}