#!/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=27/10/2020
HAST=28/10/2020
lote=./tmp/lote
>$lote
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 h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" d00"^" d01"^" d02"^" d03 > $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 | 


produ=./tmp/produ
bodeg=./tmp/bodeg
ihdm bodeg h00"^" h01"^" > $bodeg
vende=./tmp/vende
ihdm vende h00"^" h01"^" h02"^" h03"^" > $vende
opera=./tmp/opera
ihdm opera h00"^" h01"^" h02"^" h03"^" > $opera
mpapp=./tmp/mpapp
ihdm mpapp h00"^" h01"^" h02"^" h04"^" h05"^" h06"^" h07 h08"^" > $mpapp.1
desba=./tmp/desba
ihdm desba h00"^" h01"^" h03"^" h04"^" h05 > $desba.1
mprim=./tmp/mprim
ihdm mprim h00"^" h01"^" h06"^" h07"^" h08"^" h09"^" h10  > $mprim
pterm=./tmp/pterm
./appl/sh/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
codb[$11$8]=1; sti[$11$8"^"substr($3,1,2)]+=$10
;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)}
 {print $1"^"$2"^"trail(substr($3,4))"^"trail(substr($4,6))"^"$6"^"prod[$8]"^"$9"^"$10"^"$11  > "./tmp/T"$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 >'"$prods.B"'"substr(i,1,2)
	}
	 print pd["PT"] >"'"$prods.0"'"
} ' $prods - | sort -t"^" -k2  >$prods.1

# |  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 kilos1  7   01 | 10 D02 bodega  1   57 | 
ihdm prter h00"^" h01"^" h02"^" h03"^" h05"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12 > $prter.1
venta=./tmp/venta
ihdm venta h00"^" h01"^" h03"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05"^" d06"^" d07"^" d08"^" d09"^" d10"^" d11"^" d12"^" d13"^" d14"^" d15  > $venta.0
docvt=./tmp/docvt
./appl/sh/docvt > $docvt

# |  1 H00 fventa 06 00   0 | 2 H01 fechai 16 00   6 | 3 H02 kilom1 06 01  22 |
# |  4 H03 vende  04 51  28 | 5 H04 fechat 16 01  32 | 6 H05 kilom2 06 01  48 |
# |  7 H06 valorg 10 01  54 | 8 H07 gastos 246 11  64 | 9 H08 faltan 10 01 310 |
# | 10 H09 fopago 15 16 320 |11 H10 almace 512 15 335 |12 H11 status 02 55 847 |

# 13 D00 client  10  52 | 14 D01 identi  40  00 | 15 D02 guiads  6   01 | 
# 16 D03 produc  6   53 | 17 D04 unidad  6   00 | 18 D05 precv1  8   01 | 
# 19 D06 tkilos  8   01 | 20 D07 vlrvta  10  01 | 21 D08 pagado  10  15 | 
# 22 D09 frpago  2   54 | 
awk -F"^" ' 
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
FILENAME==ARGV[1] { x[$1]=$2 ; next }
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
{ rd=rdat($2) } 
$1!=ant { r=1 ;ant=$1}
{
        for (i=1;i<=NF;i++)  
        {
                if (i==3) printf "%s^",x[$i]
                else if (i==2) printf "%10.10s^",$i
                else printf "%s^",$i
                if (i==2) printf "%.2d^",r++
        }
        print ""
} ' $vende $venta.0  > $venta.1
>$prter.v
>$prter.e
>$prter.b
>$prter
>$prter.d

awk -F"^" '
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
FILENAME == ARGV[1] { op[$1]=$2 ; next }
FILENAME == ARGV[2] { vd[$1]=$2 ; next }
FILENAME==ARGV[8] { kp=$1 ; next } 
{ rd=rdat($2) } 
rd > hast { next }
rd < desd && FILENAME==ARGV[5] {
if($3=="PV") fac= -1
else fac=1
ik9 +=$6*fac
ik12 +=$7*fac
iken +=$8*fac
ikes +=$9*fac
ikcu +=$10*fac
ikc6 +=$11*fac
next
}
FILENAME == ARGV[3] { 
if ( rd <desd ) 
{
	imp -=$6; isc -=$5
	next 
}
mp +=$6; sc +=$5; lmp[$7+0] +=$6; lsc[$7+0] +=$5 
for (i=1;i<=NF;i++) if(i==3) printf "%s^",op[$i] >"'"$mpapp"'"
else printf "%s^",$i >"'"$mpapp"'"
print "" >"'"$mpapp"'"
}
FILENAME == ARGV[4] && rd >=desd{ pi +=$4; lpi[$5+0] +=$4
for (i=1;i<=NF;i++) if(i==3) printf "%s^",op[$i] >"'"$desba"'"
else printf "%s^",$i >"'"$desba"'"
print "" >"'"$desba"'"
}
FILENAME == ARGV[7] {
	imp +=$6; isc +=$5 ; iml +=$4
	next 
}
FILENAME == ARGV[6] {
if ( rd <desd ) 
{
	imp -=$15; isc -=$14 ; iml -=$13
	next 
}
for (i=1;i<=NF;i++) printf "%s^",$i >"'"$prter.e"'"
printf "%.2f\n",$7+$8+$9+$10+$11+$12 >"'"$prter.e"'"
ek9 +=$7
ek12 +=$8
eken +=$9
ekes +=$10
ekcu +=$11
ekc6 +=$12
emll +=$13
esac +=$14
egra +=$15
evta +=$18
erec +=$19
ddv[$4]=1
for (i=7;i<=20;i++) tte[$4""(i-1)] +=$i
}
FILENAME == ARGV[5]  && $3=="DV" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter.d"'"
else if(i==5) printf "%s^",vd[$i] >"'"$prter.d"'"
else if(i!=3) printf "%s^",$i >"'"$prter.d"'"
printf "%.2f\n",$6+$7+$8+$9+$10+$11 >"'"$prter.d"'"
dk9 +=$6
dk12 +=$7
dken +=$8
dkes +=$9
dkcu +=$10
dkc6 +=$11
ddv[vd[$5]]=1
for (i=6;i<=11;i++) ttd[vd[$5]""i] +=$i
}
FILENAME == ARGV[5]  && $3=="PV" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter.v"'"
else if(i==5) printf "%s^",vd[$i] >"'"$prter.v"'"
else if(i!=3) printf "%s^",$i >"'"$prter.v"'"
printf "%.2f\n",$6+$7+$8+$9+$10+$11 >"'"$prter.v"'"
vk9 +=$6
vk12 +=$7
vken +=$8
vkes +=$9
vkcu +=$10
vkc6 +=$11
ddv[vd[$5]]=1
for (i=6;i<=11;i++) ttv[vd[$5]""i] +=$i
}
FILENAME == ARGV[5]  && $3=="PT" {
for (i=1;i<=NF;i++) if(i==4) printf "%s^",op[$i] >"'"$prter"'"
else if(i!=3 && i!=5) printf "%s^",$i >"'"$prter"'"
printf "%.2f\n",$6+$7+$8+$9+$10+$11 >"'"$prter"'"
k9 +=$6
k12 +=$7
ken +=$8
kes +=$9
kcu +=$10
kc6 +=$11
}
END {
 #kp=k9+k12+kes+ken+kcu+kc6
 dkp=dk9+dk12+dkes+dken+dkcu+dkc6
 vkp=vk9+vk12+vkes+vken+vkcu+vkc6
 ekp=ek9+ek12+ekes+eken+ekcu+ekc6
 ikp=ik9+ik12+ikes+iken+ikcu+ikc6
fk9=ik9+k9+dk9-vk9
fkc6=ikc6+kc6+dkc6-vkc6
fk12=ik12+k12+dk12-vk12
fkes=ikes+kes+dkes-vkes
fken=iken+ken+dken-vken
fkcu=ikcu+kcu+dkcu-vkcu
 fkp=fk9+fk12+fkes+fken+fkcu+fkc6
fml=iml-emll
fsc=isc-esac-sc
fmp=imp-egra-mp
if(mp==0) mp=.001
if(pi==0) pi=.001
if(sc==0) sc=.001
for (i in lmp) zlmp++
 printf "REPMP^CONSUMO DE MP"
if (desd ==hast) for (i=1;i <=zlmp; i++) printf "^%.2f^100^%.2f",lmp[i],lmp[i]/lsc[i]
 printf "^%.2f^100^^%.2f\n",mp,mp/sc
 printf "^DESECHO AL DESBASTE"
if (desd ==hast) for (i=1;i <=zlmp; i++) printf "^%.2f^%.2f^%.2f",lmp[i]-lpi[i],100*(lmp[i]-lpi[i])/lmp[i],(lmp[i]-lpi[i])/lsc[i]
 printf "^%.2f^%.2f^^%.2f\n",mp-pi,100*(mp-pi)/mp,(mp-pi)/sc
 printf "REPPI^PRODUCCION DESBASTE"
if (desd ==hast) for (i=1;i <=zlmp; i++) printf "^%.2f^%.2f^%.2f",lpi[i],100*lpi[i]/lmp[i],lpi[i]/lsc[i]
 printf "^%.2f^%.2f^100^%.2f\n",pi,100*pi/mp,pi/sc
 printf "^DESECHO AL CORTE"
if (desd ==hast) for (i in lmp) printf "^^^"
 printf "^%.2f^%.2f^%.2f^%.2f\n",pi-kp,100*(pi-kp)/mp,100*(pi-kp)/pi,(pi-kp)/sc
 printf "REPPT^PRODUCTO TERMINADO"
if (desd ==hast) for (i in lmp) printf "^^^"
 printf "^%.2f^%.2f^%.2f^%.2f\n",kp,100*kp/mp,100*kp/pi,kp/sc
if (desd ==hast) for (i=1;i <=zlmp; i++) printf "%s\n",i >"'"$lote"'"
if ( a = A)
{
printf "^STOCK INICIAL^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%d^%d^%.2f\n",ik9,ik12,iken,ikes,ikcu,ikc6,ikp,iml,isc,imp >"'"$prter.b"'"
printf "REPPT^PRODUCCION^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%d^%d^%2f\n",k9,k12,ken,kes,kcu,kc6,kp,0,sc,mp >"'"$prter.b"'"
printf "REPPV^ENTREGA A VENTA ^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",vk9,vk12,vken,vkes,vkcu,vkc6,vkp >"'"$prter.b"'"
printf "REPDV^DEVOLUCION VENDEDORES^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",dk9,dk12,dken,dkes,dkcu,dkc6,dkp >"'"$prter.b"'"
printf "REPVT^VENTA^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%d^%d^%.2f^%.0f^%.0f\n",ek9,ek12,eken,ekes,ekcu,ekc6,ekp,emll,esac,egra,evta,erec >"'"$prter.b"'"
ck9=vk9-ek9-dk9
ck12=vk12-ek12-dk12
ckes=vkes-ekes-dkes
ckcu=vkcu-ekcu-dkcu
cken=vken-eken-dken
ckc6=vkc6-ekc6-dkc6
ckp=ck9+ck12+ckes+cken+ckcu+ckc6
if(ck9||ck12||ckes||cken||ckcu||ckc6) printf "^CUADRATURA EN VENTA^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f\n",ck9,ck12,cken,ckes,ckcu,ckc6,ckp >"'"$prter.b"'"
printf "^STOCK FINAL^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%d^%d^%.2f\n",fk9,fk12,fken,fkes,fkcu,fkc6,fkp,fml,fsc,fmp >"'"$prter.b"'"
for (j in ddv) 
{
	t1=0
	printf "^ENT %s^",j >"'"$prter.b"'"
	for (i=6;i<12;i++)
	{
		printf "%.2f^",ttv[j""i] >"'"$prter.b"'"
		t1  +=ttv[j""i]
	}
	printf "%.2f\n",t1 >"'"$prter.b"'"
	t1=0
	printf "^DEV %s^",j >"'"$prter.b"'"
	for (i=6;i<12;i++)
	{
		printf "%.2f^",ttd[j""i] >"'"$prter.b"'"
		t1  +=ttd[j""i]
		cua[j""i] =ttv[j""i]-ttd[j""i]-tte[j""i]
		if (cua[j""i] !=0) cua[j]=1
	}
	printf "%.2f\n",t1 >"'"$prter.b"'"
	t1=0
	printf "^VTA %s^",j >"'"$prter.b"'"
	for (i=6;i<12;i++)
	{
		printf "%.2f^",tte[j""i] >"'"$prter.b"'"
		if(i <12) t1  +=tte[j""i]
		if( i==11)
			printf "%.2f^",t1 >"'"$prter.b"'"
	}
	printf "^^^%.2f^",tte[j""17] >"'"$prter.b"'"
	printf "%.2f^",tte[j""18] >"'"$prter.b"'"
	printf "\n" >"'"$prter.b"'"
	if (cua[j])
	{
		t1=0
		printf "^CUAD %s^",j >"'"$prter.b"'"
		for (i=6;i<12;i++)
		{
			printf "%.2f^",cua[j""i] >"'"$prter.b"'"
			t1  +=cua[j""i]
		}
		printf "%.2f\n",t1 >"'"$prter.b"'"
	}
	print "STT" >"'"$prter.b"'"
}
}
} ' $opera $vende $mpapp.1 $desba.1 $prter.1 $venta.1 $mprim $prods.0 > $produ
sort -t"^" -k6,6 -k1 tmp/TPT -o $prter
#sort -t"^" -k4,4 -k2.6,2.10 -k2.3,2.5 -k2,2.3 $prter.v -o $prter.v
#sort -t"^" -k4,4 -k2.6,2.10 -k2.3,2.5 -k2,2.3 $prter.d -o $prter.d
#sort -t"^" -k4,4 -k2.6,2.10 -k2.3,2.5 -k2,2.3 $prter.e -o $prter.e
awk -F"^" ' BEGIN { 
T["00"]="REPORTE^CT"
T["01"]="CONCEPTO^LT"
tx=2
while ((getline lot < "'"$lote"'" ) >0 )
{
	T[sprintf("%.2d",tx++)]="KILOS&nbsp;<br>L: "lot"^RN2"
	T[sprintf("%.2d",tx++)]="[%]DB&nbsp;<br>L: "lot"^RN2"
	T[sprintf("%.2d",tx++)]="KG/SC&nbsp;<br>L: "lot"^RN2"
}
T[sprintf("%.2d",tx++)]="KILOS&nbsp;<br>TOTAL^RN2"
T[sprintf("%.2d",tx++)]="[%]MP&nbsp;<br>TOTAL^RN2"
T[sprintf("%.2d",tx++)]="[%]DB&nbsp;<br>TOTAL^RN2"
T[sprintf("%.2d",tx++)]="KG/SC&nbsp;<br>TOTAL^RN2"
tx++
	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 {
} ' $produ |
awk -F"^" ' BEGIN {ttop("Analisis de la Produccion");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") >0  { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
index($0,"TOTAL")==0 {
tf=NF
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 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\" name=\"T02\" OnClick=\"produ(this,%c%s al %s%c)\"></p>\n",alg[alig],value,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)
{
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!="TOTAL") if(value=="0.00") value="&nbsp"
if(index($0,"GENERAL") || index($0,"DOCUM")) printf "<td bgcolor=\"%s\">","#003399"
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>"
printf "<H1>PROD: %s al %s</H1>\n","'"$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"
} ' -

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^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("Analisis de la Produccion");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\" name=\"T02\" OnClick=\"produ(this,%c%s al %s%c)\"></p>\n",alg[alig],value,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>"
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
