#!/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
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 `
produ=/tmp/produ
opera=/tmp/opera
ihdm opera h00"^" h01"^" h02"^" h03"^" > $opera
mpapp=/tmp/mpapp
ihdm mpapp h00"^" h01"^" h02"^" h04"^" h05"^" h06 > $mpapp.1
desba=/tmp/desba
ihdm desba h00"^" h01"^" h03"^" h04 > $desba.1
prter=/tmp/prter
>$prter.1
ihdm prter h00"^" h01"^" h02"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12"^" h13"^" h14"^" h15"^" h16 > $prter.1
venta=/tmp/venta
ihdm venta h00"^" h01"^" h03"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05"^" d06"^" d07  |
awk -F"^" ' 
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
FILENAME==ARGV[1] { x[$1]=$2 ; next }
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
{ rd=rdat($2) } 
rd > hast { next }
rd < desd { 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==2) printf "%.2d^",r++
        }
        print ""
} ' $opera -  > $venta.1
>$prter.v
>$prter.e
>$prter.b
>$prter
>$prter.d

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"'") }
FILENAME == ARGV[1] { op[$1]=$2 ; next }
{ rd=rdat($2) } 
rd < desd && FILENAME==ARGV[4] {
if($3=="PV") fac= -1
else fac=1
ik9 +=$6*fac
ik12 +=$8*fac
ikes +=$10*fac
ikcu +=$12*fac
iken +=$14*fac
ikc6 +=$16*fac
}
rd > hast { next }
rd < desd { next }
FILENAME== ARGV[5] { print rd" --" desd >"/tmp/err" }
FILENAME == ARGV[2] { mp +=$6
for (i=1;i<=NF;i++) if(i==3) printf "%s^",op[$i] >"'"$mpapp"'"
else printf "%s^",$i >"'"$mpapp"'"
print "" >"'"$mpapp"'"
}
FILENAME == ARGV[3] { pi +=$4
for (i=1;i<=NF;i++) if(i==3) printf "%s^",op[$i] >"'"$desba"'"
else printf "%s^",$i >"'"$desba"'"
print "" >"'"$desba"'"
}
FILENAME == ARGV[5] {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter.e"'"
else if(i!=3) printf "%s^",$i >"'"$prter.e"'"
printf "%d^%.2f\n",$5+$7+$9+$11+$13+$15,$6+$8+$10+$12+$14+$16 >"'"$prter.e"'"
ek9 +=$7
ek12 +=$8
ekes +=$9
ekcu +=$10
eken +=$11
ekc6 +=$12
}
FILENAME == ARGV[4]  && $3=="DV" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter.d"'"
else if(i!=3) printf "%s^",$i >"'"$prter.d"'"
printf "%d^%.2f\n",$5+$7+$9+$11+$13+$15,$6+$8+$10+$12+$14+$16 >"'"$prter.d"'"
db9 +=$5
db12 +=$7
dbes +=$9
dbcu +=$11
dben +=$13
dbc6 +=$15
dk9 +=$6
dk12 +=$8
dkes +=$10
dkcu +=$12
dken +=$14
dkc6 +=$16
}
FILENAME == ARGV[4]  && $3=="PV" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter.v"'"
else if(i!=3) printf "%s^",$i >"'"$prter.v"'"
printf "%d^%.2f\n",$5+$7+$9+$11+$13+$15,$6+$8+$10+$12+$14+$16 >"'"$prter.v"'"
vb9 +=$5
vb12 +=$7
vbes +=$9
vbcu +=$11
vben +=$13
vbc6 +=$15
vk9 +=$6
vk12 +=$8
vkes +=$10
vkcu +=$12
vken +=$14
vkc6 +=$16
}
FILENAME == ARGV[4]  && $3=="PT" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter"'"
else if(i!=3) printf "%s^",$i >"'"$prter"'"
printf "%d^%.2f\n",$5+$7+$9+$11+$13+$15,$6+$8+$10+$12+$14+$16 >"'"$prter"'"
b9 +=$5
b12 +=$7
bes +=$9
bcu +=$11
ben +=$13
bc6 +=$15
k9 +=$6
k12 +=$8
kes +=$10
kcu +=$12
ken +=$14
kc6 +=$16
}
END {
 kp=k9+k12+kes+ken+kcu+kc6
 dkp=dk9+dk12+dkes+dken+dkcu+dkc6
 vkp=vk9+vk12+vkes+vken+vkcu+vkc6
 ekp=ek9+ek12+ekes+eken+ekcu+ekc6
 ikp=ik9+ik12+ikes+iken+ikcu+ikc6
fk9=ik9+k9+dk9-vk9
fkc6=ikc6+kc6+dkc6-vkc6
fk12=ik12+k12+dk12-vk12
fkes=ikes+kes+dkes-vkes
fken=iken+ken+dken-vken
fkcu=ikcu+kcu+dkcu-vkcu
 fkp=fk9+fk12+fkes+fken+fkcu+fkc6
if(mp==0) mp=.001
if(pi==0) pi=.001
 printf "REPMP^CONSUMO DE MP^%.2f^100\n",mp
 printf "^DESECHO AL DESBASTE^%.2f^%.2f\n",mp-pi,100*(mp-pi)/mp
 printf "REPPI^PRODUCCION DESBASTE^%.2f^%.2f^100\n",pi,100*pi/mp
 printf "^DESECHO AL CORTE^%.2f^%.2f^%.2f\n",pi-kp,100*(pi-kp)/mp,100*(pi-kp)/pi
 printf "REPPT^PRODUCTO TERMINADO^%.2f^%.2f^%.2f\n",kp,100*kp/mp,100*kp/pi
 
printf "^STOCK INICIAL^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",ik9,ik12,ikes,ikcu,iken,ikc6,ikp >"'"$prter.b"'"
printf "REPPT^PRODUCCION^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",k9,k12,kes,kcu,ken,kc6,kp >"'"$prter.b"'"
printf "REPPV^ENTREGA A VENTA ^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",vk9,vk12,vkes,vkcu,vken,vkc6,vkp >"'"$prter.b"'"
printf "REPVT^VENTA^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",ek9,ek12,ekes,ekcu,eken,ekc6,ekp >"'"$prter.b"'"
printf "REPDV^DEVOLUCION VENDEDORES^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",dk9,dk12,dkes,dkcu,dken,dkc6,dkp >"'"$prter.b"'"
ck9=vk9-ek9-dk9
ck12=vk12-ek12-dk12
ckes=vkes-ekes-dkes
ckcu=vkcu-ekcu-dkcu
cken=vken-eken-dken
ckc6=vkc6-ekc6-dkc6
ckp=ck9+ck12+ckes+cken+ckcu+ckc6
if(ck9||ck12||ckes||cken||ckcu||ckc6) printf "^CUADRATURA EN VENTA^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",ck9,ck12,ckes,ckcu,cken,ckc6,ckp >"'"$prter.b"'"
printf "^STOCK FINAL^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",fk9,fk12,fkes,fkcu,fken,fkc6,fkp >"'"$prter.b"'"
} ' $opera $mpapp.1 $desba.1 $prter.1 $venta.1 > $produ

awk -F"^" ' BEGIN { 
T["00"]="REPORTE^CT"
T["01"]="CONCEPTO^LT"
T["02"]="KILOS^RN2"
T["03"]="[%] MP^RN2"
T["04"]="[%] DB^RN2"
tx=6
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
		tt0[i] +=$i
	}
printf "\n"
}
END {
} ' $produ |
awk -F"^" ' BEGIN {ttop("Analisis de la Produccion");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\" 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>PROD: %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/produc.js\"> </script>\n"
} ' -

awk -F"^" ' BEGIN { 
T["00"]="REPORTE^CT"
T["01"]="CONCEPTO^LT"
T["02"]="KP9^RN2"
T["03"]="KP12^RN2"
T["04"]="KPEST^RN2"
T["05"]="KPCUB^RN2"
T["06"]="KPENT^RN2"
T["07"]="KPC6^RN2"
T["08"]="KPTT^RN2"
tx=10
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
		tt0[i] +=$i
	}
printf "\n"
}
END {
} ' $prter.b |
awk -F"^" ' BEGIN {ttop("Analisis de la Produccion");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,"STOCK") >0  { for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
index($0,"STOCK")==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)
{
if (index($0,"CUADRATURA")) printf "<td bgcolor=\"%s\">","#AA2255"
else 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!="STOCK") if(value=="0.00") value="&nbsp"
if(index($0,"GENERAL") || index($0,"STOCK")) printf "<td bgcolor=\"%s\">","#003333"
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>STOCK: %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/produc.js\"> </script>\n"
} ' -
} 2>error/produ.err
cat error/produ.err
