<?php
$ti=time();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=donation_report".$ti.".xls");
header("Pragma: no-cache");
header("Expires: 0");

#echo "<pre>";print_r($filters);echo "<br>";
#echo "<pre>";print_r($data_type);echo "<br>";print_r($results);echo "</pre>";
#echo "</br> : ".count($item);
#echo "HERE"

$field = count($filters);
?> 
                           
<?php if($data_type == 'XLS Summary Data') { ?>
    <table width="1000" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="filterTable">
        <thead>
            <tr>
                <th style="padding:1%;">S.No</th>
                <?php
                foreach($filters as $value){                    
                    echo '<th style="padding:1%;">'.$value.'</th>';                  
                }
                ?>                
            </tr>
        </thead>
        <?php $total_amt = 0; $paid_amt = 0; if(count($results) > 0) {  ?>
        <tbody>
            
            <?php foreach($results as $key=>$value) {
                    $total_amt += $value->total_amount;
                    $paid_amt += $value->paid_amount;
                    
                ?>
                <tr>
                    <td align="center" style="padding:1%;"><?php echo ++ $key;?></td>
                    <?php if (in_array("Receipt No", $filters)) {?><td style="padding:1%;"><?php echo (($general->prefix == 1) ? $value->prefix : '') . $value->receipt_no;?></td><?php } ?>
                    <?php if (in_array("Type", $filters)) {?><td style="padding:1%;"><?php echo text($value->service_name);?></td><?php } ?>
                    <?php if (in_array("Booked Date", $filters)) {?><td style="padding:1%;"><?php echo text(date('d-m-Y',strtotime($value->created_on)));?></td><?php } ?>
                    <?php if (in_array("Name", $filters)) {?><td style="padding:1%;"><?php echo text($value->name);?></td><?php } ?>
                    <?php if (in_array("Tamil Name", $filters)) {?><td style="padding:1%;"><?php echo text($value->tamil_name);?></td><?php } ?>
                    <?php if (in_array("Contact No", $filters)) {?><td style="padding:1%;"><?php echo text($value->contact_no);?></td><?php } ?>
                    <?php if (in_array("Email", $filters)) {?><td style="padding:1%;"><?php echo text($value->email);?></td><?php } ?>
                    <?php if (in_array("Address", $filters)) {?><td style="padding:1%;"><?php echo text(nl2br($value->address));?></td><?php } ?>
                    <?php if (in_array("Desc", $filters)) {?><td style="padding:1%;"><?php echo text(nl2br($value->desc));?></td><?php } ?>
                    <?php if (in_array("Amount Type", $filters)) {?><td style="padding:1%;"><?php if($value->amount_type == 1) { echo 'Amount'; } elseif($value->amount_type == 2) { echo 'Item'; } elseif($value->amount_type == 3) { echo 'Ongoing';}?></td><?php } ?>
                    <?php if (in_array("Amount ", $filters)) {?><td style="padding:1%; "><?php echo number_format($value->total_amount,2,'.','');?></td><?php } ?>
                    <?php if (in_array("Paid Amount ", $filters)) {?><td style="padding:1%;"><?php echo number_format($value->paid_amount,2,'.','');?></td><?php } ?>
                </tr>
            <?php } ?>
        </tbody>
        
        <?php } else {?>
            <tbody>
                <tr>
                    <td colspan="<?php echo $field+1; ?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
                </tr>
            </tbody>
        <?php } ?>
    </table>
<?php } elseif($data_type == 'XLS Pledge Data') { ?>
    <table width="1000" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="filterTable">
        <thead>
            <tr>
                <!--<th width="5%" class="align-center" style="display:none;">&nbsp;</th>-->
                <th style="padding:1%;">S.No</th>
                <?php
                foreach($filters as $value){
                    if($value == 'Pledge Amount' || $value == 'Paid Amount' || $value == 'Balance Amount'){
                        echo '<th style="padding:1%;">'.$value;
                        if(!empty($pledge) && $pledge == 1 && $pledge_type == 2){ echo ' (grams)'; }else{ echo ""; }
                        echo '</th>';
                    }else{
                        echo '<th style="padding:1%;">'.$value.'</th>';
                    }
                }
                ?>                
            </tr>
        </thead>
        <tbody>
            <?php if(count($results) > 0) { ?>
                <?php foreach($results as $key=>$value) { ?>
                   <?php $donation_trans = $this->donation_report_model->get_donation_pledge_transaction($value->service_id,$value->name,$value->contact_no,$value->amount_type,$start_date,$end_date,$value->id);?>
                    <tr>  
                        <!--<th width="5%" class="align-center" style="display:none;">&nbsp;</th>-->
                        <td align="center" style="padding:1%;"><?php echo ++$key;?></td>
                        <?php if (in_array("Date", $filters)) {?><td style="padding:1%;"><?php echo text(date('d-m-Y',strtotime($value->created_on))); ?></td><?php } ?>
                        <?php if (in_array("Type", $filters)) {?><td style="padding:1%;"><?php echo text($value->service_name);?></td><?php } ?>
                        <?php if (in_array("Devotee Name", $filters)) {?><td style="padding:1%;"><?php echo text($value->name);?></td><?php } ?>
                        <?php if (in_array("Contact No", $filters)) {?><td style="padding:1%;"><?php echo text($value->contact_no);?></td><?php } ?>
                        <?php if (in_array("Pledge Amount ", $filters)) {?><td style="padding:1%;" align="right"><?php echo text($value->pledge_amount);?></td><?php } ?>
                        <?php if (in_array("Paid Amount ", $filters)) {?><td style="padding:1%;" align="right"><?php echo number_format($value->amount,2,'.','');?></td><?php } ?>
                        <?php if (in_array("Balance Amount ", $filters)) {?><td style="padding:1%;" align="right"><?php echo number_format(($value->pledge_amount - $value->amount),2,'.','');?></td><?php } ?>
                    </tr>
                    
                    <?php if(count($donation_trans) > 0) { ?>
                        <tr class="remove_container not_get_columns" >
                            <td <?php if($field >=6){?>colspan="2"<?php } ?>>&nbsp;</td>
                            <td colspan="<?php if($field >=6){ echo "4"; }else{ echo $field-1; }?>" class="expand_container">                                
                                <table width="970" border="1" cellpadding="3" cellspacing="0" class="filterTable tableFilter2">
                                    <thead>
                                        <tr>
                                            <th width="10%" align="center" style="padding:1%;">S.No</th>
                                            <th align="center" style="padding:0.8%;">Receipt No</th>
                                            <th align="center" style="padding:0.8%;">Date</th>
                                            <th align="center" style="padding:0.8%;">Amount<?php echo ((!empty($pledge)) && $pledge == 1 && $pledge_type == 2) ? ' (grams)' : '';?> </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach($donation_trans as $k=>$row) { ?>
                                            <tr class="subTable">
                                                <td style="padding:0.8%;"><?php echo ++$k;?></td>
                                                <td style="padding:0.8%;"><?php echo text($row->receipt_no);?></td>
                                                <td style="padding:0.8%;"><?php echo text(date('d-m-Y',strtotime($row->pledge_date)));?></td>
                                                <td align="right" style="padding:0.8%;"><?php echo number_format($row->amount,2,'.','');?></td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>                                    
                            </td>
                            <td <?php if($field == 7){?>colspan="2"<?php } ?>>&nbsp;</td>
                        </tr>
                    <?php } ?>
                 <?php } ?>  
                
            <?php } else {?>
                <tr>
                    <td colspan="<?php echo $field+1; ?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
<?php } ?>                        