<?php
ob_start();
include "../include/config.php";
include "../function/inc_function.php";

$start_date = $_REQUEST['fromdate'];
$end_date   = $_REQUEST['todate'];
$month 		= substr($start_date, 5, 2);
$year		= substr($start_date, 0, 4);
$year_sto   = substr($end_date, 0, 4);
$month_sto  = substr($end_date, 5, 2);
$cek        = $month - 1;
if ($cek == 0) {
	$bln = 12;
	$thn = $year - 1;
} else {
	$bln = $cek;
	$thn = $year;
}
$thnbln = "$year$month";
$sqlShow 	= mysql_query("SELECT prof_name FROM profil");
$dataShow 	= mysql_fetch_array($sqlShow);
#cek closing
$cekclg = mysql_fetch_array(mysql_query("SELECT stock_date FROM stock WHERE stock_date='$start_date' AND stock_status=10 AND stock_loc<>'' GROUP BY stock_date"));
if (empty($cekclg['stock_date'])) {
	$sdate = mysql_fetch_array(mysql_query("SELECT MAX(stock_date) stock_date FROM stock WHERE stock_date<='$start_date' AND stock_status=10 AND stock_loc<>''"));
	$start_date_sa = $sdate['stock_date'];
	$end_date_sa   = date('Y-m-d', strtotime('-1 days', strtotime($start_date)));
	$bulan 	  	   = substr($start_date_sa, 5, 2);
	$tahun	  	   = substr($start_date_sa, 0, 4);	
}
$s_stock = isset($_REQUEST['s_stock']) ? $_REQUEST['s_stock'] : 101;
$sthn = substr($start_date,0,4); $ethn = substr($end_date,0,4);
?>
<html>

<head>
	<link rel='stylesheet' type='text/css' href="../include/css/printdoc_styles.css" />
</head>

<body>
	<table class="tables_inout1">
		<tr>
			<td colspan="12">LAPORAN MUTASI BARANG JADI</td>
		</tr>
		<tr>
			<td colspan="12">KAWASAN BERIKAT <?= strtoupper($dataShow['prof_name']); ?></td>
		</tr>
		<tr>
			<td colspan="12">PERIODE <?= DateToIndo($start_date); ?> S/D <?= DateToIndo($end_date); ?> </td>
		</tr>
		<tr>
			<td colspan="12">&nbsp;</td>
		</tr>
	</table>
	<?php if($sthn>=2025 && $ethn>=2025){ ?>
		<table style="width:100%;" class="tables_inout">
			<tr>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:30px;">No.</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Kode Barang</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Nama Barang</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Satuan</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Jml.Brg</th>
				<th bgcolor="#D1D1D1" style="width:80px;">Saldo Awal</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pemasukan</th>			
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pengeluaran</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Penyesuaian</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Temporary</th>
				<th bgcolor="#D1D1D1" style="width:80px;">Saldo Buku</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Stock Opname</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Selisih</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Keterangan</th>
			</tr>
			<tr>
				<th align="center" bgcolor="#D1D1D1"><?= DateToIndo($start_date); ?></th>
				<th align="center" bgcolor="#D1D1D1"><?= DateToIndo($end_date); ?></th>
			</tr>
			<?php
			$no = 1;
			$val = valstockopname($month_sto, $year_sto, 2);
			if (empty($val)) {
				$sto = 2;
			} else {
				$sto = 1;
			}
			#$qd = mysql_query("CALL spvw_mutasibj('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_type, a.prod_sat, a.prod_cat, b.qty as beg_qty, 
					ifnull(k.qty,0) as tpin_qty, ifnull(l.qty,0) as tpout_qty, 
					(ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0)) as in_qty, 
					(ifnull(d.qty,0) + ifnull(g.qty,0)+ ifnull(j.qty,0)) as out_qty,
					e.qty as adj_qty, f.qty as stock_opname		
					FROM product a             
					LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty 
					FROM stock WHERE MONTH(stock_date)='$month' AND YEAR(stock_date)='$year' AND stock_status=10 AND stock_loc=3 
					GROUP BY stock_prodid) AS b ON b.stock_prodid = a.id

					LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty 
					from transfer_detail a 
					inner join transfer b on b.id = a.trfd_hid
					where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date' and '$end_date' 
					group by trfd_prodid) AS c ON c.trfd_prodid=a.id  

					LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty 
					from outgoing_detail a 
					inner join outgoing b on b.id = a.outd_hid
					where b.outh_status=20 and outh_docdate between '$start_date' and '$end_date'
					group by outd_prodid) AS d ON d.outd_prodid=a.id	

					LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty 
					from adjusment_detail a
					inner join adjusment b on b.id=a.adjd_hid
					where b.adjh_status=20 and adjh_date between '$start_date' and '$end_date'
					group by adjd_prodid) AS e ON e.adjd_prodid=a.id

					LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty 
					from stockopname_detail a 
					inner join stockopname b on b.id=a.stod_hid
					where b.stoh_status=20 and b.stoh_date='$end_date' and b.stoh_prodcat=3
					group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  

					LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty 
					from transfer_detail a 
					inner join transfer b on b.id = a.trfd_hid
					where b.trf_status=20 and b.trf_category IN (5,6) and trf_spbdate between '$start_date' and '$end_date'
					group by trfd_prodid) AS g ON g.trfd_prodid=a.id

					LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty 
					from incoming_detail a 
					inner join incoming b on b.id = a.incd_hid
					where b.inch_status=20 and inch_docdate between '$start_date' and '$end_date' 
					group by incd_prodid) AS h ON h.incd_prodid=a.id

					LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty 
					from konversi_external where kv_status=20 and kv_date between '$start_date' and '$end_date' 
					group by kv_prodid) AS i ON i.kv_prodid=a.id

					LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty 
					from konversi_external_detail a 
					INNER JOIN konversi_external b ON b.id=a.kvd_hid  
					where b.kv_status=20 and b.kv_date between '$start_date' and '$end_date'
					group by a.kvd_prodid) AS j ON j.kvd_prodid=a.id	

					LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty 
					from konversi_barangjadi where kv_status=20 and kv_date between '$start_date' and '$end_date' 
					group by kv_prodid) AS k ON k.kv_prodid=a.id

					LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty 
					from konversi_barangjadi_detail a 
					INNER JOIN konversi_barangjadi b ON b.id=a.kvd_hid  
					where b.kv_status=20 and b.kv_date between '$start_date' and '$end_date' 
					group by a.kvd_prodid) AS l ON l.kvd_prodid=a.id

					WHERE a.prod_cat=3 ORDER BY a.prod_code ASC;");
			while ($dd = mysql_fetch_object($qd)) {
				#tanggal stock awal
				if (!empty($cekclg['stock_date'])) {
					$beg_qty  = $dd->beg_qty;
				} else {
					$beg_qty  = get_stock($start_date_sa, $end_date_sa, $bulan, $tahun, $dd->id, $dd->prod_cat, $inv);
				}
				
				#$end_qty = number_format($beg_qty, 2, '.', '') + number_format($dd->in_qty, 2, '.', '') - number_format($dd->out_qty, 2, '.', '') + number_format($dd->adj_qty, 2, '.', '');
				$temp_qty = number_format($beg_qty, 2, '.', '') + number_format($dd->in_qty, 2, '.', '') - number_format($dd->tpout_qty, 2, '.', '') + number_format($dd->adj_qty, 2, '.', '');
				$end_qty  = number_format($dd->tpin_qty, 2, '.', '') - number_format($dd->out_qty, 2, '.', '');
				if ($sto == 1) {
					if ((number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '')) > 0) {
						$ket = "Selisih Lebih";
					} elseif ((number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '')) < 0) {
						$ket = "Selisih Kurang";
					} else {
						$ket = "Sesuai";
					}
					$dif_qty = number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '');
				} elseif ($sto == 2) {
					if (number_format($end_qty, 2, '.', '') > 0) {
						$ket = "Normal";
					} elseif (number_format($end_qty, 2, '.', '') < 0) {
						$ket = "Abnormal";
					} else {
						$ket = "Normal";
					}
					$dif_qty = 0;
				}
				if (($s_stock == "101" && $end_qty > 0) || ($s_stock == "102" && $end_qty < 0) || ($s_stock == "103")) {
			?>
				<tr>
					<td align="center"><?= $no; ?></td>
					<td align="left"><?= $dd->prod_code; ?></td>
					<td align="left">
						<?php
						if (strlen($dd->prod_name) <= 40) {
							echo substr($dd->prod_name, 0, 40) . "<br>";
						} else {
							echo substr($dd->prod_name, 0, 40) . "<br>";
							echo substr($dd->prod_name, 40, 40) . "<br>";
							echo substr($dd->prod_name, 80, 40) . "<br>";
						}

						?>
					</td>
					<td align="center"><?= $dd->prod_sat; ?></td>
					<td align="center">&nbsp;</td>
					<td align="right"><?= number_format($beg_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->in_qty, 2, ',', '.'); ?></td>				
					<td align="right"><?= number_format($dd->out_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->adj_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($temp_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($end_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->stock_opname, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dif_qty, 2, ',', '.'); ?></td>
					<td align="left"><?= $ket; ?></td>
				</tr>
			<?php $no++;} } ?>
		</table>
	<?php }else{ ?>
		<table style="width:100%;" class="tables_inout">
			<tr>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:30px;">No.</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Kode Barang</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Nama Barang</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Satuan</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Jml.Brg</th>
				<th bgcolor="#D1D1D1" style="width:80px;">Saldo Awal</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pemasukan</th>			
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pengeluaran</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Penyesuaian</th>
				<th bgcolor="#D1D1D1" style="width:80px;">Saldo Buku</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Stock Opname</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Selisih</th>
				<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Keterangan</th>
			</tr>
			<tr>
				<th align="center" bgcolor="#D1D1D1"><?= DateToIndo($start_date); ?></th>
				<th align="center" bgcolor="#D1D1D1"><?= DateToIndo($end_date); ?></th>
			</tr>
			<?php
			$no = 1;
			$val = valstockopname($month_sto, $year_sto, 2);
			if (empty($val)) {
				$sto = 2;
			} else {
				$sto = 1;
			}
			#$qd = mysql_query("CALL spvw_mutasibj('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_type, a.prod_sat, a.prod_cat, b.qty as beg_qty, 
					ifnull(k.qty,0) as tpin_qty, ifnull(l.qty,0) as tpout_qty, 
					(ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0)) as in_qty, 
					(ifnull(d.qty,0) + ifnull(g.qty,0)+ ifnull(j.qty,0)) as out_qty,
					e.qty as adj_qty, f.qty as stock_opname		
					FROM product a             
					LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty 
					FROM stock WHERE MONTH(stock_date)='$month' AND YEAR(stock_date)='$year' AND stock_status=10 AND stock_loc=3 
					GROUP BY stock_prodid) AS b ON b.stock_prodid = a.id

					LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty 
					from transfer_detail a 
					inner join transfer b on b.id = a.trfd_hid
					where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date' and '$end_date' 
					group by trfd_prodid) AS c ON c.trfd_prodid=a.id  

					LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty 
					from outgoing_detail a 
					inner join outgoing b on b.id = a.outd_hid
					where b.outh_status=20 and outh_docdate between '$start_date' and '$end_date'
					group by outd_prodid) AS d ON d.outd_prodid=a.id	

					LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty 
					from adjusment_detail a
					inner join adjusment b on b.id=a.adjd_hid
					where b.adjh_status=20 and adjh_date between '$start_date' and '$end_date'
					group by adjd_prodid) AS e ON e.adjd_prodid=a.id

					LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty 
					from stockopname_detail a 
					inner join stockopname b on b.id=a.stod_hid
					where b.stoh_status=20 and b.stoh_date='$end_date' and b.stoh_prodcat=3
					group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  

					LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty 
					from transfer_detail a 
					inner join transfer b on b.id = a.trfd_hid
					where b.trf_status=20 and b.trf_category IN (5,6) and trf_spbdate between '$start_date' and '$end_date'
					group by trfd_prodid) AS g ON g.trfd_prodid=a.id

					LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty 
					from incoming_detail a 
					inner join incoming b on b.id = a.incd_hid
					where b.inch_status=20 and inch_docdate between '$start_date' and '$end_date' 
					group by incd_prodid) AS h ON h.incd_prodid=a.id

					LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty 
					from konversi_external where kv_status=20 and kv_date between '$start_date' and '$end_date' 
					group by kv_prodid) AS i ON i.kv_prodid=a.id

					LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty 
					from konversi_external_detail a 
					INNER JOIN konversi_external b ON b.id=a.kvd_hid  
					where b.kv_status=20 and b.kv_date between '$start_date' and '$end_date'
					group by a.kvd_prodid) AS j ON j.kvd_prodid=a.id	

					LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty 
					from konversi_barangjadi where kv_status=20 and kv_date between '$start_date' and '$end_date' 
					group by kv_prodid) AS k ON k.kv_prodid=a.id

					LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty 
					from konversi_barangjadi_detail a 
					INNER JOIN konversi_barangjadi b ON b.id=a.kvd_hid  
					where b.kv_status=20 and b.kv_date between '$start_date' and '$end_date' 
					group by a.kvd_prodid) AS l ON l.kvd_prodid=a.id

					WHERE a.prod_cat=3 ORDER BY a.prod_code ASC;");
			while ($dd = mysql_fetch_object($qd)) {
				#tanggal stock awal
				if (!empty($cekclg['stock_date'])) {
					$beg_qty  = $dd->beg_qty;
				} else {
					$beg_qty  = get_stock($start_date_sa, $end_date_sa, $bulan, $tahun, $dd->id, $dd->prod_cat, $inv);
				}
				
				$end_qty = number_format($beg_qty, 2, '.', '') + number_format($dd->in_qty, 2, '.', '') - number_format($dd->out_qty, 2, '.', '') + number_format($dd->adj_qty, 2, '.', '');
				if ($sto == 1) {
					if ((number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '')) > 0) {
						$ket = "Selisih Lebih";
					} elseif ((number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '')) < 0) {
						$ket = "Selisih Kurang";
					} else {
						$ket = "Sesuai";
					}
					$dif_qty = number_format($dd->stock_opname, 2, '.', '') - number_format($end_qty, 2, '.', '');
				} elseif ($sto == 2) {
					if (number_format($end_qty, 2, '.', '') > 0) {
						$ket = "Normal";
					} elseif (number_format($end_qty, 2, '.', '') < 0) {
						$ket = "Abnormal";
					} else {
						$ket = "Normal";
					}
					$dif_qty = 0;
				}
				if (($s_stock == "101" && $end_qty > 0) || ($s_stock == "102" && $end_qty < 0) || ($s_stock == "103")) {
			?>
				<tr>
					<td align="center"><?= $no; ?></td>
					<td align="left"><?= $dd->prod_code; ?></td>
					<td align="left">
						<?php
						if (strlen($dd->prod_name) <= 40) {
							echo substr($dd->prod_name, 0, 40) . "<br>";
						} else {
							echo substr($dd->prod_name, 0, 40) . "<br>";
							echo substr($dd->prod_name, 40, 40) . "<br>";
							echo substr($dd->prod_name, 80, 40) . "<br>";
						}

						?>
					</td>
					<td align="center"><?= $dd->prod_sat; ?></td>
					<td align="center">&nbsp;</td>
					<td align="right"><?= number_format($beg_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->in_qty, 2, ',', '.'); ?></td>				
					<td align="right"><?= number_format($dd->out_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->adj_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($end_qty, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dd->stock_opname, 2, ',', '.'); ?></td>
					<td align="right"><?= number_format($dif_qty, 2, ',', '.'); ?></td>
					<td align="left"><?= $ket; ?></td>
				</tr>
			<?php $no++;} } ?>
		</table>
	<?php } ?>
	<?php
	#PDF
	$filename = "laporan_mutasi_brgjadi.pdf";
	$content = ob_get_clean();
	//$content = '<page style="font-family: Arial;">'.nl2br($content).'</page>';  
	require_once('../function/html2pdf/html2pdf.class.php');
	try {
		//$html2pdf = new HTML2PDF('P',array(240,280),'en', false, 'ISO-8859-15',array(0, 0, 0, 0));  
		$html2pdf = new HTML2PDF('L', 'A4', 'en', false, 'ISO-8859-15', array(0, 0, 0, 0));
		$html2pdf->setDefaultFont('Arial');
		$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
		$html2pdf->Output($filename);
	} catch (HTML2PDF_exception $e) {
		echo $e;
	}
	?>