#!/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
DESD=01/00/2010
HAST=31/01/2010
t=tmpB/tt
splash> $t
DESD=`awk -F= ' $1=="T1" { print $2 } ' $t `
HAST=`awk -F= ' $1=="T2" { print $2 } ' $t `
CCST=`awk -F= ' $1=="T3" { print $2 } ' $t `
RESU=`awk -F= ' $1=="T4" { 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/bln.js
[ -z "$CCST" ] && CCST=Z
rm -f tmpB/*
planc=./tmpB/planc
ihdm planc h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" d00"^" d01"^" d02"^" d03"^" d04 > $planc

# |  1 H00 codigo 05 00   0 | 2 H01 codcta 09 00   5 | 3 H02 descri 40 00  14 |
# |  4 H03 debhab 01 56  54 | 5 H04 analit 01 17  55 | 6 H05 ccosto 01 17  56 |

#  7 D00 period  2   57 |  8 D01 debito  15  01 |  9 D02 credit  15  01 | 
# 10 D03 saldom  15  01 | 11 D04 saldoa  15  01 | 
vouch=./tmpB/vouch
ihdm vouch d00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h00"^" d01"^" d02"^" d03"^" d04"^" d05"^" d06"^" d07"^" d08 > $vouch

# |  1 H00 transa 05 15   0 | 2 H01 numero 06 00   5 | 3 H02 fechad 10 18  11 |
# |  4 H03 tipodo 02 55  21 | 5 H04 glosad 80 10  23 | 6 H05 prepar 35 00 103 |
# |  7 H06 fechap 10 00 138 | 8 H07 revisa 35 00 148 | 9 H08 fechar 10 00 183 |
# | 10 H09 autori 35 00 193 |11 H10 fechaa 10 00 228 |

# 12 D00 cuenta  5   56 | 13 D01 descri  5   57 | 14 D02 glosad  35  00 | 
# 15 D03 debito  15  01 | 16 D04 credit  15  01 | 17 D05 analis  14  00 | 
# 18 D06 docref  10  00 | 19 D07 fechar  10  00 | 20 D08 ccosto  2   00 | 
fld="1^2^3^4^12^14^15^16^17^18^19^20^"
awk -F"^" ' BEGIN {
c=split("'"$fld"'",a)
for (i=1;i<c;i++) r[a[i]]=1
}
$4=="ZZ" { next }
function trail(x) { while (substr(x,length(x),1)==" ") x=substr(x,1,length(x)-1); return x }
{
for (i=1;i<=NF;i++) if(r[i]==1) printf "%s^",trail($i)
print ""
} ' $vouch |sort -t"^" -k9,9 -k10,10 -k11.7,11 -k11.4,11 -k11,11 |
awk -F"^" ' 
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") ; ccst="'"$CCST"'"}
FILENAME==ARGV[1] { x[$1]=substr($2,1,2)"-"substr($2,3,2)"-"substr($2,5,2)"-"substr($2,7)"^"$3 ; next }
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
{ rd=rdat($3) } 
ccst!= $12 && ccst!="Z" { next }
"'"$RESU"'"==0 { niv=$1 }
"'"$RESU"'"==1 { niv=substr($1,1,3)"00" }
"'"$RESU"'"==2 { niv=substr($1,1,2)"000" }
"'"$RESU"'"==3 { niv=substr($1,1,1)"0000" }
rd > hast { next }
rd < desd { next
 sd[$1] += $7; sa[$1]+=$8
 for (i=2;i<NF;i++) printf "%s^",$i >"tmpB/"niv
print "" >"tmpB/"niv
next
}
{
sd[niv] +=0
sdm[niv] += $7; sam[niv]+=$8
sdr[niv""substr(rd,5,2)] += $7-$8
 for (i=2;i<NF;i++) printf "%s^",$i >"tmpB/"niv
print "" >"tmpB/"niv
}
END {
for (i in sd)
{
	deb=sd[i]+sdm[i]
	crd=sa[i]+sam[i]
	sal=deb - crd
	sl[1]+=deb
	sl[2]+=crd
	sld=slc=acd=acc=rep=reg=0
	if (sal >0) sld =sal
	else slc=-sal
	if (substr(i,1,1) < 3) { acd=sld;acc=slc }
	else { rep=sld;reg=slc }
	printf "%s^%s^%.0f^%.0f^",i,x[i],deb,crd
	printf "%.0f^%.0f^",sld,slc
	printf "%.0f^%.0f^",acd,acc
	printf "%.0f^%.0f^\n",rep,reg
}
for (i in sd)
{
	printf "%s^%s^",i,x[i] >"tmpB/sdr"
	for (j=1;j<13;j++) printf "%d^",sdr[i""sprintf("%.2d",j)] >"tmpB/sdr"
	print "" >"tmpB/sdr"
}
} ' $planc - | sort -t"^" -k2 >$planc.1

awk -F"^" ' BEGIN { 
printf "^^BALANCE GENERAL TRIBUTARIO^^%s^%s\n","'"$DESD"'","'"$HAST"'"
T["00"]="CODIGO"
F["00"]="CT"
T["01"]="CUENTA"
F["01"]="CT"
T["02"]="DESCRIPCION"
F["02"]="LT"
T["03"]="DEBITOS"
F["03"]="RN"
T["04"]="CREDITOS"
F["04"]="RN"
T["05"]="SALDO&nbsp;<br>DEUDOR"
F["05"]="RN"
T["06"]="SALDO&nbsp;<br>ACREEDOR"
F["06"]="RN"
T["07"]="INVENTARIO&nbsp;<br>ACTIVO"
F["07"]="RN"
T["08"]="INVENTARIO&nbsp;<br>PASIVO"
F["08"]="RN"
T["09"]="PERDIDAS"
F["09"]="RN"
T["10"]="GANANCIAS"
F["10"]="RN"
tx=12
	for (i=0;i<tx-1;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",F[x]
	}
	print ""
	for (i=0;i<tx-1;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
		tt0[i] +=$i
	}
printf "\n"
}
END {
printf "^^TOTALES GENERALES^"
	for (i=4;i<=tx;i++)
		printf "%s^",tt[i]
print "T"
tt[1]=tt[2]=""
utper=tt[11]-tt[10]
if (utper >0)
for (i=1;i<tx;i++) if(i==9||i==10) { printf "%s^",utper;tt[i]+=utper}
	else if(i==3) printf "UTILIDAD DEL EJERCICIO^"
		else printf "^"
else for (i=1;i<tx;i++) if(i==8||i==11) { printf "%s^",-utper;tt[i]-=utper}
	else if(i==3) printf "PERDIDA DEL EJERCICIO^"
		else printf "^"
print "^T"
for (i=1;i<=tx; i++) if(i==3) printf "TOTALES IGUALES^"
		else printf "%s^",tt[i]
print "T"
} ' $planc.1 | tee $planc.2 |
awk -F"^" ' BEGIN {ttop("Balance General");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
getline
 for (i=1; i<=NF; i++)
	{
		al[i]=substr($i,1,1)
		tp[i]=substr($i,2)
	}
next
}
NR==3 { for (i=1; i<NF; i++) labelf($i,al[i])
print "</tr>"
next
}
$NF=="T" { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
print "</tr>"
next }
$NF=="" {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 for (i=1; i<NF ; i++) if( i==1) boton($i,al[i]) 
else label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 #for (i=1; i<NF ; i++) labelf("",al[i],tp[i])
 foot() } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"button\" style=\"width: 120; text-align:%s\" value=\"%s\" name=\"T02\" OnClick=\"produ(this.value,%c%s al %s%c)\"></p>\n",alg[alig],value,39,"'"$DESD"'","'"$HAST"'",39
print "</td>"
}
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") if(value=="0.00") value="&nbsp"
if(index($0,"GENERAL") || index($0,"DOCUM")) printf "<td bgcolor=\"%s\">","#003399"
else printf "<td bgcolor=\"%s\">","#004499"
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>"
printf "<H2> Balance General Tributario<br>%s al %s</H2>\n","'"$DESD"'","'"$HAST"'"
print "<form method=\"post\" action=\"../../cgi-bin/boletbsc.cgi\" name=billform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<input type=\"hidden\" value=\"Query\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
print "</table>"
print "</center>"
print "</form>"
print "</body>"
print "</html>"
printf "<script language=\"Javascript\" src=\"../js/balan.js\"> </script>\n"
} ' -

} 2>error/balan.err
sh show error/balan.err
