#!/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
planc=tmpB/planc
set $1
echo $1 >tmpB/cuent
awk -F"^" ' NR==1 { cd=$8$9;rcd=0 }
cd!=$8$9 { if(sl) for (j=0;j<rcd;j++) print reg[j]
rcd=0
sl=0
}
{
	cd=$8$9
	reg[rcd]=$0
	rcd++
	sl +=$6-$7
}
END {
if(sl) for (j=0;j<rcd;j++) print reg[j]
} ' tmpB/$1 | tee tmpB/S$1 |
awk -F"^" ' BEGIN { 
T["00"]="FOLIO^RT"
T["01"]="FECHA^CT"
T["02"]="TD^CT"
T["03"]="REGINT^CT"
T["04"]="GLOSA DEL DOCUMENTO^LT"
T["05"]="DEBE^RN"
T["06"]="HABER^RN"
T["07"]="ANALISIS^RT"
T["08"]="# REF^RT"
T["09"]="FECHA^CT"
T["10"]="CC^CT"
T["11"]="SLD DOC^RN"
T["12"]="SLD ANL^RN"
T["13"]="SALDO^RN"
T["14"]="x^CT"
tx=14
	for (i=0;i<tx+2;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx-2;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<=tx-2;i++)
		printf "%s^",tt[i]
print ""
} ' - |
awk -F"^" ' BEGIN {
ttop("Analisis de Saldo '"$1"'");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
while ((getline < "./tmpB/planc.1") >0 ) if($1=="'"$1"'") split($0,cta)
#saldac= cta[4]
close ("./tmpB/planc.1")
}
NR==1 {
 for (i=1; i<NF-2; 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" { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
 labelf(saldac,al[i],tp[i])
 labelf(saldac,al[i],tp[i])
 labelf(saldac,al[i],tp[i])
ctanls="   ""'"$1"'"
boton(ctanls,"C",15)

print "</tr>"
}
$1!="TOTAL" {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
for (i=1; i<NF-1 ; i++) if(i==4) boton($i,al[i],60)
else label($i,al[i],tp[i])
if($8!=anta) saldaa=0
if($8$9!=antd) saldad=0
anta=$8
antd=$8$9
saldad += $6-$7
saldaa += $6-$7
saldac += $6-$7
 label(saldad,al[i],tp[i])
 label(saldaa,al[i],tp[i])
 label(saldac,al[i],tp[i])
chkb(FNR-1)
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function boton(value,alig,lar)
{
        print "<td bgcolor=\"#E1E1E1\">"
printf "<p align=\"center\" ><input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"vouchH00\"></p>\n",lar,alg[alig],value
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)
{
printf "<td bgcolor=\"%s\">",bgcolor
if(substr(tip,1,1)=="N") if(value <0) printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p 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\">"
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>"
while ((getline < "./tmpB/planc.1") >0 ) if($1=="'"$1"'") split($0,cta)
printf "<H3>An&aacute;lisis de Saldo<br>%s<br>%s<br>%s</H3>\n",cta[2]": "cta[3],"'"$2 $3 $4"'",sld
print "<form method=\"post\" action=\"../../cgi-bin/vouch.cgi?FromAnls\" name=anlsform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
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 "</body>"
print "</html>"
} ' - 
} 2>./error/anls.err
cat ./error/anls.err
