#!/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 `
CLIE=`awk -F= ' $1=="T3" { print $2 } ' $t `
RESU=`awk -F= ' $1=="T4" { print $2 } ' $t `
DESD=28/10/2020
HAST=28/10/2020
prods=./tmp/prodm
./appl/sh/prodm | awk -F"^" ' { print $1"^"$4"^"$12"^"$7 } '  > $prodm
prods=./tmp/prods
./appl/sh/prods | awk -F"^" ' { print $1"^"$4"^"$12"^"$7 } '  > $prods

# |  1 H00 identi 06 15   0 | 2 H01 codbar 15 00   6 | 3 H02 codig  12 00  21 |
# |  4 H03 descrl 40 00  33 | 5 H04 descrC 25 00  73 | 6 H05 grupo  02 51  98 |
# |  7 H06 sgrupo 02 52 100 | 8 H07 impto1 02 53 102 | 9 H08 vimp1  07 00 104 |
# | 10 H09 impto2 02 54 111 |11 H10 vimp2  07 00 113 |12 H11 unidvt 02 55 120 |
# | 13 H12 provee 13 56 122 |14 H13 provee 13 57 135 |15 H14 polit1 03 01 148 |
# | 16 H15 precv1 07 01 151 |17 H16 polit2 03 01 158 |18 H17 precv2 07 01 161 |
# | 19 H18 polit3 03 01 168 |20 H19 precv3 07 01 171 |21 H20 seriad 10 15 178 |
# | 22 H21 stock  05 01 188 |23 H22 costo  10 01 193 |24 H23 minimo 05 01 203 |
# | 25 H24 minimo 05 01 208 |26 H25 status 02 58 213 |
pterm=./tmp/pterm
./appl/sh/pterm > $pterm

# |  1 H00 folprt 06 00   0 | 2 H01 fechat 16 00   6 | 3 H02 tpdocu 02 51  22 |
# |  4 H03 operar 04 52  24 | 5 H04 opera1 04 53  28 | 6 H05 vended 04 54  32 |
# |  7 H06 vende1 04 55  36 |

#  8 D00 prods1  6   56 |  9 D01 cantid  5   01 | 10 D02 kilos1  7   01 | 
# 11 D03 bodega  2   57 | 


bodeg=./tmp/bodeg
ihdm bodeg h00"^" h01"^" > $bodeg

cat $pterm  | 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) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[1] { prod[$1]=trail($2)"^"trail(substr($3,4)); next } 
{ rd=rdat($2)} 
rd < desd {
cod[$8]=1; sti[$8"^"substr($3,1,2)]+=$10;stu[$8"^"substr($3,1,2)]+=$9
codb[$11$8]=1; sti[$11$8"^"substr($3,1,2)]+=$10;stu[$11$8"^"substr($3,1,2)]+=$9
next }
rd > hast { next }
{ pd[substr($3,1,2)]+=$10}
{ prd[$8"^"substr($3,1,2)]+=$10; cod[$8]=1 ; prd[$11$8"^"substr($3,1,2)]+=$10; codb[$11$8]=1}
 {print $1"^"$2"^"trail(substr($3,4))"^"trail(substr($4,6))"^"$6"^"prod[$8]"^"$9"^"$10"^"$11  > "./tmp/T"substr($3,1,2)}
#cod[$8]==1 {cod[$8]=2;print "000000^"$2"^STK INICIAL^^^"$8"^"stu[$8]"^"sti[$8]"^"$11  > "./tmp/T"$8 }
{print $1"^"$2"^"trail(substr($3,4))"^"trail(substr($4,6))"^"$6"^"$8"^"$9"^"$10"^"$11  > "./tmp/T"$8 }
 {print $1"^"$2"^"trail(substr($3,4))"^"trail(substr($4,6))"^"$6"^"$8"^"$9"^"$10"^"$11  > "./tmp/B"$11$8 }
END {
	#for (i in cod)
	#{
		#stki=sti[i"^PT"]-sti[i"^PV"]-sti[i"^DV"]
		#stkf=stki+prd[i"^PT"]-prd[i"^PV"]+prd[i"^DV"]
		#printf "%s^%s^%.1f^%.1f^%.1f^%.1f^%.1f\n",i,prod[i],stki,prd[i"^PT"],prd[i"^PV"],prd[i"^DV"],stkf
	#}
	#for (i in codb)
	#{
		#stki=sti[i"^PT"]-sti[i"^PV"]-sti[i"^DV"]
		#stkf=stki+prd[i"^PT"]-prd[i"^PV"]+prd[i"^DV"]
		#printf "%s^%s^%.1f^%.1f^%.1f^%.1f^%.1f\n",substr(i,3),prod[substr(i,3)],stki,prd[i"^PT"],prd[i"^PV"],prd[i"^DV"],stkf # | "sort -t\"^\" -k2 -o '"$prods.B"'"substr(i,1,2)
	#}
	 #print pd["PT"] >"'"$prods.0"'"
} ' $prods - | sort -t"^" -k2  >$prods.1
exit
for bod in $prods.1 $prods.B*
do
bg=`awk -F"^"  ' $1==substr("'"$bod"'",14) { print $2 } ' $bodeg`
[ "$bg" = "" ] && bg=TOTAL
awk -F"^" ' BEGIN { 
T["00"]="CODIGO^CT"
T["01"]="DESCRIPCION&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^LT"
T["02"]="UNIDAD^LT"
T["03"]="STOCK INICIAL^RN2"
T["04"]="PRODUCCION^RN2"
T["05"]="VENTA ENTR^RN2"
T["06"]="VENTA DEVS^RN2"
T["07"]="STOCK FINAL^RN2"
T["08"]="TOTAL^RN2"
T["09"]="MLL^RN"
T["10"]="SAC^RN"
T["11"]="GRANEL^RN2"
T["12"]="VENTA^RN"
T["13"]="COBRO^RN"
tx=9
	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 {
 printf "TOTAL^"
        for (i=2;i<=tx-1;i++) if (i<4) printf "^"
                else printf "%s^",tt[i]
print ""

} ' $bod |
awk -F"^" ' BEGIN {ttop("Producto Terminado");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") { printf "<tr>\n";for (i=1; i<tf ; i++) labelf($i,al[i],tp[i]); printf "</tr>\n"; 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"
tf=NF
 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\" id=\"%s\" name=\"T02\" OnClick=\"produ(this,%c%s al %s%c)\"></p>\n",alg[alig],value,$2,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,"CUAD")) printf "<td bgcolor=\"%s\">","#AA2255"
else 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!="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,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>"
if ("'"$bg"'"=="TOTAL") 
printf "<H1><font  face=\"Arial,helvetica\" size=\"5\">%s</H1>\n","STOCK DE PRODUCTOS TERMINADOS"
printf "<H1><font  face=\"Arial,helvetica\" size=\"5\">%s: %s al %s</H1>\n","'"$bg"'","'"$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"
} ' -
done
}
# 2>error/produ.err

#cat error/produ.err
