#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
{
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR HOME
FECI=`awk -F= ' /H01/ { print $2 } ' ./ihd/etc/pp`
rm -f tmp/*
sh prod4 $FECI >/dev/null
sh prod3.1 $FECI >/dev/null
set pedidos tmp/horneo $FECI
awk -F^ ' BEGIN {alg["L"]="left"; alg["C"]="center"; alg["R"]="right" }
FILENAME==ARGV[1] {
	T[$1+0]++
	TI[$1+0""T[$1+0]]=$2
	ST[$1+0""T[$1+0]]=$3
	CSP[$1+0""T[$1+0]]=$4
	RSP[$1+0""T[$1+0]]=$5
	if($2=="TUNDS" || $2=="TNETO"|| $2=="IVA"|| $2=="TOTAL") clr[T[$1+0]]="tl"
	else if($2=="UND"|| $2=="NETO") clr[T[$1+0]]="ng"
	else clr[T[$1+0]]="wh"
	if(row=="")  row=$1+0
	next
}
function labelf(value,style,colspan,rowspan,k)
{
alg["L"]="left"
alg["C"]="center"
alg["R"]="right"
align="align=\""alg[substr(style,1,1)]"\""
printf "<th colspan=\"%s\" rowspan=\"%s\" bgcolor=\"%s\">",colspan,rowspan,"#003399"
printf "&nbsp;%s&nbsp;",value
print "</th>"
}
function boton(value)
{
        print "<td>"
        printf "<input id=\"submit\" type=\"submit\" center value=\"%s\" name=\"T02\">\n",value
        print "</td>"
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function label(value,style,cll)
{
align="align=\""alg[substr(style,1,1)]"\""
type=substr(style,2,1)
if($2=="ZZ") printf "<td id=\"tt\" %s>\n",align
else if($1=="Z3"|| $1=="VL") printf "<td id=\"tt\" %s>\n",align
else printf "<td id=\"%s\" %s>",clr[cll],align
if(type=="T") printf "&nbsp;%s&nbsp;",trail(value)
else printf "&nbsp;%s&nbsp;\n",punto(value,0)
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
}
FNR==1{
print "<!DOCTYPE html>"
print "<html lang=\"en-US\">"
print "<meta charset=\"UTF-8\">"
print "<meta http-equiv=\"Content-Type\" content=\"text/html\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
print "<meta http-equiv=\"refresh\" content=\"15\">"
print "<link rel=\"stylesheet\" href=\"../css/repo.css\">"
print "<style>"
print "#hornear {"
print "  font-size: 15px;"
print "  border-radius: 3px;"
print "  height: 25px;"
print "  width: 350px;"
print "  font-weight: bold;"
print "  color: black;"
print "</style>"

print "<head>"
print "<center>"
print "<header id=\"hornear\" align=\"center\" >"
printf "PRODUCCION y DESPACHO: %s</header>\n","'"$3"'"

	print "<table id=\"customers\">"
	row=2
	tit(1)
next
}
function tit(k)
{
	for (j=1;j<row+1;j++)
	{

		print "<tr>"
		zz=T[j]+1
		if(k==2 && j==2) zz -=4
		for(i=1;i<zz;i++)
		{
			styl[i]=ST[j""i];lab=TI[j""i];csp=CSP[j""i];rsp=RSP[j""i]
			if(k==2)
			{
				if(j==1 && i==1) { print "<td  style=\"background-color: #F2A9F2\"></td>";lab="PRODUCCION";csp=2 }
				if(j==2 && i==1) {  print "<td  style=\"background-color: #F2A9F2\"></td>";i=2}
				if(j==2 && i==2) { lab="HORA";csp=1 }
				if(j==2 && i==3) { lab="OPERARIO";csp=1 }
				if(j==1 && i==zz-1) { lab="TUNDS";csp=1;rsp=2 }
				if (lab=="NETO") lab="" #print "<td  style=\"background-color: #F2A9F2\"></td>"
				labelf(lab,ST[j""i],csp,rsp,k)
			}
			else labelf(lab,ST[j""i],csp,rsp,k)
		}
		print "</tr>"
	}
}
{
	if($1=="Z0") print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"
	if($1=="Z1") {k=2;print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"; tit(2) }
	if($1=="VL") print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"
	print "<tr>"
	for(i=1;i<NF;i++)
	{
		if(i==1 && k==2) print "<td style=\"background-color: #F2A9F2\"></td>"
		else if($i=="VL") print "<td style=\"background-color: #F2A9F2\"></td>"
		else
		{
			rg=$i
			if($i=="z0") exit
			if($i=="Z0") rg=""
			if($i=="Z1") rg=""
			if($i=="Z2") rg=""
			if($i=="ZZ") rg=""
			label(rg,styl[i],i)
		}
	}
	print "</tr>"
}
END {
print "</table>"
print "<script language=\"Javascript\" src=\"../js/hornear.js\"></script>"
}
' RPHEADER/$1 $2
} 2>error/hornear
cat error/hornear
