
#!/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
F1=./tmp/F1
DP=./tmp/DP
t=tmpB/tt
#splash> $t
DESD=`awk -F= ' $1=="T1" { print $2 } ' $t `
HAST=`awk -F= ' $1=="T2" { print $2 } ' $t `
awk ' BEGIN {
print " var a=new Array()"
printf "a[0]=\"%s\"\n","'"$DESD"'"
printf "a[1]=\"%s\"\n","'"$HAST"'"
printf "a[2]=\"%s\"\n","'"$CCST"'"
printf "a[3]=\"%s\"\n","'"$RESU"'"
} ' > ../js/factu.js
#set $DESD $HAST
./pac1 1 1 1 >/dev/null
set 01/12/2019 31/12/2019
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 |

pc=./tmp/pc
paci=./tmp/paci
./appl/sh/pacie  | awk -F"^" ' BEGIN { 
x="15,20,16,1,2,3,4,5,7,8,19,"
r=split(x,y,",")
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
$1!=ant {
	for (i=1;i<r;i++) printf "%s^",trail($y[i])
	print ""
	ant=$1
}
{
	print $1"^"$22"^"$23 >"'"$pc"'"
 } ' >$paci

# |  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",m," ") ;inic=f1("'"$1"'") ;fina=f1("'"$2"'") };
function f2(f) { return substr(f,1,2)"/"substr(f,4,2)"/"substr(f,7) }
function f3(f) { return substr(f,7)"-"substr(f,4,2)"-"substr(f,1,2) }
function f1(f) { return substr(f,7)substr(f,4,2)substr(f,1,2) }
function fv(f) { 
 da=substr(f,1,2)-1
 me=substr(f,4,2)+1
 ye=substr(f,7)
 if  (da > m[me]) { da -= m[me]; me++ }
 if  (da > m[me]) { da -= m[me]; me++ }
 if(me >12) { me=1; ye++ }
return sprintf("%.2d/%.2d/%.4d",da,me,ye)
}
function tomrw(f) { 
 da=substr(f,1,2)
 me=substr(f,4,2)+0
 ye=substr(f,7)
	da++
 if (da > m[me]) { da =1; me++ }
 if(me >12) { me=1; ye++ }
return sprintf("%.2d/%.2d/%.4d",da,me,ye)
}
FILENAME == ARGV[1] {pr[$1]=$2;mod[$1]=$3;can[$1]=$4; next } 
FILENAME == ARGV[2] {rate[$1]=$2; next } 
FILENAME == ARGV[3] {if ($1!=ant) { reg=0;ant=$1  }
 rt[$1""sprintf("%.2d",reg++)]=rate[$2$3]
 next } 
function sacap(txt) { tx="";for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
FILENAME == ARGV[4] {pi[$4]=$1"^"$2;pc[$4]=$5;pac[$4]=$8" "$6" "$7"^"substr($9,7)"^"$10"^"substr($11,4); next } 
{ ccic=fv($3)}
f1($6) < inic { next }
f1($6) > fina { next }
$7=="C" {
printf "%s^",pi[substr($2,1,5)]
printf "%s^",$3
printf "%s^",ccic
printf "%s^",sacap(pc[substr($2,1,5)])
printf "%s^",pac[substr($2,1,5)]
printf "%s^",$6
printf "%s^%s^",sacap(substr($5,1,12)),substr($5,14)
printf "%s^",pr[substr($2,1,5)substr($4,1,2)]
printf "%s^",mod[substr($2,1,5)substr($4,1,2)]
printf "%s^",rt[substr($2,1,5)substr($4,1,2)]
print ""
fil="PAGOS/REG"
printf "%s^", sacap(pc[substr($2,1,5)]) > fil
printf "%s^", sacap(substr($5,1,12)) > fil
printf "%s^", f3($6) > fil
printf "%s^", rt[substr($2,1,5)substr($4,1,2)]> fil
printf "%s\n", $1 >fil
}
END {

} ' $DP  RATES $pc $paci $agend >$F1.1
cp $F1.1 ../../xls/tm
exit
[ "$3" != ""  ] && exit

cat $F1 |
awk -F"^" ' BEGIN { 
T["00"]="<br>&nbsp;N<sup>o</sup> REG^RT"
T["01"]="<br>&nbsp;PACIENTE^CT"
T["02"]="TERMINO<br>&nbsp;DE CICLO^CT"
T["03"]="<br>&nbsp;CLAVE^CT"
T["04"]="FECHA<br>&nbsp;PRESTACION^CT"
T["05"]="RUT<br>&nbsp;PRESTADOR^CT"
T["06"]="<br>&nbsp;PRESTACION^LT"
T["07"]="<br>&nbsp;MODALIDAD^LT"
T["08"]="<br>&nbsp;MONTO^RN"
T["09"]="<br>&nbsp;RZ^RT"
T["10"]="<br>&nbsp;CR^RT"
T["11"]="<br>&nbsp;DF^RT"
T["12"]="<br>&nbsp;A^CT"
tx=9
	for (i=0;i<=tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
tt[2]=NR
}
END {
	printf "TOTAL^"
	for (i=2;i<=tx;i++)
		printf "%s^",tt[i]
print ""
} ' - |

awk -F"^" ' BEGIN {
ttop("Inhedo - Facturaci&oacute;n '"$1 - $2"'") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2) 
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[2]="N"
for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
boton(ctanls,"C",15)

print "</tr>"
}
$1!="TOTAL" {
tit[2]=1
tit[12]=1
tit[13]=1
tit[14]=1
al[2]="L"
al[6]="L"
for (i=1; i<NF; i++) if(i==1) boton($i,al[i],70,"S")
else label($i,al[i],tp[i],tit[i])
boton("T",FNR-1,"",1)
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td>"
printf "<p><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;border:1px solid #7F9DB9;background-color:#FFFFFF\"  >\n",$1$8,selv
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
        printf "<td bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"agendH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8""i
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bordercolor=\"#C0C0C0\" bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
print "<form method=\"post\" action=\"../../cgi-bin/agend.cgi\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<H3>Facturaci&oacute;n <br>%s</H3>\n","'"$1 - $2"'"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age1.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 
} 2>./error/anls.err
cat ./error/anls.err
