#!/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 `
CLIE=`awk -F= ' $1=="T5" { print $2 } ' $t `
SALD=`awk -F= ' $1=="T6" { print $2 } ' $t `
FPAG=`awk -F= ' $1=="T7" { 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"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05"^" d06"^" d07"^" d08"^" d09"^" d10"^" d12"^" d13"^" d14"^" d15"^" |
awk -F"^" ' FILENAME==ARGV[1] { x[$1]=$4 ; next }
$3!=ant { r=1 ;ant=$3}
{
	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==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"'") }
"'"$CLIE"'"!="" && index($5,"'"$CLIE"'")==0 { next }
"'"$VEND"'"!="" && index($4,"'"$VEND"'")==0 { next }
{ rd=rdat($3) }
#{ 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
#sort -t"^" -k5,6 -k3.7 -k3.4 -k3.1  $venta $venta.0 -o $venta
cat $venta.2 $venta.0 |
awk -F"^" '
"'"$FPAG"'"!="" && index($19,"'"$FPAG"'")==0 { next }
{  for (i=1;i<=NF;i++) if (i!=5)
		{
			if(i==1) printf "%s^",$5
			printf "%s^",$i
		}
		printf "\n"
} ' -  | sort -t"^" -k5,6 -k4.6,4  -k4.3,4 -k4,4 > $venta
#awk ' { print $0 "<br>" } ' $venta
awk -F"^" ' BEGIN { 
T["01"]="DOCUM^CT"
T["02"]="LI^CT"
T["03"]="FECHA^CT"
T["04"]="VENDEDOR^LT"
T["00"]="CLIENTE^LT"
T["05"]="PEDIDO^RT"
T["06"]="P9^RN"
T["07"]="P12^RN"
T["08"]="ENT^RN"
T["09"]="EST^RN"
T["10"]="CUB^RN"
T["11"]="PC6^RN"
T["12"]="ML^RN"
T["13"]="SC^RN"
T["14"]="GRN^RN"
T["15"]="TTK^RN"
T["16"]="ENTREGA^RN"
T["17"]="RECIBO^RN"
T["18"]="FP^CT"
T["19"]="SALDO^RN"
tx=21
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
ant!=$5 { if (ant!="") totalp(ant)
	for (i=1;i<tx;i++) tt0[i]=0
l1 +=ll
ll=0
sl1 +=sl
sl=0
ant=$5 }
{
	for (i=1;i<tx-1;i++)
	{
		if ($3=="00")
			if(i==17 || i==18) printf "^"
			else printf "%s^",$i
		else
		{
			 printf "%s^",$i
			tt[i] +=$i
			tt0[i] +=$i
		}
	}
	sl +=$17-$18
ll++
printf "%.0f^\n",sl
}
function totalp(cli)
{
	printf "%s^%d^^^TOTAL^",cli,ll
	tt0[6]=0
	for (i=6;i<tx-1;i++)
	printf "%.2f^",tt0[i]
	printf "%.0f^^\n",sl
}
END {
	totalp($5)
	
	printf "TOTAL^%d^^^GENERAL^",ll+l1
	tt[6]=0
	for (i=6;i<tx-1;i++) printf "%.2f^",tt[i]
	printf "%.0f^^\n",sl1 +sl
} ' $venta |
awk -F"^" ' BEGIN {ttop("Analitico Kilos Venta");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"
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>Analitico Kilos Venta<br>%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
