<?php
	//Bagian Tanggal
use Illuminate\Support\Facades\Session;

	function tgl_full($tgl, $jenis){
		$hari_h = array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu");
		$tg = date("d", strtotime($tgl));
		$bln = date("m", strtotime($tgl));
		$bln2 = date("m", strtotime($tgl));
		$thn = date("Y", strtotime($tgl));
		$bln_h = array('01' => "Januari", "02" => "Februari", "03" => "Maret", "04" => "April", "05" => "Mei", "06" => "Juni", "07" => "Juli", "08" => "Agustus", "09" => "September", "10" => "Oktober", "11" => "Nopember", "12" => "Desember");
		$bln = $bln_h[$bln];
		$hari = $hari_h[date("w", strtotime($tgl))];

		$jam = date('H');
		$menit = date('i');
		$detik = date('s');

		$get_jam = date("H", strtotime($tgl));	
		$get_menit = date("i", strtotime($tgl));
		$get_detik = date("s", strtotime($tgl));

		if($jenis == '0'){
			$print = $tg.' '.$bln.' '.$thn;
		}else if($jenis == '1'){
			$print = $hari.', '.$tg.' '.$bln.' '.$thn;
		}else if($jenis == '2'){
			$print = $thn.'-'.$bln2.'-'.$tg;
		}else if($jenis == '3'){
			$print = $tg."/".$bln2;
		}else if($jenis == '4'){
			$print = strtotime($tgl);
		}else if($jenis == '5'){
			$print = $thn."-".$bln2."-".$tg." ".$jam.":".$menit.":".$detik;
		}else if($jenis == '6'){
			$print = $tg."-".$bln2."-".$thn." ".$jam.":".$menit;
		}else if($jenis == '98'){
			$print = $tg."-".$bln2."-".$thn;
		}else if($jenis == '99'){
			$print = $thn."-".$bln2."-".$tg;
		}else if($jenis == '100'){
			$print = $get_jam.":".$get_menit;
		}
		else{
			$print = $tg.'-'.$bln2.'-'.$thn;
		}
		return $print;
	}

	function date_range($tgl_awal, $tgl_akhir){
		$hari = date('j', strtotime($tgl_awal));
		$hari2 = date('j', strtotime($tgl_akhir));

		$bln = date('m', strtotime($tgl_awal));
		$bln2 = date('m', strtotime($tgl_akhir));

		$thn = date('Y', strtotime($tgl_awal));
		$thn2 = date('Y', strtotime($tgl_akhir));

		if($thn == $thn2){
			if($bln == $bln2){
				if($hari == $hari2){
					$tahun = $hari.' '.bln_tostr($bln).' '.$thn;
				}else{
					$tahun = $hari.' - '.$hari2.' '.bln_tostr($bln).' '.$thn;
				}
			}else{
				$tahun = $hari.' '.bln_tostr($bln).' - '.$hari2.' '.bln_tostr($bln2).' '.$thn;
			}
		}else{
			$tahun = $hari.' '.bln_tostr($bln). ' '.$thn.' - '.$hari2.' '.bln_tostr($bln2).' '.$thn2;
		}

		return $tahun;
	}

	function bln_tostr($bln = ''){
		$bln_h = array('01' => "Januari", "02" => "Februari", "03" => "Maret", "04" => "April", "05" => "Mei", "06" => "Juni", "07" => "Juli", "08" => "Agustus", "09" => "September", "10" => "Oktober", "11" => "Nopember", "12" => "Desember");

		return $bln_h[sprintf("%02d", $bln)];
	}

	function date_db($value, $reverse = false, $ver=1)
	{
		/* date format on $value d/m/Y */
		if($ver==1){
			if($reverse){ // Y-m-d to d/m/Y
				$date = explode("-", $value);
				return implode("/", array_reverse($date));

			}else{ // d/m/Y to Y-m-d
				$date = explode("/", $value);
				return implode("-", array_reverse($date));
				
			}
		}
		/* date format on $value d-m-Y */
		else if($ver==2){
			if($reverse){ // Y-m-d to d-m-Y
				$date = explode("-", $value);
				return implode("-", array_reverse($date));

			}else{ // d-m-Y to Y-m-d
				$date = explode("-", $value);
				return implode("-", array_reverse($date));
				
			}
			
		}
	}

	function cek_tanggal($var){
		$return = '';
		if($var == '01-01-1970'){
			$return = '';
		}

		return $return;
	}

	/* data format */
	function format_tampil_data_barang($data, $sparator=" ")
	{
		if(is_array($data)){
			return "$data[kode_barang] - ".$sparator.$data['nama_barang'];
		}else{
			if(!is_null($data)){
				return "$data->kode_barang - ".$sparator.$data->nama_barang;
			}
		}
		return "None";
	}

	function format_tampil_data_pelanggan($data, $sparator=" - ")
	{
		if(is_array($data)){
			if ($data['pic'] == "") {
				return "$data[nama]";
			} else {
				return "$data[nama]".$sparator.$data['pic'];
			}
		}else{
			if(!is_null($data)){
				if ($data->pic == "") {
					return $data->nama;
				} else {
					return $data->nama.$sparator.$data->pic;
				}
			}
		}
		return "None";
	}

	function nomor_orderbeli($data, $sparator="")
	{
		if(is_array($data)){
			if(isset($data['no_order'])){
				return "$data[no_order]".$sparator.$data['sufix_no_order'];
			}
			return "$data[no_faktur]".$sparator.$data['sufix_no_order'];

		}else{
			if(!is_null($data)){

				if(isset($data->no_order)){
					return "$data->no_order".$sparator.$data->sufix_no_order;
				}
				return "$data->no_faktur".$sparator.$data->sufix_no_order;
			}
		}

		return "None";
	}

	function nomor_pembelian($data, $sparator="")
	{
		if(is_array($data)){
			if(isset($data['no_order'])){
				return "$data[no_order]".$sparator.$data['sufix_no_faktur'];
			}
			return "$data[no_faktur]".$sparator.$data['sufix_no_faktur'];

		}else{
			if(!is_null($data)){

				if(isset($data->no_order)){
					return "$data->no_order".$sparator.$data->sufix_no_faktur;
				}
				return "$data->no_faktur".$sparator.$data->sufix_no_faktur;
			}
		}

		return "None";
	}

	function nomor_surat_jalan($data, $sparator="")
	{
		if(is_array($data)){
			return "$data[no_surat]".$sparator.$data['sufix_no_surat'];

		}else{
			if(!is_null($data)){
				return "$data->no_surat".$sparator.$data->sufix_no_surat;

			}
		}

		return "None";
	}

	function nomor_penjualan($data, $sparator="")
	{
		if(is_array($data)){
			return "$data[no_faktur]".$sparator.$data['sufix_no_faktur'];

		}else{
			if(!is_null($data)){
				return "$data->no_faktur".$sparator.$data->sufix_no_faktur;
				
			}
		}
		
		return "None";
	}

	function nomor_retur_penjualan($data, $sparator="")
	{
		if(is_array($data)){
			return "$data[no_retur]".$sparator.$data['sufix_no_retur'];

		}else{
			if(!is_null($data)){
				return "$data->no_retur".$sparator.$data->sufix_no_retur;
				
			}
		}
		
		return "None";
	}

	function nomor_orderservice($data, $sparator="")
	{
		if(is_array($data)){
			return "$data[no_nota]".$sparator.$data['sufix_no_nota'];

		}else{
			if(!is_null($data)){
				return "$data->no_nota".$sparator.$data->sufix_no_nota;
				
			}
		}
		
		return "None";
	}
	/* end : data format */

	// Bagian Uang
	function rupiah($uang){
		if(is_numeric($uang)){
			$uang = number_format($uang,0);
		}else{
			$uang = 0;
		}
		return $uang;
	}

	function rupiah2($uang, $decimal=0){
		if(is_numeric($uang)){
			$uang = number_format($uang,$decimal,",",".");
		}else{
			$uang = $uang;
		}

		return $uang;
	}

	function rounding($number, $decimal)
	{
		return round((float)$number, $decimal);
	}

	function format_angka($var){
		if(is_numeric($var)){
			$var = number_format($var, 0, '.', '.');
		}else{
			$var = 0;
		}

		return $var;
	}

	function format_angkav2($var){
		if(is_numeric($var)){
			
			if(floor($var) != $var){
				$var = number_format($var, 2, ',', '.');
			}else{
				$var = number_format($var, 0, ',', '.');
			}

		}else{
			$var = 0;
		}

		return $var;
	}

	function format_angkakoma($var){
		if(is_numeric($var)){
			
			if(floor($var) != $var){
				$var = number_format((float)$var, 2, ',', '');
			}else{
				$var = $var;
			}

		}else{
			$var = 0;
		}

		return $var;
	}

	function terbilang($number, $currency = 'Rupiah'){
		$before_comma = trim(to_word($number));
		$after_comma = trim(comma($number));
		return ucwords($results = $before_comma . " ".$currency);
	}

	function to_word($number){
		$words = "";
		$arr_number = array(
		"",
		"satu",
		"dua",
		"tiga",
		"empat",
		"lima",
		"enam",
		"tujuh",
		"delapan",
		"sembilan",
		"sepuluh",
		"sebelas");

		if($number<12)
		{
			$words = "".$arr_number[$number];
		}
		else if($number<20)
		{
			$words = to_word($number-10)." belas";
		}
		else if($number<100)
		{
			$words = to_word($number/10)." puluh ".to_word($number%10);
		}
		else if($number<200)
		{
			$words = "seratus ".to_word($number-100);
		}
		else if($number<1000)
		{
			$words = to_word($number/100)." ratus ".to_word($number%100);
		}
		else if($number<2000)
		{
			$words = "seribu ".to_word($number-1000);
		}
		else if($number<1000000)
		{
			$words = to_word($number/1000)." ribu ".to_word($number%1000);
		}
		else if($number<1000000000)
		{
			$words = to_word($number/1000000)." juta ".to_word($number%1000000);
		}
		else if($number<1000000000000)
		{
			$words = to_word($number/1000000000)." milyar ".to_word($number%1000000000);
		}
		else
		{
			$words = "undefined";
		}
		return $words;
	}

	function comma($number){
		$after_comma = stristr($number,',');
		$arr_number = array(
		"nol",
		"satu",
		"dua",
		"tiga",
		"empat",
		"lima",
		"enam",
		"tujuh",
		"delapan",
		"sembilan");

		$results = "";
		$length = strlen($after_comma);
		$i = 1;
		while($i<$length)
		{
			$get = substr($after_comma,$i,1);
			$results .= " ".$arr_number[$get];
			$i++;
		}
		return $results;
	}


	// ------------------------- Status -----------------------------

	function status_bayar_txt($id){
        switch ($id) {
            case '0':
                $d = 'Belum Bayar';
                break;
            case '1':
                $d = 'Proses Bayar';
            break;
            case '2':
                $d = 'Lunas';
            break;
            case '3':
                $d = 'Kelebihan Bayar';
            break;
            default:
                $d = 'None';
                break;
        }

        return $d;
    }
	function status_bayar($id){
        switch ($id) {
            case '0':
                $d = '<span class="label label-danger">Belum Bayar</span>';
                break;
            case '1':
                $d = '<span class="label label-warning">Proses Bayar</span>';
            break;
            case '2':
                $d = '<span class="label label-success">Lunas</span>';
            break;
            case '3':
                $d = '<span class="label label-info">Kelebihan Bayar</span>';
            break;
            default:
                $d = '<span class="label label-default"></span>';
                break;
        }

        return $d;
    }

	function status_penjualan($id){
        switch ($id) {
            case '0':
                $d = '<span class="label label-danger">Belum Dikeluarkan</span>';
                break;
            case '1':
                $d = '<span class="label label-warning">Sudah Dikeluarkan</span>';
            break;
            case '99':
                $d = '<span class="label label-primary">Faktur Belum Dibuat</span>';
            break;
            default:
                $d = '<span class="label label-default"></span>';
                break;
        }

        return $d;
    }

    function status_surat_jalan($value)
    {
        switch ($value) {
            case '0':
                $d = '<span class="label label-primary">Faktur Belum Dibuat</span>';
                break;
            case '1':
                $d = '<span class="label label-success">Faktur Dibuat</span>';
            break;
            default:
                $d = '<span class="label label-default">...</span>';
                break;
        }

        return $d;
    }

    function status_bon($id){
        switch ($id) {
            case '0':
                $d = '<span class="label label-warning bg-warning-600">Menunggu Gudang</span>';
                break;
            case '1':
                $d = '<span class="label label-warning">Dalam Proses</span>';
            	break;
            case '2':
                $d = '<span class="label label-success">Selesai</span>';
            	break;
            case '3';
            	$d = '<span class="label label-warning">Belum Selesai Terima</span>';
            	break;
            default:
                $d = '<span class="label label-default"></span>';
                break;
        }

        return $d;
    }  

    function status_transfer($id){
        switch ($id) {
            case '0':
                $d = '<span class="2 label label-danger">Menunggu Gudang</span>';
                break;
            case '1':
                $d = '<span class="1 label label-warning">Dalam Proses</span>';
            	break;
            case '2':
                $d = '<span class="0 label label-success">Selesai</span>';
            	break;
            default:
                $d = '<span class="label label-default"></span>';
                break;
        }

        return $d;
    } 

    function status_approve($id){
    	switch ($id) {
    		case '0':
    			$d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
    			break;
    		case '1':
    			$d = '<label class="label bg-primary-400">Terverifikasi</label>';
    			break;
    		default:
    			$d = '<label class="label bg-primary-400"></label>';
    			break;
    	}

    	return $d;
    }

    function status_terima_barang($id){

    	switch ($id) {
    		case '0':
    			$d = '<label class="label bg-warning-400">Belum Diterima</label>';
    			break;
    		
    		default:
    			$d = '<label class="label bg-primary-400">Diterima</label>';
    			break;
    	}

		return $d;
	}

    function status_retur($id){
    	switch ($id) {
    		case '0':
    			$d = '<label class="label bg-warning-400">Belum Diterima</label>';
    			break;
    		case '1':
    			$d = '<label class="label bg-primary-400">Diterima</label>';
    			break;
    		default:
    			$d = '<label class="label bg-primary-400"></label>';
    			break;
    	}

    	return $d;
    }

    // ----------- Lain ----------------------


    function pembagi($var, $var2){
    	return round((float)$var/(float)$var2, 3);
    }

    function set_null_to_zerro($value)
    {
    	return ($value==null) || ($value=='') ? 0 : $value;
    }

    function get_null($var){
    	return ($var == 0) ? "":$var;
    }

    function get_empty($var){
    	return ($var == '') ? "":" ".$var;
    }

    function get_stripes($var){
    	$count = count($var);
    	$list = "";
    	for ($i=1 ; $i <= $count; $i++) { 
    		if($i == $count){
    			$list .= ($var[$i-1] != '') ? $var[$i-1]:"";
    		}else{
    			$list .= ($var[$i-1] != '') ? $var[$i-1]." - ":"";
    		}
    		
    	}

    	return $list;
    }

    function get_stripe($var){
    	return implode(' - ', array_filter($var));
    }

    function status_checkbox($val, $id = '', $jenis = ''){
    	switch ($val) {
    		case '1':
    			$return = '<input type="checkbox" id="check_verifikasi" value="'.$id.'" jenis="'.$jenis.'">';
    			break;
    		
    		default:
    			$return = '';
    			break;
    	}

    	return $return;
    }

    function cew($data = ''){
		if(!empty($data)){
			$d = ucwords(strtolower($data));
		}

		return $d;
	}

	function get_rawquery($func){
		$PDO = DB::connection('mysql')->getPdo();

        $billingStmt = $PDO->prepare("".$func."");
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

        return $usersBills;
	}


	function setup_corak(){
		return [22, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 94];
	}

	function setup_corakbon(){
		return [22, 24, 26, 27, 28, 29, 30, 31, 32, 33];
	}

	function setup_benang(){
		return ['', 18, 80, 81, 82, 84, 85, 96];
	}

	function setup_perkiraanhasil(){
		return [25, 24, 37];
	}

	function setup_hasilatm(){
		return [80, 24, 25, 37, 84, 96];
	}

	function perkiraan_atbm(){
		return [33];	
	}

	function perkiraan_atm(){
		return [];	
	}

	//penamaan barang
	function barang_byproses($var, $jenis = '', $proses = '', $ket_hasil = ''){
		$return = array();
		$id_proses = '';
		
		if($var != ''){
			if($jenis == 1){
				foreach($var as $v => $vdata){
					
					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $vdata['nama_proses']]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						if($vdata['id_perubahansatuan'] != ''){
							$sat = 'Untuk '.get_empty($vdata['jumlah_rencana']).get_empty($vdata['nama_perubahansatuan']);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $sat, $vdata['nama_proses']]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $vdata['nama_gudang'], $vdata['nama_proses']]);
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 2){
				foreach($var as $v => $vdata){
					
					if(in_array($vdata->id_proses, setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata->kategori).get_empty($vdata->nama_barang), $vdata->no_workorder_barang, $vdata->inisial_komposisi, $vdata->nama_proses]);
					}else if(in_array($vdata->id_proses, setup_corak())){
						if($vdata->id_corak != ''){
							$ket = $vdata->nama_corak;
						}else{
							$ket = $vdata->nama_motif;
						}

						if($vdata->id_perubahansatuan != ''){
							$sat = 'Untuk '.get_empty($vdata->jumlah_rencana).get_empty($vdata->nama_perubahansatuan);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata->inisial_komposisi, $vdata->no_workorder_barang, $sat, $vdata->nama_proses]);
					}else{
						$nama_barang = get_empty($vdata->nama_barang).' '.get_stripe([$vdata->nama_motif, $vdata->inisial_komposisi, $vdata->no_workorder_barang, $vdata->nama_proses]);
					}
					$vdata->concat_nama = $nama_barang;
					$return[] = $vdata;
				}
			}else if($jenis == '3'){

					
					if(in_array($var->id_proses, setup_benang())){
						$nama_barang = get_stripe([get_empty($var->kategori).get_empty($var->nama_barang), $var->no_workorder_barang, $var->inisial_komposisi]);
					}else if(in_array($var->id_proses, setup_corak())){
						if($var->id_corak != ''){
							$ket = $var->nama_corak;
						}else{
							$ket = $var->nama_motif;
						}

						

						$nama_barang = get_stripe([$ket, $var->inisial_komposisi, $var->no_workorder_barang]);
					}else{
						$nama_barang = get_empty($var->nama_barang).' '.get_stripe([$var->nama_motif, $var->inisial_komposisi, $var->no_workorder_barang]);
					}
					$var->concat_nama = $nama_barang;
					$return = $var;
				
			}else if($jenis == 4){
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata['nama_proses'];
					}

					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $id_proses]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						if($vdata['id_perubahansatuan'] != ''){
							$sat = 'Untuk '.get_empty($vdata['jumlah_rencana']).get_empty($vdata['nama_perubahansatuan']);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $sat, $id_proses]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $id_proses]); //$vdata['nama_gudang'],
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 5){ // Tenun
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata['nama_proses'];
					}

					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $id_proses]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $id_proses]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $id_proses]); //$vdata['nama_gudang'],
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 7){
				foreach($var as $v => $vdata){
					
					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $vdata['nama_proses'], $vdata['nama_gudang']]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						if($vdata['id_perubahansatuan'] != ''){
							$sat = 'Untuk '.get_empty($vdata['jumlah_rencana']).get_empty($vdata['nama_perubahansatuan']);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $sat, $vdata['nama_proses'], $vdata['nama_gudang']]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $vdata['nama_proses'], $vdata['nama_gudang']]);
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 6){ // Tenun dengan lainnya
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata['nama_proses'];
					}

					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $id_proses]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $vdata['nama_kualitas'], $id_proses]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $vdata['nama_kualitas'], $id_proses]); //$vdata['nama_gudang'],
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 100){
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata->nama_proses;
					}
					if(in_array($vdata->id_proses, setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata->kategori).get_empty($vdata->nama_barang), $vdata->no_workorder_barang, $vdata->inisial_komposisi, $id_proses]);
					}else if(in_array($vdata->id_proses, setup_corak())){
						if($vdata->id_corak != ''){
							$ket = $vdata->nama_corak;
						}else{
							$ket = $vdata->nama_motif;
						}

						if($vdata->id_perubahansatuan != ''){
							$sat = ''; //'Untuk '.get_empty($vdata->jumlah_rencana).get_empty($vdata->nama_perubahansatuan);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata->inisial_komposisi, $vdata->no_workorder_barang, $sat, $id_proses]);
					}else{
						$nama_barang = get_empty($vdata->nama_barang).' '.get_stripe([$vdata->nama_motif, $vdata->inisial_komposisi, $vdata->no_workorder_barang, $id_proses]);
					}
					$vdata->concat_nama = $nama_barang;
					$return[] = $vdata;
				}
			}else if($jenis == 101){
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata->nama_proses;
					}
					if(in_array($vdata->id_proses, setup_benang())){
						if($vdata->id_proses == ''){
							$nama_barang = get_stripe([get_empty($vdata->nama_barang), $vdata->inisial_komposisi, $id_proses]); //$vdata->no_workorder_barang,
						}else{
							$nama_barang = get_stripe([get_empty($vdata->kategori).get_empty($vdata->nama_barang), $vdata->inisial_komposisi, $id_proses]); //$vdata->no_workorder_barang,
						}
					}else if(in_array($vdata->id_proses, setup_corak())){
						if($vdata->id_corak != ''){
							$ket = $vdata->nama_corak." - ".$vdata->inisial_komposisi;
						}else{
							$ket = $vdata->nama_motif;
						}

						if($vdata->id_perubahansatuan != ''){
							$sat = ''; //'Untuk '.get_empty($vdata->jumlah_rencana).get_empty($vdata->nama_perubahansatuan);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket,  $sat, $id_proses]); //$vdata->no_workorder_barang,
					}else{
						$nama_barang = get_empty($vdata->nama_barang).' '.get_stripe([$vdata->nama_motif, $vdata->inisial_komposisi, $vdata->no_workorder_barang, $id_proses]);
					}
					$vdata->concat_nama = $nama_barang;
					$return[] = $vdata;
				}
			}else if($jenis == 102){
				foreach($var as $v => $vdata){
					
					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $vdata['nama_proses']]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						if($vdata['id_perubahansatuan'] != ''){
							// $sat = 'Untuk '.get_empty($vdata['jumlah_rencana']).get_empty($vdata['nama_perubahansatuan']);
							$sat = '';
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $sat, $vdata['nama_proses']]);
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $vdata['nama_gudang'], $vdata['nama_proses']]);
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}else if($jenis == 103){ //Laporan Rekap Corak
				foreach($var as $v => $vdata){
					if($proses == ''){
						$id_proses = $vdata['nama_proses'];
					}

					if(in_array($vdata['id_proses'], setup_benang())){
						$nama_barang = get_stripe([get_empty($vdata['kategori']).get_empty($vdata['nama_barang']), $vdata['no_workorder_barang'], $vdata['inisial_komposisi'], $id_proses]);
					}else if(in_array($vdata['id_proses'], setup_corak())){
						if($vdata['id_corak'] != ''){
							$ket = $vdata['nama_corak'];
						}else{
							$ket = $vdata['nama_motif'];
						}

						if($vdata['id_perubahansatuan'] != ''){
							$sat = 'Untuk '.get_empty($vdata['jumlah_rencana']).get_empty($vdata['nama_perubahansatuan']);
						}else{
							$sat = '';
						}

						$nama_barang = get_stripe([$ket, $vdata['inisial_komposisi'], $sat, $id_proses]); //$vdata['no_workorder_barang'],
					}else{
						$nama_barang = get_empty($vdata['nama_barang']).' '.get_stripe([$vdata['nama_motif'], $vdata['inisial_komposisi'], $vdata['no_workorder_barang'], $id_proses]); //$vdata['nama_gudang'],
					}
					$return[] = $vdata+['concat_nama' => $nama_barang];
				}
			}
			else{

			}

		}

		return $return;
	}
	
	function setNominal($value=0, $decimal=0)
    {
        if($value < 0){
            $set = "( ".rupiah2(abs($value), $decimal)." )";
        }else{
            $set = rupiah2($value, $decimal);
        }
        return $set;
    }

    function checkPembayaran($id_penjualan)
    {
        $data = DB::table('view_penjualan2')->where('id_penjualan', '=', $id_penjualan)->where('kode_sebagai', '=', 2)->get();

        $total_bayar = 0;
        $tagihan = 0;
        foreach ($data as $key => $value) {
            $total_bayar += $value->total_bayar;

            $retur = DB::table('a_pen_penjualan_retur_groupby_penjualan')->where('id_penjualan', $value->id_penjualan);

            if ($retur->count() > 0) {
                $tagihan = $value->nominal_netto-$retur->first()->nominal_total;
            } else {
                $tagihan = $value->nominal_netto;
            }
        }
       

        if($total_bayar == 0){
            $bayar = 0;//Belum Bayar
        }else if($total_bayar < $tagihan){
            $bayar = 1;//Proses Bayar
        }else if($total_bayar > $tagihan){
            $bayar = 3;//Kelebihan Bayar
        }else{
            $bayar = 2;//Lunas
        }

        return $bayar;
    }

    function get_status_master($id = ''){
    	switch ($id) {
    		case '1':
    			$ret = '<label class="label bg-success-400">Aktif</label>';
    			break;
    		case '0':
    			$ret = '<label class="label bg-danger-400">Tidak Aktif</label>';
    		default:
    			$ret = '<label class="label bg-danger-400">Tidak Aktif</label>';
    			break;
    	}

    	return $ret;
    }

    function get_ttdproduksi(){

    	$id_user = session::get('kode_user');
    	$d_user = DB::table('tb_user')->where('kode_user', $id_user);
    	$user = "";
    	if($d_user->count() > 0){
    		if(!in_array($d_user->first()->kode_group, [3, 12])){
    			$user = $d_user->first()->nama;
    		}
    	}

    	$arr = array('0' => array("nama" => "Mengetahui", "ttd" => "Owner"), '1' => array("nama" => "Dibuat Oleh", "ttd" => $user), '2' => array("nama" => "Petugas", "ttd" => ""));

    	return $arr;
    }

    function _getorderproduksi($id_proses, $alias = "A"){
        if(in_array($id_proses, setup_benang())){
            $orderby_proses = " ORDER BY ".$alias.".nama_proses, ".$alias.".nama_barang, ".$alias.".nama_komposisi";
        }else if(in_array($id_proses, setup_corak())){
            $orderby_proses = " ORDER BY ".$alias.".nama_corak, ".$alias.".nama_motif, ".$alias.".nama_komposisi";
        }else{
            $orderby_proses = " ORDER BY ".$alias.".nama_proses, ".$alias.".nama_barang, ".$alias.".nama_corak, ".$alias.".nama_motif, ".$alias.".nama_komposisi";
        }

        return $orderby_proses;
    }

    function _getgroupbystok($id_proses, $insial = "A"){
    	if(in_array($id_proses, setup_benang()) || $id_proses == 0){
            $orderby_proses = " ".$insial.".nama_subkategori, ".$insial.".nama_barang";
            $groupBy = "GROUP BY ".$insial.".id_barang, ".$insial.".id_proses, ".$insial.".id_komposisi, ".$insial.".id_satuan";
            $concat_rekap = ", CONCAT(".$insial.".nama_subkategori) id_sort";
        }else if(in_array($id_proses, setup_corak())){
        	if($id_proses == 24){
            	$orderby_proses = " ".$insial.".nama_motif";
            	$groupBy = "GROUP BY ".$insial.".id_barang, ".$insial.".id_motif, ".$insial.".id_proses, ".$insial.".id_satuan"; //".$insial.".id_komposisi,
            	$concat_rekap = ", CONCAT(".$insial.".nama_subkategori, CAST(".$insial.".nama_motif AS UNSIGNED)) id_sort";
            }else{
                $orderby_proses = " ".$insial.".nama_corak";
                $groupBy = "GROUP BY ".$insial.".id_barang, ".$insial.".id_corak, ".$insial.".id_proses, ".$insial.".id_komposisi, ".$insial.".id_satuan";
                $concat_rekap = ", CONCAT(".$insial.".nama_subkategori, CAST(".$insial.".nama_corak AS UNSIGNED)) id_sort";
            }
        }else{
        	$orderby_proses = " ".$insial.".nama_motif";
            $groupBy = "GROUP BY ".$insial.".id_barang, ".$insial.".id_motif, ".$insial.".id_corak, ".$insial.".id_proses, ".$insial.".id_satuan"; //".$insial.".id_komposisi,
            $concat_rekap = ", CONCAT(".$insial.".nama_subkategori, CAST(".$insial.".nama_motif AS UNSIGNED)) id_sort";
        }

        return $groupBy;
    }
	