#!/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.prod/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
rm -f tmp.prod/*
prods=./tmp.prod/prods
appl/sh/prods  > $prods

prodm=./tmp.prod/prodm
appl/sh/prodm  > $prodm

# |  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 15 102 | 9 H08 vimp1  07 15 104 |
# | 10 H09 impto2 02 15 111 |11 H10 vimp2  07 15 113 |12 H11 unidvt 02 55 120 |
# | 13 H12 provee 13 56 122 |14 H13 provee 13 57 135 |15 H14 polit1 03 15 148 |
# | 16 H15 precv1 07 01 151 |17 H16 polit2 03 15 158 |18 H17 precv2 07 15 161 |
# | 19 H18 polit3 03 15 168 |20 H19 precv3 07 15 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 |

sgrup=./tmp.prod/sgrup
appl/sh/sgrup  > $sgrup

vende=./tmp.prod/vende
appl/sh/vende  > $vende

opera=./tmp.prod/opera
appl/sh/opera  > $opera

mpapp=./tmp.prod/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 |


pterm=./tmp.prod/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 | 

desba=./tmp.prod/desba
appl/sh/desba > $desba

# |  1 H00 fdesba 06 00   0 | 2 H01 fechad 16 00   6 | 3 H02 operar 04 55  22 |
# |  4 H03 operar 04 56  26 | 5 H04 produc 02 51  30 | 6 H05 kilosp 06 01  32 |
# |  7 H06 lotepr 04 00  38 |

mprim=./tmp.prod/mprim
appl/sh/mprim > $mprim

pterm=./tmp.prod/pterm
appl/sh/pterm > $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] { prodm[$1]=trail($7);prom[$1]=trail($4); next } 
FILENAME==ARGV[2] { prods[$1]=trail($7);pros[$1]=trail($4); next } 
{ rd=rdat($2)}
rd < desd { next }
rd > hast { next }
FILENAME==ARGV[3] {
 c=substr($5,1,6)
 g=substr(prodm[c],1,2)
 itemp[g]=1
 lote[trail($8)]=1
 file="./tmp.prod/MPAPP"
 mpapp[c] +=$7;mpappl[c"^"trail($8)] +=$7
 mpt[g]+=$7;mpc[g]+=$6; mptl[g"^"trail($8)]+=$7;mpcl[g"^"trail($8)]+=$6
 print $1"^"$2"^"substr($3,6)"^"substr($5,8)"^"$6"^"$7"^"$8 > file""g
 next }

FILENAME==ARGV[4] {
 c=substr($5,1,2)
 itemp[g]=1
 lote[trail($7)]=1
 file="./tmp.prod/DESBT"
 desba[c] +=$6;desbal[c"^"trail($7)] +=$6
 print $1"^"$2"^"substr($3,6)"^"substr($5,4)"^^"$6"^"$7 > file""c
 next }
FILENAME==ARGV[5] && substr($3,1,2)=="PT" { 
 c=substr($8,1,6)
 g=substr(prods[c],1,2)
 pterm[c] +=$10; ptec[c]+=$9;ptet[g] +=$10
 file="./tmp.prod/PTERM"
 print $1"^"$2"^"substr($4,6)"^"pros[c]"^"$9"^"$10  > file""g
 next}
END {
file="tmp.prod/PT"
for (i in pterm) print i"^"pros[i]"^"ptec[i]"^"pterm[i] >file""substr(prods[i],1,2)
for (i in itemp)
{
	file="tmp.prod/PRD"i
	printf "CODIGO^CT^CONCEPTO^LT^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j])
	{
		printf "&nbsp;KILOS&nbsp;<br>&nbsp;LOTE:%s^RN1^",j > file
		printf "&nbsp;[%%]DB&nbsp;<br>&nbsp;LOTE:%s^RN2^",j > file
		printf "&nbsp;KG/SC&nbsp;<br>&nbsp;LOTE:%s^RN2^",j > file
	}
	printf "&nbsp;KILOS&nbsp;<br>&nbsp;TOTAL^RN1^" > file
	printf "&nbsp;[%%]MP&nbsp;<br>&nbsp;TOTAL^RN2^" > file
	printf "&nbsp;[%%]DB&nbsp;<br>&nbsp;TOTAL^RN2^" > file
	printf "&nbsp;KG/SC&nbsp;<br>&nbsp;TOTAL^RN2^" > file
	print "" > file

	printf "MPAPP^CONSUMO MP^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j])
	{
		printf "%s^",mptl[i"^"j] > file
		printf "100.00%%^" > file
		printf "%.2f^",mptl[i"^"j]/mpcl[i"^"j] > file
	}
	printf "%s^",mpt[i] > file
	printf "100.00%%^^" > file
	printf "%.2f^",mpt[i]/mpc[i] > file
	print "^" > file
	

	printf "^DESECHO DESBASTE^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j])
	{
		printf "%s^",mptl[i"^"j]-desbal[i"^"j] > file
		printf "%.2f^",100*(mptl[i"^"j]-desbal[i"^"j])/mptl[i"^"j] > file
		printf "%.2f^",(mptl[i"^"j]-desbal[i"^"j])/mpcl[i"^"j] > file
	}
	printf "%.1f^",mpt[i]-desba[i] > file
	printf "%.2f^^",100*(mpt[i]-desba[i])/mpt[i] > file
	printf "%.2f^",(mpt[i]-desba[i])/mpc[i] > file
	print "^" > file

	printf "DESBT^PRODUCCION DESBASTE^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j]) 
	{
		printf "%s^",desbal[i"^"j] > file
		printf "%.2f%%^",100*desbal[i"^"j]/mptl[i"^"j] > file
		printf "%.2f%%^",desbal[i"^"j]/mpcl[i"^"j] > file
	}
	printf "%.1f^",desba[i] > file
	printf "%.2f^100^",100*desba[i]/mpt[i] > file
	printf "%.2f^",desba[i]/mpc[i] > file
	print "^" > file

	printf "^DESECHO CORTE^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j]) printf "^^^" > file
	printf "%.1f^",desba[i]-ptet[i] > file
	printf "%.2f^",100*(desba[i]-ptet[i])/mpt[i] > file
	printf "%.1f^",100*(desba[i]-ptet[i])/desba[i] > file
	printf "%.2f^",(desba[i]-ptet[i])/mpc[i] > file
	print "^" > file

	printf "PTERM^PRODUCTO TERMINADO^" > file
	for (j in lote) if(desbal[i"^"j]||mptl[i"^"j]) printf "^^^" > file
	printf "%.1f^",ptet[i] > file
	printf "%.2f^",100*ptet[i]/mpt[i] > file
	printf "%.1f^",100*(1-(desba[i]-ptet[i])/desba[i]) > file
	printf "%.2f^",ptet[i]/mpc[i] > file
	print "^" > file
}
printf "<center><H1><font  face=\"Arial,helvetica\" size=\"6\">%s</H1>\n","ANALISIS DE LA PRODUCCION"

}

' $prodm $prods $mpapp $desba $pterm

F=./tmp.prod/PRD
for i in $F""*
do
sg=`awk -F"^" ' $1==substr("'"$i"'",15) { print $2 } ' $sgrup`
inx=`awk ' BEGIN { print substr("'"$i"'",15) } '`
pro=./tmp.prod/PT$inx
cat $i |awk -F"^" '  BEGIN {
ttop("Proceso de Producci&oacute;n");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
rang=NF/2
 for (i=1; i<NF; 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
i=1
 if(NR%2==0) boton($i,al[i]) 
else label($i,al[i],tp[i])
 for (i=2; 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: 180; text-align:%s\" value=\"%s\" id=\"%s\" name=\"%s\" OnClick=\"produ(this,%c%s al %s%c)\"></p>\n",alg[alig],value,substr($1,1,2),"'"$inx"'",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 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,2,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 "<b><font  face=\"Arial,helvetica\" size=\"5\">%s: %s al %s</b>\n","'"$sg"'","'"$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/prodc.js\"> </script>\n"
} ' -
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"]="CANT^RN"
T["03"]="KILOS^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=5
	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=3;i<=tx-1;i++) printf "%s^",tt[i]
print ""

} ' $pro |
awk -F"^" ' BEGIN {ttop("Producto Terminado");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
rang=NF/2-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<rang ; i++) 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 labelf(value,alig,tip)
{
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>tmp.prod/error
cat tmp.prod/error
