#!/bin/sh

# disable filename globbing
BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
pc=./tmp/pc
activ=./tmp/activ
paci=./tmp/paci
t=$1
DESD=`awk -F= ' $1=="activH01" { print $2 } ' $t `
HAST=`awk -F= ' $1=="activH02" { print $2 } ' $t `
PACI=`awk -F= ' $1=="activH03" { print $2 } ' $t `
PRES=`awk -F= ' $1=="activH06" { print $2 } ' $t `
#awk ' BEGIN {
#print " var a=new Array()"
#printf "a[0]=\"%s\"\n","'"$DESD"'"
#printf "a[1]=\"%s\"\n","'"$HAST"'"
#printf "a[2]=\"%s\"\n","'"$CCST"'"
#printf "a[3]=\"%s\"\n","'"$RESU"'"
#} ' > ../js/activ.js
set $DESD $HAST "$PACI" "$PRES"
./appl/sh/pacie1  | awk -F"^" ' BEGIN { 
x="15,20,16,1,2,3,4,5,7,8,19,"
r=split(x,y,",")
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
{	print $1"^"$22"^"$23"^--" >"'"$pc"'" } 
$1!=ant {
	for (i=1;i<r;i++) printf "%s^",trail($y[i])
	print ""
	ant=$1
} ' >$paci
perso=./tmp/perso
./appl/sh/perso | awk -F"^" ' BEGIN { 
x="1,2,11,"
r=split(x,y,",")
}

function sacap(txt) { for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
{
	printf "%s^",sacap($1)
	for (i=2;i<r;i++) printf "%s^",trail($y[i])
	print ""
	tx=""
} ' -  >$perso

# |  1 H00 codigo 12 07   0 | 2 H01 identi 40 00  12 | 3 H02 nombre 40 00  52 |
# |  4 H03 patern 40 00  92 | 5 H04 matern 40 00 132 | 6 H05 direcc 40 00 172 |
# |  7 H06 comuna 25 00 212 | 8 H07 ciudad 25 00 237 | 9 H08 correo 40 00 262 |
# | 10 H09 telefo 50 00 302 |11 H10 especi 02 51 352 |12 H11 regist 06 01 354 |
# | 13 H12 fechai 10 00 360 |14 H13 fechae 10 00 370 |15 H14 zonas  03 53 380 |
# | 16 H15 status 02 52 383 |


# |  1 H00 regist 06 00   0 | 2 H01 pacien 05 51   6 | 3 H02 fechai 10 00  11 |
# |  4 H03 valida 04 00  21 | 5 H04 profes 12 52  25 | 6 H05 fechae 10 00  37 |
# |  7 H06 transs 01 00  47 | 8 H07 transc 20 00  48 | 9 H08 usuari 20 00  68 |

agend=./tmp/agend
./appl/sh/agend | awk -F"^" ' BEGIN {split("31 28 31 30 31 30 31 31 30 31 30 31",m," ") ;inic=f1("'"$1"'") ;fina=f1("'"$2"'")
x="1,2,3,4,5,6,7,8,9,"
r=split(x,y,",")
}
FILENAME==ARGV[1] { pac[$4]=substr($8" "$6" "$7,1,30)"^"substr($9,7) ; next} 
FILENAME==ARGV[2] { pro[$1]=$1"^"$2"^"substr($3,4,3) ; next} 
FILENAME==ARGV[3] && ant!=$1 { ppc=0; ant=$1 }
FILENAME==ARGV[3] {
if($3=="01") pc[$1""sprintf("%.2d",ppc)]="D"
if($3=="02") pc[$1""sprintf("%.2d",ppc)]="A"
if($3=="03") pc[$1""sprintf("%.2d",ppc)]="T"
ppc++
next} 

function sacap(txt) { tx="";for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function f2(f) { return substr(f,1,2)"/"substr(f,4,2)"/"substr(f,7) }
function f3(f) { return substr(f,7)"-"substr(f,4,2)"-"substr(f,1,2) }
function f1(f) { return substr(f,7)substr(f,4,2)substr(f,1,2) }
function fv(f) {
 da=substr(f,1,2)-1
 me=substr(f,4,2)+1
 ye=substr(f,7)
 if  (da > m[me]) { da -= m[me]; me++ }
 if  (da > m[me]) { da -= m[me]; me++ }
 if(me >12) { me=1; ye++ }
return sprintf("%.2d/%.2d/%.4d",da,me,ye)
}
function tomrw(f) {
 da=substr(f,1,2)
 me=substr(f,4,2)+0
 ye=substr(f,7)
        da++
 if (da > m[me]) { da =1; me++ }
 if(me >12) { me=1; ye++ }
return sprintf("%.2d/%.2d/%.4d",da,me,ye)
}
{ ffc = f1($6) }
ffc > fina { next } 
ffc < inic { next } 

trail("'"$3"'")!="" && "'"$3"'"!=substr($2,1,5) { next }
trail("'"$4"'")!="" && "'"$4"'"!=substr($5,1,12) { next }
trail($7)!="" {
	rp=sprintf("%5.5s",$2)
	printf "%5.5s^",rp
	printf "%s^",sacap(pac[rp])
	printf "%s^",pc[rp""substr($4,1,2)]
	printf "%s^",pro[sacap(sprintf("%12.12s",$5))]
	printf "%s^",f3($6)
	printf "%s^",$7
	printf "%s^",trail($8)
	printf "%s^",trail($9)
	print ""
} ' $paci $perso $pc - | awk -F"^" ' {
pac[$1$5$8]=$1"^"$2"^"$3"^"$4"^"$6"^"$7"^"$8
age[$1$5$8$9]=$10"&nbsp;<br>&nbsp;"$11
}
END { 
for (i in pac) printf "%s^%s^%s^%s\n",pac[i],age[i"A"],age[i"V"],age[i"C"]
} 
 ' - | sort -t"^" -k1,1 -k7 > $activ





# |  1 H00 regist 06 00   0 | 2 H01 pacien 05 51   6 | 3 H02 fechai 10 00  11 |
# |  4 H03 valida 04 00  21 | 5 H04 profes 12 52  25 | 6 H05 fechae 10 00  37 |
# |  7 H06 transs 01 00  47 | 8 H07 transc 20 00  48 | 9 H08 usuari 20 00  68 |

cat $activ | awk -F"^" ' BEGIN { 
T["01"]="N<sup>o</sup> REG^RT"
T["02"]="PACIENTE^LT"
T["03"]="COMUNA^LT"
T["04"]="MOD^CT"
T["05"]="PRESTADOR^LT"
T["06"]="PRES^LT"
T["07"]="FECHA^CT"
T["08"]="AGENDA^CT"
T["09"]="VISITA^CT"
T["10"]="CIERRE^CT"
T["11"]="<br>&nbsp;CR^RT"
T["12"]="<br>&nbsp;DF^RT"
T["13"]="<br>&nbsp;A^CT"
tx=11
	for (i=1;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
tt[2]=NR
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - Actividades '"$1 - $2"'") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])

print "</tr>"
}
$1!="TOTAL" {
tit[2]=1
tit[12]=1
tit[13]=1
tit[14]=1
al[2]="L"
al[6]="L"
for (i=1; i<NF; i++) label($i,al[i],tp[i],tit[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>Cuadro de Actividades por Paciente<br>%s</H3>\n","'"$1 - $2"'"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 
#} 2>./error/anls.err
#cat ./error/anls.err
