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

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

$settings = mysql_fetch_assoc(mysql_query("SELECT * FROM settings"));

//echo"SELECT patient_no,mode_of_payment, insurance_company FROM process_claim_no WHERE process_claim_no='$process_claim_no'";

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

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);

$previous_balance_info = mysql_fetch_assoc(mysql_query("SELECT previous_balance FROM cashier_payments WHERE patient_no='$patient_no' ORDER BY id DESC LIMIT 1"));
$previous_balance=$previous_balance_info['previous_balance'];

$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 item_service_type <> 'Optical Consultation' 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']."'"));
?>


<!DOCTYPE html>
<html lang="en" >

<head>

    <meta charset="UTF-8">
    <link rel="shortcut icon" type="image/x-icon" href="https://static.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" />
    <link rel="mask-icon" type="" href="https://static.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" />
    <title>RX - POS Receipt</title>

    <style>
        #invoice-POS ::selection {
            background: #f31544;
            color: #FFF;
        }
        #invoice-POS ::moz-selection {
            background: #f31544;
            color: #FFF;
        }
        #invoice-POS h1 {
            font-size: 1.7em;
            color: #000;
        }
        #invoice-POS h2, #invoice-POS b {
            font-size: 1.1em;
        }
        #invoice-POS h3 {
            font-size: 1.4em;
            font-weight: 300;
            line-height: 2em;
        }
        #invoice-POS p {
            font-size: .9em;
            color: #000;
            line-height: 1.2em;
        }
        #invoice-POS #top, #invoice-POS #mid, #invoice-POS #bot {
            /* Targets all id with 'col-' */
            border-bottom: 1px solid #EEE;
        }
        #invoice-POS #top {
            min-height: 100px;
        }
        #invoice-POS #mid {
            min-height: 80px;
        }
        #invoice-POS #bot {
            min-height: 50px;
        }
        #invoice-POS #top .logo {
            height: 60px;
            width: 60px;
            background: url(http://michaeltruong.ca/images/logo1.png) no-repeat;
            background-size: 60px 60px;
        }
        #invoice-POS .clientlogo {
            float: left;
            height: 60px;
            width: 60px;
            background: url(http://michaeltruong.ca/images/client.jpg) no-repeat;
            background-size: 60px 60px;
            border-radius: 50px;
        }
        #invoice-POS .info {
            display: block;
            /* margin-left: 0; */
        }
        #invoice-POS .title {
            float: right;
        }
        #invoice-POS .title p {
            text-align: right;
        }
        #invoice-POS table {
            width: 100%;
            border-collapse: collapse;
        }
        #invoice-POS .tabletitle {
            font-size: .7em;
            background: #EEE;
        }
        #invoice-POS .service {
            border-bottom: 1px solid #EEE;
        }
        #invoice-POS .item {
            width: 24mm;
        }
        #invoice-POS .itemtext {
            font-size: .75em;
        }
        #invoice-POS #legalcopy {
            margin-top: 5mm;
        }

    </style>

    <script>
        window.console = window.console || function(t) {};
    </script>



    <script>
        if (document.location.search.match(/type=embed/gi)) {
            window.parent.postMessage("resize", "*");
        }
    </script>


</head>

<body translate="no" >

<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>

<div id="invoice-POS">

    <center id="top">
        <div class="logo">
            <img class="img-responsive" src="../images/app_pics/rns_logo.jpeg" style="width: 71px;">
        </div>
        <div class="info">
            <span style="display:block;font-size: 1.3em;">Robert & Sons Ltd</span>
            <span style="display:block;font-size: .9em;"><?php echo $branch_info['branch_name']; ?> BRANCH</span>
            <span style="display:block;font-size: .7em;">TIN NUMBER : C0005582083 </span>
            <span style="display:block;font-weight: bold; font-size: .7em;">TAX INVOICE </span>
        </div><!--End Info-->
    </center><!--End InvoiceTop-->
    <hr>
    <div id="mid">
        <div class="info">
            <h2>Client Info</h2>
            <p style="font-size: .8em">
                Name   : <?php echo $patient_info['othernames'].' '.$patient_info['surname']; ?></br>
                Company: <?php echo strtoupper(($mode_of_payment != 'cash') ? getinsurance($insurance_companyPOS) : "PRIVATE"); ?></br>
                Phone   : <?php echo $patient_info['tel']; ?></br>
                Visit No : <?php echo $process_claim_no; ?><br>
                Receipt # : <?php echo $receipt_text; ?><br>
                Date Printed : <?php echo date('d/m/Y, g:ia'); ?>
            </p>
        </div>
    </div><!--End Invoice Mid-->

    <hr>
    <div id="bot">

        <div id="table">
            <table>
                <tr class="tabletitle">
                    <td class="item"><h2>Item</h2></td>
                    <td class="Hours"><h2>Qty</h2></td>
                    <td class="Hours"><h2>Unit Price</h2></td>
                    <td class="Rate"><h2>Sub Total</h2></td>
                </tr>

                <?php
                if($total_count > 0){
                    while($row = mysql_fetch_assoc($query_claim)){ ?>
                        <tr class="service">
                            <td class="tableitem"><span class="itemtext"> <?php echo $row['item'];?></span> </td>
                            <td class="tableitem"><span class="itemtext"> <?php echo $row['qty']; ?></span> </td>
                            <td class="tableitem"><span class="itemtext"> <?php echo number_format($row['unit_price']); ?></span> </td>
                            <td class="tableitem"><span class="itemtext"> <?php echo number_format($row['qty']*$row['unit_price'],2); $total_amount = $total_amount + ($row['qty']*$row['unit_price']); ?></span> </td>
                        </tr>
                    <?php } } ?>


                <tr class="tabletitle">
                    <td></td>

                    <td class="Rate"><b>Total Amount (VAT exclusive)</b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format($total_amount-$VATNHIL, 2); ?> </b></td>
                </tr>

                <tr class="tabletitle">
                    <td></td>

                    <td class="Rate"><b>VAT (<?php echo $settings['VAT'].'%'; ?>) </b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format($VATNHIL, 2); ?></b></td>
                </tr>

                <tr class="tabletitle" style="display: none;">
                    <td></td>

                    <td class="Rate"><b>NHIL</b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format('0', 2); ?> </b></td>
                </tr>

                <tr class="tabletitle" style="display: none;">
                    <td></td>

                    <td class="Rate"><b> GETFund Levy</b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format('0', 2); ?> </b></td>
                </tr>

                <tr class="tabletitle">
                    <td></td>

                    <td class="Rate"><b>Less Discount </b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format($total_discount, 2); ?> </b></td>
                </tr>

                <tr class="tabletitle">
                    <td></td>

                    <td class="Rate"><b> Total (VAT inclusive)</b></td>
                    <td></td>
                    <td class="payment"><b><?php echo number_format($total_amount-$total_discount, 2); ?> </b></td>
                </tr>



                <tr class="tabletitle">
                    <td></td>

                    <td class="Rate"><b> Amount Paid </b></td>
                    <td></td>
                    <td class="payment"><b> <?php echo number_format($total_paid, 2); ?> </strong> </b></td>
                </tr>

                <?php if($patient_balance < 0 || $total_credit_sales>0){ ?>
                    <tr class="tabletitle">
                        <td></td>

                        <td class="Rate"><b> Outstanding Balance </b></td>
                        <td></td>
                        <td class="payment"><b> <?php echo number_format(($patient_balance * -1)+$total_credit_sales, 2); ?>  </strong> </b></td>
                    </tr>
                <?php } ?>

            </table>
        </div><!--End Table-->

        <hr>
        <div id="legalcopy">
            <center>
                <p class="legal" style="font-size:.75em"><strong>Tel No: <?php echo $settings['tel_no']; ?></strong></p>
            </center>
        </div>

        <hr>
        <div id="legalcopy">
            <center>
                <p class="legal" style="font-size:.75em"><strong>Supplier's Signature: ....................</strong></p>
            </center>
        </div>

    </div><!--End InvoiceBot-->
</div><!--End Invoice-->






</body>

</html>