<?php
include('../include/session.php');



date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$production_date = $_GET['production_date'];
$mix_design_id = $_GET['mix_design_id_select'];
$skip_quantity = $_GET['qty_text'];


$day= date("d", strtotime($production_date)); 	
$month= date("m", strtotime($production_date));
$year= date("y", strtotime($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");
		$sub_category = mysql_result($item_result, 0, "subCategory");
		
		$table_name = $_SESSION['db_prefix']."_mix_design";
		$design_query = "SELECT mix_design, design_cost, est_qty FROM $table_name WHERE design_no = '".$mix_design_id."'  ";
		$design_result = mySQL_query($design_query);
		$mix_design_name = mysql_result($design_result, 0, "mix_design");
		$design_cost = mysql_result($design_result, 0, "design_cost");
		$est_quantity = mysql_result($design_result, 0, "est_qty");
		
		$qty = $est_quantity * $skip_quantity;
		
		switch ($item_type) {
    	case 'Double Roman':
        $item_code = 'DR';
		break;
		case 'Slate':
        $item_code = 'SL';
		break;
		case 'Venetian':
        $item_code = 'VE';
		break;
		case 'Mono Ridge':
        $item_code = 'MR';
		break;
		case 'Mono-Ridge':
        $item_code = 'MR';
		break;
		case 'Rake Verge':
        $item_code = 'RV';
		break;
		case 'V Ridge':
        $item_code = 'VR';
		break;
		case 'V-Ridges':
        $item_code = 'VR';
		break;
		case (('V Hip Starter')):
        $item_code = 'VHS';
		break;
		case (('V-Hip Starter')):
        $item_code = 'VHS';
		break;
		case 'Tapered Ridge':
        $item_code = 'TR';
		break;
		case 'T/Ridge':
        $item_code = 'TR';
		break;
		case 'Tapered Hip Starter':
        $item_code = 'THS';
		break;
		}
		
		switch ($item_color) {
    	case 'Black':
        $item_col = 'BL';
		break;
		case 'Deep Black':
        $item_col = 'DBL';
		break;
		case 'Brown':
        $item_col = 'BR';
		break;
		case 'Terra Cotta':
        $item_col = 'TC';
		break;
		case 'Red':
        $item_col = 'RD';
		break;
		case 'Kalahari':
        $item_col = 'KA';
		break;
		case 'Grey':
        $item_col = 'GR';
		break;
		}
		
		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; 
		
		$year_no = date("Y", strtotime($production_date));
		$month_no = date("m", strtotime($production_date));
		$day_no = date("d", strtotime($production_date));
		
		
		
		switch ($year_no) {
		case 2015: $year_code = 'A'; break;
		case 2016: $year_code = 'B'; break;
		case 2017: $year_code = 'C'; break;
		case 2018: $year_code = 'D'; break;
		case 2019: $year_code = 'E'; break;	
		case 2020: $year_code = 'F'; break;
		}
		
		switch ($month_no) {
    	case '01': $month_code = 'Z'; break;
		case '02': $month_code = 'Y'; break;
		case '03': $month_code = 'X'; break;
		case '04': $month_code = 'W'; break;
		case '05': $month_code = 'V'; break;
		case '06': $month_code = 'U'; break;
		case '07': $month_code = 'T'; break;
		case '08': $month_code = 'S'; break;
		case '09': $month_code = 'R'; break;
		case '10': $month_code = 'P'; break;
		case '11': $month_code = 'O'; break;
		case '12': $month_code = 'N'; break;
		}
		
		
		$batch_code = $year_code.$month_code.$day_no;


$table_name = $_SESSION['db_prefix']."_production_plan";
$check_query = "SELECT production_no FROM $table_name WHERE production_no = '".$production_no."'  ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);


if ($check_num==0) {

$table_name = $_SESSION['db_prefix']."_production_plan";
	$production_plan_query = "INSERT INTO $table_name (production_no, production_date, mix_design_name, mix_design_cost, batch_code, sub_category, item_type, planned) VALUES ('".$production_no."', '".$production_date."', '".$mix_design_name."', '".$design_cost."', '".$batch_code."', '".$sub_category."', '".$item_type."', 'yes' ) ";
	execsql($production_plan_query);
	
	$table_name = $_SESSION['db_prefix']."_production_plan_lines";
	$production_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'skip', '".$production_date."', '".$skip_quantity."' ) ";
	execsql($production_query);
	
	$table_name = $_SESSION['db_prefix']."_production_plan_lines";
	$est_production_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'estimated', '".$production_date."', '".$est_quantity."' ) ";
	execsql($est_production_query);

$table_name = $_SESSION['db_prefix']."_tracking_production";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '70', '".$production_no."' , '".$mix_design_id."', '".$mix_design_name."', '".$skip_quantity."' )";
execsql($trackingQuery);

$error=0;

}
else {
$error=1;
}
?>


<script language="javascript" type="text/javascript">
      window.location="../production/edit_production_plan.php?production_date=<?php echo $production_date ?>&e=<?php echo $error ?>";
</script>