#!/bin/sh

# disable filename globbing
echo 
{
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin
export APPLDIR DATADIR PATH HOME
DESD=`awk -F= ' $1=="repo1H01" { print $2 } ' $1`
HAST=`awk -F= ' $1=="repo1H02" { print $2 } ' $1`
CLIE=`awk -F= ' $1=="repo1H03" { print $2 } ' $1`
VEND=`awk -F= ' $1=="repo1H04" { print $2 } ' $1`
STAT=`awk -F= ' $1=="repo1H05" { print $2 } ' $1`

wrkor=./tmp/wrkor$$
# |  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 01 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 D00 itemmm  2   00 | 14 D01 itemss  500 12 | 15 D02 textos  2   16 | 
# 16 D03 adicio  500 15 | 17 D04 cantid  6   01 | 
cotiz=./tmp/cotiz
./appl/sh/cotiz > $cotiz

# |  1 H00 filenu 06 01   0 | 2 H01 dateor 10 00   6 | 3 H02 codigo 05 51  16 |
# |  4 H03 cusdat 512 11  21 | 5 H04 obras  07 59 533 | 6 H05 obrdat 256 11 540 |
# |  7 H06 obrass 07 15 796 | 8 H07 condxs 02 55 803 | 9 H08 cndxv1 256 11 805 |
# | 10 H09 observ 256 11 1061 |11 H10 totalo 10 01 1317 |12 H11 deposi 10 01 1327 |
# | 13 H12 balanc 10 01 1337 |14 H13 assoc  03 52 1347 |15 H14 statu  02 53 1350 |

# 16 D00 produc  6   54 | 17 D01 itemss  500 12 | 18 D02 textos  2   16 | 
# 19 D03 adicio  500 15 | 20 D04 costos  10  01 | 21 D05 cantid  6   01 | 
# 22 D06 pricel  10  01 | 23 D07 pricef  10  01 | 24 D08 amount  10  01 | 
# 25 D09 checko  1   17 | 


./appl/sh/wrkor > $wrkor
awk -F"^" '
function fec(ff) {return substr(ff,7)substr(ff,4,2)substr(ff,1,2)}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
BEGIN {
desd=fec("'"$DESD"'")
hast=fec("'"$HAST"'")
clie="'"$CLIE"'"
vend="'"$VEND"'"
stat=trail("'"$STAT"'")
ret=sprintf("%c",13)
x1="1,2,3,6,11,14,15,"
z=split(x1,x,",")
}
FILENAME==ARGV[1] && substr($12,1,2)=="99" { next } # OT NULA
FILENAME==ARGV[1] && ant!=$1 { vot[$3]=vot[$3]$1"|";ot[$3]=ot[$3]$1"&nbsp;<br>&nbsp;"; fot[$3]=fot[$3]$2"&nbsp;<br>&nbsp;";ant=$1 }
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>9) pun=substr(dato,1,l-9)","substr(dato,l-8,3)","substr(dato,l-5,3)","substr(dato,l-2)
	else 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
}
FILENAME==ARGV[1] { val[$1] +=$20*$21;  next }
ott=="" { for (i in ot) { ot[i]=substr(ot[i],1,length(ot[i])-16);fot[i]=substr(fot[i],1,length(fot[i])-16)} 
for (i in vot)
{
	x1=split(vot[i],d,"|")
	for (j=1;j<x1;j++) { vl[i]=vl[i]punto(val[d[j]])"&nbsp;<br>&nbsp;";vt[i] +=val[d[j]]}
	vl[i]=substr(vl[i],1,length(vl[i])-16)}
ott=1 }
fec($2) < desd { next }
fec($2) > hast { next }
clie+0!=0 && clie!=substr($3,1,5) { next }
vend+0!=0 && vend!=substr($14,1,3) { next }
stat !=""  && stat!=substr($15,1,2) { next }

clie+0!=0 && cl=="" {cl=substr($3,7)}
vend+0!=0 && vd=="" {vd=substr($14,4)}
stat+0!=0 && st=="" {st=substr($15,4)}
ant!=$1 { 
for (i=1;i<z;i++)  if(i==4) {
	split(trail($x[i]),az,"|")
	split(az[1],c,ret)
	printf "%s",trail(c[1])
	if(c[4]!="" && c[4]!=" -") printf "<br>&nbsp;%s",c[4]
	printf "^"
}
else
{
	zz=$x[i]
	if(index(zz,"|")) { split(zz,c,"|"); zz=c[2] }
	printf "%s^",substr(zz,1,35)
}
	
print ot[$1]"^"fot[$1]"^"vl[$1]"^"vt[$1]
ant=$1

} ' $wrkor $cotiz > $cotiz.dat

awk -F"^" ' BEGIN { 
T["00"]="# COTIZ^CT"
T["01"]="FECHA^CT"
T["02"]="CLIENTE^LT"
T["03"]="OBRA^LT"
T["04"]="VALOR&nbsp;NETO^RN"
T["05"]="EJECUTIVO^LT"
T["06"]="STATUS^LT"
T["07"]="O.T.^CT"
T["08"]="FECHA OT^CT"
T["09"]="VALOR OT^RT"
tx=10
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
	tt[i] +=$i
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<tx;i++) printf "%.2f^",tt[i]
	printf "%.2f^",tt[i+1]
print ""
} ' $cotiz.dat |
awk -F"^" ' BEGIN {
ttop("Cotizaciones '"$DESD - $HAST"'");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2)
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelt($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" {
tp[NF-1]="N"
 for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
$1!="TOTAL" {
 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="#EEE0EF"
else bgcolor="#FFFFFF"
}
END {
print "</table>"
 #foot()
printf "<script language=\"Javascript\" src=\"../js/vendi.js\"> </script>\n"
 } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"submit\" style=\"width: 80; text-align:%s\" value=\"%s\" name=\"%s\"></p>\n",alg[alig],value,"cotizH00"
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>9) pun=substr(dato,1,l-9)","substr(dato,l-8,3)","substr(dato,l-5,3)","substr(dato,l-2)
	else 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") if(value <0) printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&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],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 labelt(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">&nbsp;<b>%s</b>&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td bgcolor=\"%s\">","#003399"
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=\"#EEE0EF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../logol.png\" width=\"200\" height=\"97\"></p>"
print "<a href=\"../../\"><img border=\"0\" src=\"../../logol.png\" width=\"200\" height=\"97\"></a>"
print "<a href=\"../../xls/Repo1.xls\"><img border=\"0\" src=\"../../images/XLS.png\" width=\"200\" height=\"97\"></a>"
printf "<H3><font face=\"Arial\"><b>COTIZACIONES EMITIDAS: %s</b></H3>\n","'"$DESD al $HAST"'"
print "<form method=\"post\" action=\"../cgi-bin/cotiz.cgi\" name=billform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
print "</table>"
print "<table><tr>"
labels("SALDOS","L")
labels("DEBITOS","R")
labels("CREDITOS","R")
labels("DIFERENCIA","R")
print "</tr>"
print "<tr>"
labels("ANTERIOR","L")
label(cta[4],"R","N")
label(cta[5],"R","N")
label(cta[6],"R","N")
print "</tr>"
print "<tr>"
labels("DEL PERIODO","L")
label(cta[7],"R","N")
label(cta[8],"R","N")
label(cta[9],"R","N")
print "</tr>"
print "<tr>"
labels("ACTUAL","L")
label(cta[10],"R","N")
label(cta[11],"R","N")
label(cta[12],"R","N")
print "</tr>"
print "</center>"
print "</form>"
print "</body>"
print "</html>"
} ' - 
} 2>error/repo1.err
cat error/repo1.err
./repo2 $1
