
<STYLE type="text/css">
    .tableTd {
        border-width: 0.5pt;
        border: solid;
    }
    .tableTdContent{
        border-width: 0.5pt;
        border: solid;
        padding: 1px 2px;
    }
    #titles{
        font-weight: bolder;
        background-color:#ccc;
    }

</STYLE>

<?php  use Cake\ORM\TableRegistry;
    $companies = TableRegistry::get('Companies');
    $branches = TableRegistry::get('Branches');
    $brokers = TableRegistry::get('Brokers');
    $users = TableRegistry::get('Users');
?>
<?php if(!empty($fds)) :?>
     <table class="tbl-wrapper" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th class="tableTd">Sr No</th>
                <th class="tableTd">Applicant Name</th>
                <th class="tableTd">Referred By</th>
                <th class="tableTd">Branch</th>
                <th class="tableTd">Deposit type</th>
                <th class="tableTd">period (months)</th>
                <th class="tableTd">Company</th>
                <th class="tableTd">Investment Amount</th>
                <th class="tableTd">Created</th>
                <th class="tableTd">Sub-Brokrage Paid</th>
                <th class="tableTd">Sub-Broker</th>
                <th class="tableTd">Percent</th>
                <th class="tableTd">Sub-Broker Amount</th>
                <th class="tableTd">TDS Percent</th>
                <th class="tableTd">Net Amount</th>
                <th class="tableTd">Bank Name</th>
                <th class="tableTd">Cheque No</th>
                <th class="tableTd">Paid Date</th>
            </tr>
        </thead>
         <tbody>
            <?php 
                $srlno = 0;
                $i=1;$total='';
                foreach ($fds as $fd): $srlno++;
            ?>
            <tr>
                <td class="tableTdContent"><?= $srlno ?></td>
                <td class="tableTdContent"><?php 
                        if($fd->minor == 1) {
                             echo  $fd->minorname.'&nbsp;&nbsp;U/G&nbsp;&nbsp;'.$fd->fapplicantname;
                          }else{
                              echo  $fd->fapplicantname;
                    } ?>
                </td>
                <td class="tableTdContent"><?php echo isset($subbrokers[$fd->subbroker_id]) ? $subbrokers[$fd->subbroker_id] : ''; ?></td>
                <td class="tableTdContent">  <?php if($fd->branch_id){
                        $branch = $branches->find()->where(['id' => $fd->branch_id]);
                        $branchdata = $branch->toArray();
                        $branchname = $branchdata[0]->branchname;
                    }else{
                        $branchname = '';
                    } ?>
                    <?= $branchname ?>
                </td>
                <td class="tableTdContent"><?php 
                    if($fd->typeofdeposit == 'onlinerenewal'){
                        echo 'online renewal';
                    }else if($fd->typeofdeposit == 'onlinefresh'){
                        echo 'online fresh';
                    }else{
                        echo $fd->typeofdeposit ;
                    } ?>
                </td>
                <td class="tableTdContent"><?php if($fd->tenure == 0){echo '';} else{echo $fd->tenure;} ?></td>
                <td class="tableTdContent"><?php if($fd->productcompanyid > 0) {
                        $productcompanyid = $companies->find()->where(['id' => $fd->productcompanyid]);
                        $data1 = $productcompanyid->toArray();
                        $productcompanyName = $data1[0]->company;
                    }else{
                        $productcompanyName = '';
                    }?>
                    <?=$productcompanyName?>
                </td>
                <td class="tableTdContent"><?php 
                    $amt=h($fd->amount);
                    $fdramt=h($fd->fdramount);
                    $totalamt=$amt+$fdramt;
                    echo $totalamt; ?>
                </td>
                
                
                <td class="tableTdContent"><?php $created = explode(" ", $fd->created);
                        echo $created[0];
                    ?>
                </td>
                <td class="tableTdContent"><?php if($fd->sub_brokerage_paid == 1){
                        $subbrokragestatus = 'Yes';
                        $statusClass = 'text-success';
                    } else{
                        $subbrokragestatus = 'No';
                        $statusClass = 'text-danger';
                    }?>
                    <span class="<?= $statusClass ?>"><?= $subbrokragestatus; ?></span>
                </td>

                <?php if (!empty($fd->fd_sub_brokerage_payments[0])) {
                    foreach ($fd->fd_sub_brokerage_payments as $fsp): ?>
                   <td class="tableTdContent"><?= $subbrokers[$fsp->sub_brokerage_payment->subbroker_id] ? $subbrokers[$fsp->sub_brokerage_payment->subbroker_id] : 0 ?></td>
                   <td class="tableTdContent"><?= number_format($fsp->percentage,2) ?>%</td>
                   <td class="tableTdContent"><?= number_format($fsp->sub_brokerage_amount,2) ?></td>
                   <td class="tableTdContent"><?= number_format($fsp->tds_amount,2) ?></td>
                   <td class="tableTdContent"><?= number_format($fsp->net_amount,2) ?></td>
                   <td class="tableTdContent"><?= $banks[$fsp->sub_brokerage_payment->bank_id] ? $banks[$fsp->sub_brokerage_payment->bank_id] : '' ?></td>
                   <td class="tableTdContent"><?= $fsp->sub_brokerage_payment->chequeno ?></td>
                   <td class="tableTdContent"><?php $paiddate = explode(" ", $fsp->sub_brokerage_payment->paid_date);
                        echo $paiddate[0];
                    ?>
                    </td>
                <?php endforeach; }else { ?>
                    <td colspan="8" class="text-center" class="tableTdContent">No Data</td>
                <?php } ?>
            </tr>
            <?php endforeach;?>
            
            <tr class="total">
                <td class="tableTdContent" colspan="7" style="text-align:right"><b>Overall Grand Total</b></td>
                <td class="tableTdContent"><b><?= $grandTotals['fd_amount']; ?></b></td> 
                <td class="tableTdContent" colspan="4"></td>
                <td class="tableTdContent"><b><?= number_format($grandTotals['sub_brokerage_amount']); ?></b></td>
                <td class="tableTdContent"><b><?= number_format($grandTotals['tds_amount']); ?></b></td>
                <td class="tableTdContent"><b><?= number_format($grandTotals['net_amount']); ?></b></td>
                <td class="tableTdContent" colspan="3"></td>
            </tr>
            
         </tbody>
    </table>

<?php else: ?>
    <p>No records found.</p>
<?php endif; ?>
