<?php
include('../includes/con2pma3.php');
 if (isset($_SESSION['UserName']))
{				
				
				if(isset($_SESSION['id'])){					
				$companyID = $_SESSION['id'];
				}
				else
				{
				$companyID = 0;
				}
				$code =  $_POST['WBSN'];
				$WBStype =  $_POST['WBST'];
			 $importCSV =  $_FILES["importCSV"]["name"];
				$WBSremarks =  $_POST['WBSR'];
				$creationDate = date('Y-m-d');
				
				
	if(isset($_REQUEST['del'])){
					
					
						$WID = $_REQUEST['del'];
			
						if(mysql_query("delete from wbslist WHERE `WBSList_id` = '$WID'"))
						{
							header("location: WBS1.php?msg=DELETED");
						}
						else
						{
							header("location: WBS1.php?msg=NOT DELETED");
						}
		
				}
				else if(isset($_REQUEST['Active'])){
	$IID = $_REQUEST['Active'];

			if(mysql_query("update wbslist set WBSList_status='1' WHERE `WBSList_id` = '$IID'"))
			{
				header("location: WBS1.php?msg1=MADE ACTIVED");
			}
			else
			{
				header("location: WBS1.php?msg1=NOT ACTIVED");

			}

	}
	else if(isset($_REQUEST['InActive'])){
	$IID = $_REQUEST['InActive'];            
			if(mysql_query("update wbslist set WBSList_status='0' WHERE `WBSList_id` = '$IID'"))
			{
				header("location: WBS1.php?msg1=MADE HIDDEN");
			}
			else
			{
				header("location: WBS1.php?msg1=NOT HIDDEN");

			}

	}
				else if(isset($_REQUEST['Edit'])){
						$WID = $_REQUEST['Edit'];
if(mysql_query("UPDATE wbslist SET `WBSList_code` = '$code', WBSList_remarks = '$WBSremarks' WHERE  WBSList_id = '$WID'"))
	
						{
								header("location: WBS1.php?msg=UPDATED");
						}
						else
						{
								header("location: WBS1.php?msg=NOT UPDATED");
						}
				}
				else if(mysql_query("insert into wbslist set `WBSList_for` = '1', `WBSList_code` = '$code', `WBSList_type` = '$WBStype', `WBSList_file_name` = '$importCSV', `WBSList_remarks` = '$WBSremarks', `WBSList_datecreation` = '$creationDate', `WBSList_companyid` = '$companyID', `WBSList_status` = '1'"))
				{
					
						if(isset($_POST['importPM'])){
							$WID = mysql_insert_id();
							$WIDO = $_POST['importPM'];
							
							$parentB = 0;
							$parentC = 0;
							$parentD = 0;
							$parentE = 0;
							$PNode = 0;
							$pitemid = 0;
							
			$val = mysql_query("SELECT sortCode, WBSIDA, WBSIDB, WBSIDC, WBSIDD, WBSIDE, description FROM globalwbs where gwbslist_id = '$WIDO' && companyID = '$id' ORDER BY WBSIDA ASC,WBSIDA ASC,WBSIDB ASC,WBSIDC ASC,WBSIDD ASC,WBSIDE ASC");
							while($row = mysql_fetch_array($val , MYSQL_ASSOC))						
								{
							$SCode = $row['sortCode'];
							$WBSIDA = $row['WBSIDA'];
							$WBSIDB = $row['WBSIDB'];
							$WBSIDC = $row['WBSIDC'];
							$WBSIDD = $row['WBSIDD'];
							$WBSIDE = $row['WBSIDE'];
							$description = $row['description'];
							
							////
							if($WBSIDB=="0" && $WBSIDC=="0" && $WBSIDD=="0" && $WBSIDE=="0")
										{
											$parentB = 0;
											$parentC = 0;
											$parentD = 0;
											$parentE = 0;
											
											
	$sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','0','$description','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentB = mysql_insert_id();	
										}
									else if($WBSIDB !=0 && $WBSIDC==0 && $WBSIDD==0 && $WBSIDE==0)
	{
		$sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentB','$description','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentC = mysql_insert_id();	
	}
else if($WBSIDC !=0 && $WBSIDD==0 && $WBSIDE==0)
	{
		
		$sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentC','$description','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentD = mysql_insert_id();

	}	
	else if($WBSIDD !=0 && $WBSIDE==0)
	{
		$sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentD','$description','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$parentE = mysql_insert_id();
	}
	
	else if($WBSIDE !=0)
	{
		$sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentE','$description','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	}
							////		
}
							header("location: WBS1.php?msg=WBS copied");
						
						} //end of copy PM
						else 
					$msg1 = "";
					if(isset($_FILES["importCSV"]["name"])){
						$parentB = 0;
											$parentC = 0;
											$parentD = 0;
											$parentE = 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 wbslist where wbslist_id='$WID'");
      							$msg1 = "FILE ALREADY EXISTS. RETRY WITH A DIFFERENT 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]=="MWBS" || $data[0]=="WBS"){
								$ver = explode(".",$data[1]); 
								
								$WBSIDA = $ver[0];   
								$WBSIDB = $ver[1];
								$WBSIDC = $ver[2];
								$WBSIDD = $ver[3];
								$WBSIDE = $ver[4];
										//sql insert query;
											if($ver[1]=="0" && $ver[2]=="0" && $ver[3]=="0" && $ver[4]=="0")
										{
											$parentB = 0;
											$parentC = 0;
											$parentD = 0;
											$parentE = 0;
											
											
	$sql = "insert into globalwbs values('','$data[0]','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','0','$data[2]','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentB = mysql_insert_id();	
										}
									else if($ver[1] !=0 && $ver[2]==0 && $ver[3]==0 && $ver[4]==0)
	{
		$sql = "insert into globalwbs values('','$data[0]','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentB','$data[2]','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentC = mysql_insert_id();	
	}
else if($ver[2] !=0 && $ver[3]==0 && $ver[4]==0)
	{
		
		$sql = "insert into globalwbs values('','$data[0]','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentC','$data[2]','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentD = mysql_insert_id();

	} 
	
	
	else if($ver[3] !=0 && $ver[4]==0)
	{
		$sql = "insert into globalwbs values('','$data[0]','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentD','$data[2]','','$creationDate','$WID','$companyID')";
	
	mysql_query($sql);
	$PNode = $parentE = mysql_insert_id();
	}
	
	else if($ver[4] !=0)
	{
		$sql = "insert into globalwbs values('','$data[0]','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentE','$data[2]','','$creationDate','$WID','$companyID')";
	
	$PNode = mysql_query($sql);
	}
	
								} else if($data[0]=="ITEM"){
									
	$sql = "insert into globalitems values('NULL','$data[0]','$PNode','$data[2]','','0','$creationDate','0','$companyID')";
	
	mysql_query($sql);
	$pitemid = mysql_insert_id();
	}//item end
	
	 else if($data[0]=="SITEM"){
									
	$sql = "insert into globalitems values('NULL','$data[0]','$PNode','$data[2]','','$pitemid','$creationDate','0','$companyID')";
	
	mysql_query($sql);
	
	}//Sitem end
	
}//while
							
								fclose($handle);
							$msg1 = "INFORMATION IS SAVED-Imported";
							
							}
							
							}
							
							}
							else
							{
								mysql_query("delete from wbslist where wbslist_id='$WID'");
							$msg1 = "Only .csv files can be imported.";	
							}
							header("location: WBS1.php?msg1=$msg1");
							
					} else	header("location: WBS1.php?msg= SAVED");
				}
				else
				{
					header("location: WBS1.php?msg= NOT SAVED: ERROR");
				}

}

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