</head>
<?php include('../include/wait.php'); 

$table_name = $_SESSION['db_prefix']."_production_plan";
$check_query = "SELECT production_no, production_date, mix_design_name FROM $table_name WHERE  production_date = '".$_SESSION['production_date']."' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
	if ($check_num>0) {
	 	   $show='yes';
	}
	else {
		$show='no';
	}



$table_name = $_SESSION['db_prefix']."_production_plan_lines_temp";
$plan_query = "SELECT sub_category, mix_design_id, mix_design_name, item_type, skip_quantity FROM $table_name WHERE  user_name = '".$_SESSION['username']."' ";
$plan_result = mySQL_query($plan_query);
$plan_num = mysql_num_rows($plan_result);

$show_message = 'no';
$ii=0;

?>
<link href="../css/wait.css" rel="stylesheet" type="text/css">
<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">
<div class="page_middle_frame">
<form id="feedbackform" method="get" action="production_planWiz6.php" >
<div class="wizard_frame">
Planned production for:&nbsp;&nbsp; <?php echo date("l, d-m-Y", strtotime($_SESSION['production_date'])) ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Batch code: <?php echo $_SESSION['batch_code'] ?></div>


<div class="wizard_frame">
<table width="650" id="table_details_display">
  <tr>
    <td width="101">Production no</td>
    <td width="165">Product</td>
    <td width="67">Group</td>
    <td width="83">Skip/batch</td>
    <td width="59">Est qty</td>
    <td width="62">Skip cost</td>
    <td width="81">Est production cost</td>
    </tr>
<?php
$total=0;
$i=0;
while ($i<$plan_num) {
	
		$mix_design_id = mysql_result($plan_result, $i, "mix_design_id");
		$mix_design_name = mysql_result($plan_result, $i, "mix_design_name");
		$skip_quantity = mysql_result($plan_result, $i, "skip_quantity");
		$sub_category = mysql_result($plan_result, $i, "sub_category");
		
		$table_name = $_SESSION['db_prefix']."_production_plan";
		$check_query = "SELECT mix_design_name FROM $table_name WHERE mix_design_name = '".$mix_design_name."' AND  production_date = '".$_SESSION['production_date']."' ";
		$check_result = mySQL_query($check_query);
		$check_num = mysql_num_rows($check_result);
		
		if ($check_num==0) {
		
		$table_name = $_SESSION['db_prefix']."_mix_design";
		$number_query = "SELECT est_qty, design_cost FROM $table_name WHERE design_no = '".$mix_design_id."'  ";
		$number_result = mySQL_query($number_query);
		$est_qty = mysql_result($number_result, 0, "est_qty");
		$est_quantity = $skip_quantity * $est_qty;
		
		$table_name = $_SESSION['db_prefix']."_mix_design_lines";
		$cost_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$mix_design_id."' ";
		$cost_result = mySQL_query($cost_query);
		$cost_num = mysql_num_rows($cost_result);
		$design_cost = 0;
		$k=0;
		while ($k<$cost_num) {
			$store_item_id = mysql_result($cost_result, $k, "store_item_id");
			$quantity = mysql_result($cost_result, $k, "quantity");
	
			$table_name = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
			$item_result = mySQL_query($item_query);
			$price = mysql_result($item_result, 0, "price");
			$design_cost = $design_cost + ($quantity*$price);
			$design_cost = number_format($design_cost, 2, '.', ',');
	
		$k++;
	  }
		$production_cost = $design_cost * $skip_quantity;	
		
		$day= date("d", strtotime($_SESSION['production_date'])); 	
		$month= date("m", strtotime($_SESSION['production_date']));
		$year= date("y", strtotime($_SESSION['production_date']));
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_type, item_color, item_antique, subCategory FROM $table_name WHERE mix_design_id = '".$mix_design_id."'  ";
		$item_result = mySQL_query($item_query);
		$item_num = mysql_num_rows($item_result);
		$item_type = mysql_result($item_result, 0, "item_type");
		$item_color = mysql_result($item_result, 0, "item_color");
		$item_antique = mysql_result($item_result, 0, "item_antique");
		$subCategory = mysql_result($item_result, 0, "subCategory");
		
		if ($subCategory=='Tiles') {
		$table_name = $_SESSION['db_prefix']."_list_tiles";
		$item_query = "SELECT item_code FROM $table_name WHERE item_type = '".$item_type."' ";
		$item_result = mySQL_query($item_query);
		$item_code = mysql_result($item_result, 0, "item_code");
		}
		
		if ($subCategory=='Accessories') {
		$table_name = $_SESSION['db_prefix']."_list_accessories";
		$item_query = "SELECT item_code FROM $table_name WHERE item_type = '".$item_type."' ";
		
		$item_result = mySQL_query($item_query);
		$item_code = mysql_result($item_result, 0, "item_code");
		}
				
		$table_name = $_SESSION['db_prefix']."_list_colors";
		$color_query = "SELECT item_code FROM $table_name WHERE item_name = '".$item_color."' ";
		$color_result = mySQL_query($color_query);
		$item_col = mysql_result($color_result, 0, "item_code");
				
				
		switch ($item_antique) {
    	case 'Antique':
        $item_ant = 'A';
		break;
		case '':
        $item_ant = 'S';
		break;
		}
		
		$production_no = $year.$month.$day.$item_code.'-'.$item_col.'/'.$item_ant; 
		
				
		?>
  <tr>
    <td><?php echo $production_no ?>
      <input type="hidden" name="production_no<?php echo $i ?>" id="production_no<?php echo $i ?>" value="<?php echo $production_no ?>"></td>
    <td>
	<?php echo $mix_design_name ?>
    <input name="mix_design_no<?php echo $i ?>" type="hidden" value="<? echo $mix_design_id ?>">
    <input name="mix_design_name<?php echo $i ?>" type="hidden" value="<? echo $mix_design_name ?>">
    <input name="item_type<?php echo $i ?>" type="hidden" value="<? echo $item_type ?>">
    <input name="design_cost<?php echo $i ?>"  id="design_cost<?php echo $i ?>" type="hidden" value="<?php echo $design_cost ?>"></td>
    <td><?php echo $sub_category ?><input name="sub_category<?php echo $i ?>" type="hidden" value="<? echo $sub_category ?>"></td>
    <td><?php echo number_format($skip_quantity, 1, '.', ',') ?><input name="skip_quantity<?php echo $i ?>" type="hidden" value="<? echo $skip_quantity ?>"></td>
    <td align="right"><?php echo number_format($est_quantity, 0, '.', ',') ?><input name="est_quantity<?php echo $i ?>" type="hidden" value="<? echo number_format($est_quantity, 0, '.', '') ?>"></td>
    <td align="right"><?php echo number_format($design_cost, 2, '.', ','); ?></td>
    <td align="right"><?php echo number_format($production_cost, 2, '.', ','); ?></td>
    </tr>
  <?php
		}
  else { 
  ?>
  <input type="hidden" name="production_no<?php echo $i ?>" id="production_no<?php echo $i ?>" value="">
  <?php  
  $table_name = $_SESSION['db_prefix']."_production_plan_lines_temp";
  $delete_query = "DELETE FROM $table_name WHERE mix_design_name = '".$mix_design_name."' AND user_name = '".$_SESSION['username']."' "; 
  execsql($delete_query);
  
  $show_message = 'yes';
    
  }
  $i++;
}

?>
<input name="line_num" type="hidden" value="<?php echo $i?>">
</table>

<?php
if ($show_message=='yes') { ?>
<script>
  needToConfirm = false;
  alertMessage(1, '', 'Production for some products already exist. These products are not added for production. <br>To edit quantity or remove the production, go to Edit production plan.');
  </script>
<?php } ?>
</div>
<div class="wizard_frame">
       <label class="label_std">If you want to make changes use the 'Prev' button to go back. <br />
            Click 'New' to make a new entry or to finish the wizard click 'Finish'.</label>
    </div><table width="590" height="50" align="left" id="table_button_bar">
          <tr>
            <td width="117" valign="middle"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); MM_goToURL('parent','production_planWizbase.php?s=&p=2&e=0');return document.MM_returnValue" /></td>
            <td width="115" valign="middle"><input name="s" type="hidden" id="s" value="5" />
              <input name="p" type="hidden" id="p" value="5" /></td>
            <td width="99" valign="middle"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; cancel_confirmation(1, '../production/production_main.php?date=0&subCatSelect=Tiles');;" value="Cancel" /></td>
            <td align="right" valign="middle"><input  name="new_button" type="submit" class="imagebutton" id="new_button" value="New" onClick="needToConfirm = false;MM_showHideLayers('wait','','show'); "    onMouseUp="MM_showHideLayers('buttons','','hide');" /></td>
            <td align="right" valign="middle"><input  name="finish_button" type="submit" class="imagebutton" id="finish_button" value="Finish" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); "    onMouseUp="MM_showHideLayers('buttons','','hide');" /></td>
          </tr>
        </table>
 
</form>
</div>
</body>