<%
crlf=CHR(13)+CHR(10)
SET DATe YMD
SET MARK TO [-]
SET CENTURY ON
SET CENTURY ON 
IF TYPE([oprop])=[O]
	oprophome=addbs(oprop.appstartpath)
ELSE
	oprophome=[d:\shahprjs\supermarket\mh\]
endif
m_transid=orequest.form('thetransid')
m_printdoc=orequest.form('printdoc')
if isnull(m_transid)
    m_transid=orequest.querystring('t') 
endif
if isnull(m_printdoc)
    m_printdoc=orequest.querystring('a') 
endif

*theuser=orequest.querystring('user')
pagetype=lower(transform(orequest.querystring('pw')))
*<Option val="POS">POS English</Option> 80mm
*<Option val="posmix">POS MIX</Option>
*<Option val="poslocal">POS Local Language</Option>
*<Option val="A4">A4 (8.3 x 11.7 inches)</Option>
*<Option val="A5">A5 (5.8 x 8.3 inches)</Option>
*<Option val="A6">A6 (4.1 x 5.8 inches)</Option>
*@page {size: <length> | auto | [ <page-size> || [ landscape | portrait ] ]}
do case
    *case pagetype='poslocal'
    *case pagetype='posmix'
    case pagetype='pos'
        pagewidth='8cm'
        pageheight='auto'
    case pagetype='a4'
        pagewidth='8.3inch'
        pageheight='11.7inch'
    case pagetype='a5'
        pagewidth='5.8inch'
        pageheight='8.3inch'
    case pagetype='a6'
        pagewidth='4.1inch'
        pageheight='5.8inch'
    OTHER
        pagewidth='8.3inch'
        pageheight='11.7inch'
endcase

theuser=[]
tsql=[select * from ("]+dbclocation+[visits.dbf") where allt(transid)==']+allt(m_transid)+[' into cursor maincursor]
&tsql
thepatientid=allt(patientid)
tsql=[select * from ("]+dbclocation+[patients.dbf") where allt(patientid)==']+allt(thepatientid)+[' into cursor ptcursor]
&tsql
today=ptcursor.birth 
thedate=today
store 0 to totaldays, totalmonths, totalyears
if not empty(thedate)
		currentdate=date()
		totaldays=day(currentdate)-day(thedate)
		if totaldays<0
			totaldays=currentdate-GOMONTH(currentdate,-1)
			totalmonths=month(GOMONTH(currentdate,-1))-MONTH(thedate)
			totalyears=YEAR(GOMONTH(currentdate,-1))-YEAR(thedate)
			IF totalmonths<0
				totalmonths=totalmonths+12
				totalyears=totalyears-1
			ENDIF
		ELSE
			totalmonths=month(currentdate)-MONTH(thedate)
			totalyears=YEAR(GOMONTH(currentdate,-1))-YEAR(thedate)
			IF totalmonths<0
				totalmonths=totalmonths+12
				totalyears=totalyears-1
			ENDIF
		ENDIF
endif
***This portion will get test data and will formate it as needed 
*possible groups

*Actual test data
tsql=[select val(x.testid) as vtest,x.testid,x.result, y.testname, y.unit, y.groupname, y.range,y.g from ("]+dbclocation+[lab.dbf") x inner join ]
tsql=tsql+[ (']+oprophome+[temp\labmaster') y ON(x.testid=y.testid) where allt(x.transid)==']+allt(m_transid)+[' ]
tsql=tsql+[ order by vtest into cursor labcursor readwrite ]
&tsql
INDEX on vtest TAG vtest
LOCATE FOR g='A'
IF NOT EOF()
	tsql=[select * FROM (']+oprophome+[temp\labmaster') where testid NOT in (select testid FROM (']+dbclocation+[lab.dbf') where transid=']+m_transid+[') AND g='A' into cursor xcursor ]	
	&tsql
	tdbf=DBF()
	SELECT [labcursor]
	APPEND FROM (tdbf)
endif
LOCATE FOR g='B'
IF NOT EOF()
	tsql=[select * FROM (']+oprophome+[temp\labmaster') where testid NOT in (select testid FROM (']+dbclocation+[lab.dbf') where transid=']+m_transid+[' ') AND g='B' into cursor xcursor ]	
	&tsql
	tdbf=DBF()
	SELECT [labcursor]
	APPEND FROM (tdbf)
endif
LOCATE FOR g='C'
IF NOT EOF()
	tsql=[select * FROM (']+oprophome+[temp\labmaster') where testid NOT in (select testid FROM (']+dbclocation+[lab.dbf') where transid=']+m_transid+[' ) AND g='C' into cursor xcursor ]	
	&tsql
	tdbf=DBF()
	SELECT [labcursor]
	APPEND FROM (tdbf)
endif
select [labcursor]
GO top
tstr=[<table><tr><td>IDENTITY</TD><TD>TEST</TD><TD>RESULT</TD><TD>RESULT</TD><TD>UNIT</TD><TD>RANGE</TD></TR>]
DO WHILE NOT EOF()
	thisgroup=g
	thistest=testid
	tgroupname=groupname
	firstline=.t.
	DO WHILE groupname=tgroupname AND g=thisgroup
		if not (empty(result) and empty(g))
            tstr=tstr+[<tr>]
            IF firstline
                tstr=tstr+[<td>]+tgroupname+[</td>]
                firstline=.f.
                ELSE
                tstr=tstr+[<td>&nbsp;</td>]
            ENDIF
			tstr=tstr+[<td>]+TESTNAME+[</td><td>]+IIF(EMPTY(result),'&nbsp;',ALLTRIM(result))+[</td><td>]+IIF(EMPTY(unit),'&nbsp;',ALLTRIM(unit))+[</td><td>]+IIF(EMPTY(range),'&nbsp;',ALLTRIM(range))+[</td></tr>]
		endif
        skip
	enddo
enddo
tstr=tstr+[</table>]
***
select [ptcursor]
%>
<!DocType HTML>
<html>
<html lang="ar" translate="no">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="google" content="notranslate">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="icon" href="images/logo.bmp" type="image/x-icon">
    <style>
        
      body {
            font-family: 'Calibri', 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
           
       }
        @media print {
            .printnoshow {
                display: none;

            }
        }

        @media screen {
            .screennoshow {
                display: none;
            }
        }

        @media screen,
        print {
            .noshowall {
                display: none;
            }
        }
        @media print {
        #wrapper {
            position: absolute;
           /* left: 0;*/
            top: 0;
        }
    }
        #wrapper {
            width: <%=pagewidth%>;
            text-align: center;
            margin: auto;
        }
        @page {
         size: <%=pagewidth%> <%=pageheight%>;  /* width height */
      }
    h1, p, table {
        margin:auto;
    }
    TD {
        font-size: 10PT;
    }

    </style>
</head>
<body onload="window.print();">
<div id='wrapper'>
    <div style="margin:auto"><img src='images/logo.bmp'></div>
    <h1 style="margin:auto;font-stretch: ultra-expanded	"><%=xcompany.name%></h1>
        <p><%=allt(allt(xcompany.address1)+[ ]+allt(xcompany.address2))%></p>
         <p><span style="font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;font-size: large;font-weight: bold;text-decoration: underline;" >ENTRY TOKEN <%=maincursor.transid%></span></p>
        <%if m_printdoc='token'%>
            <DIV>
                <table style='text-align:center' width="auto" >
                    <tr>    
                        <td style='text-align:left'>Entry:</td><td style='text-align:left'><%=transform(maincursor.dtime)%>
                            <%=icase(ptcursor.gender='m','GENDER: MALE',ptcursor.gender='F','GENDER: FEMALE','')%>
                        </td>
                    </tr>
                    <tr>
                        <td style='text-align:left'>Speciman:</td><td style='text-align:left'><%=allt(maincursor.speciman)%></td>
                    </tr>
                    <tr>
                        <td style='text-align:left'>Patient:</td><td style='text-align:left'><%=ptcursor.name%>
                            <%=iif(totaldays<1 or totalyears>2,'','DAYS: '+transform(totaldays))%> <%=iif(totalmonths<1 or totalyears>2,'','MONTHS: '+transform(totalmonths))%> <%=iif(totalyears<1,'','YEARS: '+transform(totalyears))%>
                            <%=iif(empty(ptcursor.weight),'','WEIGHT: '+transform(ptcursor.weight))%>
                        </td>
                    </tr>
                    <tr>
                        <td style='text-align:left'>Ref. By:</td><td style='text-align:left'><%=maincursor.doctor%></td>
                    </tr>
                    <%if not empty(maincursor.solution)%>
                    <tr>
                        <td style='text-align:left'>Opinion</td><td style='text-align:left'><%=allt(maincursor.solution)%></td>
                    </tr>
                    <%endif%>
                </table>
            </DIV>
        <%endif%>
        <%=tstr%>
</div>
</body>
</html>


