<?php session_start();
include('../accesscheck2.php');
require_once('../Connections/courier.php'); 
require_once '../functions/functions.php';
mysql_select_db($database_courier, $courier);

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


$start_date = $_GET['start_date'];
$end_date = $_GET['end_date'];
$payment_methods = $_GET['payment_method'];
$payment_statuss = $_GET['payment_status'];
list($report_type, $report_type_txt) = explode("@@@", $_GET['report_type']);

if (count($payment_statuss) > 0) {
    foreach ($payment_statuss as $payment_status) {
        $payment_sts_qry .= "a.ins_company='$payment_status' OR ";
    }
	$payment_sts_qry = substr($payment_sts_qry, 0, -4);
	$payment_sts_qry = "AND (".$payment_sts_qry.")";
}


$date_range = "AND (a.date_of_consultation>='$start_date' AND a.date_of_consultation<='$end_date')";
$query_rsodest = "select a.process_claim_no, a.ins_company,  DATE_FORMAT(a.date_of_consultation, '%e %b %Y') as datenew, b.*,SUM(a.unit_price*a.qty) AS total_amount from claims_details a LEFT JOIN patient_information b on a.patient_no=b.card_number WHERE (a.mode_of_payment='$payment_methods') $date_range $payment_sts_qry GROUP BY a.process_claim_no ORDER BY a.date_of_consultation DESC";
$rsodest = mysql_query($query_rsodest);
$totalRows_rsodest = mysql_num_rows($rsodest);?>

<html lang="en">
    <!--<![endif]-->
    <!-- BEGIN HEAD -->

    <head>
        <meta charset="utf-8" />
        <title> Optical Management System </title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta content="width=device-width, initial-scale=1" name="viewport" />
        <meta content="" name="description" />
        <meta content="" name="author" />
        <!-- BEGIN GLOBAL MANDATORY STYLES -->
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" />
        <!--<link href="../assets/global/css/font.css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" />-->
        <link href="../assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/global/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/global/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css" />
        <link href="../assets/global/plugins/bootstrap-switch/css/bootstrap-switch.min.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="all">
    body{
        margin: 0;
    }
    .text-danger strong {
    		color: #9f181c;
		}
    .text-danger {
    		color: #9f181c;
		}
		.receipt-main {
			background: #ffffff none repeat scroll 0 0;
			/*border-bottom: 12px solid #333333;*/
			border-top: 12px solid #9f181c;
			/*margin-top: 50px;*/
			margin-bottom: 50px;
			padding: 40px 30px !important;
			position: relative;
			box-shadow: 0 1px 21px #acacac;
			color: #333333;
			font-family: open sans;
		}
		.receipt-main p {
			color: #333333;
			font-family: open sans;
			line-height: 1.42857;
		}
		.receipt-footer h1 {
			font-size: 15px;
			font-weight: 400 !important;
			margin: 0 !important;
		}
		.receipt-main::after {
			background: #414143 none repeat scroll 0 0;
			content: "";
			height: 5px;
			left: 0;
			position: absolute;
			right: 0;
			top: -13px;
		}
		.receipt-main thead {
			background: #414143 none repeat scroll 0 0;
		}
		.receipt-main thead th {
			color:#fff;
		}
		.receipt-main .amount {
                        text-align: right
		}
		.receipt-right h5 {
			font-size: 16px;
			font-weight: bold;
			margin: 0 0 7px 0;
		}
		.receipt-right p {
			font-size: 12px;
			margin: 0px;
		}
		.receipt-right p i {
			text-align: center;
			width: 18px;
		}
		.receipt-main td {
			padding: 9px 20px !important;
		}
		.receipt-main th {
			padding: 12px 15px !important;
		}
		.receipt-main td {
			font-size: 13px;
			font-weight: initial !important;
		}
		.receipt-main td p:last-child {
			margin: 0;
			padding: 0;
		}	
		.receipt-main td h2 {
			font-size: 20px;
			font-weight: 900;
			margin: 0;
			text-transform: uppercase;
		}
		.receipt-header-mid .receipt-left h1 {
			font-weight: 100;
			margin: 34px 0 0;
			text-align: right;
			text-transform: uppercase;
		}
		.receipt-header-mid {
			margin: 24px 0;
			overflow: hidden;
		}
		
		#container {
			background-color: #dcdcdc;
		}
</style>

</head>

<body>

<div class="container-fluid">
	<div class="row">
        <div class="receipt-main col-xs-12 col-sm-12 col-md-12">
            <div class="row">
    			<div class="receipt-header">
					<div class="col-xs-6 col-sm-6 col-md-6">
						<div class="receipt-left">
							<img class="img-responsive" src="../images/app_pics/rns_logo.jpeg" style="width: 71px;">
						</div>
					</div>
					<div class="col-xs-6 col-sm-6 col-md-6 text-right">
						<div class="receipt-right">
							<h5>ROBERT AND SONS LTD.</h5>
							<p>+233034789/09678909 <i class="fa fa-phone"></i></p>
							<p>INFO@ROBERTANDSON.COM <i class="fa fa-envelope-o"></i></p>
							<p>ADABRAKA BRANCH <i class="fa fa-location-arrow"></i></p>
						</div>
					</div>
				</div>
            </div>
			
			<div class="row">
				<div class="receipt-header receipt-header-mid">
					<div class="col-xs-8 col-sm-8 col-md-8 text-left">
						<div class="receipt-right">
                                                    <h3> <?php echo strtoupper($report_type_txt); ?> </h3>
                                                        <h4 class="text-danger"> DATE RANGE :  <?php echo date('jS M, Y',  strtotime($start_date));?> - <?php echo date('jS M, Y',  strtotime($end_date)); ?> </h4>
						</div>
					</div>
					<div class="col-xs-4 col-sm-4 col-md-4">
						<div class="receipt-left">
							<h1>Report</h1>
						</div>
					</div>
				</div>
            </div>

<table class="table table-striped table-bordered table-hover dataTable no-footer" id="datatable_products" aria-describedby="datatable_products_info" role="grid">
                                    <thead>
                                        <tr>
                                            <th>SN</th>
                                            <th>DATE TIME</th>
                                            <th>VISIT NO.</th>
                                            <?php if($payment_methods == 'insurance' || $payment_methods == 'company'){?>
                                            <th>INSURANCE COMPANY</th>
                                            <?php } ?>
                                            <th>PATIENT NAME</th>
                                            <th>PATIENT NO.</th>
                                            <?php if($payment_methods == 'insurance' || $payment_methods == 'company'){?>
                                            <th>MEMBERSHIP NO.</th>
                                            <?php } ?>
                                            <th>STATUS</th>
                                            <th>PAYMENT MODE</th>
                                            <th>TOTAL AMOUNT</th>
                                        </tr>
                                    </thead>
                                    <tbody>

                              <?php if($totalRows_rsodest>0){ 
								  $i=1;
                                           while ($row_rsodest = mysql_fetch_assoc($rsodest)){  ?>
            <tr>
                  <td height="23" valign="top" class="count"><span><?php echo $i; ?></span></td>
                  <td valign="top" style="cursor: pointer;"><?php echo strtoupper($row_rsodest['datenew']); ?></td>
                  <td valign="top" style="cursor: pointer;"><?php echo strtoupper($row_rsodest['process_claim_no']); ?></td>
                  <?php if($payment_methods == 'insurance' || $payment_methods == 'company'){?>
                  <td valign="top" style="cursor: pointer;"><?php echo strtoupper(getinsurance($row_rsodest['ins_company'])); ?></td>
                  <?php } ?>
                  <td valign="top" style="cursor: pointer;"><?php if(!empty($row_rsodest['card_number'])){ echo strtoupper($row_rsodest['surname']." ".$row_rsodest['othernames']);}else{ echo strtoupper($row_rsodest['surname1']." ".$row_rsodest['othernames1']);} ?></td>
                  <td valign="top" style="cursor: pointer;"><?php if(!empty($row_rsodest['card_number'])){ echo strtoupper($row_rsodest['card_number']);}else{ echo strtoupper($row_rsodest['card_number1']);} ?></td>
                  <?php if($payment_methods == 'insurance' || $payment_methods == 'company'){?>
                  <td valign="top" style="cursor: pointer;"><?php echo $row_rsodest['insurance_no1'];?></td>
                  <?php } ?>
                  <td valign="top" style="cursor: pointer;">
                    <?php if($row_rsodest['patient_status']=='In Use'){$statuscolor='green';}
				  if($row_rsodest['patient_status']=='Done'){$statuscolor='red';}
				  echo "<span style='color:$statuscolor; font-weight:bold;'>".strtoupper($row_rsodest['patient_status'])."</span>";?>
                    </td>
                  <td valign="top" style="cursor: pointer;"><?php echo strtoupper($payment_methods); ?></td>
                  <td valign="top" style="cursor: pointer;"><?php echo number_format($row_rsodest['total_amount'],2); $total_amount += $row_rsodest['total_amount']; ?></td>
                </tr>

<?php if($report_type == "gdsr"){
	
	?>
                <tr>
                    <td colspan="<?php if($payment_methods == 'insurance' || $payment_methods == 'company'){10;}else{ echo 8;} ?>">
                        <div>
								<table class="table">
								<tr>
									<th>Item Name</th>
									<th>Unit Price</th>
									<th>Quantity</th>
									<th>Payment Status</th>
									</tr>
									<tbody>
										<?php
											$rs = mysql_query("SELECT * FROM claims_details WHERE process_claim_no='".$row_rsodest['process_claim_no']."'");
											while($r = mysql_fetch_assoc($rs)){ ?>
												<tr>
													<td><?php echo $r['item']; ?></td>
													<td><?php echo $r['unit_price']; ?></td>
													<td><?php echo $r['qty']; ?></td>
													<td><?php echo $r['payment_status']; ?></td>
												</tr>
											<?php	} ?>
									</tbody>
								</table>
						</div>
                    </td>
                </tr>
				  <?php } ?>

                                         <?php $i++; } 
                                 } else { echo '<tr><td colspan="10" style="color:red;text-align:center;">No Records Found.</td></tr>';} ?>
                    <tr>
                        <td style="font-weight:bold;" colspan="<?php if($payment_methods == 'insurance' || $payment_methods == 'company'){ echo 9;}else{ echo 7;} ?>">OVERALL TOTAL AMOUNT </td>
                        <td style="font-weight:bold;"> <?php echo number_format($total_amount,2); ?> </td>
                    </tr>
             </tbody>
                                </table>






</body>