<?php

session_start();
include "database.php";

			$from = $_GET["from"];
      $to = $_GET["to"];
			$order_status = $_GET["order_status"];
      $contractor_id = $_SESSION["merchant_id"];

							$data_table = '';
							$data_table = '
								<table border="1">
									<tr>
										<td style="font-weight:bold;">Date</td>
										<td style="font-weight:bold;">Order ID</td>
										<td style="font-weight:bold;">Order Status</td>
										<td style="font-weight:bold;">Product Name</td>
										<td style="font-weight:bold;">Quantity</td>
										<td style="font-weight:bold;">Price Per Item</td>
										<td style="font-weight:bold;">Payment Via</td>
										<td style="font-weight:bold;">Service Charge</td>
										<td style="font-weight:bold;">Sub Total </td>
										<td style="font-weight:bold;">Voucher Code</td>
										<td style="font-weight:bold;">Discount</td>
										<td style="font-weight:bold;">Grand Total(All item in same order ID)</td>
										<td style="font-weight:bold;">Customer Name</td>
										<td style="font-weight:bold;">Customer Address</td>
										<td style="font-weight:bold;">Customer Contact Number</td>


									</tr>
							';

			if($order_status == 0 ){


				$sql = "SELECT * FROM member_order
								LEFT JOIN member_order_detail ON member_order_detail.order_id = member_order.order_id
								LEFT JOIN member ON member.member_id = member_order.order_member_id
								LEFT JOIN product ON product.product_id = member_order_detail.order_product_id
								LEFT JOIN address ON address.address_id = member_order.order_address_id
								WHERE member_order.order_merchant_id LIKE '%".$contractor_id."%'
								AND product.item_category = 1
								AND date(member_order.order_datetime) BETWEEN '".$from."' AND '".$to."'
								ORDER BY member_order.order_id DESC";
				$query = mysqli_query($conn,$sql);
				$count = mysqli_num_rows($query);
				// if(mysqli_num_rows($query) > 0){
				while($row = $query->fetch_assoc()) {
					if($row["order_status"]== 1){
						$status = "Success";
					}else if($row["order_status"]== 2){
						$status = "Fail";
					}

					if($row["order_payment_method"]== 1){
						$method = "E-Credit";
					}else if($row["order_payment_method"]== 2){
						$method = "FPX Paynet";
					}else if($row["order_payment_method"]== 3){
						$method = "Redeem";
					}else if($row["order_payment_method"]== 4){
						$method = "JomPAY";
					}
					$check='';
					$space = ', ';

					if($row["order_address_id"]=="Self Pick Up"){
						$check = "Self Pick Up";
					}else{
						$check = $row["address_line1"].$space.$row["address_postcode"].$space.$row["address_city"].$space.$row["address_state"];
					}

					if($row["order_service_charge"] >  0){
						$servicecharge = $row["order_service_charge"];

					}else{
						$servicecharge = "0.00";

					}

					if($row["order_voucher"]!= NULL){
					$voucher =$row["order_voucher"];
					$discount =$row["order_total_discount"];

					}else{
						$voucher = "-";
						$discount = "-";
					}
					$data_table .= '
					<tr>
					<td>'.date("Y-m-d", strtotime($row["order_datetime"])).'</td>
					<td>'.$row["order_id"].'</td>
					<td>'.$status.'</td>
					<td>'.$row["product_name"].'</td>
					<td>'.$row["order_quantity"].'</td>
					<td>'.$row["product_price"].'</td>
					<td>'.$method.'</td>
					<td>'.$servicecharge.'</td>
					<td>'.$voucher.'</td>
					<td>'.$discount.'</td>
					<td>'.$row["order_grand_total"].'</td>
					<td>'.$row["member_name"].'</td>
					<td>'.$check.'</td>
					<td>'.$row["member_phone_number"].'</td>

					</tr>
					';
				}
				$data_table .= '</table>';
				$filename = "All order " . $from . " - ". $to . ".xls";

			}else{
				$sql = "SELECT *, IF(member_order_detail.order_d2d_service != 1,1,0) AS countD2d,
								IF( member_order_detail.order_d2d_service != 1,area.area_price * member_order_detail.order_quantity, 0 ) AS d2dPrice,
								IF( member_order_detail.order_baja_service != 0,member_order_detail.order_baja_service * member_order_detail.order_quantity, 0 ) AS bajaPrice,
								member_order.order_status AS status
								FROM member_order
								LEFT JOIN member_order_detail ON member_order_detail.order_id = member_order.order_id
								LEFT JOIN member ON member.member_id = member_order.order_member_id
								LEFT JOIN product ON product.product_id = member_order_detail.order_product_id
								LEFT JOIN area ON area.area_id = member_order_detail.order_d2d_service
								LEFT JOIN address ON address.address_id = member_order.order_address_id
								WHERE member_order.order_merchant_id LIKE '%".$contractor_id."%'
								AND product.item_category = 1
								AND date(member_order.order_datetime) BETWEEN '".$from."' AND '".$to."'
								AND member_order.order_status = '".$order_status."'
								ORDER BY member_order.order_id DESC";
				$query = mysqli_query($conn,$sql);
				$count = mysqli_num_rows($query);
				// if(mysqli_num_rows($query) > 0){
				while($row = $query->fetch_assoc()) {
					if($row["status"]== 1){
						$status = "Success";
					}else if($row["status"]== 2){
						$status = "Fail";
					}

					if($row["order_payment_method"]== 1){
						$method = "E-Credit";
					}else if($row["order_payment_method"]== 2){
						$method = "FPX Paynet";
					}else if($row["order_payment_method"]== 3){
						$method = "Redeem";
					}else if($row["order_payment_method"]== 4){
						$method = "JomPAY";
					}

					$check='';
					$space = ', ';

					if($row["countD2d"] == 1){
						$check = $row["address_line1"].$space.$row["address_postcode"].$space.$row["address_city"].$space.$row["address_state"];
					}else{
						$check = "Self Pick Up";
					}
          $servicecharge = $row["d2dPrice"] + $row["bajaPrice"];
					if($servicecharge >  0){
						$servicechargeRM = $servicecharge;
					}else{
						$servicechargeRM = "0.00";
					}

					if($row["order_voucher"]!= NULL){
					$voucher =$row["order_voucher"];
					$discount =$row["order_total_discount"];

					}else{
						$voucher = "-";
						$discount = "0.00";
					}
					$total = ($row["order_sub_total"] + $servicecharge) - $discount;
					$subtotal = ($row["product_price"] *$row["order_quantity"]) + $servicecharge;

					$data_table .= '
					<tr>
					<td>'.date("Y-m-d", strtotime($row["order_datetime"])).'</td>
					<td>'.$row["order_id"].'</td>
					<td>'.$status.'</td>
					<td>'.$row["product_name"].'</td>
					<td>'.$row["order_quantity"].'</td>
					<td>'.$row["product_price"].'</td>
					<td>'.$method.'</td>
					<td>'.$servicechargeRM.'</td>
					<td>'.number_format($subtotal,2,'.','').'</td>
					<td>'.$voucher.'</td>
					<td>'.$discount.'</td>
					<td>'.number_format($total,2,'.','').'</td>
					<td>'.$row["member_name"].'</td>
					<td>'.$check.'</td>
					<td>'.$row["member_phone_number"].'</td>

					</tr>
					';
				}
				$data_table .= '</table>';
				$filename = "All order " . $from . " - ". $to . ".xls";

			}

			header("Content-Disposition: attachment; filename=\"$filename\"");
			header("Content-Type: application/vnd.ms-excel");
			header("Pragma: no-cache");
			header("Expires: 0");
			echo $data_table;

?>
