<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');

$companyID = $_REQUEST['companyID'];
$WID = $_REQUEST['WID'];
$WIDO = $_REQUEST['WIDO'];
$creationDate = date('Y-m-d');

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

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


            }

        }


    }
////
}
mysql_query("UPDATE wbslist SET WBSList_type=1 WHERE WBSList_id='$WIDO'");
header("location: 03-04Scope-01WBS01.php?msg=WBS Imported Successfully.");

