<?php
    include("../include/session.php");
    if(!$session->isRadstoneStoreUser() && !$session->isAdmin() && !$session->isRadstoneReceptionUser()){
	header("Location: ../Store/storeMenu.php");
    }
    else {
    	include("../productionHeader.php");
//        include("../include/db.php");
           
		require_once('classes/tc_calendar.php');
		$d = date("d");
		$m = date("m");
		$y = date("Y");
		$currentmonth = date("F");
						
		$today=date('Y-m-d'); 
		$tomorrow = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") + 1, date("Y"))); 
		$tomorrow1 = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") + 2, date("Y")));
		
		$start = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
        $displayDay = date('l', strtotime($start)); 
		$curingTime = isset($_REQUEST["curingText"]) ? $_REQUEST["curingText"] : "";
		
		$d = date("d", strtotime($start));
		$m = date("m", strtotime($start));
		$y = date("Y", strtotime($start));
		 
		if ($displayDay=='Saturday') { 
		  $start = date('Y-m-d', strtotime ( '+1 day' , strtotime ( $start ) ) ) ;
		  $errorMessage = "<font color=red> (Selected day is a saturday, displaying Monday.)</font>";
		  $displayDay = 'Monday';
		   }
		  
		if ($displayDay=='Sunday') { 
		  $start = date('Y-m-d', strtotime ( '+1 day' , strtotime ( $start ) ) ) ;
		  $errorMessage = "<font color=red> (Selected day is a Sunday, displaying Monday.)</font>";
		  $displayDay = 'Monday';
		   }
		 
		$startnext = strtotime ( '+1 day' , strtotime ( $start ) ) ;
		$start2 =  date ( 'Y-m-d' , $startnext);  
		
        $productionQuery = "SELECT products.productId, orders.productId, products.productName, SUM(orders.reserved) AS totReserved FROM orders, products WHERE products.productId = orders.productId AND delDate <= '$start'  GROUP BY orders.productId ORDER BY products.productName";
		$productionResult = execsql($productionQuery);
    	$num = mysql_num_rows($productionResult);
		
		$productionQuery2 = "SELECT products.productId, orders.productId, products.productName, SUM(orders.reserved) AS totReserved FROM orders, products WHERE products.productId = orders.productId AND delDate <= '$start2'  GROUP BY orders.productId ORDER BY products.productName";
		$productionResult2 = execsql($productionQuery2);
    	$num = mysql_num_rows($productionResult2);
		
		$message="End of list";
		
	
		  
	if ($displayDay=='Monday') {
	        $prodDateA = strtotime ( '-5 day' , strtotime ( $start ) ) ;  
			$prodDate =  date ( 'Y-m-d' , $prodDateA ) ;  
			$prodDay = date('l', strtotime($prodDate));
			$prodDateB = strtotime ( '-4 day' , strtotime ( $start ) ) ;  
			$prodDate2 =  date ( 'Y-m-d' , $prodDateB ) ;  
			$prodDay2 = date('l', strtotime($prodDate2));
			$displayDay2='Tuesday';
	   }  
	if ($displayDay=='Tuesday') {
	        $prodDateA = strtotime ( '-5 day' , strtotime ( $start ) ) ;  
			$prodDate =  date ( 'Y-m-d' , $prodDateA ) ;  
			$prodDay = date('l', strtotime($prodDate));
			$prodDateB = strtotime ( '-4 day' , strtotime ( $start ) ) ;  
			$prodDate2 =  date ( 'Y-m-d' , $prodDateB ) ;  
			$prodDay2 = date('l', strtotime($prodDate2));
			$displayDay2='Wednesday';
	   } 
	if ($displayDay=='Wednesday') {
	        $prodDateA = strtotime ( '-5 day' , strtotime ( $start ) ) ;  
			$prodDate =  date ( 'Y-m-d' , $prodDateA ) ;  
			$prodDay = date('l', strtotime($prodDate));
			$prodDateB = strtotime ( '-2 day' , strtotime ( $start ) ) ;  
			$prodDate2 =  date ( 'Y-m-d' , $prodDateB ) ;  
			$prodDay2 = date('l', strtotime($prodDate2));
			$displayDay2='Thursday';
	   }  
	if ($displayDay=='Thursday') {
	        $prodDateA = strtotime ( '-3 day' , strtotime ( $start ) ) ;  
			$prodDate =  date ( 'Y-m-d' , $prodDateA ) ;  
			$prodDay = date('l', strtotime($prodDate));
			$prodDateB = strtotime ( '-2 day' , strtotime ( $start ) ) ;  
			$prodDate2 =  date ( 'Y-m-d' , $prodDateB ) ;  
			$prodDay2 = date('l', strtotime($prodDate2));
			$displayDay2='Friday';
	   } 
	   
	   
	if ($displayDay=='Friday') {
	        $prodDateA = strtotime ( '-3 day' , strtotime ( $start ) ) ;  
			$prodDate =  date ( 'Y-m-d' , $prodDateA ) ;  
			$prodDay = date('l', strtotime($prodDate));
			$prodDateB = strtotime ( '-2 day' , strtotime ( $start ) ) ;  
			$prodDate2 =  date ( 'Y-m-d' , $prodDateB ) ;  
			$prodDay2 = date('l', strtotime($prodDate2));
			$displayDay2='Monday';
			$startnext = strtotime ( '+3 day' , strtotime ( $start ) ) ;
		    $start2 =  date ( 'Y-m-d' , $startnext);
	   }       	  
		  
	$weekday = date('l', strtotime($start)); 
	$id=$curingTime;
	

	 
	
	
	
		  
	 	  
?>
<script type="text/JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>

<link
	href="calendar.css" rel="stylesheet" type="text/css" />
<script
	language="javascript" src="../include/calendar.js"></script>
<div class="headerbox">Required Production </div>
<div class="menubox">
<div align="right">
      <form action="requiredProduction.php" method="get" >
        <div align="right"><?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($d, $m, $y);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
	 // $myCalendar->dateAllow('2008-05-13', '2010-03-01', false);
		//$myCalendar->setHeight(350);
		//$myCalendar->autoSubmit(true, "form1");
		$myCalendar->writeScript();?><br />
          curing period: 
          <select name="curingText" id="curingText">
            <option value="3" selected="selected">3</option>
            <option value="5">5</option>
            <option value="7">7</option>
          </select>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="submitok"  type="image" src="../../Images/go_btn.png" align="right" width="40" height="25" />
          <br />
        </div>
      </form>
  </div>
    
  
</div><br /><br />
<div class="subheaderbox">Production advice for:&nbsp;<b><?php echo $prodDay.", ".date("d-m-Y",strtotime($prodDate)) ?></b><br />
  Required production before:<b>&nbsp;<?php echo $displayDay.", ".date("d-m-Y",strtotime($start)) ?></b> <br />
  <?php echo $errorMessage ?><br />
  Production date based on a curing time of  <strong><?php echo $curingTime ?></strong> days.<br />
  <?php echo $dayMessage ?><br />
</div>
<br /><br />

<table width="590" border="0" cellpadding="0" cellspacing="0" class="rowstyle-alt colstyle-alt no-arrow" id="test1">
    <thead>
      <tr>
        <th width="154" class="sortable-text">Product</th>
        <th width="68" class="sortable-numeric">Stock</th>
        <th width="92" class="sortable-numeric">Required<br /><?php echo $displayDay ."<br>". date("d-m-Y",strtotime($start))?></th>
		<th width="92" class="sortable-numeric">Produce<br />
	    <?php echo  $prodDay ."<br>".  date("d-m-Y",strtotime($prodDate))?></th>
		<th width="92" class="sortable-text">Required<br />
	    <?php echo $displayDay2 ."<br>".  date("d-m-Y",strtotime($start2))?></th>
		<th width="92" class="sortable-text">Produce<br /><?php echo  $prodDay2 ."<br>".  date("d-m-Y",strtotime($prodDate2))?></th>
        
        <!--        <th>Reserved Stock</th>-->
      </tr>
    </thead>
    <tbody>
    <?php 
        $i=0;
		
    	while($i < $num){
	       $b1 = mysql_result($productionResult, $i, "productId");
		   $b3 = mysql_result($productionResult, 0, "productName");
		     $productNameQuery = "SELECT productId, productName, quantity FROM products WHERE productId = $b1 ";
		     $nameResult = execsql($productNameQuery);
		     $b3 = mysql_result($nameResult, 0, "productName");
			 $b4 = mysql_result($nameResult, 0, "quantity");
		   $b2 = mysql_result($productionResult, $i, "totReserved");
		   $b5 = $b2-$b4;
		   
		   $c1 = mysql_result($productionResult2, $i, "productId");
		   $c3 = mysql_result($productionResult2, 0, "productName");
		     $productNameQuery2 = "SELECT productId, productName, quantity FROM products WHERE productId = $c1 ";
		     $nameResult2 = execsql($productNameQuery2);
		     $c3 = mysql_result($nameResult2, 0, "productName");
			 $c4 = mysql_result($nameResult2, 0, "quantity");
		   $c2 = mysql_result($productionResult2, $i, "totReserved");
		   $c5 = $c2-$c4;
		   if ($b5==$c5) { $c5=0; }
		   else  {$c5=$c5-$b5 ;}
		
		  
		   
		   $reservedQuery = "SELECT productId, client, orderNo, reserved, delDate FROM orders WHERE productId = $b1  AND reserved > 0 ORDER BY delDate DESC ";
		$reservedResult = execsql($reservedQuery);
		$j = 0;
	    $o_num = mysql_num_rows($reservedResult);
		$toolTipText = "";
		$o2tot=0;
		while ($j < $o_num) {
		$o0 = mysql_result($reservedResult, $j, "orderNo");
		$o1 = mysql_result($reservedResult, $j, "client"); 
		$o2 = mysql_result($reservedResult, $j, "reserved"); 
		$o3a = mysql_result($reservedResult, $j, "delDate"); 
		$o3 = date("d-m-Y",strtotime($o3a));
		if ($o3a<'2000-01-01') {
		  $o3='tba&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';}
		else {
		  $o3 = date("d-m-Y",strtotime($o3a));
		  }
		if ($o3a<$start) {
		   	$toolTipText = "<font color=red>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0." &nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font> <br>"; } 
			else  {
		if ($o3a==$start) {
		   	$toolTipText = "<font color=orange>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font><br>"; }
			else
			{
		   	$toolTipText = "<font color=black>". $toolTipText . $o3."&nbsp;&nbsp;&nbsp;".$o0."&nbsp;&nbsp;&nbsp;".$o1."&nbsp;&nbsp;&nbsp;".$o2."</font><br>"; }
			}
		$j++;
		$o2tot = $o2tot + $o2;
		} 
       
    ?>
	
	<?php if ($b5>0) { ?>
    <tr class="alt">
	   <td><?php echo $b3 ?></td>
       <td align="right"><?php echo number_format($b4, 0, '.', ',')."<br> (".number_format($o2tot, 0, '.', ',').")" ?></td>
       <td align="right"><b><a href="../Store/showReserved.php?productId=<?php echo $b1 ?>" onmouseover="ddrivetip('<?php echo "<b>required for orders:</b><br>". $toolTipText . "<br><b>total on order: ".$o2tot."</b><br><b>click to view complete list</b>" ?>','#FFFFCC', 275)" onmouseout="hideddrivetip();">
         <?php echo number_format($b2)?>
       </a></b></td>
      <td width="92" align="right"> <font color="red"><?php echo number_format($b5, 0, '.', ','); ?></font></td>
        <td width="92" align="right"><b><a href="../Store/showReserved.php?productId=<?php echo $b1 ?>" onmouseover="ddrivetip('<?php echo "<b>required for orders:</b><br>". $toolTipText . "<br><b>total on order: ".$o2tot."</b><br><b>click to view complete list</b>" ?>','#FFFFCC', 275)" onmouseout="hideddrivetip();">
          <?php echo number_format($c2)?>
        </a></b></td>
        <td width="92" align="right"><font color="red"><?php echo number_format($c5, 0, '.', ','); ?></font></td>
      </tr> 
	  <?php } ?>
    
<?php 
		$i++;
    	}
    	?></tbody>
</table>
<b><font color='#FF0000'><?php  echo $message ?></font></b>
    	<?php 
    	include("../footer.php");
    }
//    
	
?>
