<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');
 if (isset($_SESSION['UserName']))
{				
				
				if(isset($_SESSION['id'])){					
				$companyID = $_SESSION['id'];
				}
				else
				{
				$companyID = 0;
				}
				$code =  $_POST['code'];
				$WBStype =  $_POST['WBStype'];
				$title = $_POST['WBStitle'];
			 	$importCSV =  $_FILES["importCSV"]["name"];
			 //	die($importCSV);
				$WBSremarks =  $_POST['WBSR'];
				$creationDate = date('Y-m-d');



				if(isset($_REQUEST['import'])){
                    if(isset($_POST['importPM'])){


                        ////////////
                        $company_id = 261;
                        $WID = $_POST['WID'];
                        $WIDO = $_POST['importPM'];
                        $creationDate = date('Y-m-d');

                        $parentB = 0;
                        $parentC = 0;
                        $parentD = 0;
                        $parentE = 0;
                        $PNode = 0;
                        $pitemid = 0;

///////////////////////////////////Checking existence////////////////////////////

                        $val = mysql_query("SELECT WBSID, sortCode, WBSIDA, WBSIDB, WBSIDC, WBSIDD, WBSIDE, description FROM globalwbs where gwbslist_id = '$WIDO' && companyID = '$company_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'];

                            ////MWBS Level 1
                            if ($WBSIDB == "0" && $WBSIDC == "0" && $WBSIDD == "0" && $WBSIDE == "0") {

// check if the starting wbs code already exists or not
                                $resultExist = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' AND companyID = '$id'
                       AND  WBSIDA='$WBSIDA' AND WBSIDB='$WBSIDB' AND WBSIDC='$WBSIDC' AND WBSIDD='$WBSIDD' AND WBSIDE='$WBSIDE'");
                                if (mysql_num_rows($resultExist) > 0) {
                                    header("location: 03-04Scope-01WBS01.php?msg=WBS could not be imported because a similar WBS code already exists");
                                    die();
                                }
                            }
                        }

                        ///////////////////////////////existence-end///////////////////////////////////

                        $val = mysql_query("SELECT WBSID, sortCode, WBSIDA, WBSIDB, WBSIDC, WBSIDD, WBSIDE, description FROM globalwbs where gwbslist_id = '$WIDO' && companyID = '$company_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'];

                            ////MWBS Level 1
                            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','$id')";

                                mysql_query($sql);
                                $parentB = mysql_insert_id();

// Level 1 Items Copy Code

                                $items1Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = 0";
                                $result1Q = mysql_query($items1Q);
                                while( $row1Q = mysql_fetch_object($result1Q)){
                                    $itemParent1=null;

                                    $itemInsert1 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$row1Q->sortCode',
                                '$parentB', '$row1Q->description', '$row1Q->itemParent', '$creationDate', '0', '$id')";
                                    mysql_query( $itemInsert1);
                                    $itemParent1 = mysql_insert_id();

                                    // Sub-Items Copy Code

                                    $subItems1Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = '$row1Q->itemID'";
                                    $subResult1Q = mysql_query($subItems1Q);
                                    while( $subRow1Q = mysql_fetch_object($subResult1Q)){

                                        $itemInsert1 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$subRow1Q->sortCode',
                                '$parentB', '$subRow1Q->description', '$itemParent1', '$creationDate', '0', '$id')";
                                        mysql_query( $itemInsert1);


                                    }

                                }



                            }// WBS Level 2
                            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','$id')";

                                mysql_query($sql);
                                $parentC = mysql_insert_id();


                                // Level 2 Items Copy Code

                                $items2Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = 0";
                                $result2Q = mysql_query($items2Q);
                                while( $row2Q = mysql_fetch_object($result2Q)){
                                    $itemParent2=null;

                                    $itemInsert2 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$row2Q->sortCode',
                                '$parentC', '$row2Q->description', '$row2Q->itemParent', '$creationDate', '0', '$id')";
                                    mysql_query( $itemInsert2);
                                    $itemParent2 = mysql_insert_id();

                                    // Sub-Items Copy Code

                                    $subItems2Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = '$row2Q->itemID'";
                                    $subResult2Q = mysql_query($subItems2Q);
                                    while( $subRow2Q = mysql_fetch_object($subResult2Q)){

                                        $itemInsert2 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$subRow2Q->sortCode',
                                '$parentC', '$subRow2Q->description', '$itemParent2', '$creationDate', '0', '$id')";
                                        mysql_query( $itemInsert2);


                                    }

                                }







                            }// WBS Level 3
                            else if($WBSIDC !=0 && $WBSIDD==0 && $WBSIDE==0)
                            {

                                $sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentC','$description','','$creationDate','$WID','$id')";

                                mysql_query($sql);
                                $parentD = mysql_insert_id();


                                // Level 3 Items Copy Code

                                $items3Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = 0";
                                $result3Q = mysql_query($items3Q);
                                while( $row3Q = mysql_fetch_object($result3Q)){
                                    $itemParent3=null;

                                    $itemInsert3 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$row3Q->sortCode',
                                '$parentD', '$row3Q->description', '$row3Q->itemParent', '$creationDate', '0', '$id')";
                                    mysql_query( $itemInsert3);
                                    $itemParent3 = mysql_insert_id();

                                    // Sub-Items Copy Code

                                    $subItems3Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = '$row3Q->itemID'";
                                    $subResult3Q = mysql_query($subItems3Q);
                                    while( $subRow3Q = mysql_fetch_object($subResult3Q)){

                                        $itemInsert3 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$subRow3Q->sortCode',
                                '$parentD', '$subRow3Q->description', '$itemParent3', '$creationDate', '0', '$id')";
                                        mysql_query( $itemInsert3);


                                    }

                                }



                            } // WBS Level 4
                            else if($WBSIDD !=0 && $WBSIDE==0)
                            {
                                $sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentD','$description','','$creationDate','$WID','$id')";

                                mysql_query($sql);
                                $parentE = mysql_insert_id();



                                // Level 4 Items Copy Code

                                $items4Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = 0";
                                $result4Q = mysql_query($items4Q);
                                while( $row4Q = mysql_fetch_object($result4Q)){
                                    $itemParent4=null;

                                    $itemInsert4 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$row4Q->sortCode',
                                '$parentE', '$row4Q->description', '$row4Q->itemParent', '$creationDate', '0', '$id')";
                                    mysql_query( $itemInsert4);
                                    $itemParent4 = mysql_insert_id();

                                    // Sub-Items Copy Code

                                    $subItems4Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = '$row4Q->itemID'";
                                    $subResult4Q = mysql_query($subItems4Q);
                                    while( $subRow4Q = mysql_fetch_object($subResult4Q)){

                                        $itemInsert4 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$subRow4Q->sortCode',
                                '$parentE', '$subRow4Q->description', '$itemParent4', '$creationDate', '0', '$id')";
                                        mysql_query( $itemInsert4);


                                    }

                                }



                            }
// WBS Level 5
                            else if($WBSIDE !=0)
                            {
                                $sql = "insert into globalwbs values('','$SCode','NULL','$WBSIDA','$WBSIDB','$WBSIDC','$WBSIDD','$WBSIDE','$parentE','$description','','$creationDate','$WID','$id')";

                                mysql_query($sql);
                                $parentF = mysql_insert_id();


                                // Level 5 Items Copy Code

                                $items5Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = 0";
                                $result5Q = mysql_query($items5Q);
                                while( $row5Q = mysql_fetch_object($result5Q)){
                                    $itemParent5=null;

                                    $itemInsert5 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$row5Q->sortCode',
                                '$parentF', '$row5Q->description', '$row5Q->itemParent', '$creationDate', '0', '$id')";
                                    mysql_query( $itemInsert5);
                                    $itemParent5 = mysql_insert_id();

                                    // Sub-Items Copy Code

                                    $subItems5Q = "SELECT * FROM globalitems WHERE WBSID = ".$row['WBSID']." AND itemParent = '$row5Q->itemID'";
                                    $subResult5Q = mysql_query($subItems5Q);
                                    while( $subRow5Q = mysql_fetch_object($subResult5Q)){

                                        $itemInsert5 = "INSERT INTO globalitems (sortCode, WBSID, description,  itemParent, dateCreated, unitID, companyID) VALUES ('$subRow5Q->sortCode',
                                '$parentF', '$subRow5Q->description', '$itemParent5', '$creationDate', '0', '$id')";
                                        mysql_query( $itemInsert5);


                                    }

                                }


                            }
////
                        }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


                        $resultN = mysql_query("SELECT  WBSList_code,WBSList_title FROM wbslist where WBSList_id='$WIDO' and WBSList_status=1");

                        if ($rowN = mysql_fetch_array($resultN, MYSQL_ASSOC)) {
                            $fileName = $rowN["WBSList_code"].' - '.$rowN["WBSList_title"];
                        }
                        mysql_query("UPDATE wbslist SET WBSList_type=1,WBSList_file_name='$fileName' WHERE WBSList_id='$WID'");

                        header("location: 03-04Scope-01WBS01.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 = $_POST['WID']; // getting from import page hidden field
                        $importCSV = $_FILES["importCSV"]["name"];
                        $fileName = basename($_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);

                                //////////////////////////////////////Existence-check//////////////////////////////////////////////////////
                                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") {
                                                // check if the starting wbs code already exists or not
                                                $resultExist = mysql_query("SELECT * FROM globalwbs where gwbslist_id = '$WID' AND companyID = '$id'
                       AND  WBSIDA='$WBSIDA' AND WBSIDB='$WBSIDB' AND WBSIDC='$WBSIDC' AND WBSIDD='$WBSIDD' AND WBSIDE='$WBSIDE'");
                                                if (mysql_num_rows($resultExist) > 0) {
                                                    header("location: 03-04Scope-01WBS01.php?msg=File could not be imported because a similar WBS code already exists");
                                                    die();
                                                }

                                            }
                                        }
                                    }//whileend
                                    fclose($handle);
                                }
                                ////////////////////////////////////////check-end////////////////////////////////////////////////////



                                $ro = 0;

                                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";
                                    mysql_query("UPDATE wbslist SET WBSList_file_name='$fileName', WBSList_type=2 WHERE WBSList_id='$WID'");

                                }

                            }

                        }
                        else
                        {
                           // mysql_query("delete from wbslist where wbslist_id='$WID'");
                            $msg1 = "Only .csv files can be imported.";
                        }
                        header("location: 03-04Scope-01WBS01.php?msg=$msg1");

                    }
				}

				
	else if(isset($_REQUEST['del'])){
					
					
						$WID = $_REQUEST['del'];
			
						if(mysql_query("delete from wbslist WHERE `WBSList_id` = '$WID'"))
						{
							header("location: 03-04Scope-01WBS01.php?msg=DELETED");
						}
						else
						{
							header("location: 03-04Scope-01WBS01.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: 03-04Scope-01WBS01.php?msg=MADE ACTIVE");
			}
			else
			{
				header("location: 03-04Scope-01WBS01.php?msg=ARCHIVED");

			}

	}
	else if(isset($_REQUEST['InActive'])){
	$IID = $_REQUEST['InActive'];            
			if(mysql_query("update wbslist set WBSList_status='0' WHERE `WBSList_id` = '$IID'"))
			{
				header("location: 03-04Scope-01WBS01.php?msg=ARCHIVED");
			}
			else
			{
				header("location: 03-04Scope-01WBS01.php?msg=MADE ACTIVE");

			}

	}
				else if(isset($_REQUEST['Edit'])){
						$WID = $_REQUEST['Edit'];
if(mysql_query("UPDATE wbslist SET `WBSList_code` = '$code', WBSList_remarks = '$WBSremarks', WBSList_title='$title' WHERE  WBSList_id = '$WID'"))
	
						{
								header("location: 03-04Scope-01WBS01.php?msg=UPDATED");
						}
						else
						{
								header("location: 03-04Scope-01WBS01.php?msg=NOT UPDATED");
						}
				}
				else if(mysql_query("insert into wbslist set `WBSList_for` = '1', `WBSList_title` = '$title', `WBSList_code` = '$code', `WBSList_type` = '$WBStype', `WBSList_file_name` = '$importCSV', `WBSList_remarks` = '$WBSremarks', `WBSList_datecreation` = '$creationDate', `WBSList_companyid` = '$companyID', `WBSList_status` = '1'"))
				{
					
						 	header("location: 03-04Scope-01WBS01.php?msg= SAVED");
				}
				else
				{
					header("location: 03-04Scope-01WBS01.php?msg= NOT SAVED: ERROR");
				}

}

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