#!/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
venta=/tmp/venta
ihdm venta h00"^" h01"^" h03"^" h08 > $venta

# |  1 H00 fventa 06 00   0 | 2 H01 fechai 16 00   6 | 3 H02 kilom1 06 00  22 |
# |  4 H03 vende  04 56  28 | 5 H04 fechat 16 00  32 | 6 H05 kilom2 06 01  48 |
# |  7 H06 valorg 10 01  54 | 8 H07 gastos 246 11  64 | 9 H08 faltan 10 01 310 |

# 10 D00 client  10  55 | 11 D01 guiads  6   01 | 12 D02 9milim  7   01 | 
# 13 D03 12mili  7   01 | 14 D04 penter  7   01 | 15 D05 pestof  7   01 | 
# 16 D06 ppcubo  7   01 | 17 D07 pcort6  7   01 | 18 D08 mallas  4   01 | 
# 19 D09 sacosp  4   01 | 20 D10 kgrane  7   01 | 21 D11 codigs  20  15 | 
# 22 D12 tkilos  8   01 | 23 D13 vlrvta  10  01 | 24 D14 vlrpag  10  01 | 
# 25 D15 frpago  2   57 | 
antic=/tmp/antic
ihdm antic h00"^" h01"^" h02"^" h04"^" h06 > $antic
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
ihdm desba h00"^" h01"^" h02"^" h04 > $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]=$0; tr[$1]=$4;next }
FILENAME == ARGV[4] {v[$1]=$1"^"$2"^"$3"^"; tr[$1]=$4;next }
{ rd=rdat($2) }
rd > hast { next }
rd < desd { next }
FILENAME == ARGV[2] {ant[$3] +=$5; ant[$4]+=$5; ant[$3substr($2,1,10)] +=$5; ant[$4substr($2,1,10)] +=$5; next }
FILENAME == ARGV[3] {fal[$3] +=$4; fal[$3substr($2,1,10)] +=$4; next }
{
	x[$3] +=$4
	x1[$3substr($2,1,10)] +=$4
}
END {
		for (j in x)
			printf "%s^%.2f^%.2f^%d^%d^%.0f\n",y[j],x[j],tr[j]*x[j],ant[j],fal[j],tr[j]*x[j]-ant[j]-fal[j]
		for (r in x1)
		{
			j=substr(r,1,4)
			printf "%s^%s^%.2f^%.2f^%d^%d^%.2f\n",y[j],substr(r,5),x1[r],tr[j]*x1[r],ant[r],fal[r],tr[j]*x1[r]-ant[r]-fal[r] >"/tmp/"j
		}
		for (j in v)
			printf "%s^%s^%.2f^%d^%d^%.0f\n",v[j],"",tr[j],ant[j],fal[j],tr[j]-ant[j]-fal[j]
		for (j in v)
			printf "%s^%s^^%.2f^%d^%d^%.0f\n",v[j],"",tr[j],ant[j],fal[j],tr[j]-ant[j]-fal[j] >"/tmp/"j
}

 ' $opera $antic $venta $vende $desba > $desba.1
awk -F"^" ' BEGIN { 
T["00"]="CODIGO^CT"
T["01"]="EMPLEADO^LT"
T["02"]="SX^CT"
T["03"]="TARIFA^RN2"
T["04"]="DESBASTE^RN2"
T["05"]="SUELDO^RN"
T["06"]="ANTICIPOS^RN"
T["07"]="FALTANTE^RN"
T["08"]="SALDO^RN"

tx=10
 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=5;i<tx;i++)
        {
                printf "%s^",tt[i]
        }
printf "^\n"
} ' $desba.1 |
awk -F"^" ' BEGIN {ttop("Tarifado y Sueldos");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>TARIFADO: %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/tarif.js\"> </script>\n"
} ' -

#} 2>error/produ.err
cat ./error/tarif.err
