#!/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=09/08/2009
HAST=10/08/2009
RESU=""
CLIE=
t=/tmp/tt
splash> $t
DESD=`awk -F= ' $1=="T1" { print $2 } ' $t `
HAST=`awk -F= ' $1=="T2" { print $2 } ' $t `
CLIE=`awk -F= ' $1=="T3" { print $2 } ' $t `
RESU=`awk -F= ' $1=="T4" { print $2 } ' $t `
opera=/tmp/opera
ihdm opera 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"^" d09"^" d10"^" d11"^" d12 |
awk -F"^" ' FILENAME==ARGV[1] { x[$1]=$2 ; 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==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 }
{ rd=rdat($3) } 
rd > hast { next }
rd < desd { ent[$5] +=$14 ;rec[$5]+=$15 ;next }
{ print }
END { lin="000000^00^"sprintf("%.2d%s",substr(desd,7)-1,substr("'"$DESD"'",3))"^SALDO ANT"
 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
sort -t"^" -k5,6 -k1  $venta.2 $venta.0 |
awk -F"^" ' $6+0 >0 {  for (i=1;i<=NF;i++) if (i!=5)
		{
			if(i==1) printf "%s^",$5
			printf "%s^",$i
		}
		printf "\n"
} ' -  > $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"]="EST^RN"
T["09"]="CUB^RN"
T["10"]="ENT^RN"
T["11"]="PC6^RN"
T["12"]="KGT^RN"
T["13"]="ENTREGA^RN"
T["14"]="RECIBO^RN"
T["15"]="FP^CT"
T["16"]="SALDO^RN"
tx=18
	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-1;i++)
	{
		if ($2=="00")
			if(i==14 || i==15) printf "^"
			else printf "%s^",$i
		else
		{
			 printf "%s^",$i
			tt[i] +=$i
			tt0[i] +=$i
		}
	}
	sl +=$14-$15
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($1)
	
	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("Facturacion");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\" 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 "<H1>FACTURACION %s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
printf "<H3>%s<H3>\n",ln
print "<form method=\"post\" action=\"../cgi-bin/fact0.cgi\" name=bill>"
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 "<table>"
print "<td><input type=\"submit\" value=\"Continua\" name=\"X01\" OnMouseOver=\"enabot()\" style=\"height: 40; width: 250; font-size: 12pt; font-weight: bold; float:right\"></td>"
print "<td><input type=\"submit\" value=\"Factura\" name=\"X02\" ></td>"

print "</table>"
print "</center>"
print "<script language=\"JavaScript\">"
print "document.bill.X02.disabled=true\nnzd=0"
print "function enabot() {if(++nzd >5) document.bill.X02.disabled=false}"
print"</script>"
print "</form>"
print "</body>"

print "</html>"
} ' -
sort -t"^" -k6,7 $venta  -o $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"]="EST^RN"
T["09"]="CUB^RN"
T["10"]="ENT^RN"
T["11"]="PC6^RN"
T["12"]="KGT^RN"
T["13"]="ENTREGA^RN"
T["14"]="RECIBO^RN"
T["15"]="FP^CT"
T["16"]="SALDO^RN"
tx=18
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
ant!=$6 {
 #if (ant!="") totalp(ant)
	for (i=1;i<tx;i++) tt0[i]=0
l1 +=ll
ll=0
sl1 +=sl
sl=0
ant=$6 }
{
	for (i=1;i<tx-1;i++)
	{
		if ($2=="00")
			if(i==14 || i==15) printf "^"
			else printf "%s^",$i
		else
		{
			 printf "%s^",$i
			tt[i] +=$i
			tt0[i] +=$i
		}
	}
	sl +=$14-$15
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($1)
	
	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("Facturacion");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 (NR!=2 && $6!=ma+1 && "'"$CLIE"'"=="") bgcolor="#FF0000"
ma=$6
 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\" 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 "<H1>CORRELATIVO %s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
printf "<H3>%s<H3>\n",ln
print "<form method=\"post\" action=\"../cgi-bin/fact0.cgi\" name=bill>"
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 "<table>"
#print "<td><input type=\"submit\" value=\"Continua\" name=\"X01\" OnMouseOver=\"enabot()\" style=\"height: 40; width: 250; font-size: 12pt; font-weight: bold; float:right\"></td>"
#print "<td><input type=\"submit\" value=\"Factura\" name=\"X02\" ></td>"

print "</table>"
print "</center>"
print "<script language=\"JavaScript\">"
print "document.bill.X02.disabled=true\nnzd=0"
print "function enabot() {if(++nzd >5) document.bill.X02.disabled=false}"
print"</script>"
print "</form>"
print "</body>"

print "</html>"
} ' -
