#!/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 `
CLIE=`awk -F= ' $1=="T3" { print $2 } ' $t `
FACT=`awk -F= ' $1=="T4" { print $2 } ' $t `
VEND=`awk -F= ' $1=="T5" { print $2 } ' $t `
echo $HAST >./tmp/FEC
opera=/tmp/opera
ihdm vende h00"^" h01"^" h02"^" h03"^" h04"^" h05 > $opera
venta=./tmp/venta
ffact=./tmp/ffact
ihdm ffact h01"^" h02"^" h03"^" h04"^" h05"^" h17"^" h18"^" h19"^" |
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"'") }
{ rd=rdat($2) }
#{ print rd, desd "<br>" ; print "<br>" } 
rd > hast { next }
rd < desd { next }
{ print } 
 ' | sort -t"^" -k2.6,2  -k2.3,2 -k2,2 -k1,1 > $ffact
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]=$2 ; next }
$1!=ant { r=1 ;ant=$1}
index($4,"LOCAL") && $5+0 {
	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 - |
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 { next }
{ print }
END { 
} 
'  - > $venta.1
awk -F"^" ' $6+0 == 0 { next }
{  for (i=1;i<=NF;i++) if (i!=5)
		{
			if(i==1) printf "%s^",$5
			printf "%s^",$i
		}
		printf "\n"
} ' $venta.1  | sort -t"^" -k1,1 -k4.6,4  -k4.3,4 -k4,4 > $venta
sort -t"^" -k6,7 $venta  -o $venta
awk -F"^" ' 
bin[$4]=="" {bin[$4]=$6 ; ff[1+z++]=$4}
{
	bfi[$4]=$6
	for (i=7;i<18;i++) btt[i":"$4]+=$i
}
END {
	for (j=1;j<=z;j++)
	{
		printf "%s^%7d^%7d^",ff[j],bin[ff[j]],bfi[ff[j]] >"tmp/lvenb"
		for (i=7;i<17;i++) printf "%.1f^",btt[i":"ff[j]] >"tmp/lvenb"
		printf "%.0f^",btt[i":"ff[j]]/1.19 >"tmp/lvenb"
		printf "%.0f^",btt[i":"ff[j]]*.19/1.19 >"tmp/lvenb"
		printf "%.0f^\n",btt[i":"ff[j]] >"tmp/lvenb"
		#print "<br>"
	}
	
 } ' $venta
awk -F"^" ' BEGIN { 
T["00"]="CLIENTE^LT"
T["01"]="DOCUM^CT"
T["02"]="LI^CT"
T["03"]="FECHA^CT"
T["04"]="VENDEDOR^LT"
T["05"]="PEDIDO^RT"
T["06"]="9MM^RN1"
T["07"]="12MM^RN1"
T["08"]="ENT^RN1"
T["09"]="EST^RN1"
T["10"]="CUB^RN1"
T["11"]="PC6^RN1"
T["12"]="MLL^RN"
T["13"]="SAC^RN"
T["14"]="GRN^RN"
T["15"]="TTK^RN1"
T["16"]="ENTREGA^RN"
T["17"]="RECIBO^RN"
tx=19
	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 "\n"
}
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("Correlativos");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-3; 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"
clie=$1
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,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>CORRELATIVO BOLETAS %s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
#printf "<H3>%s<H3>\n",ln
print "<form method=\"post\" action=\"../cgi-bin/lven.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=\"Libro Ventas\" 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>"
} ' -
} 2>error/lvent.err
cat ./error/lvent.err
