

<?php 
    $pID = $_REQUEST['pID'];

    $stts = $db->prepare('SELECT *FROM tbl_projects p
    INNER JOIN tbl_clients c ON c.ct_id=p.clnt 
    WHERE projid="'.$pID.'"
    ORDER BY projid DESC');
    $stts->execute();
    while($rr = $stts->fetch()){
       $projid = $rr['projid'];
       $projname = $rr['projname'];
       $ctNames = $rr['ct_names'];
       $agreeMont = $rr['agreeMont'];
       $wrkp = $rr['wps'];
       if($rr['wt']==1){
        $wt="Applicable";
        $withTax=15;
       }else{
        $wt="Not Applicable";
        $withTax =0;
       } 

    #### FILES UPLOADS ####
    if(isset($_REQUEST['saveDocs'])){
        $todate = date('Y-m-d H:i:s');

        $userRec = $_REQUEST['userRec'];
        $recAppoint = $_REQUEST['recAppoint'];
        $Deptrec = $_REQUEST['Deptrec'];
        $proj = $_REQUEST['proj'];

        $stt = 1;
        ######## FILES UPLOAD #############
        $countfiles = count($_FILES['file']['name']);
    
        $totalFileUploaded = 0;
        for($i=0;$i<$countfiles;$i++){
        $stms_last = $db->prepare("SELECT doc_id FROM tbl_docs  ORDER BY doc_id DESC LIMIT 1");
            try {
                $stms_last->execute(array());
                $row_count_last = $stms_last->rowCount();
                if ($row_count_last > 0) {
                    $rows = $stms_last->fetch(PDO::FETCH_ASSOC);
                        $doc_id = $rows['doc_id']+1;
                }else{
                    $doc_id = 1;
                }
            }
            catch (PDOException $ex) {
            echo $ex->getMessage();
            } 
    
            
            $filename = $doc_id."_".$_FILES['file']['name'][$i];
    
            ## Location
            $location = $path_upload.$filename;
            $extension = pathinfo($location,PATHINFO_EXTENSION);
            $extension = strtolower($extension);
    
            ## File upload allowed extensions
            $valid_extensions = array("jpg","jpeg","png","pdf","doc","docx","xls","xlsx","ppt","pptx","zip","tar"."gz");
    
            $response = 0;
            ## Check file extension
            if(in_array(strtolower($extension), $valid_extensions)) {
                ## Upload file
                if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){
    
                    //    echo "file name : ".$filename."<br/>";
                $stmt2 = $db->prepare("INSERT INTO tbl_docs(rec_dpt,prjID,usID,docAppoint,docName,uploaded_on,stts_doc) VALUES(:rec_dpt,:prjID,:usID,:docAppoint,:docName,:uploaded_on,:stts_doc)");
                $stmt2->execute(array(':rec_dpt'=>$Deptrec,':prjID' => $proj, ':usID' => $userRec, ':docAppoint' => $recAppoint, ':docName' => $filename, ':uploaded_on' => $todate,':stts_doc'=>$stt));
                // end to insert tbl_doc
    
                        $totalFileUploaded++;
                }
            }
    
        }
        echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("File(s) saved", "Successfully", "success", {
        button: "Continue",
        });
        });
    </script>';
    
    }
    
    ###### INVOICE #####
    $invce = $db->query('SELECT SUM(invceMont) AS totInv FROM tbl_invoices WHERE projInv="'.$pID.'"');
    $invce->execute();
    $ffinv = $invce->fetch();
    $totInv = $ffinv['totInv'];

    $totPercent = $totInv*100/$agreeMont;
    $perFin = number_format($totPercent, 0, '.', '' );

    ###### PAYMENT #####
    $pymt = $db->query('SELECT SUM(payMont) AS totPymt FROM tbl_payments WHERE projPay="'.$pID.'"');
    $pymt->execute();
    $ffpmt = $pymt->fetch();
    $totPmnt = $ffpmt['totPymt'];

    ##### WITH TAX #####
    $totWtax = $agreeMont*$withTax/100;
    $PaidWtax = $totPmnt*$withTax/100;

?>

<div class="d-flex flex-column flex-grow-1 bg-white">
    <!-- project header -->
    <div class="flex-grow-0">
        <!-- inbox button shortcut -->
        <div class="d-flex flex-wrap align-items-center pl-2 pr-3 py-2 px-sm-4 pr-lg-5 pl-lg-0 border-faded border-top-0 border-left-0 border-right-0">
            <div class="flex-1 d-flex align-items-center">
                <a href="<?php echo web_root;?>beam/bm?b=pj" class="btn btn-icon rounded-circle mr-2 mr-lg-3">
                    <i class="fa fa-arrow-left fs-lg"></i>
                </a>
                <a href="javascript:void(0);" onclick="window.location.reload(true);" class="btn btn-icon rounded-circle mr-1">
                    <i class="fa fa-redo fs-md"></i>
                </a>
            </div>

            <div class="d-flex flex-wrap">
                
            </div>
        </div>
        <!-- end inbox button shortcut -->
    </div>
    <!-- end inbox header -->

<div class="flex-wrap align-items-center flex-grow-1 position-relative bg-white">
    <div class=" position-absolute pos-top pos-bottom w-100 custom-scroll">
        <div class="d-flex h-100 flex-column">
            <!-- inbox title -->
            <div class="d-flex align-items-center pl-2 pr-3 py-3 pl-sm-3 pr-sm-4 py-sm-4 px-lg-5 py-lg-3 flex-shrink-0">
                <!-- button for mobile -->
                <a href="javascript:void(0);" class="pl-3 pr-3 py-2 d-flex d-lg-none align-items-center justify-content-center mr-2 btn" data-action="toggle" data-class="slide-on-mobile-left-show" data-target="#js-inbox-menu">
                    <i class="fal fa-ellipsis-v h1 mb-0 "></i>
                </a>
                <!-- end button for mobile -->
                <h1 class="subheader-title mb-0 ml-2 ml-lg-5">
                    PROJECT: <?php echo $projname;?>
                </h1>
                
                <div class="d-flex position-relative ml-auto">
                    <a href="javascript:void(0);" class="btn btn-xs ml-1 fs-lg hidden-lg-down" data-toggle="modal" data-target="#default-example-modal-sm-center">
                        <i class="fa fa-paperclip" aria-hidden="true"></i> My Docs
                    </a>
                </div>
            </div>
            <!-- end inbox title -->

            <!-- message -->
            <div id="msg-03" class="d-flex flex-column border-faded border-top-0 border-left-0 border-right-0 py-3 px-3 px-sm-4 px-lg-0 mr-0 mr-lg-5 flex-shrink-0">
                <div class="d-flex align-items-center flex-row">
                    <div class="ml-0 mr-3 mx-lg-3 width-2">
                        <img src="../img/folder.jpg" class="profile-image profile-image-md rounded-circle" alt="Melissa Ayre">
                    </div>
                    <div class="fw-500 flex-1 d-flex flex-column cursor-pointer" data-toggle="collapse" data-target="#msg-03 > .js-collapse">
                        <div class="fs-lg">
                            <?php echo $rr['codeproj'];?>
                            <span class="fs-nano fw-400 ml-2"><?php echo $rr['aggreno'];?></span>
                        </div>
                        <div class="fs-nano">
                            Client:  <?php echo $ctNames;?>
                        </div>
                    </div>

                    <div class="collapsed-reveal">
                        <a href="javascript:void(0);" class="btn btn-icon ml-1 fs-lg rounded-circle">
                            <i class="fa fa-reply"></i>
                        </a>
                    </div>
                </div>
                <div id="js-msg-1" class="js-collapse">
                    <div class="pl-lg-5 ml-lg-5 pt-3 pb-4 ">
                        
                    <div class="row">
                        <div class="col-sm-4 d-flex">
                            <div class="table-responsive">
                                <table class="table table-clean table-sm align-self-end">
                                    <tbody>
                                        <tr>
                                            <td>
                                            <strong>Signed Date:</strong>
                                            </td>
                                            <td>
                                                <?php echo date('d/m/Y', strtotime($rr['signdate']));?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <strong>Starting Date:</strong>
                                            </td>
                                            <td>
                                                <?php echo date('d/m/Y', strtotime($rr['startDue']));?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Duration:
                                            </td>
                                            <td>
                                               <?php echo $rr['durat']." Months";?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                WithHolding Tax:
                                            </td>
                                            <td>
                                                <?php echo $wt;?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Physical Progress</td>
                                            <td> 
                                                <div class="demo js-progress-animated">
                                                    <div class="progress progress-lg">
                                                        <div class="progress-bar progress-bar-striped bg-primary-500" role="progressbar" style="width: <?php echo $wrkp;?>%" aria-valuenow="<?php echo $wrkp;?>" aria-valuemin="0" aria-valuemax="100"><?php echo $wrkp;?>%</div>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td>Financial Progress</td>
                                            <td> 
                                            <div class="demo js-progress-animated">
                                                <div class="progress progress-lg">
                                                    <div class="progress-bar progress-bar-striped bg-primary-500" role="progressbar" style="width: <?php echo $perFin;?>%" aria-valuenow="<?php echo $perFin;?>" aria-valuemin="0" aria-valuemax="100"><?php echo $perFin;?>%</div>
                                                </div>
                                            </div>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="col-sm-4 ml-sm-auto">
                            <div class="table-responsive">
                                <table class="table table-sm table-clean text-right">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <strong>Progress History:</strong>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <strong></strong>
                                            </td>
                                        </tr>
                                        
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="table-responsive">
                                <table class="table mt-5">
                                    <thead class="bg-primary-500">
                                        <tr>                                            
                                            <th class="border-top-0 table-scale-border-bottom fw-700">Financial Status</th>
                                            <th class="border-top-0 table-scale-border-bottom fw-700">Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td class="text-left">Agreement Amount</td>
                                            <td class="text-left strong"><?php echo number_format($agreeMont);?></td>
                                        </tr>
                                        
                                        <tr>
                                            <td class="text-left">Total WithHolding Tax</td>
                                            <td class="text-left strong"><?php echo number_format($totWtax);?></td>
                                        </tr>

                                        <tr>
                                            <td class="text-left">Invoice Amount(s)</td>
                                            <td class="text-left strong"><?php echo number_format($totInv);?></td>
                                        </tr>

                                        <tr>
                                            <td class="text-left">Received Amount(s)</td>
                                            <td class="text-left strong"><?php echo number_format($totPmnt);?></td>
                                        </tr>

                                        <tr>
                                            <td class="text-left">Paid Tax</td>
                                            <td class="text-left strong"><?php echo number_format($PaidWtax);?></td>
                                        </tr>

                                        <tr>
                                            <td class="fw-700">Remaining Amount</td>
                                            <td class="fw-700"><?php echo number_format($agreeMont-$totPmnt);?></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                      

                    </div>

                    <div class="row">
                    <?php 
                        $i=0;
                        if($role_id==2 || $role_id==3 || $role_id==9 || $role_id==15){
                        $select = $db->prepare("SELECT * FROM `tbl_docs` WHERE prjID = '".$pID."' ORDER BY doc_id DESC"); 
                        }else{                       
                        $select = $db->prepare("SELECT * FROM `tbl_docs` WHERE prjID='".$pID."' AND rec_dpt='".$role_id."' ORDER BY doc_id DESC");
                        }                       
                        $select->execute(); 
                        while($row = $select->fetch()){
                            $doc_id = $row['doc_id'];
                            $uploaded_on = $row['uploaded_on'];
                            $docName = $row['docName'];
                            $rec_dpt = $row['rec_dpt'];
                            $usID = $row['usID'];
                            $stts_doc = $row['stts_doc'];
                            $i++;
                            
                            //Hide Extension
                            $file = new SplFileInfo($docName);
                            $ext  = $file->getExtension();
                            //Get File Name
                            $fileName = basename($docName);
                            $fileNameNoExtension = preg_replace("/\.[^.]+$/", "", $fileName);

                            //DOC ICON
                            if($ext == "pdf"){
                               $imgIcon = "pdf.png";
                            }if($ext == 'xls' || $ext == 'xlsx'){
                               $imgIcon = "excel.png";  
                            }if($ext == 'doc' || $ext == 'docx'){
                                $imgIcon = "docx.png";
                            }if($ext == 'ppt' || $ext == 'pptx'){
                                $imgIcon = "pptx.png";
                            }

                            
                            ####### DEPARTMENT #####
                            $dept = $db->prepare('SELECT *FROM tbl_role WHERE role_id = "'.$rec_dpt.'"');
                            $dept->execute();
                            $fDept = $dept->fetch();
                            $role_name = $fDept['role_name'];
                            
                            ###### USER #####
                            
                            $getUser = $db->prepare('SELECT *FROM tbl_user_accounts WHERE user_id = "'.$usID.'"');
                            $getUser->execute();
                            $fUs = $getUser->fetch();
                            $ffirst = $fUs['first_name'];
                            $fLst = $fUs['family_name'];
                    ?>
                        <div class="col-md-offset-2 col-md-2">
                                <div class="panel panel-default bootcards-file">
                                    <div class="list-group">
                                        <div class="list-group-item">
                                            <a href="../docs/<?php echo $docName;?>">
                                            <img src="../img/icon/<?php echo $imgIcon;?>" width="100">
                                            </a>
                                            
                                            <p class="list-group-item-text"><strong>
                                                <a href="../docs/<?php echo $docName;?>" style="color: #777;">
                                                    <?php echo $fileNameNoExtension;?>
                                                </a>
                                                </strong></p>
                                        </div>
                                    </div>

                            </div>
                        </div>
                        <?php  
                            }
                        ?>
                    </div>

                </div>
            </div>
            <!-- end message -->
        </div>
    </div>

   <?php include 'modalattach.php';?>

</div>

</div>

<?php 
  }
?>
      