<?php
include_once("../../class/clsConnection.php");
include_once(
"../../class/clsDB.php");
include_once 
"../global.php";
include_once 
"../class/clsDepartment.php";
include_once 
"../link/function.php";
include_once 
"../class/clsPerson.php";
include_once 
"../class/clsdeptref.php";

$oC = new clsConnection($GLOBALS['DBHOST'], $GLOBALS['DBNAME_EPERSON'], $GLOBALS['DBUSER_EPERSON'], $GLOBALS['DBPASS_EPERSON']);

$obj = new Department($oC);
$obj1 = new Department($oC);
$oDp= new Department($oC);
$oDp2= new Department($oC);
$oPS = new Person($oC);
$oDr= new deptref($oC);
if(
$method=="add"){
            
$obj->AddNew();
            
$obj->deptId=$obj->GetNextCode();
            
$obj->deptName=$deptName;
            
$obj->depttypeId=$depttypeId;
            
$obj->deptParentId=$deptParentId;
            
$obj->deptLevel=$deptLevel;
            
$obj->deptstartyear=$startyear;
            
$obj->deptendyear=$endyear;
            
$obj->deptgroup=$deptgroup;
            
$obj->Save();
}else if(
$method=="edit"){
            
$obj->SearchByKey($deptId);
            
$obj->GetRecord();
            
$obj->Edit();
            
$obj->deptId=$deptId;
            
$obj->deptName=$deptName;
            
$obj->depttypeId=$depttypeId;
            
$obj->deptParentId=$obj->deptParentId;
            
$obj->deptLevel=$obj->deptLevel;
            
$obj->deptstartyear=$obj->deptstartyear;
            
$obj->deptendyear=$obj->deptendyear;
            
$obj->deptgroup=$obj->deptgroup;
            
$obj->Save();
}else if(
$method=="confirmdeptdate"){
            
$oDp->RSMainDepartmentdeptDate0();
            while(
$oDp->GetRecord()){
                
$oDp->Edit();
                
$oDp->deptId=$oDp->deptId;
                
$oDp->deptName=$oDp->deptName;
                
$oDp->deptDesc=$oDp->deptDesc;
                
$oDp->deptCode=$oDp->deptCode;
                
$oDp->deptParent=$oDp->deptParent;
                
$oDp->deptYear=$oDp->deptYear;
                
$oDp->deptDate=splitDateForm($DocDate,"/");
                
$oDp->deptRef=$oDp->deptRef;
                
$oDp->pbriId=$oDp->pbriId;
                
$oDp->depttypeId=$oDp->depttypeId;
                
$oDp->deptParentId=$oDp->deptParentId;
                
$oDp->deptLevel=$oDp->deptLevel;
                
$oDp->deptgroup=$oDp->deptgroup;
                
$oDp->Save();
            }
            
            
//---update person move dept 
            
$oPS->RSPersonf1(); 
             while(
$oPS->GetRecord()){
                                
$oPS->Edit();
                                
$oPS->personId=$oPS->personId;
                                
//echo "personId=".$oPS->personId."<br>";
                                
$oDr->searchBydeptRefId($oPS->deptId);
                                if(
$oDr->GetRecord()==1){
                                    
$oPS->deptId=$oDr->deptId;
                                    
//echo "deptIdnew=".$oPS->deptId."<br>";
                                
}else{
                                    
$oPS->deptId=$oPS->deptId;
                                    
//echo "deptIdold=".$oPS->deptId."<br>";
                                
}
                                
$oPS->workGpId=$oPS->workGpId;
                                
$oPS->prefixId=$oPS->prefixId;
                                
$oPS->fName=$oPS->fName;
                                
$oPS->lName=$oPS->lName;
                                
$oPS->fStatus=$oPS->fStatus;
                                
$oPS->adminId=$oPS->adminId;
                                
$oPS->fRetire=$oPS->fRetire;
                                
$oPS->hireId=$oPS->hireId;
                                
//echo "เจอ=update=".$oPS->fName."".$oPS->lName."fstatus=".$oPS->fStatus."<br>";
                                
$oPS->Save();
                                
//echo "-----------------<br>";
             
}
        echo 
"<meta http-equiv='refresh' content='0; URL=showdept.php'>";
}else if(
$method=="delete"){

    
$obj->SearchByKey($deptId);
    
$obj->GetRecord();
    
$obj->Delete();
    
    
DelDeptParent($deptId);
    echo 
"<meta http-equiv='refresh' content='0; URL=deptstructure.php'>";
}else if(
$method=="deleteyear"){
    
$obj->RSyear($startyear,$endyear);
    while(
$obj->GetRecord()){
            
$obj1->SearchByKey($obj->deptId);
            
$obj1->GetRecord();
            
$obj1->Delete();
    }
}else if(
$method=="deletedept"){
    
$obj->searchdeptDate($deptDate);
    while(
$obj->GetRecord()){
        
$obj->Delete();
        
$oDr->deleteDept($obj->deptId);
    }
    echo 
"<meta http-equiv='refresh' content='0; URL=showdept.php'>";
}
function 
DelDeptParent($p){
include_once(
"../../class/clsConnection.php");
include_once(
"../../class/clsDB.php");
include_once 
"../global.php";
include_once 
"../class/clsDepartment.php";
include_once 
"../class/clsdeptref.php";

$oC = new clsConnection($GLOBALS['DBHOST'], $GLOBALS['DBNAME_EPERSON'], $GLOBALS['DBUSER_EPERSON'], $GLOBALS['DBPASS_EPERSON']);
$obj1 = new Department($oC);
$oDr= new deptref($oC);

        
$obj1->RSdepthavedeptParentId($p);
        while(
$obj1->GetRecord()){
            if(
$obj1->deptId!=""){
                
$p=$obj1->deptId;
                
$obj1->Delete();
                
DelDeptParent($p);
            }
        }
        
$oDr->deleteDept($p);
}
$oC->Disconnect();
?>