#!/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 `
VEND=`awk -F= ' $1=="T3" { print $2 } ' $t `
RESU=`awk -F= ' $1=="T4" { print $2 } ' $t `
opera=/tmp/opera
ihdm vende h00"^" h01"^" h02"^" h03"^" h04"^" h05 > $opera
venta=/tmp/venta
>$venta.0
ihdm venta h00"^" h01"^" h03"^" h04"^" h02"^" h05"^" h06"^" h08"^" h07"^" d10"^" d11"^" d12"^" d13"^" d14"^" |
awk -F"^" ' FILENAME==ARGV[1] { x[$1]=$4 ; next }
$1!=ant { r=1 ;ant=$1}
{
	for (i=1;i<=NF;i++)  
	{
		if (i==3) printf "%s^",x[$i]
		else if (i==2) printf "%10.10s^",$i
		else printf "%s^",$i
		if (i==6) printf "%.2d^",$6-$5
		#if (i==1) printf "%.2d^",r++
	}
	print ""
} ' $opera -  > $venta.1

awk -F"^" '
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
"'"$VEND"'"!="" && index($3,"'"$VEND"'")==0 { next }
{ rd=rdat($2) }
#{ print rd, desd "<br>" ; print "<br>" } 
rd > hast { next }
rd < desd { ent[$5] +=$17 ;rec[$5]+=$18 ;next }
{ print }
END { lin="000000^00^"sprintf("%.2d%s",substr(desd,7)-1,substr("'"$DESD"'",3))"^SALDO ANT"
 #if ("'"$SALD"'"!="") for (i in ent) if(ent[i]!=rec[i]) printf "%s^%s^^^^^^^^^^^^%s^%s^SL\n",lin,i,ent[i],rec[i] >"'"$venta.0"'"
}
' $venta.1 > $venta.2
echo | awk -F"^" '
$1!=ant {
	if (ant!="")
	{
		printf "%s^",rant
		#printf "%s^%d^%d^%d^%d^",rant,kgr,kgp,vta,pag
		x=split(ant9,a," ")
		j=1
		glos=""
		if (x>0)
		{
			for (i=1;i<=x;i++) if(a[i]+0>0)
			{
				if (j>1) printf "%6.6s^^^^^^^^^",rant
				#if (j>1) printf "%6.6s^^^^^^^^^^^^",rant
				for(z=j;z<i;z++) printf ("%s ",a[z])
				printf ("^%s\n",a[i])
				j=i+1
			}
		}
		else print ""
	}
	rant=sprintf ("%106.106s",$0)
	ant9=$10
	kgr =0
	kgp =0
	vta =0
	pag =0
	ant=$1
} 
{
kgr +=$11
kgp +=$12
vta +=$13
pag +=$14
}
END { 
 } ' $venta.2 -  >$venta

#sort -t"^" -k5,6 -k3.7 -k3.4 -k3.1  $venta $venta.0 -o $venta
#awk ' { print $0 "<br>" } ' $venta
awk -F"^" ' BEGIN { 
T["00"]="CLIENTE^LT"
T["00"]="DOCUM^CT"
T["01"]="FECHA^CT"
T["02"]="VENDEDOR^LT"
T["03"]="PETROLEO^RN"
T["04"]="KM.INIC^RN"
T["05"]="KM.FINL^RN"
T["06"]="KM.CONS^RN"
T["07"]="GASTOS^RN"
T["08"]="FALTANTE^RN"
T["09"]="GLOSA GASTO^LT"
T["10"]="MONTO^RN"
tx=12
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
ant!=$1 { if (ant!="") totalp(ant)
	for (i=1;i<tx;i++) tt0[i]=0
l1 +=ll
ll=0
sl1 +=sl
sl=0
ant=$1 }
{
	for (i=1;i<tx;i++)
	{
		if ($3=="00")
			if(i==17 || i==18) printf "^"
			else printf "%s^",$i
		else
		{
			 printf "%s^",$i
			tt[i] +=$i
			tt0[i] +=$i
		}
	}
ll++
printf "\n",sl
}
function totalp(cli)
{
	printf "%s^%d^TOTAL^",cli,ll
	tt0[5]=0
	tt0[6]=0
	for (i=4;i<tx;i++)
	printf "%.2f^",tt0[i]
	printf "^\n",sl
}
END {
	totalp($1)
	
	printf "TOTAL^%d^GENERAL^",ll+l1
	tt[5]=0
	tt[6]=0
	for (i=4;i<tx;i++) printf "%.2f^",tt[i]
	printf "^\n",sl1 +sl
} ' $venta |
awk -F"^" ' BEGIN {ttop("Gastos Operacionales");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-1; 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>"
}
"'"$RESU"'"!="" { next }
index($0,"TOTAL")==0 {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 for (i=1; i<NF ; i++) label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END { foot() } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"submit\" style=\"width: 120; text-align:%s\" value=\"&nbsp;%s&nbsp;\" name=\"T02\"></p>\n",alg[alig],value
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"
coltot="#003399"
if($8!= $4+$11) coltot="#993300"
if(index($0,"GENERAL") || index($0,"DOCUM")) printf "<td bgcolor=\"%s\">","#003399"
else printf "<td bgcolor=\"%s\">",coltot
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>Analisis GG.OO.: %s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
if ("'"$VEND"'"!="") 
printf "<H2>Vendedor: %s </H2>\n","'"$VEND"'"
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>"
} ' -
} 2> ./error/ctacte.err
cat ./error/ctacte.err
