<?php

/* 
 * Author       : Jai K
 * Purpose      : View for Print Format 8 (Common purposes)
 * Created On   : 2016-09-22 18:14
 */

                $amount = is_array($amount) ? $amount[0] : $amount;
?>
            </table>
<?php
            $k = 0;
            $cols = count($data['fields']);
            if( $cols > 0 ) :
                ?>
            <table width="<?php echo WIDTH; ?>" <?php echo $data['fields']['attr']; ?> >
                    <?php
                        unset($data['fields']['attr']);
                        foreach( $data['fields'] as $label => $value ) :
                    ?>
                            <tr>
                                <th colspan="<?php echo $cols; ?>"><?php echo $label; ?></th>
                            </tr>
                            <tr height="5px"></tr>
                            <tr>
                                <th colspan="<?php echo $cols; ?>" style="border-bottom:2px dotted #000;"><?php echo $value; ?></th>
                            </tr>
                            <tr height="10px"></tr>
                    <?php 
                        endforeach; 
            endif;    
            ?>                                
                </table>
        </td>
    </tr>
</table>

<?php

if( !empty( $data['tables'] ) ) :
    $tables = !empty( $data['tables']['head'] ) ? array($data['tables']) : $data['tables'];
    foreach( $tables as $k => $table ) :
        $cols = count( $table['head'] );
    ?>
        <table width="<?php echo WIDTH; ?>" border="1" style="border-collapse:collapse; margin: 20px 0px;" cellpadding="5" <?php echo $table['attr']; ?> >
            <tr>
                   <th width="5%">S.No</th>
                   <?php foreach( $table['head'] as $h => $th ) : ?>
                       <th><?php echo $th; ?></th>
                   <?php endforeach; ?>
            </tr> 

            <?php 
                
                if( !empty( $table['body'] ) ) :
                    $cnt    = 0;
                    $x      = array();
                    
                    foreach( $table['body'] as $b => $row ) : 
                        $indx = 0;
                    ?>
                    <tr>
                        <td align="center"><?php echo ++$cnt; ?></td>
                        <?php foreach( $row as $t => $td ) : ?>
                            <td>
                                <?php 
                                    ++$indx;
                                    if( is_numeric( $td ) ) :
                                        $x[$indx] += $td;
                                    else :
                                        $x[$indx] = '';
                                    endif;
                                    
                                    echo $td; 
                                ?>
                            </td>
                        <?php endforeach;?>
                    </tr>
            <?php 
                endforeach;
                
                // Total
                if( !empty( $x ) ) :
                    ?>
                        <tr>
                            <td>&nbsp;</td>
                            <?php
                                foreach( $x as $i => $val ) :
                                   ?><td align="right" style="font-weight: bold;font-size:18px;"><?php echo !empty( $val ) ? ( substr_count($val, '.') == 1 ? number_format( $val, 2) : $val ) : '&nbsp;' ?></td><?php
                                endforeach;
                                
                                // Fill Remaining Empty cells
                                for( $i = count($x); $i < $cols; $i++ ) :
                                    ?>
                                        <td>&nbsp;</td>
                                    <?php
                                endfor;
                            ?>
                        </tr>
                    <?php
                endif;
            
                else :
                   ?>
                   <tr>
                        <td colspan="<?php echo  $cols + 1; ?>"><center>No Records Found!</center></td>
                   </tr> 
                   <?php
               endif;
           ?>
        </table>
        <?php
    endforeach;
endif; 

echo $data['html'];


