#!/bin/sh

# disable filename globbing

 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
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
echo $*
export ZONN
ZONA=""
paci1=./tmp/paci1
D=./tmp/DS
D1=./tmp/D1S
P=./tmp/PS
DP=./tmp/DPS
V=./tmp/VS
V1=./tmp/V1S
perso=./tmp/perso
agend=./tmp/agendS
inic=$1
inte=$2
fina=$3
if [ A = A ]
then
./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 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 |


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

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

awk -F"^" ' BEGIN {  split("31:28:31:30:31:30:31:31:30:31:30:31",dm,":"); modal["01"]="Domicilio";modal["02"]="Ambulatoria" }
FILENAME==ARGV[1] { pr[$1]=trail($2); 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 }
#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);reg=0 }
x[$1]=="" { x[$1]=trail($2)"^"trail($5)" "trail($3)"^"trail($19)"^"trail($7)"^"trail($15);reg=0 }
{
	tmrx=$16
	#if(substr($21,1,2)=="00") for (i=0;i<37;i++)
	for (i=0;i<37;i++)
	{
		dat=tmrx
		if ($(25+i)=="PD" || $(25+i)=="PA" || $(25+i)=="RD" || $(25+i)=="RA") 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
		#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)
	}
	dp[$1""sprintf("%.2d",reg)] = pr[$22]"^"modal[$23]"^"$24
	v[$1]=dat
	reg++
}
END {
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 $paci1 >$D
awk -F"^" ' BEGIN { inic="'"$inte"'"; 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"^" ' { print substr($0,10) } ' > $D1
perso=./tmp/perso 
./appl/sh/perso | awk -F"^" '{ print $1"^"substr($11,4,1) } ' - > $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 zonas  03 53 354 |
# | 13 H12 status 02 52 357 |



# |  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 |
./appl/sh/agend | 
awk -F"^" ' BEGIN {
inic=fec("'"$1"'")
fina=fec("'"$2"'")
}
{
 pr[substr($2,1,5)substr($4,1,2)]=substr($5,1,12)
}
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
fec($6) >= inic {
#if($7=="A") print "ag[\""substr($2,1,5)$4"\"]=\""$8"+"substr($2,1,5)$4"+"substr($5,1,12)"+"$7"+"$3"+"$6"+"$9"\""
#if($7=="V") print "rz[\""substr($2,1,5)$4"\"]=\""$8"+"substr($2,1,5)$4"+"substr($5,1,12)"+"$7"+"$3"+"$6"+"$9"\""
#if($7=="C") print "cr[\""substr($2,1,5)$4"\"]=\""$8"+"substr($2,1,5)$4"+"substr($5,1,12)"+"$7"+"$3"+"$6"+"$9"\""
split($2,a,"|")
split($5,b,"|")
if($7=="C") print  a[1]"^"b[1]"^"$6"^"$7
}
END {
#for ( i in pr)  print "pr[\""i"\"]=\""pr[i]"\""

}  '  > $agend

fi 
awk -F"^" ' FILENAME == ARGV[1] { x[$1"^"substr($4,1,1)]=x[$1"^"substr($4,1,1)]$10"|" ;y[$1]=1}
FILENAME == ARGV[2] { r[$1]=$2 }
FILENAME == ARGV[3] { g[$1"^"r[$2]]=g[$1"^"r[$2]]$3"|" ; y[$1]=1} 
FILENAME == ARGV[4] { z[$1]=$0 }
END {
pr[1]="M"
pr[2]="E"
pr[3]="K"
pr[4]="P"
pr[5]="N"
pr[6]="T"
for (i in y)
{
	printf "%s^",z[i]
	for (j=1;j<7;j++) printf "%s","^"x[i"^"pr[j]]"^"g[i"^"pr[j]]
	print ""
}

} ' $D1 $perso $agend $P | sort > ../xls/SEMN
