#!/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
t=/tmp/tt
splash> $t
DESD=`awk -F= ' $1=="T1" { print $2 } ' $t `
HAST=`awk -F= ' $1=="T2" { print $2 } ' $t `
#DESD=01/09/2009
#HAST=30/09/2009
vende=/tmp/vende
ihdm vende h00"^" h03"^" h04"^" h07 | awk -F"^" ' $4+0 ' > $vende
opera=/tmp/opera
ihdm opera h00"^" h03"^" h04"^" h07 > $opera
antic=/tmp/antic
ihdm antic h00"^" h01"^" h02"^" h04"^" h06"^" h07 > $antic
desba=/tmp/desba
awk -F"^" '
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'")}
function rdat(rec) { return substr(rec,7,4)substr(rec,4,2)substr(rec,1,2) }
FILENAME == ARGV[1] {y[$1]=$1"^"$2"^"$3"^"; tr[$1]=$4;next }
FILENAME == ARGV[2] {v[$1]=$1"^"$2"^"$3"^"; tr[$1]=$4;next }
{ rd=rdat($2) }
rd > hast { next }
rd < desd { next }
{
	if($3+0)
	{
		ant[$3] +=$5
		ff="/tmp/"$3
		print y[$3]$1"^"$2"^"$5"^"$6"^" >ff
	}
	else
	{
		ant[$4] +=$5
		ff="/tmp/"$4
		print v[$4]$1"^"$2"^"$5"^"$6"^" >ff
	}
}
END {
	for (i in y) if(ant[i]) print y[i]""ant[i]
	for (i in v) if(ant[i]) print v[i]""ant[i]
}

 ' $opera $vende $antic > $antic.1
awk -F"^" ' BEGIN { 
T["00"]="CODIGO^CT"
T["01"]="EMPLEADO^LT"
T["02"]="SX^CT"
T["03"]="ANTICIPOS^RN"

tx=5
 for (i=0;i<tx;i++)
        {
                x=sprintf("%.2d",i)
                printf "%s^",T[x]
        }
	printf "\n"
}
{
        t1=0
        for (i=1;i<tx;i++)
        {
                printf "%s^",$i
                tt[i] +=$i
        }
        tt[i] +=t1
printf "\n"
}
END {
        t1=0
	printf "TOTAL^^"
        for (i=3;i<tx;i++)
        {
                printf "%s^",tt[i]
        }
printf "^\n"
} ' $antic.1 |
awk -F"^" ' BEGIN {ttop("Anticipos");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
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
}
index($0,"TOTAL") >0  { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
index($0,"TOTAL")==0 {
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,%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>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") printf "<p align=\"%s\"><font face=\"Arial,helvetica\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial,helvetica\" 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,helvetica\" 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,helvetica\" 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 "<H1>ANTICIPOS<BR>%s al %s</H1>\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/antop.js\"> </script>\n"
} ' -

# } 2> ./error/antop.err
#cat ./error/antop.err
