
<div class="panel-content">
    <div class="panel-tag">
       Bill of Quantity (BoQ)
    </div>
    <ul class="nav nav-pills" role="tablist">
        <?php 
        ####### TITLE #####
        $stmt = $db->prepare('SELECT * FROM tbl_boq_title WHERE req_tle="'.$req.'" AND conbtle="'.$con.'" AND sts_btle=4 ORDER BY btle_id DESC');
            
        $stmt->execute(array());
        $title_count = $stmt->rowCount();

        if($title_count > 0)
        {
            while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $btle_id=$rows['btle_id'];

        ?>
        <li class="nav-item"><a href="gate?b=validReq&con=<?php echo $conid; ?>&boq=<?php echo $btle_id;?>" 
        class="nav-link <?php if($_REQUEST['boq']==$btle_id)echo'active';?>"><b><?php echo $rows['boqtitle_name'];?></b></a></li>
        <?php 	
            }//END. TITLE
            
        }//End Count.	
        ?>
    </ul>

    <br>
    <table class="table w-100" border="2" style="font-size: 11px;">
        <thead class="header bg-primary">
            <tr>
                <th style="font-weight: bold;width: 5px;"></th>            
                <th style="font-weight: bold;">DESCRIPTION</th>
                <th class="text-right" style="font-weight: bold;">UNIT</th>
                <th class="text-right" style="font-weight: bold;">QTY</th>
                <th class="text-right" style="font-weight: bold;"> RATE (Rwf)</th>
                <th class="text-right" style="font-weight: bold;">AMOUNT (RWF)</th>
            </tr>
        </thead>

        <tbody>
            <?php
                try {
                
                $i = 0;
                $Gtot=0;
                ####### TITLE #####
                $stmt = $db->prepare('SELECT * FROM tbl_boq_title WHERE req_tle="'.$req.'" AND conbtle="'.$con.'" AND sts_btle=8
                ORDER BY btle_id DESC LIMIT 1');
            
                $stmt->execute(array());
                $title_count = $stmt->rowCount();
        
                if($title_count > 0)
                {
                    while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
                $btle_id=$rows['btle_id'];
            ?>
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 16px;color: #555;"><?php echo $rows['boqtitle_name'];?></td>
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>          
            </tr>
            
            <?php 
            ######### COMPONENT #########
            $stmt2=$db->prepare('SELECT *FROM tbl_boq_compnt WHERE bcpnt_id 
            IN(select copnt_id from tbl_boqs where boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
            
            $stmt2->execute();
            while($getcomp = $stmt2->fetch()){
                $bcpnt_id = $getcomp['bcpnt_id'];
                $CompSubTot=0;
            ?>
            
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 13px;color: #555;"><?php echo $getcomp['boqcpnt_name'];?></td>
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>          
            </tr>
        
            <?php 
            ######### DIVISION #########
            $stmt3=$db->prepare('SELECT *FROM tbl_boq_div WHERE bdiv_id 
            IN(select div_id from tbl_boqs where copnt_id="'.$bcpnt_id.'" AND boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
            
            $stmt3->execute();
            while($getdv = $stmt3->fetch()){
                $bdiv = $getdv['bdiv_id'];
                $divSubTot=0;
            ?>
            
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 13px;color: #555;"><?php echo $getdv['boqdiv_name'];?></td>
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>          
            </tr>
        
            <?php 
            ########## ELEMENTS #################
            $eno=0;
            $elemQue=$db->prepare('SELECT *FROM tbl_elements WHERE elemid 
            IN(select elmnt from tbl_boqs where div_id="'.$bdiv.'" AND copnt_id="'.$bcpnt_id.'" AND boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
            $elemQue->execute();
            while($getelem = $elemQue->fetch()){
                $elemid = $getelem['elemid'];
                $eno++;
                
                $subTot=0;
            ?>
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 14px;"><?php echo "Element N<sup>o</sup>".$eno.": ".$getelem['elemName'];?></td>      
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>  
            </tr>
        
        
            <?php
            ###### CATEGORY #####
            $ctgQue=$db->prepare('SELECT *FROM tbl_ictgs WHERE ictg_id 
            IN(select bctg from tbl_boqs where elmnt="'.$elemid.'" AND div_id="'.$bdiv.'" AND copnt_id="'.$bcpnt_id.'" AND boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
            $ctgQue->execute();
            while($qq=$ctgQue->fetch()){
                $ictg_id=$qq['ictg_id'];
            ?>
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 11px;color: #555;font-style: italic;">
                    <?php echo $qq['ctgi_name'];?>
                </td>  
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>      
            </tr>
                    
            <tr>
                <td></td>        
                <td style="font-weight: bold;font-size: 11px;color: #000;font-style: italic;text-decoration: underline;">
                    <?php echo $qq['ctgdesc'];?>
                </td>  
                <td></td>  
                <td></td>  
                <td></td>  
                <td></td>      
            </tr>

            <?php 
            ####### ITEM ########
            
            $itemQue=$db->prepare('SELECT *FROM tbl_boqs b
            INNER JOIN tbl_items i ON i.item_id=b.itmid 
            INNER JOIN tbl_units u ON u.unit_id=i.iUnit
            WHERE bctg="'.$ictg_id.'" AND elmnt="'.$elemid.'" AND div_id="'.$bdiv.'" AND copnt_id="'.$bcpnt_id.'" AND boqtitle="'.$btle_id.'" 
            AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4');
            $itemQue->execute(array());
            while($bit=$itemQue->fetch()){
                $boqty=$bit['boqty'];
                $unit_prix=$bit['unit_prix'];
                $totMont=$boqty*$unit_prix;
                
                ##### SUBTOT ELEM #######
                $subTot=$subTot+$totMont;
        
                ##### SUBTOT ELEM #######
                $divSubTot=$divSubTot+$totMont;
        
                ##### SUBTOT ELEM #######
                $CompSubTot=$CompSubTot+$totMont;
        
                ###### TOTAL ########
                $Gtot= $Gtot+$totMont;
            
            ?>
            <tr>
            <td>
                <div class="custom-control custom-checkbox">
                    <input type="checkbox" name="delids[]" value="<?php echo $bit['boq_id'];?>" class="custom-control-input active" id="ckit_<?php echo $bit['boq_id'];?>">
                    <label class="custom-control-label" for="ckit_<?php echo $bit['boq_id'];?>"></label>
                </div>
            </td>
        
            <td><?php echo $bit['item_name'];?></td>
            <td class="text-right"><?php echo $bit['unit_name'];?></td>
            <td class="text-right"><?php echo $boqty;?></td>
            <td class="text-right"><?php echo number_format($unit_prix);?></td>
            <td class="text-right" style="font-weight: bold;"><?php echo number_format(round($totMont, 2));?></td>
        
            </tr>
            <?php 
                }
            //ITEM END
            ?>
        
            <?php 
                }
            //CATEGORY END
            ?>
            <tr style="background-color: #f3f3f3;">
                <th></th>
                <th style="font-weight: bold;"><?php echo $getelem['elemName'];?> SUB. TOTAL</th>
                <th></th>
                <th></th>
                <th></th>        
                <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($subTot, 2));?></th>
            </tr>
            <?php 
                }
            //ELEMENT END
            ?>
            
            <tr style="background-color: #dcdcdc;">
                <th></th>
                <th style="font-weight: bold;"><?php echo $getdv['boqdiv_name'];?> SUB. TOTAL</th>
                <th></th>
                <th></th>
                <th></th>        
                <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($divSubTot, 2));?></th>
            </tr>
        
            <?php 
                }
            //DIVISION END
            ?>
        
            <tr style="background-color: #b7b7b7;">
                <th></th>
                <th style="font-weight: bold;"><?php echo $getcomp['boqcpnt_name'];?> SUB. TOTAL</th>
                <th></th>
                <th></th>
                <th></th>        
                <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($CompSubTot, 2));?></th>
            </tr>
        
            <?php 
                }
            //COMPONENT END
            ?>
        
        
            <?php 	
                    }//END. TITLE
                    
                }//End Count.	
                }
                catch (PDOException $ex) {
                echo $ex->getMessage();
                }	            
            ?>
            <tr>
                <th style="font-weight: bold;" colspan="5">TOTAL</th>
                <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($Gtot, 2));?></th>
            </tr>
        
        </tbody>                      
    </table>

<div class="row">
    <div class="col-sm-4 ml-sm-auto">
        <table class="table table-clean">
            <tbody>            
                <?php 
                 $MainTot=0;
                 $itemQue=$db->prepare('SELECT *FROM tbl_boqs WHERE breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4');
                 $itemQue->execute(array());
                 while($bit=$itemQue->fetch()){
                    $boqty=$bit['boqty'];
                    $unit_prix=$bit['unit_prix'];
                    $totMont=$boqty*$unit_prix;
                    $MainTot=$MainTot+$totMont;
                 }
                ?>    
                <tr>
                    <td class="text-left keep-print-font">
                        <h4 class="m-0 fw-700 h2 keep-print-font color-primary-600">BOQ AMOUNT:</h4>
                    </td>
                    <td class="text-right keep-print-font">
                        <h4 class="m-0 fw-700 h2 keep-print-font text-danger"><?php echo number_format(round($MainTot, 2));?></h4>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

</div>