#!/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
rm -f tmp/*
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=01/11/2020
#HAST=01/11/2020
prodm=./tmp/prodm
./appl/sh/prodm | awk -F"^" ' { print $1"^"$4"^"$12"^"$7 } '  > $prodm
rm -f $prods.M*

bodem=./tmp/bodem
ihdm bodem h00"^" h01"^" > $bodem
mprim=./tmp/mprim
./appl/sh/mprim > $mprim

# |  1 H00 folioo 06 00   0 | 2 H01 fechad 16 00   6 | 3 H02 provee 40 00  22 |
# |  4 H03 telefo 20 00  62 | 5 H04 fopago 15 00  82 | 6 H05 zonapa 33 00  97 |
# |  7 H06 tpdopp 02 54 130 | 8 H07 tipopa 06 51 132 | 9 H08 unidad 02 52 138 |
# | 10 H09 sacosp 06 01 140 |11 H10 kilosp 08 01 146 |12 H11 valork 10 01 154 |
# | 13 H12 bodega 02 53 164 |

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

# |  1 H00 fmpapp 06 00   0 | 2 H01 fechah 16 00   6 | 3 H02 operar 04 51  22 |
# |  4 H03 operar 04 52  26 | 5 H04 tipopp 06 53  30 | 6 H05 sacosp 04 01  36 |
# |  7 H06 kilosp 05 01  40 | 8 H07 lotepr 04 00  45 | 9 H08 bodega 02 54  49 |
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] { prom[$1]=trail($2)"^"trail(substr($3,4)); next } 
FILENAME==ARGV[2] { bodm[$1]=trail($2); next } 
{ rd=rdat(substr($2,1,10))}
rd > hast { next }
FILENAME==ARGV[3] && rd < desd {
if($8+0==0) next
if($13+0==0) next
im=substr($8,1,6)
cod[im]=1
bod=substr($13,1,2)
#print "^"FILENAME" ANT ",bod,im $2"<br>"
stk[im"^"substr($7,1,2)]+=$11;stu[im"^"substr($7,1,2)]+=$10
codb[bod""im]=1; stk[bod""im"^"substr($7,1,2)]+=$11;stu[bod""im"^"substr($7,1,2)]+=$10
next }
FILENAME==ARGV[3] {
if($8+0==0) next
if($13+0==0) next
im=substr($8,1,6)
cod[im]=1
bod=substr($13,1,2)
#print "^"FILENAME" ACT ",bod,im $2"<br>"
 prd[im"^"substr($7,1,2)]+=$11; cod[im]=1 ; prd[bod""im"^"substr($7,1,2)]+=$11; codb[bod""im]=1
 pru[im"^"substr($7,1,2)]+=$10; pru[bod""im"^"substr($7,1,2)]+=$10
 #print $1"^"$2"^"trail(substr($7,4))"^"trail(substr($4,6))"^"trail(substr($6,6))"^"$10"^"$11"^"bodm[bod]"^"im"^"bod  > "./tmp/M"im
 print $1"^"$2"^"trail(substr($7,4))"^^"$6"^"$10"^"$11"^"bodm[bod]"^"im"^"bod  > "./tmp/M"im
 print $1"^"$2"^"trail(substr($7,4))"^^"$6"^"$10"^"$11"^"bodm[bod]"^"im"^"bod  > "./tmp/M"bod""im
next
}
rd < desd {
im=substr($5,1,6)
bod=substr($9,1,2)
cod[im]=1
#print "^"FILENAME" ANT ",bod,im $2"<br>"
stk[im"^PP"]+=$7;stu[im"^PP"]+=$6
codb[bod""im]=1; stk[bod""im"^PP"]+=$7;stu[bod""im"^PP"]+=$6
next }
{
im=substr($5,1,6)
bod=substr($9,1,2)
cod[im]=1
#print"^" FILENAME" ACT ",bod,im $2"<br>"
 prd[im"^PP"]+=$7; cod[im]=1 ; prd[bod""im"^PP"]+=$7; codb[bod""im]=1
 pru[im"^PP"]+=$6; pru[bod""im"^PP"]+=$6
 print $1"^"$2"^MP a PP^"trail(substr($4,6))"^"trail(substr($6,6))"^-"$6"^-"$7"^"bodm[bod]"^"im"^"bod  > "./tmp/M"im
 print $1"^"$2"^MP a PP^"trail(substr($4,6))"^"trail(substr($6,6))"^-"$6"^-"$7"^"bodm[bod]"^"im"^"bod  > "./tmp/M"bod""im
}
END {
	for (i in cod) if (i >0) 
	{
		stki=stk[i"^01"]-stk[i"^PP"]+stk[i"^02"]
		stui=stu[i"^01"]-stu[i"^PP"]+stu[i"^02"]
		print "000000^'"$DESD"' 00:00^STK INICIAL^^^"stui"^"stki"^TODAS^"i  > "./tmp/M"i
		stkf=stki+prd[i"^01"]-prd[i"^PP"]+prd[i"^02"]
		stuf=stui+pru[i"^01"]-pru[i"^PP"]+pru[i"^02"]
		#printf "%s^%s^%.1f^%.1f^%.1f^%.1f^%.1f\n",i,prom[i],stki,prd[i"^01"],prd[i"^PP"],prd[i"^02"],stkf
		printf "%s^%s^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f\n",i,prom[i],stui,stki,pru[i"^01"],prd[i"^01"],pru[i"^PP"],prd[i"^PP"],pru[i"^02"],prd[i"^02"],stuf,stkf
	}
	for (i in codb) if(substr(i,4)+0 >0) 
	{
		stki=stk[i"^01"]-stk[i"^PP"]+stk[i"^02"]
		stui=stu[i"^01"]-stu[i"^PP"]+stu[i"^02"]
		print "000000^'"$DESD"' 00:00^STK INICIAL^^^"stui"^"stki"^"bodm[substr(i,1,2)]"^"substr(i,3)  > "./tmp/M"i
		stkf=stki+prd[i"^01"]-prd[i"^PP"]+prd[i"^02"]
		stuf=stui+pru[i"^01"]-pru[i"^PP"]+pru[i"^02"]
		printf "%s^%s^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f^%.0f^%.1f\n",substr(i,3),prom[substr(i,3)],stui,stki,pru[i"^01"],prd[i"^01"],pru[i"^PP"],prd[i"^PP"],pru[i"^02"],prd[i"^02"],stuf,stkf | "sort -t\"^\" -k2 -o '"$prodm.B"'"substr(i,1,2)
		#printf "%s^%s^%.1f^%.1f^%.1f^%.1f^%.1f\n",substr(i,3),prom[substr(i,3)],stki,prd[i"^01"],prd[i"^PP"],prd[i"^02"],stkf  | "sort -t\"^\" -k2 -o '"$prodm.B"'"substr(i,1,2)
	}
} ' $prodm $bodem $mprim $mpapp  | sort -t"^" -k2  >$prodm.1
for bod in $prodm.1 $prodm.B*
do
BD=`awk -F"^"  ' BEGIN { print substr("'"$bod"'",14);exit } '`
bg=`awk -F"^"  ' $1==substr("'"$bod"'",14) { print $2 } ' $bodem`
[ "$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"]="CANT^RN"
T["04"]="KILOS^RN1"
T["05"]="CANT^RN"
T["06"]="KILOS^RN1"
T["07"]="CANT^RN"
T["08"]="KILOS^RN1"
T["09"]="CANT^RN"
T["10"]="KILOS^RN1"
T["11"]="CANT^RN"
T["12"]="KILOS^RN1"
T["13"]="TOTAL^RN1"
tx=14
	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
		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("Materias Primas");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
lbu[1]="<b>STK INICIAL</b>"
lbu[2]="<b>INGRESOS</b>"
lbu[3]="<b>PRODUCCION</b>"
lbu[4]="<b>OTROS MOVS</b>"
lbu[5]="<b>STK FINAL</b>"
rang=NF/2-1
 labelr("CODIGO","L","T")
 labelr("DESCRIPCION&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;","L","T")
 labelr("UNIDS","L","T")
 for (i=1; i<rang-8 ; i++)  labelu(lbu[i],"C","T")
print "</tr>"
print "<tr>"

 for (i=7; 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<rang ; 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=\"%s\" OnClick=\"produ(this,%c%s al %s%c)\"></p>\n",alg[alig],value,$2,"'"$BD"'",39,"'"$DESD"'","'"$HAST"'"" | '"$bg"'",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 labelr(value,alig,tip)
{
printf "<td rowspan=2 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 labelu(value,alig,tip)
{
printf "<td colspan=2 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 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 MATERIAS PRIMAS"
printf "<font  face=\"Arial,helvetica\" size=\"4\">%s: %s al %s\n","'"$bg"'","'"$DESD"'","'"$HAST"'"
print "<form method=\"post\" action=\"../../cgi-bin/boletbsc.cgi\" id=\"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/produm.js\"> </script>\n"
} ' -
done
} 2>error/produ.err

cat error/produ.err
