<?php
include_once("../../class/clsConnection.php");
include_once(
"../../class/clsDB.php");
include_once 
"../global.php";
include_once 
"../class/clsDepartment.php";
include_once 
"../class/clsPerson.php";
include_once 
"../link/function.php";
include_once 
"../link/functionshow.php";
include_once 
"../class/clsDocLinePosition.php";
include_once 
"../class/clsDocLineConfig.php";
include_once 
"../class/clsReceiveSendType.php";
include_once 
"../class/clsDocType.php";
include_once 
"../class/clsDocSpeedLevel.php";
include_once 
"../class/clsDocSecreLevel.php";
include_once 
"../class/clsDocattatchesTmp.php";
include_once 
"../class/clsDocuments.php";
include_once 
"../class/clsDocattatches.php";
include_once 
"../class/clsDocReceiveSend.php";
include_once 
"../class/clsRunningDoc.php";
include_once 
"funct.php";

$oC = new clsConnection($GLOBALS['DBHOST'], $GLOBALS['DBNAME_EOFFICE'], $GLOBALS['DBUSER_EOFFICE'], $GLOBALS['DBPASS_EOFFICE']);

$oDP = new Department($oC);
$oDP2 = new Department($oC);
$oDP3 = new Department($oC);
$oDP4 = new Department($oC);
$oDP5 = new Department($oC);
$oPS = new person($oC);
$oPS2 = new person($oC);
$oPS3 = new person($oC);
$oDlc = new DocLineConfig($oC);
$oDlc2 = new DocLineConfig($oC);
$oDlc3 = new DocLineConfig($oC);
$oDlp = new docLinePosition($oC);
$oDlp1 = new docLinePosition($oC);
$oRSt = new receiveSendType($oC);
$oDt = new doctype($oC);
$oDsl = new DocSpeedLevel($oC);
$oDcl = new DocSecretLevel($oC);
$oDtmp = new DocattatchesTmp($oC);
$oDoc = new Documents($oC);
$oDoc2 = new Documents($oC);
$oDatt = new Docattatches($oC);
$oRs = new DocReceiveSend($oC);
$oRs1 = new DocReceiveSend($oC);
$oRs2 = new DocReceiveSend($oC);
$oRs3 = new DocReceiveSend($oC);
$oRs4 = new DocReceiveSend($oC);
$oRd = new runningdoc($oC);

$MaxDocGroup=$oDP->SearchMaxDocGroup();

if(
$method=="addReceiveDoc"){ 
    
$flagCommit true;
    
$oC->BeginTrans();
    
                
$timesend=getNowDateTh()." ".date('H:i:s');
                
                
//-----check runningNo
                
$checkerror=0;
                if(
$DtID!=15){  $insertDtID="3"; }else{  $insertDtID="15"; }
                
$DocTypeNo=ShowRunningDoc(3,$insertDtID,$deptId); 
                
//echo "==".$DocTypeNo;
                
$oDP->SearchByKey($deptId); $oDP->GetRecord();  //echo $oDP->deptName;
                
if($DocTypeNo=="N"){
                    
$checkerror++;
                
?>
                    <script>alert("หน่วยงาน<?  echo $oDP->deptName?>\nยังไม่มีการตั้งค่าการใช้งานเลขทะเบียนหนังสือภายใน \nกรุณาติดต่อผู้รับผิดชอบระบบ");</script>
                <?     
                    
echo "<meta http-equiv='refresh' content='0; URL=showReceiveDoc.php?DtID=$DtID&RsID=$RsID&DocID=$DocID&DrsID=$DrsID'>";
                }
                if(
$DocTypeNo=="NoConfirm"){
                    
$checkerror++;
                
?>
                    <script>alert("หน่วยงาน<?  echo $oDP->deptName?>\nยังไม่ยืนยันการใช้งานเลขทะเบียนหนังสือภายใน \nกรุณาติดต่อผู้รับผิดชอบระบบ");</script>
                <?
                    
echo "<meta http-equiv='refresh' content='0; URL=showReceiveDoc.php?DtID=$DtID&RsID=$RsID&DocID=$DocID&DrsID=$DrsID'>";
                }
                
                
//----set running Doc
                
$oRd->SearchRunningBydocGroupDtIDDept($DocGroup,$insertDtID,$deptId);
                
$oRd->GetRecord();
                
$oRd->Edit();
                
$oRd->receiveNo=$DocTypeNo;
                
$flagCommit $oRd->Save();
                
                
//----edit set Document
                    
$oDoc2->SearchByKey($DocID);
                    
$oDoc2->GetRecord();
                    
$oDoc2->Edit();
                    
$oDoc2->DsID="1";
                    
$oDoc2->DocDateCreate=$timesend;
                    
$oDoc2->DocTypeNo=$oRd->receiveNo;
                    
$flagCommit $oDoc2->Save();
        
                    
//-----edit set RsDoc
                    
$oRs->SearchByKey($DrsID);
                    
//echo "DrsID_edit=".$DrsID."<br>";
                    
$oRs->GetRecord();
                    
$oRs->Edit();
                    
$oRs->DrsSendDate=$timesend;
                    
$oRs->DrsSendPersonId=$personId;
                    
$oRs->DrsSendDlcID=$DlcID;    
                    
$flagCommit $oRs->Save();
                    
                    
$oRs2->AddNew();
                    
$oRs2->DrsID=$oRs2->GetNextCode();
                    
//echo "DrsID=".$oRs2->DrsID."<br>";
                    
$oRs2->DocID=$oRs->DocID;
                    
$oRs2->DlcID=$DlcID;
                    
$oRs2->DsID="1";
                    
$oRs2->PtID=$oRs->PtID;
                    
$oRs2->DrsDocTypeNo=$DocTypeNo;
                    
$oRs2->DrsReceiveDate=$timesend;
                    
$oDlc->SearchByKey($oDoc->DlcID); $oDlc->GetRecord(); 
                    
$oRs2->DrsReceivePersonId=$personId;
                    
$oRs2->DrsDlcIDCreate=$DlcID ;
                    
$oRs2->DrsPsIDCreate=$personId;
                    
$oRs2->DrsPropose=$DrsSend;
                    
$oRs2->DrsSign=$oRs2->DrsSign;
                    
$oRs2->DrsFromDrsID=$oRs->DrsID;
                    
$oRs2->DocGroup=$oRs->DocGroup;
                    
//echo $oRs->AnID;
                    
$oRs2->AnID=$oRs->AnID;
                    
$oRs2->DrsstartDatePost=$oRs->DrsstartDatePost;
                    
$oRs2->DrsendDatePost=$oRs->DrsendDatePost;    
                    
$flagCommit $oRs2->Save();
                    
            if(
$flagCommit)
                
$oC->CommitTrans();
            else
                
$oC->RollbackTrans();
                            
    echo 
"<meta http-equiv='refresh' content='0; URL=regisDoc.php?DtID=$DtID&RsID=$RsID'>";
}else if(
$method=="Noaccept"){
    
$flagCommit true;
    
$oC->BeginTrans();
//----edit set Document
                    
$timesend=getNowDateTh()." ".date('H:i:s');
                    
$oDoc2->SearchByKey($DocID);
                    
$oDoc2->GetRecord();
                    
$oDoc2->Edit();
                    
$oDoc2->DsID="6";
                    
$flagCommit $oDoc2->Save();
        
                    
//-----edit set RsDoc
                    
$oRs->SearchByKey($DrsID);
                    
//echo "DrsID_edit=".$DrsID."<br>";
                    
$oRs->GetRecord();
                    
$oRs->Edit();
                    
$oRs->DrsSendDate=$timesend;
                    
$oRs->DrsSendPersonId=$personId;
                    
$oRs->DrsSendDlcID=$DlcID;    
                    
$flagCommit $oRs->Save();
                    
                    
$oRs2->AddNew();
                    
$oRs2->DrsID=$oRs2->GetNextCode();
                    
//echo "DrsID=".$oRs2->DrsID."<br>";
                    
$oRs2->DocID=$oRs->DocID;
                    
$oRs2->DlcID=$DlcID;
                    
$oRs2->DsID="6";
                    
$oRs2->personId=$oRs->personId;
                    
$oRs2->PtID=$oRs->PtID;
                    
$oRs2->DrsDocTypeNo=$oRs->DrsDocTypeNo;
                    
$oRs2->DrsReceiveDate=$timesend;
                    
$oDlc->SearchByKey($oDoc->DlcID); $oDlc->GetRecord(); 
                    
$oRs2->DrsReceivePersonId=$personId;
                    
$oRs2->DrsDlcIDCreate=$DlcID;
                    
$oRs2->DrsPsIDCreate=$personId;
                    
$oRs2->DrsPropose=$DrsSend;
                    
$oRs2->DrsSign=$oRs2->DrsSign;
                    
$oRs2->DrsFromDrsID=$oRs->DrsID;
                    
$oRs2->DocGroup=$oRs->DocGroup;
                    
//echo $oRs->AnID;
                    
$oRs2->AnID=$oRs->AnID;
                    
$oRs2->DrsstartDatePost=$oRs->DrsstartDatePost;
                    
$oRs2->DrsendDatePost=$oRs->DrsendDatePost;    
                    
$flagCommit $oRs2->Save();
                    
            if(
$flagCommit)
                
$oC->CommitTrans();
            else
                
$oC->RollbackTrans();
                                    
    echo 
"<meta http-equiv='refresh' content='0; URL=receiveDoc.php'>";
}else if(
$method=="SendBack"){
    
$flagCommit true;
    
$oC->BeginTrans();
//----edit set Document
                    
$timesend=getNowDateTh()." ".date('H:i:s');
                    
$oDoc2->SearchByKey($DocID);
                    
$oDoc2->GetRecord();
                    
$oDoc2->Edit();
                    
$oDoc2->DsID="7";
                    
$flagCommit $oDoc2->Save();
        
                    
//-----edit set RsDoc
                    
$oRs->SearchByKey($DrsID);
                    
//echo "DrsID_edit=".$DrsID."<br>";
                    
$oRs->GetRecord();
                    
$oRs->Edit();
                    
$oRs->DrsSendDate=$timesend;
                    
$oRs->DrsSendPersonId=$personId;
                    
$oRs->DrsSendDlcID=$DlcID;    
                    
$flagCommit $oRs->Save();
                    
                    
$oRs3->SearchByDocIDDsID3Docgroup($oDoc2->DocPID,$MaxDocGroup);
                    
$oRs3->GetRecord();
                    
                    
                    
$oRs2->AddNew();
                    
$oRs2->DrsID=$oRs2->GetNextCode();
                    
//echo "DrsID=".$oRs2->DrsID."<br>";
                    
$oRs2->DocID=$oRs->DocID;
                    
$oRs2->DlcID=$DlcID;
                    
$oRs2->DsID="7";
                    
$oRs2->personId=$oRs->personId;
                    
$oRs2->PtID=$oRs->PtID;
                    
$oRs2->DrsDocTypeNo=$oRs->DrsDocTypeNo;
                    
$oRs2->DrsReceiveDate=$timesend;
                    
$oDlc->SearchByKey($oDoc->DlcID); $oDlc->GetRecord(); 
                    
$oRs2->DrsReceivePersonId=$personId;
                    
$oRs2->DrsDlcIDCreate=$DlcID;
                    
$oRs2->DrsPsIDCreate=$personId;
                    
$oRs2->DrsPropose=$DrsSend;
                    
$oRs2->DrsSign=$oRs2->DrsSign;
                    
$oRs2->DrsFromDrsID=$oRs->DrsID;
                    
$oRs2->DocGroup=$oRs->DocGroup;
                    
//echo $oRs->AnID;
                    
$oRs2->AnID=$oRs->AnID;
                    
$oRs2->DrsstartDatePost=$oRs->DrsstartDatePost;
                    
$oRs2->DrsendDatePost=$oRs->DrsendDatePost;
                    
$oRs2->DrsSendBack=$DrsSendBack;
                    
$oRs2->DrsSendBackDlcID=$oRs3->DlcID;    
                    
$flagCommit $oRs2->Save();
                    
            if(
$flagCommit)
                
$oC->CommitTrans();
            else
                
$oC->RollbackTrans();
                                    
    echo 
"<meta http-equiv='refresh' content='0; URL=receiveDoc.php'>";
}
?>