<?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'];
$branches = $_GET['branches'];
$item_types = $_GET['item_type'];
list($report_type, $report_type_txt) = explode("@@@", $_GET['report_type']);

if (count($branches) > 0) {
    foreach ($branches as $branch) {
        $branch_qry .= "branch_id='$branch' OR ";
        $branch_txt .= $branch.',';
    }
    $branch_qry = substr($branch_qry, 0, -4);
    $branch_txt = substr($branch_txt, 0,-1);
}

if (count($item_types) > 0) {
    foreach ($item_types as $item_type) {
        $item_type_qry .= "item_service_type='$item_type' OR ";
    }
    $item_type_qry = substr($item_type_qry, 0, -4);
}

if($report_type=='dsr'){
$report_qry = mysql_query("SELECT * FROM claims_details WHERE date_added_short BETWEEN '$start_date' AND '$end_date' AND ($branch_qry) AND ($item_type_qry) ORDER BY date_added,mode_of_payment");
$total_count = mysql_num_rows($report_qry);
} else if($report_type=='ssrbonc'){
  $report_qry = mysql_query("SELECT p.id,surname,othernames,p.card_number,tel,email,sum(qty*unit_price) AS total_amount FROM patient_information p,claims_details c WHERE c.date_added_short BETWEEN '$start_date' AND '$end_date' AND ($branch_qry) AND ($item_type_qry) AND p.card_number=c.patient_no GROUP BY p.id ORDER BY c.date_added,mode_of_payment");
  $total_count = mysql_num_rows($report_qry);  
} else if($report_type=='ssrboncv'){
  $report_qry = mysql_query("SELECT p.id,surname,othernames,p.card_number,tel,email,process_claim_no,date_of_consultation,sum(qty*unit_price) AS total_amount FROM patient_information p,claims_details c WHERE c.date_added_short BETWEEN '$start_date' AND '$end_date' AND ($branch_qry) AND ($item_type_qry) AND p.card_number=c.patient_no GROUP BY process_claim_no ORDER BY date_of_consultation");
  $total_count = mysql_num_rows($report_qry);  
}

$report_qry = mysql_query("SELECT item,item_service_type,branch_id,unit_price, SUM(qty) AS total_qty, SUM(qty*unit_price) AS total_amount FROM claims_details WHERE date_added_short BETWEEN '$start_date' AND '$end_date' AND ($branch_qry) AND ($item_type_qry) GROUP BY item ORDER BY branch_id,item");
$total_count = mysql_num_rows($report_qry);

?>


<head>
        <meta charset="utf-8" />
        <title> Report Generation </title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta content="width=device-width, initial-scale=0" name="viewport" />
        <meta content="" name="description" />
        <meta content="" name="author" />
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" media="all" />
        <link href="../assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="all"/>
</head>
<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>



<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>
                                                        <h5 class="text-danger"> BRANCHES :  <?php 
                                                        foreach ($branches as $branch) {
                                                           echo branchName($branch, $courier).', ';
                                                        }
                                                        ?> </h5>
						</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-bordered">
                    <thead>
                        <tr>
                            <th>SN</th>
                            <th>Branch Name</th>
                            <th>Item Name</th>
                            <th>Item Type</th>
                            <th class="amount">Price</th>
                            <th class="amount">Qty Sold</th>
                            <th class="amount">Total Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        if($total_count > 0){
                            $i=1;
                            while($row = mysql_fetch_assoc($report_qry)){ ?>
                        <tr>
                            <td class=""> <?php echo $i;?> </td>
                            <td class=""> <?php echo branchName($row['branch_id'], $courier); ?> </td>
                            <td class=""> <?php echo $row['item'];?> </td>
                            <td><i class="fa fa-inr"></i> <?php echo $row['item_service_type'];?> </td>
                            <td class="amount"><i class="fa fa-inr"></i> <?php echo number_format($row['unit_price'],2);?> </td>
                            <td class="amount"><i class="fa fa-inr"></i> <?php echo $row['total_qty']; ?> </td>
                            <td class="amount"><i class="fa fa-inr"></i> <?php echo number_format($row['total_amount'],2); $total_amount = $total_amount + ($row['total_amount']); ?> </td>
                        </tr>
                        <?php $i++; }  ?>
                        <tr>
                            <td class="text-right" colspan="6"><h2><strong>Total </strong></h2></td>
                            <td class="text-left text-danger amount"><h2><strong><i class="fa fa-inr"></i> GHS <?php echo number_format($total_amount, 2); ?> </strong></h2></td>
                        </tr>
                        <?php }else{ ?>
                        <tr><td colspan="7" style="text-align: center;">No Records Found</td></tr>
                         <?php } ?>
                    </tbody>
                </table>
			
			
        </div>    
	</div>
</div>
