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

#echo "ONE";
#echo "<br>";
#echo "<pre>";print_r($dataType);echo "<br>";print_r($results);echo "</pre>";
#echo "</br> : ".count($item);
#exit;
?>
<?php $general_data = get_general_setting();?>
<?php if($dataType == 'XLS Summary Data') { ?>
<table width="100%" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="tdhgt">
    <thead>
        <tr>
            <th width="6%" align="center" style="padding:1.5%;">S.No</th>
            <th style="padding:1.5%;">Receipt No</th>
            <th style="padding:1.5%;">Name</th>
            <!--<th width="10%" style="padding:1%;">Paid Type</th>-->
            <th width="10%" style="padding:1%;">GST Type</th>
            <th style="padding:1.5%;">Quantity</th>
            <th width="8%" style="padding:1.5%;">Unit Price</th>
            <?php if($general_data->gst > 0) { ?>
                <th style="padding:1.5%; width: 11%;">Before GST</th>
                <th style="padding:1.5%; width: 11%;">GST Amount</th>
            <?php } ?>
            <th style="padding:1.5%; width: 11%;">Total</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $total_amt = $totalwith_gst = $ttAmount = $ttbg = 0; 
        $bg_std_rate = $gs_std_rate = $ag_std_rate = $ttgst = 0; 
        $bg_zero_rate = $gs_zero_rate = $ag_zero_rate = 0;
               
        $search_date = (!empty($search_date)) ? $search_date : date('d-m-Y');
        if(count($results) > 0) { ?>
            <?php $i = 0;
            foreach( $results as $key => $row) :
                //Refund Amount
                $Refund = $this->daily_summary_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date);
              
                if(!empty($Refund)) {
                    $recentAmt = $row['amount'] - $Refund;
                }
                else {
                   $recentAmt = $row['amount'];
                }
                
                $total_amt += $recentAmt;
                
                if(!empty($row['gst'])) {
                    #$AmtwithGst = $recentAmt * ($row['gst'] / 100);
                    $AmtwithGst = (($recentAmt /107) * 100);
                    $gst = $recentAmt - $AmtwithGst;
                } else {
                    $AmtwithGst = $recentAmt;
                    $gst = 0;
                }
                
                $before_gst = (!empty($recentAmt)) ? number_format($AmtwithGst,2,'.','') : '0.00';
                $gst_amount = (!empty($row['gst'])) ? number_format($gst,2,'.','') : '0.00';                                                
                
                if($row['is_gst'] == 1) {
                    #$totalwith_gst = $total_amt;
                    
                    $bg_std_rate += $before_gst;
                    $gs_std_rate += $gst_amount;
                    $ag_std_rate += ($before_gst + $gst_amount);
                } else {
                    $bg_zero_rate += $before_gst;
                    $gs_zero_rate = '0.00';
                    $ag_zero_rate += $before_gst;
                }
                
                $ttAmount = $ag_std_rate + $ag_zero_rate;
                $ttbg = $bg_std_rate + $bg_zero_rate;
                $ttgst = $gs_std_rate + $gs_zero_rate;
            ?>
            <?php $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                      $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                      $emp_id = ''; // not necessary to send auto id
                $paymentType = $this->daily_summary_model->get_payment_details($row['type_id'],$emp_id,$search_date,$mem_card_id);?>
                
                <?php if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                    $payType = '-';
                } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                    $payType = $paymentType[0]['payment'];
                } else { $payType = $paymentType[0]['payment']; }?>
                
                <tr>
                    <td align="center" style="padding:1%;"><?php echo ++$i;?></td>
                    <td style="padding:1%;"><?php echo $row['rec_no']; ?></td>
                    <td width="20%" style="padding:1%;"><?php echo $row['name']; ?></td>
                    <!--<td width="10%" style="padding:1%;"><?php #echo $payType; ?></td>-->
                    <td width="10%" align="center" style="padding:1%;"><?php echo ($general_data->gst > 0 && $row['is_gst'] == 1) ? 'Yes' : 'No';?></td>
                    <td align="center" style="padding:1%;"><?php echo $row['qty'];?></td>
                    <td align="center" style="padding:1%;"><?php echo $row['price'];?></td>
                    <?php if($general_data->gst > 0) { ?>
                        <td align="right" style="padding:1%;"><?php echo (!empty($AmtwithGst)) ? number_format($AmtwithGst,2,'.','') : '0.00';?></td>
                        <td align="right" style="padding:0.9%;"><?php echo (!empty($row['gst'])) ? number_format($gst,2,'.','') : '0.00';?></td>
                    <?php } ?>
                    <td align="right" style="padding:1%;"><?php echo number_format($recentAmt,2,'.','');?></td>
                </tr>
            
            <?php endforeach; ?>                                        
                                                               
        <?php } else {?>
            <tr>
                <td colspan="<?php echo ($general_data->gst > 0) ? 9 : 7;?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
            </tr>
        <?php } ?>
    </tbody>
</table>
<?php } else if($dataType == 'XLS Details Data') {?>
<table width="100%" border="1" cellpadding="3" cellspacing="0" class="filterTable" id="filterTable">
    <thead>
        <tr>
            <th width="5%" class="align-center">S.No</th>
            <th width="14%" class="align-center">Receipt No</th>
            <th width="23%" class="align-center">Category Name</th>
            <th width="20%" class="align-center">Sub Category Name</th>
            <th width="15%" class="align-center">Payment Type</th>
            <th width="14%" class="align-center">Total</th>
        </tr>
    </thead>
    <tbody>
        <?php $total_amt = 0; $search_datee = (!empty($search_date)) ? $search_date : date('d-m-Y');
        if(count($results) > 0) { ?>
            <?php $i = 0; 
            foreach( $results as $key => $row) :
                //Refund Amount
                $Refund = $this->daily_summary_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date);
              
                if(!empty($Refund)) {
                    $recentAmt = $row['amount'] - $Refund;
                }
                else {
                   $recentAmt = $row['amount'];
                }
                
                $total_amt += $recentAmt;
            ?>  
                <?php $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                      $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                $paymentType = $this->daily_summary_model->get_payment_details($row['type_id'],$auto_id,$search_date,$mem_card_id);?>
                
                <?php 
                if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                    $payType = '-';
                } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                    $payType = $paymentType[0]['payment'];
                } else { $payType = $paymentType[0]['payment']; }
                
                if(strpos($row['name'], ' - ') !== false){
                    $categoryData   = explode(" - ", $row['name']);
                    $category       = $categoryData[0];
                    $subcategory    = $categoryData[1];
                }else{
                    $category       = $row['cat_name'];
                    $subcategory    = $row['name'];
                }
                ?>
                
                <tr>
                    <td align="center" style="padding:1.5%;"><?php echo ++$i;?></td>
                    <td style="padding:1.5%;"><?php echo $row['rec_no']; ?></td>
                    <td width="25%" style="padding:1.5%;"><?php echo $category; ?></td>
                    <td style="padding:1.5%;"><?php echo $subcategory;?></td>
                    <td style="padding:1.5%;"><?php echo $payType;?></td>
                    <td align="right" style="padding:1.5%;"><?php echo number_format($recentAmt,2,'.','');?></td>
                </tr>
            
            <?php endforeach; ?>
        <?php } else {?>
            <tr>
                <td colspan="6" style="color: #0B6CCB;"><center>No Records Found!</center></td>
            </tr>
        <?php } ?>
    </tbody>
</table>
<?php } ?>

<br/>

<?php if($dataType == 'XLS Details Data') {?>
<table width="50%" border="1" cellpadding="5" cellspacing="0">
    <tbody>
        <tr>            
            <td colspan="5" align="right" style="padding:1%;">Total <?php echo ($general_data->gst > 0) ? '(with GST)' : '';?></td>
            <td style="padding:1%;" align="right"><?php echo number_format($total_amt,2,'.','');?></td>
        </tr>
        
        <?php foreach($mode as $key=>$value) { ?>
            <?php $paidAmount = $this->daily_summary_model->get_total_paid_amt_by_type($value->id,$search_date);?>
            <?php if($value->payment_mode == 'cash') { 
                    $refundAmt = $this->daily_summary_model->get_refund_amount($search_date);
                } else { 
                    $refundAmt = '0.00';
                }
            ?>
            <tr>               
                <td colspan="5" align="right" style="padding:1%;"><?php echo ucfirst($value->name);?></td>
                <td align="right" style="padding:1%;"><?php echo (!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt,2,'.','') :  $paidAmount->amount ) : '0.00';?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>
<?php } else {?>
<table width="50%" border="1" cellpadding="5" cellspacing="0" id="subhht">
    <tbody>
        <tr>
        	<td colspan="4" align="left"><b>Zero Rated Supplies Percentage(%): </b>
				<?php 
                    if($general_data->gst > 0) {
                        $zrsp = ($bg_zero_rate/$bg_std_rate) * 100;
                    }else{
                        $zrsp = "0.00";
                    }
                    
                    echo number_format($zrsp,2,'.','');
                ?>
            </td>
            <td colspan="2" align="right" style="padding:1%;">Total Value of the Standard Rate Supplies</td>
            <?php if($general_data->gst > 0) { ?>
                <td style="padding:1%; width: 20%;" class="Before_GST" align="right"><?php echo number_format($bg_std_rate,2,'.','');?></td>
                <td style="padding:1%; width: 20%;" class="GST_Amount" align="right"><?php echo number_format($gs_std_rate,2,'.','');?></td>
            <?php } ?>
            <td style="padding:1%; width: 15%;" class="Total" align="right" ><?php echo number_format($ag_std_rate,2,'.','');?></td>
        </tr>
        <tr>            
            <td colspan="6" align="right" style="padding:1%;">Total Value of the Zero Rated Supplies</td>
            <?php if($general_data->gst > 0) { ?>
                <td style="padding:1%; width: 20%;" class="Before_GST" align="right"><?php echo number_format($bg_zero_rate,2,'.','');?></td>
                <td style="padding:1%; width: 20%;" class="GST_Amount" align="right"><?php echo $gs_zero_rate;?></td>
            <?php } ?>
            <td style="padding:1%; width: 15%;" class="Total" align="right"><?php echo number_format($ag_zero_rate,2,'.','');?></td>
        </tr>
        <tr>            
            <td colspan="6" align="right" style="padding:1%;">Total S$</td>
            <?php if($general_data->gst > 0) { ?>
               <td style="padding:1%; width: 20%;" class="Before_GST" align="right"><?php echo number_format($ttbg,2,'.','');?></td>
               <td style="padding:1%; width: 20%;" class="GST_Amount" align="right"><?php echo number_format($ttgst,2,'.','');?></td>
            <?php } ?>
            <td style="padding:1%; width: 15%;" class="Total" align="right"><?php echo number_format($ttAmount,2,'.','');?></td>
        </tr>
        <?php foreach($mode as $key=>$value) { ?>
        <?php $paidAmount = $this->daily_summary_model->get_total_paid_amt_by_type($value->id,$search_date);?>
        <?php 
            $cashbgst = (($paidAmount->amount / 107) * 100);
            $cashgst = $paidAmount->amount - $cashbgst;
       
           if($value->payment_mode == 'cash') { 
                $refundAmt = $this->daily_summary_model->get_refund_amount($search_date);
            } else { 
                $refundAmt = '0.00';
            }
        ?>
        <tr>            
            <td colspan="6" style="padding:1%;" align="right"><?php echo ucfirst($value->name);?> S$</td>
            <?php if($general_data->gst > 0) { ?>
            <td style="padding:1%; width: 20%;" class="Before_GST" align="right"><?php // echo number_format($cashbgst,2,'.','');?></td>
            <td style="padding:1%; width: 20%;" class="GST_Amount" align="right"><?php // echo number_format($cashgst,2,'.','');?></td>
        <?php } ?>
            <td style="padding:1%; width: 15%;" class="Total" align="right"><?php echo (!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt,2,'.','') :  $paidAmount->amount ) : '0.00';?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php } ?>