<?php
    include("../include/session.php");
    if(!$session->isAdmin() && !$session->isRadstoneReceptionUser()){
        header("Location: ../main.php");
    }
    else {
    	include("../productionHeader.php");
		require_once('classes/tc_calendar.php');
		
		$currentmonth = date("F");
		$lastmonthtemp = $m-1;
		$lastmonth  = date("F", mktime(0, 0, 0, date("m")-1, date("d"),   date("Y")) );
				
		$today=date('Y-m-d'); 
		$yesterday = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y"))); 
		$yesterday1 = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 2, date("Y")));
		
		$numdays = cal_days_in_month(CAL_GREGORIAN, $m, $y) ; 
		$numdays2 = cal_days_in_month(CAL_GREGORIAN, $m-1, $y) ; 
        		
		$CurrentMonthStart = date('Y-m-d', mktime(0, 0, 0, date("m") , 1, date("Y"))); 
		$CurrentMonthEnd = date('Y-m-d', mktime(0, 0, 0, date("m") , $numdays, date("Y"))); 
		$LastMonthStart = date('Y-m-d', mktime(0, 0, 0, date("m")-1 , 1, date("Y"))); 
		$LastMonthEnd = date('Y-m-d', mktime(0, 0, 0, date("m")-1 , $numdays2, date("Y"))); 


        $start = mysql_real_escape_string($_GET['date1']);
        $end = mysql_real_escape_string($_GET['date2']);
		$monthdisplay =  mysql_real_escape_string($_GET['monthdisplay']);
        $cat =  mysql_real_escape_string($_GET['cat']);
		$productId =  mysql_real_escape_string($_GET['productIdSelect']);
		$cumulative =  mysql_real_escape_string($_GET['cumcheckbox']);
		$prodId = $productId;	 
		$start = strtotime($start);
		$end = strtotime($end);
		
				 
		$start1=date("Y-m-d", $start); 
		$end1=date("Y-m-d", $end);
	    
	    if ($monthdisplay==0) { 
		  $start1= $CurrentMonthStart; 
		  $end1= $today;
		  
		  }
		  
		if ($monthdisplay=="") {
		 $start1=date("Y-m-d", $start); 
		 $end1=date("Y-m-d", $end);
		 }   
		
		$start = strtotime($start1);
		$end = strtotime($end1);
		
		$ds = date("d", $start );
		$ms = date("m", $start );
		$ys = date("Y", $start ); 
		
		$de = date("d", $end );
		$me = date("m", $end );
		$ye = date("Y", $end );
		 
        $product = mysql_real_escape_string($_GET['productIdSelect']);
		$prodQuery = "SELECT recipieName FROM recipie WHERE recipieId ='$product' ";
	      $prodResult = execsql($prodQuery);
	      $recipieName = mysql_result($prodResult, 0, "recipieName");
		
		
	$selectQuery = "SELECT recipieName, recipieId FROM recipie  ORDER BY recipieName ASC";
	
	$selectResult = execsql($selectQuery);
	$n = mysql_num_rows($selectResult);
	$i = 0;
	while($i < $n){
		$prod[mysql_result($selectResult, $i, "recipieId")] = mysql_result($selectResult, $i, "recipieName");
		$i++;
	}
	
	if ($cumulative=="cum") { 
	   $recipieName="All";
	   $productId="All";
	   $message="<font color=red><b>Note: showing item totals over selected period.</b></font>";
	   }
	
		
	if ($cumulative=="cum") {
		
		$q = "SELECT  recipieId, SUM(skips) AS skips, SUM(cost) AS cost, SUM(wet) AS wet  FROM production WHERE date >= '$start1' AND date <= '$end1' GROUP BY recipieId ORDER BY recipieId ASC";
	}
	else {
		
		if ($product=="") { $product="All"; }

		if ($product=="All") {
		  $q = "SELECT * FROM production WHERE date >= '$start1' AND date <= '$end1'  ORDER BY date DESC";
		}
		else {
		  $q = "SELECT * FROM production WHERE date >= '$start1' AND date <= '$end1' AND recipieId = '$product' ORDER BY date DESC";
		}
		
	}
	
		$resultLate = execsql($q);
		$num = mysql_num_rows($resultLate);
		$i = 0;
		
		
		$start = strtotime($start);
		$end = strtotime($end);
		 
		$start1=date("Y-m-d", $start); 
		$end1=date("Y-m-d", $end);
		
		
	
	
?>	
<link
	href="calendar.css" rel="stylesheet" type="text/css" />
<script
	language="javascript" src="../include/calendar.js"></script>
	
<div class="headerbox2">Production</div>
<div class="selectionbox">
  <form action="productionByDates.php" method="get" name="dateform" class="today" id="dateform">
    <span class="style7">from:
      &nbsp;&nbsp;
      <?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
	 	$myCalendar->writeScript();?>
      &nbsp;&nbsp;to:&nbsp;&nbsp;
    <?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
		$myCalendar->writeScript();?>
      &nbsp;&nbsp;
    <label>
        <input type="submit" name="button" id="button" value="Go" />
        <br />
    </label>
  product:
         <select name="productIdSelect" >
        <option value="<?php echo $productId ?>"><?php echo $recipieName ?></option>
        <option value="<?php echo "All" ?>"><?php echo All ?></option>
        <?php
	foreach($prod as $key => $value){
		echo "<option value=\"$key\">$value</option>";
	}
	?>
    <option value="<?php echo "  " ?>"><?php echo All ?></option>
</select>
         &nbsp;&nbsp;(  select &quot;All&quot; to show complete list)
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <label>
    <input name="cumcheckbox" type="checkbox" id="cumcheckbox" value="cum" />
    show cumulative list
  </label>
   
  
  </form>
</div><br />		
<?php echo $message ?>
<table width="600" border="0" cellpadding="0" cellspacing="0" class="rowstyle-alt colstyle-alt no-arrow" id="test1">
    <thead>
      <tr>
        <th width="112" class="sortable-date-ymd">Date</th>
        <th width="201" class="sortable-text">Recipie Id</th>
        <th width="87" class="sortable-numeric">Skips</th>
        <th width="105" class="sortable-numeric">Wet Tiles</th>
        <th width="95" class="sortable-numeric">Cost</th>
        



      </tr>
    </thead>
    <tbody>
<?php
    $i = 0;
    while($i < $num){
       $b2 = mysql_result($resultLate, $i, "date");
       $b3 = mysql_result($resultLate, $i, "skips");
       $b4 = mysql_result($resultLate, $i, "cost");
       $b5 = mysql_result($resultLate, $i, "wet");
       $b7 = mysql_result($resultLate, $i, "recipieId");
	      $recipieQuery = "SELECT recipieId, recipieName FROM recipie WHERE recipieId ='$b7' ";
	      $recipieResult = execsql($recipieQuery);
	      $b8 = mysql_result($recipieResult, 0, "recipieName");
       	$tSkips += $b3;
		$tCost += $b4;
		$tWet += $b5;

      // echo "<tr><td class="sortable-numeric">$b1</td><td>$b2</td><td>$permission</td><td>$b4</td><td>$b5</td><td>$b6</td></tr>";
      // $i++;
   // }
?>

        <tr class="alt">
        <td><?php if ($cumulative=="cum") {echo "period";} else { echo  date("d-m-Y", strtotime($b2)); } ?></td>
        <td><a href="../Production/viewRecipies.php?id=<?php echo $b7?>" onmouseover="ddrivetip('<?php echo "recipy: <br>" . $b8 . "<br><b>Click to edit recipy<b>" ?>','#FFFFCC', 120)" onmouseout="hideddrivetip()";><?php echo $b8 ?></a></td>
        <td align="right"><?php echo $b3?></td>
        <td align="right"><?php echo $b5?></td>
        <td align="right"><?php echo $b4?></td>
        </tr> 
<?php
    $i++;
    }
?>
<tr><td></td><td></td><td align="right"><b><?php echo $tSkips?></b></td><td align="right"><b><?php echo number_format($tWet)?></b></td><td align="right"><b><?php echo number_format($tCost,2)?></b></td></tr>
</tbody>
</table>

<?php
	include("../footer.php");
    }
    ?>
	other links: