#!/bin/sh

# disable filename globbing
set `./ags` 
 echo Content-type: text/html
 echo
 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
pacie=./tmp/pacie
REMOTE_USER=RALCARAZ
ZONA=`./appl/sh/usuar|awk -v a=$REMOTE_USER -F"^" 'index($3,a){print substr($4,1,3);exit}' -`
ZONN=`./appl/sh/zonas|awk -v a=$ZONA -F"^" '$1==a {print $3;exit}' -`
[ "$ZONA" = "" ] && ZONN="TODAS LAS ZONAS"
#echo ZONA=$ZONA USER=$REMOTE_USER ZONN=$ZONN
export ZONN ZONA
D=./tmp/D
D1=./tmp/D1
P=./tmp/P
DP=./tmp/DP
V=./tmp/V
V1=./tmp/V1
inic=$1
inte=$2
fina=$3
perso=./tmp/perso
./appl/sh/perso  | awk -F"^" ' # substr($15,1,3)!="'"$ZONA"'" && "'"$ZONA"'"!="" { next } 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
substr($16,1,2)=="00" {
 #print trail($1)"^"trail($2)"^"NR"<br>"
 print trail($1)"^"trail($2) >"./tmp/"substr($11,1,2) } ' -

# |  1 H00 codigo 13 07   0 | 2 H01 identi 40 00  13 | 3 H02 nombre 40 00  53 |
# |  4 H03 patern 40 00  93 | 5 H04 matern 40 00 133 | 6 H05 direcc 40 00 173 |
# |  7 H06 comuna 25 00 213 | 8 H07 ciudad 25 00 238 | 9 H08 correo 40 00 263 |
# | 10 H09 telefo 50 00 303 |11 H10 especi 02 51 353 |12 H11 status 02 52 355 |

profs=./tmp/profs
./appl/sh/profs > $profs

# |  1 H00 codigo 02 00   0 | 2 H01 identi 20 00   2 |
zonas=./tmp/zonas
./appl/sh/zonas  > $zonas

# |  1 H00 correl 03 15   0 | 2 H01 codigo 03 00   3 | 3 H02 identi 30 00   6 |
# |  4 H03 observ 512 12  36 |

#  5 D00 comuna  5   51 |  6 D01 comuna  5   51 | 
./appl/sh/pacie | awk -F"^" '
FILENAME==ARGV[1] { if($1=="'"$ZONA"'")
{
	if($5+0) zon[$5]=$1
	if($6+0) zon[$6]=$1
}
next
 }
zon[substr($7,1,5)]=="'"$ZONA"'" || "'"$ZONA"'"=="" ' $zonas - > $pacie
awk -F"^" ' $1!=ant { x=0 ; ant=$1 }
{
printf "%s^%.2d^%s\n",$1,x++,$22
} ' $pacie > tmp/PP

# |  1 H00 indice 05 15   0 | 2 H01 codigo 13 07   5 | 3 H02 patern 40 00  18 |
# |  4 H03 matern 40 00  58 | 5 H04 nombre 40 00  98 | 6 H05 direcc 50 00 138 |
# |  7 H06 comuna 20 00 188 | 8 H07 ciudad 20 00 208 | 9 H08 sexoss 09 50 228 |
# | 10 H09 nacimt 10 00 237 |11 H10 contac 40 00 247 |12 H11 telefo 40 00 287 |
# | 13 H12 tcelul 40 00 327 |14 H13 correo 50 00 367 |15 H14 actges 10 00 417 |
# | 16 H15 renova 10 00 427 |17 H16 diagno 80 00 437 |18 H17 observ 512 13 517 |
# | 19 H18 isapre 02 51 1029 |20 H19 fechat 10 00 1031 |21 H20 status 02 53 1041 |

# 22 D00 presta  2   54 | 23 D01 modali  2   52 | 24 D02 topes   2   01 | 
# 25 D03 di      2   00 | 26 D04 di      2   00 | 27 D05 di      2   00 | 
# 28 D06 di      2   00 | 29 D07 di      2   00 | 30 D08 di      2   00 | 
# 31 D09 di      2   00 | 32 D10 di      2   00 | 33 D11 di      2   00 | 
# 34 D12 di      2   00 | 35 D13 di      2   00 | 36 D14 di      2   00 | 
# 37 D15 di      2   00 | 38 D16 di      2   00 | 39 D17 di      2   00 | 
# 40 D18 di      2   00 | 41 D19 di      2   00 | 42 D20 di      2   00 | 
# 43 D21 di      2   00 | 44 D22 di      2   00 | 45 D23 di      2   00 | 
# 46 D24 di      2   00 | 47 D25 di      2   00 | 48 D26 di      2   00 | 
# 49 D27 di      2   00 | 50 D28 di      2   00 | 51 D29 di      2   00 | 
# 52 D30 di      2   00 | 53 D31 di      2   00 | 54 D32 di      2   00 | 
# 55 D33 total   1   01 | 
agend=./tmp/agend
./appl/sh/agend > $agend

# |  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 |
awk -F"^" ' BEGIN {
inic=fec("'"$1"'")
fina=fec("'"$2"'")
}

fec($6) >= inic  && fec($6) <= fina && $7=="V" {
#if(substr($2,1,5) =="00069") { print >"/dev/tty" }
	aa=substr($2,1,5)"^"substr($4,1,2)
	pr[aa]++
	rep[aa]=rep[aa]$6" "trail(substr($5,14))"|"
	if($10!="  ") ecog[substr($2,1,5)]=trail($10)"|"$6
	if($11!="  ") eva[substr($2,1,5)]=trail($11)"|"$6
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
END {

for (i in ecog) print i"^"ecog[i] >"tmp/EC"
for (i in eva) print i"^"eva[i] >"tmp/EV"
for (i in pr) print i"^"pr[i]"^"rep[i]
}  ' $agend >$agend.1

awk -F"^" ' BEGIN {  split("31:28:31:30:31:30:31:31:30:31:30:31",dm,":"); modal["01"]="Domicilio";modal["02"]="Ambulatoria" ;modal["03"]="Telemedicina" }
FILENAME==ARGV[1] { pr[$1]=trail($2); next }
FILENAME==ARGV[2] { pa[$1]=1; next }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function tomorrow(dt)
{
	split(dt,dd,"/")
	dd[1]++
	if( dd[2]+0==2 && !(dd[3]%4)) ab=1
	else ab=0
	if (dd[1]+0 >dm[dd[2]+0]+ab) { dd[2]++; dd[1]=1 }
	if (dd[2]+0 >12) { dd[2]=1; dd[3]++ }
	return sprintf("%.2d/%.2d/%.4d",dd[1],dd[2],dd[3])
}
substr($22,1,2)=="  " { next }
substr($21,1,2)!="00" && pa[$1]=="" { next }
x[$1]=="" { x[$1]=trail($2)"^"trail($5)" "trail($3)"^"trail($6)"^"trail($7)"^"trail($11)"^"trail($12)"^"trail($15)"^"trail($16)"^"trail($17)"^"trail($19)"^"trail($20)"^"trail($21);isp[$1]=trail($19);reg=0 
	split($5,nn," ")
	printf "%s^%s^%s^%s^%s^%s^%.2d%.2d^%s^%s\n",$1,nn[1]" "trail($3),trail($7),pr[$22]"^"$22,modal[$23],$24,reg,i+3,$16,""
}
{
	tmrx=$16
	for (i=0;i<37;i++)
	{
		dat=tmrx
		if (substr($(25+i),1,1)=="P" || substr($(25+i),1,1)=="R") printf "%s^%s^%s^%s^%s^%s^%.2d%.2d^%s^%s\n",$1,nn[1]" "trail($3),trail($7),pr[$22]"^"$22,modal[$23],$24,reg,i+3,$16,dat
		#if ($(25+i)!="  ") printf "%s^%s^%s^%s^%s^%s^%.2d%.2d^%s^%s\n",$1,trail($5)" "trail($3),trail($7),pr[$22]"^"$22,modal[$23],$24,reg,i+3,$16,dat
		tmrx=tomorrow(dat)
		if(i==29) v[$1]=dat
	}
	dp[$1""sprintf("%.2d",reg)] = pr[$22]"^"modal[$23]"^"$24"^"$22
	reg++
}
END {
for (i in isp) printf "%s^%s\n",i,isp[i] >"'"tmp/IP"'"
for (i in v) printf "%s^%s\n",i,v[i] >"'"$V"'"
for (i in v) printf "%s^%s\n",i,x[i] >"'"$P"'"
for (i in dp) printf "%s^%s\n",i,dp[i] >"'"$DP"'"
}
' $profs $agend.1 $pacie >$D


awk -F"^" ' BEGIN { inic="'"$inic"'"; fina="'"$fina"'" ;ini=dn(inic);fin=dn(fina)}
FILENAME==ARGV[1] { v[$1]=$2; next }
function dn(x) { split(x,d,"/");return d[3]""d[2]""d[1]}
{ zz=dn($10) }
zz < ini { next }
zz > fin { next }
{
	print zz"^"$0"^"v[$1]
} ' $V $D  | sort |
awk -F"^" ' { for (i=1;i<NF-1;i++)
printf "%s^",$i
if (i==3) printf "%s^%s^",$9,$11
print ""
print substr($0,10) } ' > $D1
awk -F"^" ' BEGIN {lu1=fec("'"$inic"'");lu2=fec("'"$inte"'");lu3=fec("'"$fina"'")
print "'"$inic^$inte^$fina"'"
}
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
FILENAME==ARGV[1] { isp[$1]=substr($2,4) ; next } 
FILENAME==ARGV[2] { pr[$1$2]=$3 ; next }
FILENAME==ARGV[3] { vs[$1""pr[$1$2]]=$3 ; vis[$1""pr[$1$2]]=$4 ; next }
FILENAME==ARGV[4] { ecog[$1]=$2;next }
FILENAME==ARGV[5] { eva[$1]=$2;next }
r0!=$1 { 
	pac[$1]=$2"^"isp[$1]"^"substr($3,7)
	r0=$1
}
fec($10) >=lu2 && fec($10)<lu3  {
#print fec($10) ,lu2,lu3 
aa=$1""pr[$1""substr($8,1,2)];pln[aa]=pln[aa]$10"|";pl[aa]++}

END { for ( i in pac)
{
	printf "%s^%s^%s^%s^%s^%s^%s^%s^%s^^%s^%s^%s^%s^%s^%s^",pac[i],vs[i"01"],vs[i"02"],vs[i"03"],vs[i"04"],vs[i"05"],vs[i"07"],ecog[i],eva[i],pl[i"01"],pl[i"02"],pl[i"03"],pl[i"04"],pl[i"05"],pl[i"07"]
	printf "^%s^%s^%s^%s^%s^%s^^%s^%s^%s^%s^%s^%s\n",vis[i"01"],vis[i"02"],vis[i"03"],vis[i"04"],vis[i"05"],vis[i"07"],pln[i"01"],pln[i"02"],pln[i"03"],pln[i"04"],pln[i"05"],pln[i"07"]
}
} ' tmp/IP tmp/PP $agend.1 tmp/EC tmp/EV $D | sort > ../../xls/tm/repos.dat
cat ../repos
exit
==> tmp/D <==
00005^JUANA CERENIC^00315|Olmu^Mdico^01^Domicilio^5 ^0025^31/10/2019^22/11/2019
00006^KATHERINE GONZALEZ^00287|Valparaso^Psiclogo^04^Ambulatoria^4 ^0321^04/11/2019^22/11/2019

==> tmp/IP <==
00160^02|COLMENA                  
00147^07|NUEVA MAS VIDA           

==> tmp/agend.1 <==
00164^03^1^15/11/2019 Valentina Arroyo    |
00178^00^1^14/11/2019 Alina Ahtamon       |
