<?php
include('../includes/con2pma3.php');
 if (isset($_SESSION['UserName']))
{				
				
				if(isset($_SESSION['id'])){					
				$companyID = $_SESSION['id'];
				}
				else
				{
				$companyID = 0;
				}
				$MFList_for_project =  $_POST['projname'];
				$MFList_type =  $_POST['importmasterformat'];
			    $importCSV =  $_FILES["importCSV"]["name"];
				$MFList_name =  $_POST['masterformatname'];
				$creationDate = date('Y-m-d');				
	if(isset($_REQUEST['del'])){
					
					
						$WID = $_REQUEST['del'];
			
						if(mysql_query("delete from mflist WHERE `MFList_id` = '$WID'"))
						{
							header("location: MasterFormat1.php?msg=DELETED");
						}
						else
						{
							header("location: MasterFormat1.php?msg=NOT DELETED");
						}
		
				}
				else if(isset($_REQUEST['Edit'])){
						$WID = $_REQUEST['Edit'];
if(mysql_query("UPDATE mflist SET `MFList_name` = '$MFList_name' WHERE  MFList_id = '$WID'"))
	
						{
								header("location: MasterFormat1.php?msg=UPDATED");
						}
						else
						{
								header("location: MasterFormat1.php?msg=NOT UPDATED");
						}
				}
				else if(mysql_query("insert into mflist set `MFList_name` = '$MFList_name', MFList_for = '1', MFList_for_project = '$MFList_for_project', MFList_type = '$MFList_type', MFList_file_name = '$importCSV', MFList_datecreation = '$creationDate', MFList_companyid = '$companyID'"))
				{
					    if(isset($_POST['importPMProject'])){
							$WID = mysql_insert_id();
							$WIDO = $_POST['importPMProject'];							
							$parentB = 0;
							$parentC = 0;
							$parentD = 0;							
							$PNode = 0;
							$pitemid = 0;
							
			$val = mysql_query("SELECT specCode, MFIDA, MFIDB, MFIDC, MFIDD, MFIDE, gmf_name FROM globalmf where MFList_for_project = '$WIDO' && companyID = '$id' ORDER BY MFIDA ASC,MFIDA ASC,MFIDB ASC,MFIDC ASC,MFIDD ASC");
							while($row = mysql_fetch_array($val , MYSQL_ASSOC))						
								{
							$SCode = $row['specCode'];							
							$MFIDA = $row['MFIDA'];
							$MFIDB = $row['MFIDB'];
							$MFIDC = $row['MFIDC'];
							$MFIDD = $row['MFIDD'];							
							$gmf_name = $row['gmf_name'];							////
							if($MFIDB=="0" && $MFIDC=="0" && $MFIDD=="0")
										{
											$parentB = 0;
											$parentC = 0;
											$parentD = 0;
											
											
											
	$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','0','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentB = mysql_insert_id();	
										}
									else if($MFIDB !=0 && $MFIDC==0 && $MFIDD==0)
	{
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentB','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentC = mysql_insert_id();	
	}
else if($MFIDC !=0 && $MFIDD==0)
	{
		
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentC','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentD = mysql_insert_id();

	}	
	else if($MFIDD !=0)
	{
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentD','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	}
							////		
}
							header("location: MasterFormat1.php?msg=MF copied");
						
						} //end of copy PM projects
						else if(isset($_POST['importPM'])){
							$WID = mysql_insert_id();
							$WIDO = $_POST['importPM'];							
							$parentB = 0;
							$parentC = 0;
							$parentD = 0;							
							$PNode = 0;
							$pitemid = 0;
							
			$val = mysql_query("SELECT specCode, MFIDA, MFIDB, MFIDC, MFIDD, MFIDE, gmf_name FROM globalmf where gmflist_id = '$WIDO' && companyID = '$id' ORDER BY MFIDA ASC,MFIDA ASC,MFIDB ASC,MFIDC ASC,MFIDD ASC");
							while($row = mysql_fetch_array($val , MYSQL_ASSOC))						
								{
							$SCode = $row['specCode'];							
							$MFIDA = $row['MFIDA'];
							$MFIDB = $row['MFIDB'];
							$MFIDC = $row['MFIDC'];
							$MFIDD = $row['MFIDD'];							
							$gmf_name = $row['gmf_name'];
							
							////
							if($MFIDB=="0" && $MFIDC=="0" && $MFIDD=="0")
										{
											$parentB = 0;
											$parentC = 0;
											$parentD = 0;
											
											
											
	$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','0','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentB = mysql_insert_id();	
										}
									else if($MFIDB !=0 && $MFIDC==0 && $MFIDD==0)
	{
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentB','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentC = mysql_insert_id();	
	}
else if($MFIDC !=0 && $MFIDD==0)
	{
		
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentC','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentD = mysql_insert_id();

	}	
	/*else if($MFIDD !=0 && $MFIDE==0)
	{
		$sql = "insert into globalmf values('','$SCode','$SSCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$MFIDE','$parentD','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentE = mysql_insert_id();
	}*/
	
	else if($MFIDD !=0)
	{
		$sql = "insert into globalmf values('','$SCode','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentD','$gmf_name','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	}
							////		
}
							header("location: MasterFormat1.php?msg=MF copied");
						
						} //end of copy PM
						else 
					$msg1 = "";
					if(isset($_FILES["importCSV"]["name"])){
						                    $parentB = 0;
											$parentC = 0;
											$parentD = 0;											
											$PNode = 0;
							$pitemid = 0;

							$WID = mysql_insert_id();
							$importCSV = $_FILES["importCSV"]["name"];
													
$extension = end(explode(".", $_FILES["importCSV"]["name"]));
if ($extension=="csv" || $extension=="CSV"){
														
							if (file_exists("../CLIENT-CSV/" . $_FILES["importCSV"]["name"]))
      						{
								mysql_query("delete from mflist where MFList_id='$WID'");
      							$msg1 = "File Already Exist. Retry With Differnt File Name.";
     						}
    						else
      						{
								$ro = 0;
								
								$imgpath =  $_FILES["importCSV"]["name"];
    							move_uploaded_file($_FILES["importCSV"]["tmp_name"],"../CLIENT-CSV/" . $imgpath);
								if (($handle = fopen("../CLIENT-CSV/".$importCSV, "r")) !== false) {
								while (($data = fgetcsv($handle, 1000, ",")) !== false) {
								/*if($ro ==0)
								{
									$ro=1;
								continue;
									}*/
									//if($data[0]=="MMF" || $data[0]=="MF"){
								$ver = explode(".", str_replace(' ', '.', $data[0])); 
								
								$MFIDA = ($ver[0] == '00' ? 0 : $ver[0]);   
								$MFIDB = ($ver[1] == '00' ? 0 : $ver[1]); 
								$MFIDC = ($ver[2] == '00' ? 0 : $ver[2]); 
								if(isset($ver[3]) && is_numeric ($ver[3])){
								$MFIDD = ($ver[3] == '00' ? 0 : $ver[3]); 
								}
								else{
									$MFIDD = '00';
								}							
										//sql insert query;
											if($MFIDB=="0" && $MFIDC=="0" && $MFIDD=="0")
										{
											$parentB = 0;
											$parentC = 0;
											$parentD = 0;											
											
	$sql = "insert into globalmf values('','$data[0]','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','0','$data[1]','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentB = mysql_insert_id();	
										}
else if($MFIDB !=0 && ($MFIDC==0 || $MFIDC!=0) && ($MFIDD==0 || $MFIDD!=0))
	{
		$sql = "insert into globalmf values('','$data[0]','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentB','$data[1]','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentC = mysql_insert_id();	
	}
else if($MFIDC !=0 && ($MFIDD==0 || $MFIDD!=0))
	{
		
		$sql = "insert into globalmf values('','$data[0]','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentC','$data[1]','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentD = mysql_insert_id();

	}	
	else if($MFIDD !=0)
	{
		$sql = "insert into globalmf values('','$data[0]','NULL','$MFIDA','$MFIDB','$MFIDC','$MFIDD','$parentD','$data[1]','$creationDate','$WID','$companyID')";
	
	$PNode = mysql_query($sql);
	}							
	
}//while
							
								fclose($handle);
							$msg1 = "INFORMATION IS SAVED-Imported";
							
							}
							
							}
							
							}
							else
							{
								mysql_query("delete from mflist where MFList_id='$WID'");
							$msg1 = "Only .csv files can be imported.";	
							}
							header("location: MasterFormat1.php?msg1=$msg1");
							
					} else	header("location: MasterFormat1.php?msg= SAVED");
				}
				else
				{
					header("location: MasterFormat1.php?msg= NOT SAVED: ERROR");
				}

}

else
{
	header("location: ../index.php?msg=YOU ARE NOT LOGINED");
}
?> 