<?php  use Cake\ORM\TableRegistry;
$user1 = $this->request->session()->read('Auth.User');
$user  = TableRegistry::get('Users');
$banks = TableRegistry::get('Banks');
$companies = TableRegistry::get('Companies');
$branches = TableRegistry::get('Branches');
$brokers = 	 TableRegistry::get('Brokers');
$scheme = TableRegistry::get('Mffunds');
$plans = TableRegistry::get('Plans');
?>
    <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 if(!empty($fds)): ?>
    <?php  if($user1['group_id'] == '3'){
        $branchesid1 = $branches->find()->where(['id' => $user1['branch_id']]);
        $databr = $branchesid1->toArray();
    }
    else{
        if($branch != '' && $branch != 'all' ){
            $branchesid = $branches->find()->where(['id' => $branch]);
            $databr = $branchesid->toArray();
        }else{
            $branchesAll = $branches->find()->where([]);
            $databr = $branchesAll->toArray();
        }
    } ?>

    <?php foreach ($databr as $branchsingle): ?>
    <table class="tbl-wrapper" cellpadding="0" cellspacing="0" >
        <thead>
        <tr>
            <th class="tableTd" scope="col" colspan="5"><b>
                    <div style="clear: both;">Product: Fixed Deposit</div>
            </th>
            <th class="tableTd" scope="col" colspan="3"><b>
                    <?php
                    if($datedform != '' && $datedto != '' ){
                        echo 'Date :  '.$dated2;
                        echo ' - '.$dated1;
                    }
                    else{
                        echo 'Date : all' ;
                    }
                    ?>
                </b>
            <th class="tableTd" scope="col" colspan="3">
                <b>
                    <?php
                /*if($user1['group_id'] == '3'){
                    $branchesid1 = $branches->find()->where(['id' => $user1['branch_id']]);
                    $databr = $branchesid1->toArray();
                    echo 'Branch Name :'. $databr[0]->branchname;;
                }
                else{
                    if($branch != '' && $branch != 'all' ){
                        $branchesid = $branches->find()->where(['id' => $branch]);
                        $databr = $branchesid->toArray();
                        echo 'Branch Name :  '. $databr[0]->branchname;
                    }else{
                        echo 'Branch Name : All';
                    }
                } */
                    echo 'Branch Name :'. $branchsingle->branchname;
                ?></b>
            </th>
        </tr>
        <tr>
            <th class="tableTd" scope="col"><?= __('Sr No') ?></th>
            <th class="tableTd" scope="col" style="width:12%;"><?= __('Name') ?></th>
            <th class="tableTd" scope="col"><?= __('Type of Deposit') ?></th>
            <th class="tableTd" scope="col" style="width:10%;"><?= __('Product') ?></th>
            <!--<th class="tableTd" scope="col"><?= __('Scheme') ?></th>-->
            <th class="tableTd" scope="col"><?= __('Period') ?></th>
            <th class="tableTd" scope="col"><?= __('Amount') ?></th>
            <!--<th class="tableTd" scope="col"><?= __('Branch') ?></th>-->
            <th class="tableTd" scope="col"><?= __('Cheque No') ?></th>
            <th class="tableTd" scope="col"><?= __('Folio/FDR No') ?></th>
            <th class="tableTd" scope="col"><?= __('Despatch Date') ?></th>
            <th class="tableTd" scope="col"><?= __('Bank Name') ?></th>
            <th class="tableTd" scope="col"><?= __('Sub Broker Name') ?></th>
            <!--<th class="tableTd" scope="col"><?= __('Company') ?></th> -->
        </tr>
        </thead>
        <tbody>
        <?php  $srlno = ($this->request->params['paging']['Fds']['page']-1) * $this->request->params['paging']['Fds']['perPage'];
        $i=1;$total=' ';
        foreach ($fds as $fd):
            if($fd->branch_id!=$branchsingle->id){
                continue;
            }
            $srlno++;
			
			$amount = $fd->amount;
			//As per the zoom discussion 01-12-2021
			if($fd->typeofdeposit=='both'){
				if(!empty($fd->fdramount)){
					$amount = $fd->amount + $fd->fdramount;
				}
			}
        ?>
            <tr>
                <td class="tableTdContent"><?=$srlno ?></td>
                <td class="tableTdContent"><?= h($fd->fapplicantname) ? $fd->fapplicantname : ''  ?></td>
                <td class="tableTdContent"><?= h($fd->typeofdeposit) ?></td>
                <td class="tableTdContent">
                    <?php
                    if($fd->productcompanyid > 0){
                        $companiesdata = $companies->find()->where(['id' => $fd->productcompanyid]);
                        $data2 = $companiesdata->toArray();
                        if($data2){
                            echo $companiesname = $data2[0]->company;
                        }
                    }
                    ?>
                </td>
                <!--   <td class="tableTdContent">
                        <?php
                if($fd->plan_id > 0){
                    $planid = $plans->find()->where(['id' => $fd->plan_id]);
                    $data2 = $planid->toArray();
                    if($data2){
                        echo $planname = $data2[0]->Planname;
                    }
                } ?>
                    </td>-->
                <td class="tableTdContent"><?= h($fd->tenure) ?></td>
				
				<td class="tableTdContent"><?= $amount ?></td>
               <!--  <td class="tableTdContent"><?= h($fd->amount) ?></td> -->
                <!--     <td class="tableTdContent">
                        <?php
                if($fd->branch_id > 0){
                    $branchdata = $branches->find()->where(['id' => $fd->branch_id]);
                    $data = $branchdata->toArray();
                    if($data){
                        echo $branchdata = $data[0]->branchname;
                    }
                } ?>
                    </td> -->
                <td class="tableTdContent"><?=  wordwrap(h($fd->chequeno),10,"<br>", true);  ?></td>
                <td class="tableTdContent"><?=  wordwrap(h($fd->fdrno),10,"<br>", true); ?></td>
                <td class="tableTdContent"><?= h($fd->despatchdate) ?></td>
                <td class="tableTdContent">
                    <?php
                    if($fd->typeofdeposit != 'renewal'){
                        if($fd->bank_id > 0){
                            $banksid = $banks->find()->where(['id' => $fd->bank_id]);
                            $data1 = $banksid->toArray();
                            echo  $banksName = $data1[0]->banks;
                        }
                    }
                    ?></td>
                <td class="tableTdContent"><?=  !empty($subbrokers[$fd->subbroker_id]) ? $subbrokers[$fd->subbroker_id] : '' ?></td>
               <!-- <td class="tableTdContent"><?=  !empty($subbrokers->$fd->subbroker_id) ? $subbrokers->$fd->subbroker_id : '' ?></td>
                
                	<td class="tableTdContent">
						<?php
                if($fd->company_id > 0){
                    $companiesdata = $companies->find()->where(['id' => $fd->company_id]);
                    $data = $companiesdata->toArray();
                    if($data){
                        echo $companiesname1 = $data[0]->company;
                    }
                }
                ?>
					</td>-->
            </tr>
            <?php
			$total += $amount;
            $i++;
        endforeach; ?>
        <tr class="total" style="margin-bottom: 15px">
            <td class="tableTdContent" colspan="5" style="text-align:right"><b>Sub Total</b></td>
            <td class="tableTdContent"><b><?=$total?></b></td>
            <td class="tableTdContent" colspan="5"></td>
        </tr>
        
        </tbody>
    </table>
    <?php endforeach; ?>

<?php endif; ?>