#!/bin/sh

# disable filename globbing
set -f

 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=02/11/2020
#HAST=02/11/2020
prods=./tmp/prods
./appl/sh/prods | awk -F"^" ' { print $1"^"$4"^"$12 } '  > $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 |

produ=./tmp/produ
vende=./tmp/vende
ihdm vende h00"^" h01"^" h02"^" h03"^" > $vende
cliok=./tmp/cliok
ihdm cliok h00"^" h01 > $cliok
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) }
FILENAME==ARGV[1] { prod[$1]=$2"^"substr($3,4); next } 
{ rd=rdat($2)} 
substr($3,1,2)=="PT" { next }
substr($3,1,2)=="AD" { next }
substr($3,1,2)=="AJ" { next }
rd < desd {  next ; cod[$8]=1; sti[$8"^"substr($3,1,2)]+=$10;next }
rd > hast { next }
{
 pu[substr($6,1,4)"^"$8"^"substr($3,1,2)]+=$9
 pd[substr($6,1,4)"^"$8"^"substr($3,1,2)]+=$10
 prd[$8"^"substr($3,1,2)]+=$10
 pru[$8"^"substr($3,1,2)]+=$9
 cod[$8]=1
 print $1"^"$2"^"substr($4,6)"^"substr($6,6)"^"prod[$8]"^"$9"^"$10  > "./tmp/T"substr($3,1,2)}
END {
	for (i in pd) print i"^"pu[i]"^"pd[i]
	for (i in prd) print i"^"pru[i]"^"prd[i] } 
' $prods - >$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 | 

docvt=./tmp/docvt
>$docvt.0
>$docvt.1
>$docvt.xv
>$docvt.pg
./appl/sh/docvt  | awk -F"^" ' BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
FILENAME==ARGV[1] {
# CLIENTES EN cli
cli[$1+0]=trail($2); next } 
FILENAME==ARGV[2] {
#PRODUCTOS + UNIDADES en prod
prod[$1]=trail($2)"^"trail(substr($3,4)); next } 
FILENAME==ARGV[3] && NF==5 {
# ENTREGA A VENTAS
 ev[$1"^"$2]=1; evu[$1"^"$2"^"$3] =$4; evv[$1"^"$2"^"$3] =$5; next } 
FILENAME==ARGV[3] && NF==4 { cod[$1]=1; eut[$1"^"$2]=$3; evt[$1"^"$2]=$4; next }  
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) }

{ rd=rdat($2) }
$16+0==15 { next } 
rd > hast || rd < desd { next }
{
# |  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 H12 efecti 00 15 849 |14 H13 cheque 00 15 849 |15 H14 transf 00 15 849 |

# 16 D00 client  6   52 | 17 D01 identi  40  00 | 18 D02 guiads  6   01 | 
# 19 D03 produc  6   53 | 20 D04 unidad  6   00 | 21 D05 precv1  8   01 | 
# 22 D06 cantid  7   01 | 23 D07 tkilos  8   01 | 24 D08 vlrvta  10  01 | 
# 25 D09 pagado  10  01 | 26 D10 frpago  2   54 | 
printf "%s\n",$1"^"$2"^"trail(substr($4,6))"^"cli[$16+0]"^"$18"^"prod[$19]"^"$22+0"^"$23+0"^"$24+0"^"$25"^"$26"^"$24-$25 >"'"$docvt.xv"'"
rr=substr($4,1,4)
ven[rr]=trail(substr($4,6))
ev[rr"^"$19]=1 
unv[rr"^"$19] +=$22 
kgv[rr"^"$19] +=$23 
vlv[rr"^"$19] +=$24 
crv[rr"^"$19] +=$25 
cod[$19]=1
unt[$19] +=$22 
kgt[$19] +=$23 
vlt[$19] +=$24 
crt[$19] +=$25 
}
trail($11)!="|" && ant!=$1 { 
	j=split($11,pg,"|")
	for (i=5;i<j; i+=5) pgt[rr] +=pg[i]
	pt +=pgt[rr]
	print  rr"^"pgt[rr] >"'"$docvt.pg"'"
}
{
ant=$1

}
END {
print pt >"'"$docvt.pg"'"
	#for (i in ev) print substr(i,1,4)"^"ven[substr(i,1,4)]"^"prod[substr(i,6)]"^"evu[i"^PV"]"^"kgv[i]"^"evv[i"^DV"]"^"evv[i"^PV"]-kgv[i]-evv[i"^DV"]"^"vlv[i]"^"crv[i]"^"vlv[i]-crv[i] >"'"$docvt.1"'"
	for (i in ev) print substr(i,1,4)"^"ven[substr(i,1,4)]"^"prod[substr(i,6)]"^"evv[i"^PV"]"^"kgv[i]"^"evv[i"^DV"]"^"evv[i"^PV"]-kgv[i]-evv[i"^DV"]"^"vlv[i]"^"crv[i]"^"vlv[i]-crv[i] >"'"$docvt.1"'"
	for (i in cod)
		 print i"^"prod[i]"^"evt[i"^PV"]"^"kgt[i]"^"evt[i"^DV"]"^"evt[i"^PV"]-kgt[i]-evt[i"^DV"]"^"vlt[i]"^"crt[i]"^"vlt[i]-crt[i] >"'"$docvt.0"'"
}
 ' $cliok $prods $prods.1 - 
sort -t"^" -k2 $docvt.0 -o $docvt.0
sort $docvt.1 -o $docvt.1
echo "<center><H1><font  face=\"Arial,helvetica\" size=\"5\">ANALISIS DE LA VENTA</H1>"
awk -F"^" ' BEGIN { 
T["00"]="COD^CT"
T["01"]="DESCRIPCION^LT"
T["02"]="UNIDS^LT"
T["03"]="ENTREGA^RN1"
T["04"]="VENTA^RN1"
T["05"]="DEVS^RN1"
T["06"]="CUADR^RN1"
T["07"]="VENTA^RN"
T["08"]="CONTADO^RN"
T["09"]="CREDITO^RN"
tx=11
	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
	}
printf "\n"
ant=$6
}
END {
	printf "TOTAL^"
	for (i=2;i<=tx-1;i++)
		printf "%.1f^",tt[i]
print ""
} ' $docvt.0 |
awk -F"^" ' BEGIN {ttop("An&aacute;lisis de la Venta");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)
		if(i>6 && i<13) boton($i,al[(i+1)/2],i)
		else labelf($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" { for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
$1!="TOTAL" {
 for (i=1; i<NF ; i++) label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function boton(value,alig,ind)
{
	print "<td bgcolor=\"#003399\">"
	printf "<p><input type=\"button\" style=\"width: 100; text-align:center\" value=\"%s\" name=\"T%.2d\" OnClick=\"anven(this,%c%s al %s%c)\"></p>\n",value,ind,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\" 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 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=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
printf "<font  face=\"Arial,helvetica\" size=\"5\"><b>PRODUCTOS: %s</b>\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>"
printf "<script language=\"Javascript\" src=\"../js/anven.js\"> </script>\n"
print "</html>"
} ' -
awk -F"^" ' BEGIN { 
T["00"]="CODIG^CT"
T["01"]="VENDEDOR^LT"
T["02"]="PRODUCTO^LT"
T["03"]="UNIDS^LT"
T["04"]="ENTREGA^RN1"
T["05"]="VENTA^RN1"
T["06"]="DEVS^RN1"
T["07"]="CUAD^RN1"
T["08"]="VENTA TT^RN"
T["09"]="CONTADO^RN"
T["10"]="CREDITO^RN"
tx=12
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
ant!= $1 && NR >1 {
	printf "TOTAL^"
	for (i=2;i<=tx-1;i++)
	{
		printf "%.1f^",st[i]
		st[i]=0
	}
print ""
}
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
		st[i] +=$i
	}
printf "\n"
ant=$1
}
END {
	printf "TOTAL^"
	for (i=2;i<=tx-1;i++) printf "%.1f^",st[i]
print ""
	printf "TOTAL^"
	for (i=2;i<=tx-1;i++) printf "%.1f^",tt[i]
print ""
} ' $docvt.1 |
awk -F"^" ' BEGIN {ttop("An&aacute;lisis de la Venta");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
}
$1=="TOTAL" { for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
$1!="TOTAL" {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 for (i=1; i<NF ; i++) label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"button\" style=\"width: 120; text-align:%s\" value=\"&nbsp;%s&nbsp;\" name=\"T02\"></p>\n",alg[alig],value
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\" 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 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=\"#D0D0DF\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
printf "<font  face=\"Arial,helvetica\" size=\"5\"><b>VENDEDORES: %s</b>\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>"
printf "<script language=\"Javascript\" src=\"../js/anven.js\"> </script>\n"
print "</html>"
} ' -
