<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php 
include ("../navigation/header.php");

$page_name = "Production-main";
include ("../navigation/access.php");

if ($permission_num==1) {

include ("../navigation/production_sidebar.php");

if (isset($_GET['m'])) {
		$m = $_GET['m'];
}
else {
	$m = date("m");
}

if (isset($_GET['Y'])) {
		$Y = $_GET['Y'];
}
else {
	$Y = date("Y");
}

	$start = $Y."-".$m."-01";
	$end = $Y."-".$m."-31";
	if ($m=='02') {
		  $end = $Y."-".$m."-28";
		  
	}
	if ( ($m=='04') OR ($m=='06')  OR ($m=='09')  OR ($m=='11') ) {
		  $end = $Y."-".$m."-30";
		  
	}
	
switch ($m) {
    case 1: $month_name = 'January'; break;
	case 2: $month_name = 'February'; break;
	case 3: $month_name = 'March'; break;
	case 4: $month_name = 'April'; break;
	case 5: $month_name = 'May'; break;
	case 6: $month_name = 'June'; break;
	case 7: $month_name = 'July'; break;
	case 8: $month_name = 'August'; break;
	case 9: $month_name = 'September'; break;
	case 10: $month_name = 'October'; break;
	case 11: $month_name = 'November'; break;
	case 12: $month_name = 'December'; break;
}

$table_name = $_SESSION['db_prefix']."_production_plan";	 	
$display_query = "SELECT production_no, production_date, mix_design_name, mix_design_cost  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' AND sub_category = 'Tiles' AND produced_wet = 'yes' ";
$display_result = execsql($display_query);
$i = 0;
$display_num = mysql_num_rows($display_result);

$table_name = $_SESSION['db_prefix']."_production_plan";	 	
$display_2_query = "SELECT production_no, production_date, mix_design_name, mix_design_cost  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' AND sub_category = 'Accessories' AND produced_wet = 'yes' ";
$display_2_result = execsql($display_2_query);
$i = 0;
$display_2_num = mysql_num_rows($display_2_result);
		
?> 

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 

</head>


<body>

<div class="main_frame">
<div class="page_head_frame">
  <label class="label_page_head">Production <?php echo $month_name.' '.$Y ?></label></div>
   
<br />
<div class="page_middle_frame">

<div  style=" float:right;">
<?php 
if ($m!=12) {$m_next=$m+1; $y_next=$Y;} else { $m_next=1; $y_next=$Y+1;}
if ($m!=1)  {$m_prev=$m-1; $y_prev=$Y;} else { $m_prev=12; $y_prev=$Y-1;}
?>
<a href="production_main.php?m=<?php echo $m_prev ?>&Y=<?php echo $y_prev ?> "><input name="prev_button" type="button" value="< previous month" class="imagebutton" style="width:120px;"></a>
<a href="production_main.php?m=<?php echo $m_next ?>&Y=<?php echo $y_next ?> "><input name="next_button" type="button" value="next month >" class="imagebutton" style="width:120px;"></a>
</div>   
<label style="font-size:24px; font-weight:bold">Tiles</label>
  <!--jscript-->
  <script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "250px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
                         	 ]
				} );
			} );
		</script>    	
<table width="572" id="scroll_table" class="data_display" >
	<thead>
		<tr>
		  <th width="80" align="left">Production date</th>
		  <th width="94" align="left">Production no</th>
		  <th width="134" align="left">Mix design</th>
		  <th width="73" align="left">Skips produced</th>
		  <th width="67" align="left"> Wet produced</th>
		  <th width="96" align="left">Cost</th>
	    </tr>
	</thead>
	<tbody>
  <?php
  	$b4_total = 0;
	$b5_total = 0;
	$b6_total = 0;
    $i=0;
    while($i < $display_num){
		 $b0 = mysql_result($display_result, $i, "production_no");
		 $b1 = mysql_result($display_result, $i, "production_date");
		 $b2 = mysql_result($display_result, $i, "mix_design_name");
		 $b3 = mysql_result($display_result, $i, "mix_design_cost");
		 $b10 = substr($b0,4,8);
		 
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_count_query = "SELECT quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND  action = 'skip_count' AND action_date = '".$b1."' ";
		$skip_count_result = mySQL_query($skip_count_query);
		$skip_count_num = mysql_num_rows($skip_count_result);
		if ($skip_count_num>0) {
				$b4 = mysql_result($skip_count_result, 0, "quantity");
		}
		else { 
		  $b4=0;
		}
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$wet_count_query = "SELECT quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND  action = 'wet_count' AND action_date = '".$b1."' ";
		$wet_count_result = mySQL_query($wet_count_query);
		$wet_count_num = mysql_num_rows($wet_count_result);
		if ($wet_count_num>0) {
				$b5 = mysql_result($wet_count_result, 0, "quantity");
		}
		else { 
		  $b5=0;
		}
		
		$b6 = $b4*$b3;
		
		//$table_name = $_SESSION['db_prefix']."_mix_design";
//		$est_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$b2."' ";
//		$est_result = mySQL_query($est_query);
//		$est_num = mysql_num_rows($est_result);
//		$b7 = mysql_result($est_result, 0, "est_qty");
//		
//		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
//		$production_query = "SELECT production_no, quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND (action = 'skip_count') ";
//		$production_result = mySQL_query($production_query);
//		$production_num = mysql_num_rows($production_result);
//		$production_line = '';
//		$k=0;
//		while ($k<$production_num) {
//		$production_no = mysql_result($production_result, $k, "production_no");
//		$skip_count = mysql_result($production_result, $k, "quantity");
//		
//		
//		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
//		$wet_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$production_no."' AND action = 'wet_count'  ";
//		$wet_count_result = mySQL_query($wet_count_query);
//		$wet_count_num = mysql_num_rows($wet_count_result);
//		$wet_count = mysql_result($wet_count_result, 0, "quantity");
		
		
		
//		$production_line = $production_line."<tr><td>".$production_no."</td><td>".$skip_count."</td><td>".$wet_count."</td></tr>";
//		$k++;
//		}
		
//		$productionTT = "<table width=200px;><tr><td width=100px;>Production no</td><td width=50px;>Skips</td><td  width=50px;>Produced</td></tr>".$production_line."</table>"
		
		
		?>
		<tr >
		  <td align="left"><?php echo date("d-m-Y", strtotime($b1)) ?></td>
		  <td align="left" ><a href="production_plan_report.php?production_no=<?php echo $b0 ?>" onMouseover="ddrivetip('Click to go to production report', 150)"; onMouseout="hideddrivetip()"><?php echo $b0 ?></a></td>
		  <td align="left"><?php echo $b2 ?> </td>
		  <td align="right"><?php echo number_format($b4, 1, '.', ','); $b4_total = $b4_total + $b4; ?></td>
		  <td align="right"><?php echo number_format($b5, 0, '.', ',');  $b5_total = $b5_total + $b5; ?></td>
		  <td align="right"><?php echo number_format($b6, 2, '.', ',');  $b6_total = $b6_total + $b6;  ?></td>
	    </tr>
	<?php
		$i++; 
	}
	?>
	<tfoot>	
        <tr >
		  <td colspan="3" align="right">Totals:</td>
		  <td align="right"><?php echo number_format($b4_total, 1, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b5_total, 0, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b6_total, 2, '.', ',') ?></td>
	    </tr> 
    </tfoot>      
</table>
<br>
<div align="left" style="font-size:24px; font-weight:bold;" >Accessories</div>
  <!--jscript-->
  <script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table_2').dataTable( {
					"sScrollY": "250px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
                         	 ]
				} );
			} );
		</script>    	
<table width="572" id="scroll_table_2" class="data_display" >
	<thead>
		<tr>
		  <th width="80" align="left">Production date</th>
		  <th width="94" align="left">Production no</th>
		  <th width="134" align="left">Mix design</th>
		  <th width="73" align="left">Skips produced</th>
		  <th width="67" align="left"> Wet produced</th>
		  <th width="96" align="left">Cost</th>
	    </tr>
	</thead>
	<tbody>
  <?php
  	$b4_total = 0;
	$b5_total = 0;
	$b6_total = 0;
    $i=0;
    while($i < $display_2_num){
		 $b0 = mysql_result($display_2_result, $i, "production_no");
		 $b1 = mysql_result($display_2_result, $i, "production_date");
		 $b2 = mysql_result($display_2_result, $i, "mix_design_name");
		 $b3 = mysql_result($display_2_result, $i, "mix_design_cost");
		 $b10 = substr($b0,4,8);
		 
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_count_query = "SELECT quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND  action = 'skip_count' AND action_date = '".$b1."' ";
		$skip_count_result = mySQL_query($skip_count_query);
		$skip_count_num = mysql_num_rows($skip_count_result);
		if ($skip_count_num>0) {
				$b4 = mysql_result($skip_count_result, 0, "quantity");
		}
		else { 
		  $b4=0;
		}
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$wet_count_query = "SELECT quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND  action = 'wet_count' AND action_date = '".$b1."' ";
		$wet_count_result = mySQL_query($wet_count_query);
		$wet_count_num = mysql_num_rows($wet_count_result);
		if ($wet_count_num>0) {
				$b5 = mysql_result($wet_count_result, 0, "quantity");
		}
		else { 
		  $b5=0;
		}
		
		$b6 = $b4*$b3;
		
		//$table_name = $_SESSION['db_prefix']."_mix_design";
//		$est_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$b2."' ";
//		$est_result = mySQL_query($est_query);
//		$est_num = mysql_num_rows($est_result);
//		$b7 = mysql_result($est_result, 0, "est_qty");
//		
//		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
//		$production_query = "SELECT production_no, quantity FROM $table_name WHERE mix_design_name = '".$b2."' AND (action = 'skip_count') ";
//		$production_result = mySQL_query($production_query);
//		$production_num = mysql_num_rows($production_result);
//		$production_line = '';
//		$k=0;
//		while ($k<$production_num) {
//		$production_no = mysql_result($production_result, $k, "production_no");
//		$skip_count = mysql_result($production_result, $k, "quantity");
//		
//		
//		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
//		$wet_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$production_no."' AND action = 'wet_count'  ";
//		$wet_count_result = mySQL_query($wet_count_query);
//		$wet_count_num = mysql_num_rows($wet_count_result);
//		$wet_count = mysql_result($wet_count_result, 0, "quantity");
		
		
		
//		$production_line = $production_line."<tr><td>".$production_no."</td><td>".$skip_count."</td><td>".$wet_count."</td></tr>";
//		$k++;
//		}
		
//		$productionTT = "<table width=200px;><tr><td width=100px;>Production no</td><td width=50px;>Skips</td><td  width=50px;>Produced</td></tr>".$production_line."</table>"
		
		
		?>
		<tr >
		  <td align="left"><?php echo date("d-m-Y", strtotime($b1)) ?></td>
		  <td align="left" ><a href="production_plan_report.php?production_no=<?php echo $b0 ?>" onMouseover="ddrivetip('Click to go to production report', 150)"; onMouseout="hideddrivetip()"><?php echo $b0 ?></a></td>
		  <td align="left"><?php echo $b2 ?></td>
		  <td align="right"><?php echo number_format($b4, 1, '.', ','); $b4_total = $b4_total + $b4; ?></td>
		  <td align="right"><?php echo number_format($b5, 0, '.', ',');  $b5_total = $b5_total + $b5; ?></td>
		  <td align="right"><?php echo number_format($b6, 2, '.', ',');  $b6_total = $b6_total + $b6;  ?></td>
	    </tr>
	<?php
		$i++; 
	}
	?>
	<tfoot>	
        <tr >
		  <td colspan="3" align="right">Totals:</td>
		  <td align="right"><?php echo number_format($b4_total, 1, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b5_total, 0, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b6_total, 2, '.', ',') ?></td>
	    </tr> 
    </tfoot>      
</table>
</div>    
</div>
</body>
<?php } else {  ?>
<script> needToConfirm = false; </script>
<?php
    $_SESSION['denied_return'] = "production/production_main.php?";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>