#!/bin/sh

# disable filename globbing

 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
pacie=./tmp/pacie
fec=01/07/2019
agend=./tmp/agend
./appl/sh/agend | awk -F"^" ' BEGIN { feci=fec("'"$fec"'") }
function fec(x) { return substr(x,7)""substr(x,4,2) }
$7=="C"  && fec($6) == feci {
 y[substr($2,1,5)]=1;x[substr($2,1,5)substr($4,1,2)]++
}
END {
for (i in y) print i"^"x[i"00"]"^"x[i"01"]"^"x[i"02"]"^"x[i"03"]"^"x[i"04"]"^"x[i"05"]
}

' - >$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/pacie | awk -F"^" ' BEGIN { feci=fec("'"$fec"'") }
#{ print $1,feci, fec($15),fec($16),fec($20),$21 }
feci < fec($15) { next } 
feci < fec($20) { next } 
feci <= fec($16) { 
	print $1"^"substr($19,1,2)"^"$15"^"$16"^"$20"^"$22"^"$23"^"$24

}
function fec(x) { return substr(x,7)""substr(x,4,2) }
function fech(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }


' -

# |  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 512 13 502 |
# | 19 H18 isapre 02 51 1014 |20 H19 fechat 10 00 1016 |21 H20 status 02 53 1026 |

# 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 di      2   00 | 56 D34 di      2   00 | 57 D35 di      2   00 | 
# 58 D36 di      2   00 | 59 D37 di      2   00 | 60 D38 di      2   00 | 
# 61 D39 di      2   00 | 62 D40 total   1   01 | 
