<?php
    include("../include/session.php");
    if(!$session->isAdmin() && !$session->isRadstoneReceptionUser()){
        header("Location: ../main.php");
    }
    else {
    	include("../productionHeader.php");
//        include("../include/db.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 nam_recipie WHERE recipieId ='$product' ";
	      $prodResult = execsql($prodQuery);
	      $recipieName = mysql_result($prodResult, 0, "recipieName");
		
		if ($cumulative=="cum") { 
	   $recipieName="All";
	   $productId="All";
	   $message="<font color=red><b>Note: showing item totals over selected period.</b></font>";
	   }
		
		
		$selectQuery = "SELECT recipieName, recipieId FROM nam_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") {
		
		$q1 = "SELECT  recipieId, SUM(amountOfTiles) AS amountOfTiles, SUM(cost) AS cost  FROM nam_productiondepalet WHERE date >= '$start1' AND date <= '$end1' GROUP BY recipieId ORDER BY recipieId ASC";
	}
	else {
		
		if ($product=="") { $product="All"; }

		if ($product=="All") {
		  $q1 = "SELECT * FROM nam_productiondepalet WHERE date >= '$start1' AND date <= '$end1' ORDER BY date DESC";
		}
		else {
		  $q1 = "SELECT * FROM nam_productiondepalet WHERE date >= '$start1' AND date <= '$end1' AND recipieId = '$product' ORDER BY date DESC";
		}
		
	}
		 
		
		$resultLate = execsql($q1);
		$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);
		
		
		$q = "SELECT recipieId, recipieName FROM nam_recipie";
	$resultPro = execsql($q);
	$numPro = mysql_num_rows($resultPro);
	$j = 0;
	$proArray = array();
	while($j < $numPro){
		$proArray[mysql_result($resultPro, $j, "recipieId")] = mysql_result($resultPro, $j, "recipieName");
		$j++;
	}		
?>	
<link
	href="calendar.css" rel="stylesheet" type="text/css" />
<script
	language="javascript" src="../include/calendar.js"></script>

  
<div class="headerbox2">De-palleted</div>
<div class="selectionbox">
  <form action="depByDates.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>		

<?php echo $message ?><br />
<table id="test1" cellpadding="0" cellspacing="0" border="0" class="rowstyle-alt colstyle-alt no-arrow">
    <thead>
      <tr>
        <th width="107" class="sortable-date-ymd">Date</th>
        <th width="205" class="sortable-text">Recipie Id</th>
        <th width="155" class="sortable-numeric">Number of Tiles</th>
        <th width="125" class="sortable-numeric">Cost</th>
        
        
        

      </tr>
    </thead>
    <tbody>
<?php
    $i = 0;
    while($i < $num){
       $b1 = mysql_result($resultLate, $i, "date");
       $b2 = mysql_result($resultLate, $i, "recipieId");
       $b3 = mysql_result($resultLate, $i, "cost");
       $b4 = mysql_result($resultLate, $i, "amountOfTiles");
       $tCost += $b3;
       $tDep += $b4;


      // 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($b1));} ?></td>
        <td><a href="../Production/viewRecipies.php?id=<?php echo $b2?>"><?php echo $proArray[$b2]?></a></td>
        <td align="right"><?php echo $b4?></td>
        <td align="right"><?php echo $b3?></td>
        
       
      </tr>
<?php
    $i++;
    }
?>
<tr><td></td><td></td><td align="right"><b><?php echo $tDep?></b></td><td align="right"><b><?php echo number_format($tCost,2)?></b></td></tr></tbody></b>
</tbody>
</table>

<?php
    include "../footer.php";
}
?>
