if [ "$1" == "" ]
then
echo Periodo 01-12 para 2022
exit
fi
sh aw2 $1
PER=2022-$1
cd $PER
>AGE
>ATD
>BAD
>DUP-ATD
>PAR
>TLM
cd ../..

perso=./tmp/perso.f
./appl/sh/perso | awk -F^ ' { print $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 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 |17 H16 conara 06 00 385 |


agend=./tmp/agend.f
./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 |
# | 10 H09 ecog00 02 01  88 |11 H10 eva000 02 01  90 |12 H11 filler 296 15  92 |

pacie=./tmp/pacie.f
./appl/sh/pacie | awk -F^ ' substr($23,1,2)=="02"  { print $1"^"$2"^"$23 } ' | sort -u >$pacie

# |  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 05 55 188 | 8 H07 ciudad 20 00 193 | 9 H08 sexoss 09 50 213 |
# | 10 H09 nacimt 10 00 222 |11 H10 contac 40 00 232 |12 H11 telefo 40 00 272 |
# | 13 H12 tcelul 40 00 312 |14 H13 correo 50 00 352 |15 H14 actges 10 00 402 |
# | 16 H15 renova 10 00 412 |17 H16 diagno 80 00 422 |18 H17 observ 999 14 502 |
# | 19 H18 isapre 02 51 1501 |20 H19 fechat 10 00 1503 |21 H20 status 02 53 1513 |
# | 22 H21 fecref 10 00 1515 |23 H22 client 02 56 1525 |

# 24 D00 presta  2   54 | 25 D01 modali  2   52 | 26 D02 topes   2   01 | 
# 27 D03 di      2   00 | 28 D04 di      2   00 | 29 D05 di      2   00 | 
# 30 D06 di      2   00 | 31 D07 di      2   00 | 32 D08 di      2   00 | 
# 33 D09 di      2   00 | 34 D10 di      2   00 | 35 D11 di      2   00 | 
# 36 D12 di      2   00 | 37 D13 di      2   00 | 38 D14 di      2   00 | 
# 39 D15 di      2   00 | 40 D16 di      2   00 | 41 D17 di      2   00 | 
# 42 D18 di      2   00 | 43 D19 di      2   00 | 44 D20 di      2   00 | 
# 45 D21 di      2   00 | 46 D22 di      2   00 | 47 D23 di      2   00 | 
# 48 D24 di      2   00 | 49 D25 di      2   00 | 50 D26 di      2   00 | 
# 51 D27 di      2   00 | 52 D28 di      2   00 | 53 D29 di      2   00 | 
# 54 D30 di      2   00 | 55 D31 di      2   00 | 56 D32 di      2   00 | 
# 57 D33 di      2   00 | 58 D34 di      2   00 | 59 D35 di      2   00 | 
# 60 D36 di      2   00 | 61 D37 di      2   00 | 62 D38 di      2   00 | 
# 63 D39 di      2   00 | 64 D40 total   1   01 | 
cd TMC
FINI=01/$1/2022
FFIN=31/$1/2022

awk -F^ ' BEGIN { fi=fec("'"$FINI"'"); ft=fec("'"$FFIN"'"); print fi,ft }
FILENAME==ARGV[1] {ru[$1]=trail($2) ;vg[trail($2)]=1; next }
FILENAME==ARGV[2] {rp[trail($1)]=1; next }
function fec(ff) { return substr(ff,7)"-"substr(ff,4,2)"-"substr(ff,1,2) }
function fcc(ff) { return substr(ff,1,4)"-"substr(ff,6,2)"-"substr(ff,9) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[3] {
r2=toupper(trail($11))
if(substr($11,2,1)==".") r2=trail("0"r2)
rr=toupper(trail($9))
if(substr($9,2,1)==".") rr=trail("0"rr)
if(NF >3)
{
	mm=r2"^"rr"^"trail($(NF-3))
	if(rp[rr] && vg[r2] && index($(NF-3),"'"$PER"'")&&!z[mm])
		print  mm"^"$2"^A" >"'"$PER/BAD"'"
		z[mm]++
	if(rp[rr] && vg[r2] && index($(NF-3),"'"$PER"'"))
		w[mm]=trail(w[mm])trail($2)"^"
}
next
}
#ru[$2]=="" { next }
$7!="C" { next }
fec($6)<fi { next }
fec($6)>ft { next }
{
printf "%s^%s%s\n",$1,ru[substr($2,1,5)],substr($0,13) >"'"$PER/AGE"'"
pr[substr($5,1,12)]=1
x[ru[substr($2,1,5)]"^"substr($5,1,12)"^"fec($6)]++
y[ru[substr($2,1,5)]"^"substr($5,1,12)"^"fec($6)]=y[ru[substr($2,1,5)]"^"substr($5,1,12)"^"fec($6)]$1"^"
}

END {
print "-------------------------------------------------- DUP-TLM"
for (i in x) if (x[i] >1 ) print i"^"y[i]
#for (i in x)  print i,y[i]
print "-------------------------------------------------- DUP-ATD"
for (i in z) if (z[i] >1 ) print i"^"w[i] > "'"$PER/DUP-ATD"'"
#for (i in z) print i,w[i]
print "--------------------------------------------------PAREADOS"

for (i in x) if (z[i] >0 )
{
	print i"^"trail(y[i])trail(w[i]) >"'"$PER/PAR"'"
	z[i]=0;x[i]=0
}
print "--------------------------------------------------SOLO ATD"
for (i in z) if  ( z[i] >0 )
{
	split(i,a)
	#print a[1]"="a[2]"="vg[a[1]]"="pr[a[2]]
	if(vg[a[1]] && pr[a[2]]) print i"^"w[i]"A" >"'"$PER/ATD"'"
	z[i]=0
}
print "-------------------------------------------------- SOLO TLM"
for (i in x) if  ( x[i] >0 )
{
	print i"^"y[i]"T" >"'"$PER/TLM"'"
	x[i]=0
}
}
 ' .$pacie .$perso $PER/BIT .$agend

