<?php
$ti=time();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=daily_summary_report".$ti.".xls");
header("Pragma: no-cache");
header("Expires: 0");
#echo "<pre>";print_r($item);echo "</pre>";
#echo "</br> : ".count($item);
$general = get_general_setting();
?>
<?php if ($type == 'Excel Summary Data') { ?>
<table class="table table-bordered table-striped filterTable" id="filterTable">
    <thead>
        <tr>
            <th width="5%" class="align-center" style="font-size:13px">S.No</th>
            <th width="14%" class="align-center" style="width:150px;font-size:13px">Receipt No</th>
            <th width="22%" class="align-center" style="width:200px;font-size:13px">Name</th>
            <th width="10%" class="align-center" style="font-size:13px">Qty</th>
            <th width="12%" class="align-center" style="font-size:13px">Unit Price S$</th>
			<?php if($general->gst > 0) { ?>
				<th width="13%" class="align-center">Before GST S$</th>
				<th width="13%" class="align-center">GST S$</th>
			<?php } ?>
            <th width="15%" class="align-center" style="width:250px;font-size:15px">Total S$ <?php echo ($general->gst > 0) ? '(Incl. of GST)' : '';?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $total_amt 			= 0;
        $before_gst_total 	= 0;
		$gst_total 			= 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);
              
                $gstval = 100 + $general->gst;
                if(!empty($Refund)) {
					$recentAmt = $row['amount'] - $Refund;
				}
				else {
				   $recentAmt = $row['amount'];
				}

                if(!empty($row['gst'])) {
					$AmtwithGst = (($recentAmt /107) * 100);
					$gst_amt 	= number_format($recentAmt - $AmtwithGst,2,'.','');
				} else {
					$AmtwithGst = 0;
					$gst_amt 	= 0;
				}
                $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'] : '';
                $emp_id = ''; // not necessary to send auto id               
                ?>
                <tr>
                    <td class="align-center"><?php echo ++$i; ?></td>
                    <td><?php echo $row['rec_no']; ?></td>
                    <td width="25%"><?php echo $row['name']; ?></td>
                    <td class="align-center"><?php echo $row['qty']; ?></td>
                    <td class="align-center" align="right">&nbsp;<?php echo is_numeric($row['price']) ? number_format($row['price'], 2) : $row['price']; ?></td>
					<?php if($general->gst > 0) { ?>
							<td class="align-right" align="right">&nbsp;
							<?php 
								if(!empty($recentAmt)) {										
									$before_gst 		= number_format($recentAmt - $gst_amt,2,'.','');
									$before_gst_total 	= $before_gst_total + $before_gst;
									echo $before_gst;
								}else{
									echo '0.00';
								}
								?>
							</td>
							<td class="align-right" align="right">&nbsp;
							<?php 
								if(!empty($row['gst'])) {									
									$gst_total 	= $gst_total + $gst_amt;
									echo $gst_amt;
								}else{															
									echo '0.00';
								}
								?>
							</td>
					<?php } ?>
                    <td class="align-right" align="right">&nbsp;<?php echo number_format($recentAmt, 2); ?></td>
                </tr>

            <?php endforeach; ?>
        <?php } else { ?>
            <tr>
                <td colspan="<?php echo ($general->gst > 0) ? 8 : 6;?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php } else if ($type == 'Excel Details Data') { ?>
<table class="table table-bordered table-striped filterTable" id="filterTable">
    <thead>
        <tr>
            <th width="5%" class="align-center">S.No</th>
            <th width="14%" class="align-center">Recpt No</th>
            <th width="23%" class="align-center">Cat Name</th>
            <th width="20%" class="align-center">Sub Cat 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_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);
//echo $Refund;
                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'];
                }

                $tooltip = ($recentAmt == 0) ? $row['del_reason'] : '';
                ?>
                <tr data-toggle="tooltip" data-placement="auto" title="<?php echo $tooltip; ?>">
                    <td class="align-center"><?php echo ++$i; ?></td>
                    <td><?php echo $row['rec_no']; ?></td>
                    <td width="25%"><?php echo $row['cat_name']; ?></td>
                    <td><?php echo $row['name']; ?></td>
                    <td><?php echo $payType; ?></td>
                    <td class="align-right" align="right">&nbsp;<?php echo number_format($recentAmt, 2); ?></td>
                </tr>
            <?php endforeach; ?>
        <?php } else { ?>
            <tr>
                <td colspan="7" style="color: #0B6CCB;"><center>No Records Found!</center></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php } ?>

<table>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>

</table>

<table class="table table-bordered table-striped">
	<thead>
		<tr>
			<th colspan="4" style="color: #0B6CCB;">&nbsp;</th>
			<th>&nbsp;</th>
			<?php if($before_gst_total > 0){ ?>
			<th class="align-center" align="center">Before GST</th>
			<th class="align-center" align="center">GST</th>
			<?php } ?>
			<th class="align-right" align="right">Amount</th>
		</tr>
	</thead>
    <tbody>
        <tr>
        	<td colspan="4" style="color: #0B6CCB;">&nbsp;</td>
            <td align="left">Total S$</td>
			<?php if($before_gst_total > 0){ ?>
			<td align="right" width="27%">&nbsp;<?php echo number_format($before_gst_total,2,'.','');?></td>
			<td align="right" width="29%">&nbsp;<?php echo number_format($gst_total,2,'.','');?></td>
			<?php } ?>
            <td align="right">&nbsp;<?php echo number_format($total_amt, 2, '.', ''); ?></td>
        </tr>
        <?php
        foreach ($mode as $key => $value) {
    
            if ($value->name != 'Direct Payment' && $value->name != 'Membership card' && $value->name != 'Credit Card'):
                ?>
                <?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="4" style="color: #0B6CCB;">&nbsp;</td>
                    <td align="left"><?php echo ucfirst($value->name); ?> S$</td>
                    <td align="right" <?php echo (($before_gst_total > 0)? 'colspan="3"' : ''); ?>>&nbsp;<?php echo (!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt, 2) : $paidAmount->amount ) : '0.00'; ?></td>
                </tr>
            <?php endif; ?>
        <?php } ?>
    </tbody>
</table>