<?php
$ti=time();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=weekly_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;
         $beforegstamt=0;
         $totgstamount=0;
         $start_date = (!empty($start_date)) ? $start_date : date('d-m-Y');
		 $add_days = 6;
		 $d = date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
		 $end_date = (!empty($end_date)) ? $end_date : $d;
        if (count($results) > 0) {
            ?>
            <?php
            $i = 0;
            foreach ($results as $key => $row) :
                //Refund Amount
                $Refund = $this->weekly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_date);
              
               $gstval = 100 + $general->gst;
                if (!empty($Refund)) {
                    $AmtwithGst1 = (100 / $gstval) * $recentAmt;
                    $recentAmt = $row['amount'] - $Refund;
                } else {
                    $recentAmt = $row['amount'];
                    $AmtwithGst1 = $recentAmt;
                }



                if (!empty($row['gst'])) {
                    $AmtwithGst1 = (100 / $gstval) * $recentAmt;
                    $AmtwithGst = $recentAmt - $AmtwithGst1;
//                                                    $AmtwithGst = $recentAmt * ($row['gst'] / 100);
                    $amountval = $recentAmt - $AmtwithGst;
                } else {
                    $AmtwithGst = 0;
                    $amountval = $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'] : '';
                $emp_id = ''; // not necessary to send auto id
                //$paymentType = $this->weekly_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 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"><?php echo number_format($row['price'], 2); ?></td>
					<?php if($general->gst > 0) { ?>
							<td class="align-right"><?php echo (!empty($recentAmt)) ? number_format($recentAmt - $AmtwithGst,2,'.','') : '0.00';?></td>
							<td class="align-right"><?php echo (!empty($row['gst'])) ? number_format($AmtwithGst,2,'.','') : '0.00';?></td>
					<?php } ?>
                    <td class="align-right"><?php echo number_format($amountval + $AmtwithGst, 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->weekly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_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->weekly_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"><?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 } ?>

<table>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>

</table>

<table class="table table-bordered table-striped">
    <tbody>
        <tr>
        	<td colspan="<?php echo ($general->gst > 0) ? 6 : 4;?>" style="color: #0B6CCB;">&nbsp;</td>
            <td align="left">Total S$</td>
            <td><?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->weekly_model->get_total_paid_amt_by_type($value->id, $start_date,$end_date); ?>
                <?php
                if ($value->payment_mode == 'cash') {
                    $refundAmt = $this->weekly_model->get_refund_amount($search_date);
                } else {
                    $refundAmt = '0.00';
                }
                ?>
                <tr>
                	<td colspan="<?php echo ($general->gst > 0) ? 6 : 4;?>" style="color: #0B6CCB;">&nbsp;</td>
                    <td align="left"><?php echo ucfirst($value->name); ?> S$</td>
                    <td><?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>