<style type="text/css">
table {
	border-collapse: collapse;
}

th {
	border: 1px solid #000;
}

.center {
  text-align: center;
  vertical-align: middle;
}

table{
    table-layout: fixed;
}

td{
    word-wrap:break-word
}

.border{
	border: 1px solid #000;
}

.align-right{
	text-align: right;
}

.align-center{
	text-align: center;
}
</style>

<table>
	<tbody>
	<tr>
		<td colspan="10" class="center"><h3><b>MUTASI PERSEDIAAN GUDANG</b></h3></td>
	</tr>
	@php
	$barang = "Semua Barang";
	if(count($induk['barang']) > 0){
        $barang = $induk['barang']->nama_barang;
    }
    if($induk['tanggal_awal'] != '' && $induk['tanggal_akhir'] != ''){
    	$tgl = date_range($induk['tanggal_awal'], $induk['tanggal_akhir']);
    }
	@endphp
	<tr>
		<td width="5%"></td>
		<td width="15%">Nama Barang</td>
		<td width="5%">:</td>
		<td width="30%">{{ $barang }}</td>

		<td width="15%">Nama Kategori</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['kategori'] != '') ? $induk['kategori']:" " }}</td>
	</tr>
	<tr>
		<td width="5%"></td>
		<td width="15%">Periode</td>
		<td width="5%">:</td>
		<td width="30%">{{ $tgl }}</td>

		<td width="15%">Warna</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['nama_komposisi'] != '') ? $induk['nama_komposisi']:"Semua Warna" }}</td>
	</tr>
	<tr>
		<td width="5%"></td>
		<td width="15%">Gudang</td>
		<td width="5%">:</td>
		<td width="30%">{{ ($induk['nama_gudang'] != '') ? $induk['nama_gudang']:"-" }}</td>

		<td width="15%">Lokasi</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['nama_lokasi'] != '') ? $induk['nama_lokasi']:$induk['lokasi_gudang'] }}</td>
	</tr>

	<tr>
		@php
		if($induk['nama_proses'] != ''){
		@endphp
		<td width="5%"></td>
		<td width="15%">Nama Proses</td>
		<td width="5%">:</td>
		<td width="30%">{{ $induk['nama_proses'] }}</td>
		@php
		}else{
		@endphp
		<td colspan="5"></td>
		@php
		}
		@endphp		
		<td colspan="5"></td>
	</tr>
	</tbody>
</table>
<br>
<table>
	<thead>
	<tr>
		<th rowspan="2" class="center">NO</th>
		<th rowspan="2" colspan="3" class="center">KET. BARANG</th>
		<th class="center" colspan="5">JUMLAH BARANG</th>
		<th rowspan="2" class="center">SATUAN</th>
	</tr>
	<tr>
		<th colspan="1"></th>
		<th colspan="3"></th>
		<th class="center">AWAL</th>
		<th colspan="2" class="center">MASUK</th>
		<th class="center">KELUAR</th>
		<th class="center">SISA</th>
	</tr>
	</thead>
	<tbody>
		@php 
		$no = 1;
		$jumlah = array();
		foreach($detail as $d){
			if(in_array($d['id_proses'], setup_corak())){
                $nama_barang = get_stripe([$d['nama_motif']]); //, $d['inisial_komposisi']
            }else{
                $nama_barang = get_stripe([$d['nama_barang'], $d['nama_motif'], $d['inisial_komposisi'], $d['no_workorder_barang']]);
            } 
			if($d['awal'] != 0 || $d['masuk'] != 0 || $d['keluar'] != 0 || $d['sisa'] != 0){
		@endphp
		<tr>
			<td class="border">{{ $no }}</td>
			<td colspan="3" class="border">{{ $nama_barang }}</td>
			<td class="border align-right">{{ format_angkakoma($d['awal']) }}</td>
			<td colspan="2" class="border align-right">{{ format_angkakoma($d['masuk']) }}</td>
			<td class="border align-right">{{ format_angkakoma($d['keluar']) }}</td>
			<td class="border align-right">{{ format_angkakoma($d['sisa']) }}</td>
			<td class="border center">{{ $d['nama_satuan'] }}</td>
		</tr>
		@php
			$no++;
				if(isset($jumlah[$d['id_satuan']])){
	            	$jumlah[$d['id_satuan']] = ['nama_satuan' => $d['nama_satuan'], 'awal' => $jumlah[$d['id_satuan']]['awal']+$d['awal'] ,'masuk' => $jumlah[$d['id_satuan']]['masuk']+$d['masuk'], 'keluar' => $jumlah[$d['id_satuan']]['keluar']+$d['keluar'], 'sisa' => $jumlah[$d['id_satuan']]['sisa']+$d['sisa']];
	            }else{
	            	$jumlah[$d['id_satuan']] = ['nama_satuan' => $d['nama_satuan'], 'awal' => $d['awal'], 'masuk' => $d['masuk'], 'keluar' => $d['keluar'], 'sisa' => $d['sisa']];
	            }
			}
		}
		@endphp

		@php
		$jum = array_merge($jumlah, array());
        $jumlah_total = 0;
        if($jumlah > 0){
            for($i=0; $i<count($jumlah); $i++){
                echo "
                <tr>
                <td colspan='4' class='border align-right'>Total</td>
                <td class='border align-right'>".format_angkakoma($jum[$i]['awal'])."</td>
                <td colspan='2' class='border align-right'>".format_angkakoma($jum[$i]['masuk'])."</td>
                <td class='border align-right'>".format_angkakoma($jum[$i]['keluar'])."</td>
                <td class='border align-right'>".format_angkakoma($jum[$i]['sisa'])."</td>
                <td class='border center'>".$jum[$i]['nama_satuan']."</td>
                </tr>
                ";
            }
        }else{
            foreach($jumlah as $j){
                echo "
                <tr>
                <td colspan='4' class='border align-right'>Total</td>
                <td class='border align-right'>".format_angkakoma($j['awal'])."</td>
                <td colspan='2' class='border align-right'>".format_angkakoma($j['masuk'])."</td>
                <td class='border align-right'>".format_angkakoma($j['keluar'])."</td>
                <td class='border align-right'>".format_angkakoma($j['sisa'])."</td>
                <td class='border center'>".$j['nama_satuan']."</td>
                </tr>
                ";   
            }  
        }
		@endphp
	</tbody>
</table>