<?php session_start();
include('../accesscheck2.php');
require_once('../Connections/courier.php'); 

$process_claim_no = $_GET['num'];
$receiptno = $_GET['receipt'];
$typeofvisit = $_GET['visit'];
$currency = "GHS";
$print_options = $_GET['print_options'];

$patient_info_r = mysql_fetch_assoc(mysql_query("SELECT patient_no FROM process_claim_no WHERE process_claim_no='$process_claim_no'"));
$patient_no = $patient_info_r['patient_no'];

if (count($print_options) > 0) {
    foreach ($print_options as $print_option) {
        $receipt_no_qry .= "receipt_number='$print_option' OR ";
        $receipt_text .= $print_option.' & ';
    }
} else {
    $receipt_no_qry = "receipt_number='$receiptno' OR ";
    $receipt_text .= $receiptno.' & ';
}

$receipt_no_qry = substr($receipt_no_qry, 0, -4);
$receipt_text = substr($receipt_text, 0,-3);

if ($typeofvisit != 'Guest') {
    $query_claim = "select * from claims_details where process_claim_no='$process_claim_no' and ($receipt_no_qry) order by receipt_number";
    $patient_info = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_information WHERE card_number='$patient_no'"));
} else {
    $query_claim = "select * from claims_details where process_claim_no='$process_claim_no' and ($receipt_no_qry) order by receipt_number";
    $patient_info = mysql_fetch_assoc(mysql_query("SELECT * FROM guest_patients WHERE card_number='$patient_no'"));
}

$rsclaim_total=mysql_query("select sum(qty*unit_price) as totalprice from claims_details where patient_no='$patient_no' and mode_of_payment='credit sales' AND payment_status='Approved'", $courier) or die(mysql_error());
$row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
$total_credit_sales=$row_rsclaim_total['totalprice'];

$patient_balance = $patient_info['patient_balance'];
$query_claim = mysql_query($query_claim);
$total_count = mysql_num_rows($query_claim);

$total_discount_info = mysql_fetch_assoc(mysql_query("SELECT sum(DISTINCT discount_amount) AS total_discount FROM patient_visit_discounts p, claims_details c WHERE p.id=c.discount_id AND p.process_claim_no='$process_claim_no' AND p.discount_status='Approved' AND p.discount_used='used' AND ($receipt_no_qry)"));
$total_discount = $total_discount_info['total_discount'];

$rsclaim_total=mysql_query("select sum(qty*unit_price) as totalprice from claims_details where process_claim_no='$process_claim_no' and (mode_of_payment='cash' OR mode_of_payment='credit sales') and (payment_status='Paid' OR payment_status='Approved-Paid') AND ($receipt_no_qry)", $courier) or die(mysql_error($courier));
$row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
$total_cash=$row_rsclaim_total['totalprice']; 

$settings = mysql_fetch_assoc(mysql_query("SELECT * FROM settings WHERE VAT_NHIL = 'exclusive'"));
// $VATNHIL =  (($settings['VAT']+$settings['NHIL'])/100) * $total_cash;

$VATNHIL_INITIAL =  (($settings['VAT']+$settings['NHIL'])/100) * $total_cash; 
$REMAINDER =  $total_cash - $VATNHIL_INITIAL;
$VATNHIL =  (($settings['VAT']+$settings['NHIL'])/100) * $REMAINDER;

$total_paid_info = mysql_fetch_assoc(mysql_query("SELECT sum(amount) AS total_paid FROM cashier_payments WHERE process_claim_no='$process_claim_no' AND ".  str_replace('receipt_number', 'receiptno', $receipt_no_qry)));
$total_paid = $total_paid_info['total_paid'];

//PICK BRANCH DETAILS
 $branch_info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id='".$_SESSION['branch_id']."'"));
?>


<head>
        <meta charset="utf-8" />
        <title> Receipt Print Out </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>
<div class="col-md-12" id="print_button"><br></br>
		<button onclick="window.print();" class="btn green" style="float:right;">Print Receipt</button>
</div>
<style type="text/css" media="print">
	#print_button{
		display:none;
	}
</style>
<style type="text/css" media="all">
    .text-danger strong {
    		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">
	<div class="row">
		
        <div class="receipt-main col-xs-10 col-sm-10 col-md-6 col-xs-offset-1 col-sm-offset-1 col-md-offset-3">
            <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>(+233) 050 - 1519111 <i class="fa fa-phone"></i></p>
							<p>INFO@ROBERTANDSONOPTICAL.COM <i class="fa fa-envelope-o"></i></p>
							<p><?php echo $branch_info['branch_name']; ?> BRANCH <i class="fa fa-location-arrow"></i></p>
							<p> TIN NUMBER : <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">
							<h5> <?php echo $patient_info['othernames'].' '.$patient_info['surname']; ?> <small>  |   Visit No : <?php echo $process_claim_no; ?> </small></h5>
							<p><b>Mobile :</b> <?php echo $patient_info['tel']; ?> </p>
                                                        <p><b>Email :</b> <?php if(!empty($patient_info['email'])){echo $patient_info['email'];}else{echo 'N/A';} ?></p>
							<p><b>Receipt #. :</b> <?php echo $receipt_text; ?></p>
						</div>
					</div>
					<div class="col-xs-4 col-sm-4 col-md-4">
						<div class="receipt-left">
							<h1>Receipt</h1>
						</div>
					</div>
				</div>
            </div>
			
            <div>
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Item (Description)</th>
                            <th class="amount">Price</th>
                            <th class="amount">Qty</th>
                            <th class="amount">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        if($total_count > 0){
                            while($row = mysql_fetch_assoc($query_claim)){ ?>
                        <tr>
                            <td class="col-md-7"> <?php echo $row['item'];?> </td>
                            <td class="col-md-1 amount"><i class="fa fa-inr"></i> <?php echo number_format($row['unit_price'],2);?> </td>
                            <td class="col-md-1 amount"><i class="fa fa-inr"></i> <?php echo $row['qty']; ?> </td>
                            <td class="col-md-3 amount"><i class="fa fa-inr"></i> <?php echo number_format($row['qty']*$row['unit_price'],2); $total_amount = $total_amount + ($row['qty']*$row['unit_price']); ?> </td>
                        </tr>
                        <?php } } ?>
                        <tr>
                            <td class="text-right" colspan="3">
                                <p>
                                    <strong>Total Amount </strong>
                                </p>
                            </td>
                            <td class="amount">
                                <p>
                                    <strong><i class="fa fa-inr"></i> <?php echo number_format($total_amount-$VATNHIL, 2); ?> </strong>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td class="text-right" colspan="3">
                                <p>
                                    <strong>VAT/NHIL (<?php echo $settings['VAT']+$settings['NHIL'].'%'; ?>) </strong>
                                </p>
                            </td>
                            <td class="amount">
                                <p>
                                    <strong><i class="fa fa-inr"></i> <?php echo number_format($VATNHIL, 2); ?> </strong>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td class="text-right" colspan="3">
                                <p>
                                    <strong>Less Discount </strong>
                                </p>
                            </td>
                            <td class="amount">
                                <p>
                                    <strong><i class="fa fa-inr"></i> <?php echo number_format($total_discount, 2); ?> </strong>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td class="text-right" colspan="3"><h2><strong>Total </strong></h2></td>
                            <td class="text-left text-danger amount"><h2><strong><i class="fa fa-inr"></i>  <?php echo number_format($total_amount-$total_discount, 2); ?> </strong></h2></td>
                        </tr>
                        <tr>
                            <td class="text-right" colspan="3"><h2><strong> Amount Paid </strong></h2></td>
                            <td class="text-left text-danger amount"><h2><strong><i class="fa fa-inr"></i>  <?php echo number_format($total_paid, 2); ?> </strong></h2></td>
                        </tr>
                        <?php if($patient_balance < 0 || $total_credit_sales>0){ ?>
                        <tr>
                            <td class="text-right" colspan="3"><strong> Outstanding Balance </strong></td>
                            <td class="text-left amount"><strong><i class="fa fa-inr"></i>  <?php echo number_format(($patient_balance * -1)+$total_credit_sales, 2); ?> </strong></td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
			
			<div class="row">
				<div class="receipt-header receipt-header-mid receipt-footer">
					<div class="col-xs-8 col-sm-8 col-md-8 text-left">
						<div class="receipt-right">
                                                    <p><b>Date :</b> <?php echo date('jS F, Y g:ia'); ?> </p>
							<!-- <h5 style="color: rgb(140, 140, 140);">Thank you for your service!</h5> -->
						</div>
					</div>
					<div class="col-xs-4 col-sm-4 col-md-4">
						<div class="receipt-left">
							<h1>Signature</h1>
						</div>
					</div>
				</div>
            </div>
			
        </div>    
	</div>
</div>