<?php include "../koneksi.php"; ?>
<!DOCTYPE html>
<!-- Designined by CodingLab | www.youtube.com/codinglabyt -->
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="bootstrap.css">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
<body style="font-size: 11px">
<table class="table table-bordered" style="width:100%">
	<td width="20%">
		<text style="font-size: 16px; font-weight: bold;">LAPORAN HASIL PRODUKSI</text>
	</td>
</table>

<?php
	$data = mysqli_query ($koneksi, " select 
                                            plan.PlanID,
                                            plan.DatePlan,
                                            plan.MesinID,
                                            mesin.Mesin,
                                            plan.ProductID,
                                            product.ItemDescription,
                                            product.SLoc,
                                            product.QtyUnit,
                                            plan.QtyPlan,
                                            plan.Prioritas,
                                            plan.MaterialID,
                                            material.RawMat,
                                            plan.POOR
                                    from 
                                    plan
                                    INNER JOIN mesin
                                    ON plan.MesinID=mesin.MesinID
                                    INNER JOIN product
                                    ON plan.ProductID=product.ProductID
                                    INNER JOIN material
                                    ON plan.MaterialID=material.MaterialID
                                    WHERE PlanID ='$_GET[id]'");
    $row = mysqli_fetch_array ($data);
?>
<table class="table table-bordered">
   	<tr>
   		<td width="30%"><text style="font-size: 13px; font-weight: bold;">TANGGAL:  <?php echo date ("d-m-Y", strtotime( $row['DatePlan'])); ?></text></td>
   		<td width="30%"><text style="font-size: 13px; font-weight: bold;">MESIN :  <?php echo $row['Mesin']; ?></text></td>
   	</tr>
</table>
<?php for( $i=1; $i <= 3; $i++ ) {
  $data1 = mysqli_query ($koneksi, " select *
                                    from 
                                    lap_prod 
                                    where PlanID='$_GET[id]' and Shift='$i'");
  $count = mysqli_num_rows($data1);
  if($count>0) {
?>
<table class="table table-bordered">
   	<tr >
   		<td width="30%" colspan="2"><text style="font-size: 13px; font-weight: bold;">SHIFT <?php echo $i; ?></text></td>
   	</tr>
</table>
<table class="table table-bordered">
   	<tr>
     <td width="50%" style="text-align: left; padding-left: 5px">POOR:  <?php echo $row['POOR']; ?></td>
      <td width="50%" style="text-align: left; padding-left: 5px">KEMASAN :  <?php echo $row['SLoc']; ?>&ensp;<?php echo $row['QtyUnit']; ?></td>
   	</tr>
   	<tr>
      <td width="50%" style="text-align: left; padding-left: 5px">PRODUCT NAME:  <?php echo $row['ItemDescription']; ?></td>
      <td width="50%" style="text-align: left; padding-left: 5px">MATERIAL :  <?php echo $row['RawMat']; ?></td>
   	</tr>
</table>
<table class="table table-bordered">
	<?php
		$data4 = mysqli_query ($koneksi, " SELECT count(stpj.STPJ_ID) AS stpj
	                        FROM stpj
	                        WHERE stpj.PlanID ='$_GET[id]' AND Shift='$i'");
	  $row4 = mysqli_fetch_array($data4);
		$data3 = mysqli_query ($koneksi, "SELECT *
                          FROM lap_prod
                          where PlanID='$_GET[id]' AND Shift='$i'");
	  $row3 = mysqli_fetch_array($data3);
    $data11 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Shift ='$i' AND Jenis ='1'");
    $row11 = mysqli_fetch_array($data11);
    $data12 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Shift ='$i' AND Jenis ='2'");
    $row12 = mysqli_fetch_array($data12);
	    $pack3 =  $row4['stpj'];
	    $stpj2 = $row4['stpj']*$row['QtyUnit'];
	    $sisa2 = $row3['ProdOK']+$row3['Awal']-$stpj2;
	    $total1 = $row3['ProdOK']+$row11['Qty']+$row12['Qty']+$row3['Pending'];
	?>
	<tr>
     <td width="22%" style="text-align: left; padding-left: 5px">PRODUCT OK:</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row3['ProdOK'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">OP 0020 :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($stpj2, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
    </tr>
    <tr>
    <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="22%" style="text-align: left; padding-left: 5px">REJECT BOTOL:</td>
     <?php } else { ?>
      <td width="22%" style="text-align: left; padding-left: 5px">REJECT PREFORM:</td>
      <?php } ?>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row11['Qty'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">SET UP TIME :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo date("H:i", strtotime($row3['SetupTime'])); ?></td>
      <td width="8%">H</td>
    </tr>
    <tr>
    <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="22%" style="text-align: left; padding-left: 5px">REJECT PREFORM:</td>
     <?php } else { ?>
      <td width="22%" style="text-align: left; padding-left: 5px">REJECT GUMPALAN:</td>
      <?php } ?>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row12['Qty'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">UP TIME :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo date("H:i", strtotime($row3['UpTime'])); ?></td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">PENDING :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row3['Pending'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT MACHINE:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
      	$data7 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='1' AND Shift='$i'");
          $row7 = mysqli_fetch_array($data7);
        ?>
        <text><?php echo date("H:i", strtotime($row7['totdtm'])); ?></text>
      </td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">TOTAL :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($total1, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT EQUIPMENT:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
         $data8 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='2' AND Shift='$i'");
          $row8 = mysqli_fetch_array($data8);
        ?>
        <text><?php echo date("H:i", strtotime($row8['totdtm'])); ?></text>
        </td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">AWAL :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row3['Awal'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT TOOLS:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
         $data9 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='3' AND Shift='$i'");
          $row9 = mysqli_fetch_array($data9);
        ?>
        <text><?php echo date("H:i", strtotime($row9['totdtm'])); ?></text>
        </td>
      <td width="8%">H</td>
    </tr>
    <td rowspan=2 width="22%" style="text-align: left; padding-left: 5px">STPJ :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo $pack3; ?></td>
      <td width="8%">PLT</td>
      <td rowspan=4 width="22%" style="text-align: left; padding-left: 5px">CATATAN :</td>
      <td rowspan='4' colspan="2" width="22%" style="text-align: left; padding-left: 5px">
        <?php echo $row3['Catat'].'<br>';
          $data10 = mysqli_query ($koneksi, "SELECT Reason
                                              FROM downtime
                                        WHERE PlanID='$_GET[id]' AND Shift='$i'
                                        ORDER BY Shift, Jenis ASC");
          
          $totaldt= '';
          while($row10 =mysqli_fetch_array($data10)) {
          $dt = $row10['Reason'].',';
          $totaldt .= $dt;
          }
          echo $totaldt;
        ?>    
      </td>
    </tr>
     <tr>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($stpj2, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
    </tr>
     <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">SISA PRODUKSI:</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo $sisa2; ?></td>
      <td width="8%">PCS</td>
    </tr>
     <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">SPEED:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">16.800</td>
      <td width="8%">BPH</td>
    </tr>
</table>
<?php }} ?>
<table class="table table-bordered">
   	<tr >
   		<td width="30%" colspan="2"><text style="font-size: 13px; font-weight: bold;">TOTAL SHIFT</text></td>
   	</tr>
   	<tr>
     <td width="50%" style="text-align: left; padding-left: 5px">POOR:  <?php echo $row['POOR']; ?></td>
      <td width="50%" style="text-align: left; padding-left: 5px">KEMASAN :  <?php echo $row['SLoc']; ?>&ensp;<?php echo $row['QtyUnit']; ?></td>
   	</tr>
   	<tr>
      <td width="50%" style="text-align: left; padding-left: 5px">PRODUCT NAME:  <?php echo $row['ItemDescription']; ?></td>
      <td width="50%" style="text-align: left; padding-left: 5px">MATERIAL :  <?php echo $row['RawMat']; ?></td>
   	</tr>
</table>
	<?php
		$data6 = mysqli_query ($koneksi, " SELECT count(stpj.STPJ_ID) AS stpj
	                        FROM stpj
	                        WHERE stpj.PlanID ='$_GET[id]'");
    $row6 = mysqli_fetch_array($data6);
    $data13 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Jenis ='1'");
    $row13 = mysqli_fetch_array($data13);
    $data14 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Jenis ='2'");
    $row14 = mysqli_fetch_array($data14);
		$data2 = mysqli_query ($koneksi, "SELECT SUM(ProdOk) AS ProdOk,
                                      SUM(Pending) AS Pending,
                                      SUM(Awal) AS Awal,
                                      Catat,
                                      sec_to_time(SUM(TIME_TO_SEC(SetupTime))) AS SetupTime,
                                      sec_to_time(SUM(TIME_TO_SEC(UpTime))) AS UpTime
                          FROM lap_prod
                          where PlanID='$_GET[id]'");
	    $row2 = mysqli_fetch_array($data2);
	    $pack4 =  $row6['stpj'];
	    $stpj1 = $row6['stpj']*$row['QtyUnit'];
	    $sisa2 = $row2['ProdOk']-$stpj1;
	    $total1 = $row2['ProdOk']+$row13['Qty']+$row14['Qty']+$row2['Pending'];
	?>
  <table class="table table-bordered">
	<tr>
     <td width="22%" style="text-align: left; padding-left: 5px">PRODUCT OK:</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row2['ProdOk'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">OP 0020 :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($stpj1, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
    </tr>
    <tr>
    <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="22%" style="text-align: left; padding-left: 5px">REJECT BOTOL:</td>
     <?php } else { ?>
      <td width="22%" style="text-align: left; padding-left: 5px">REJECT PREFORM:</td>
      <?php } ?>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row13['Qty'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">SET UP TIME :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo date("H:i", strtotime($row2['SetupTime'])); ?></td>
      <td width="8%">H</td>
    </tr>
    <tr>
    <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="22%" style="text-align: left; padding-left: 5px">REJECT PREFORM:</td>
     <?php } else { ?>
      <td width="22%" style="text-align: left; padding-left: 5px">REJECT GUMPALAN:</td>
      <?php } ?>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row14['Qty'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">UP TIME :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo date("H:i", strtotime($row2['UpTime'])); ?></td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">PENDING :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row2['Pending'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT MACHINE:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
      	$data7 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='1'");
          $row7 = mysqli_fetch_array($data7);
        ?>
        <text><?php echo date("H:i", strtotime($row7['totdtm'])); ?></text>
      </td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">TOTAL :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($total1, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT EQUIPMENT:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
         $data8 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='2'");
          $row8 = mysqli_fetch_array($data8);
        ?>
        <text><?php echo date("H:i", strtotime($row8['totdtm'])); ?></text>
        </td>
      <td width="8%">H</td>
    </tr>
    <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">AWAL :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($row2['Awal'], 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
      <td width="22%" style="text-align: left; padding-left: 5px">DT TOOLS:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">
      	<?php
         $data9 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='3'");
          $row9 = mysqli_fetch_array($data9);
        ?>
        <text><?php echo date("H:i", strtotime($row9['totdtm'])); ?></text>
        </td>
      <td width="8%">H</td>
    </tr>
    <td rowspan=2 width="22%" style="text-align: left; padding-left: 5px">STPJ :</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo $pack4; ?></td>
      <td width="8%">PLT</td>
      <td rowspan=4 width="22%" style="text-align: left; padding-left: 5px">CATATAN :</td>
      <td rowspan='4' colspan="2" width="22%" style="text-align: left; padding-left: 5px">
        <?php echo $row2['Catat'].'<br>';
          $data10 = mysqli_query ($koneksi, "SELECT Reason
                                              FROM downtime
                                        WHERE PlanID='$_GET[id]'
                                        ORDER BY Shift, Jenis ASC");
          
          $totaldt= '';
          while($row10 =mysqli_fetch_array($data10)) {
          $dt = $row10['Reason'].' ,';
          $totaldt .= $dt;
          }
          echo $totaldt;
        ?>    
      </td>
    </tr>
     <tr>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo number_format($stpj1, 0, '', '.'); ?></td>
      <td width="8%">PCS</td>
    </tr>
     <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">SISA PRODUKSI:</td>
      <td width="20%" style="text-align: left; padding-left: 5px"><?php echo $sisa2; ?></td>
      <td width="8%">PCS</td>
    </tr>
     <tr>
     <td width="22%" style="text-align: left; padding-left: 5px">SPEED:</td>
      <td width="20%" style="text-align: left; padding-left: 5px">16.800</td>
      <td width="8%">BPH</td>
    </tr>
</table>
</body>
</html>