#!/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
otreport=./tmp/otreport
disen=./tmp/disen
./appl/sh/disen > $disen

# |  1 H00 codigo 02 00   0 | 2 H01 descri 15 00   2 |

tipoe=./tmp/tipoe
./appl/sh/tipoe  > $tipoe

# |  1 H00 codigo 02 01   0 | 2 H01 descri 25 00   2 |


wrkor=./tmp/wrkor
./appl/sh/wrkor> $wrkor
FECI=01/09/2022
FECF=30/09/2022
awk -F^ ' BEGIN { feci=fec("'"$FECI"'");fecf=fec("'"$FECF"'") }
function fec(ff) {return substr(ff,7)substr(ff,4,2)substr(ff,1,2)}
FILENAME==ARGV[1] { dise[$1]=trail($2); next}
FILENAME==ARGV[2] { tipe[$1]=trail(substr($2,4)); next}
substr($12,1,2)=="99" { next } 
fec($2) > fecf { next }
fec($2) < feci { next }
ot[$1]!="" { next }
ot[$1]=="" { ot[$1]=1 }
{
printf "%s^",$1
printf "%s^",$2
printf "%s^",$3
printf "%s^",$4
x=split($5,a,"
")
printf "%s^",a[1]
x=split($6,a,"
")
printf "%s^",a[1]
printf "%s^",$9
printf "%s^",trail(substr($12,7))
printf "%s^",trail($7)
x=split($15,a,"|")
printf "%s^",tipe[a[5]]
x=split($13,a,"|")
printf "%s^",dise[a[1]]

print ""
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
END { 
} ' $disen $tipoe $wrkor > $otreport

# |  1 H00 filenu 06 01   0 | 2 H01 dateor 10 00   6 | 3 H02 cotiza 06 01  16 |
# |  4 H03 fechco 10 00  22 | 5 H04 cusdat 512 11  32 | 6 H05 obrdat 256 11 544 |
# |  7 H06 observ 256 11 800 | 8 H07 ordenc 20 00 1056 | 9 H08 fechae 10 00 1076 |
# | 10 H09 assoc  03 52 1086 |11 H10 respon 03 54 1089 |12 H11 statu  02 53 1092 |
# | 13 H12 diseno 500 15 1094 |14 H13 produc 500 15 1594 |15 H14 entreg 500 15 2094 |

# 16 D00 itemmm  2   01 | 17 D01 itemss  500 12 | 18 D02 textos  2   16 | 
# 19 D03 adicio  500 15 | 20 D04 cantid  6   01 | 21 D05 precio  10  15 | 
# 22 D06 bcost   10  15 | 23 D07 gcost   10  15 | 24 D08 mcost   10  15 | 
# 25 D09 pcost   10  15 | 
} > error/rpwrkor
cat error/rpwrkor
set otreport tmp/otreport
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
	next
}
function labelf(value,style,colspan,rowspan)
{
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"
if(type=="N") printf "<align=\"%s\">&nbsp;%s&nbsp;</font>\n",alg[alig],punto(value,substr(tip,2))
else 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)
{
align="align=\""alg[substr(style,1,1)]"\""
type=substr(style,2,1)
printf "<td %s>",align,sty
if(type=="T") printf "&nbsp;%s&nbsp;",trail(value)
else printf "&nbsp;%s&nbsp;\n",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
}
FNR==1{
print "<!DOCTYPE html>"
print "<html lang=\"en-US\">"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
print "<link rel=\"stylesheet\" href=\"../css/repo.css\">"
print "<center>"
print "<header id=\"titulo\" align=\"center\" >"
printf "REPORTE DE OT ENTRE EL %s AL %s</header>\n","'"$FECI"'","'"$FECF"'"



	print "<table id=\"customers\">"
	for (j=1;j<2;j++)
	{
		print "<tr>"
		for(i=1;i<T[j]+1;i++) { labelf(TI[j""i],ST[j""i],CSP[j""i],RSP[j""i])}
		print "</tr>"
	}
next
}
{
	print "<tr>"
	for(i=1;i<NF;i++)
	{
		rg=$i
		label(rg,styl[i])
	}
	print "</tr>"
}
	
' RPHEADER/$1 $2
