#!/bin/sh

# disable filename globbing
 echo Content-type: text/html
 echo
 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR

set 27/07/2020 03/08/2020 09/08/2020 

pacie=./tmp/pacie
./appl/sh/pacie | 
 awk -F"^" ' 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
substr($21,1,2)=="00" {
if(ant!=$1) {
	printf "%s^",$1
	printf "%s^",trail($2)
	printf "%s^",trail($5)
	printf "%s ",trail($3)
	printf "%s^",trail($4)
	printf "%s^",substr(trail($19),4)
	printf "%s^",trail($15)
	printf "%s^",substr(trail($21),4)
	printf "%s^",substr(trail($7),7)
	print ""
	li=0
}
ant=$1
printf "%s^%.2d^%s^%s\n",$1,li++,$22,$23 >"'"$pacie.B"'"
} ' - | sort -t"^" -k3 > $pacie

agend=./tmp/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 |

./appl/sh/agend |
awk -F"^" ' BEGIN { inic=fec("'"$1"'");fina=fec("'"$2"'") }
FILENAME==ARGV[1] { p[$1$2]=$3; c[$1]=1;next } 
c[substr($2,1,5)]=="" { next } 
$7=="A" {
 idx=substr($2,1,5)substr($4,1,2)
 pr[substr($2,1,5)"^"p[idx]]=substr($5,1,12) }
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
END {
for(i in pr) print i"^"pr[i]
} 
' $pacie.B - > $agend

perso=./tmp/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 |


./appl/sh/perso | 
awk -F"^" '
FILENAME==ARGV[1] { pr[$3]=1;next } 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
pr[$1]=="" { next }
{
 print trail($3)" "trail($4)" "trail($5)"^"trail($1)"^"substr(trail($11),4)"^"trail($12)"^"trail($7)"^"trail($17)"^"trail($13)"^"trail($14)"^"trail($10)"^"trail($9)"^"trail(substr($16,4))
} 
' $agend - | sort  >$perso
cp $perso ../../xls/tm/perso.data


awk -F"^" '
FILENAME==ARGV[1] { pro[$2]=$1"^"$2"^"$4"^"$10 ; next }
FILENAME==ARGV[2] { lin[$1"^"$2]=pro[$3] ; next }
{
	printf "%s^",substr($0,7)
	for (i=1;i<6;i++)
	{
		emp="^^^"
		if (lin[$1"^"sprintf("%.2d",i)]!="") emp=lin[$1"^"sprintf("%.2d",i)]
		printf"%s^",emp
	}
	print ""
 }

 ' $perso $agend $pacie | sort -t"^" -k2 > ../../xls/tm/mesp.data
cat ../rpmes


exit




==> tmp/agend <==
00240^01^07.837.871-9
00240^02^16.273.226-9
00240^03^17.885.100-4
00240^04^18.953.579-1
00247^01^18.103.536-6
00240^05^17.796.580-4
00247^02^18.709.054-7
00247^04^19.097.705-6
00247^05^19.669.049-2
00261^01^18.808.376-5

==> tmp/perso <==
14.030.562-6^Patricio Carvajal ^Psicólogo^^^^^^^^RETIRADO
14.593.649-7^AILYN BASAEZ FERNANDEZ^Kinesiólogo^273409^VIÑA DEL MAR^^09/01/2019^^^ailyn_basaez@hotmail.com^VIGENTE
14.710.932-6^Mónica Alvarez ^Médico^^^^^^^^VIGENTE
15.035.545-1^SEBASTIAN TORREJON JIMENEZ^Kinesiólogo^^CURACAVI^^^^^^VIGENTE
15.075.305-8^Natalie Allende ^Enfermera^^^^^^^^RETIRADO
15.753.914-0^Karen Villalón ^Nutricionista^^VIÑA DEL MAR^^^^^^RETIRADO
15.916.217-6^NATALIA FUENTES ^Médico^^^^^^^^RETIRADO
16.035.875-0^LILIAN BELLO PALACIOS^Psicólogo^^^^^^^^VIGENTE
16.103.814-8^XIMENA SOTO ^Psicólogo^^VIÑA DEL MAR^^^^^^RETIRADO

==> tmp/pacie <==
00247^05.344.314-1^ANA JULIA^AQUEVEQUE^SALGADO^BANMEDICA/VIDATRES^05/02/2020^VIGENTE^Cauquenes^
00301^05.682.044-2^ARNALDO ISRAEL^ARAYA^ARAYA^ISAPRES DEL COBRE^28/05/2020^VIGENTE^Salamanca^
00192^05.426.280-9^ARTURO^CACERES^CAÑETE^BANMEDICA/VIDATRES^28/11/2019^VIGENTE^Rancagua^
00317^04.770.096-5^BARBARA FLORA DE LAS MERCEDES^ERLANDSEN^ GREENE^CONSALUD^08/07/2020^VIGENTE^Pichilemu^
00016^08.085.247-9^CARMEN^POZO^CORIO^NUEVA MAS VIDA^04/01/2019^VIGENTE^Valparaíso^
00278^05.610.959-5^CLAUDIO^CAMINO^YUNGE^NUEVA MAS VIDA^05/12/2019^VIGENTE^Talca^
00126^11.680.772-6^CORINA^MOLINA^AEDO^NUEVA MAS VIDA^18/06/2019^VIGENTE^Hualpén^
00176^06.839.573-9^DANIZA^VERNAL^PADLEY^NUEVA MAS VIDA^23/10/2018^VIGENTE^Iquique^
00232^05.082.369-5^ELISEO^CARRASCO^BLAZQUEZ^CONSALUD^30/01/2020^VIGENTE^Concepción^
00261^05.586.980-4^ERICA^ORELLANA^LOPEZ^NUEVA MAS VIDA^11/10/2019^VIGENTE^Concepción^





