#!/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=./tmp1/tf
rm -f ./tmp1/*
splash> $t
DESD=`awk -F= ' $1=="T1" { print substr($2,9,2)"/"substr($2,6,2)"/"substr($2,1,4) } ' $t `
HAST=`awk -F= ' $1=="T2" { print substr($2,9,2)"/"substr($2,6,2)"/"substr($2,1,4) } ' $t `
CLIE=`awk -F= ' $1=="T3" { print $2 } ' $t `
FACT=`awk -F= ' $1=="T4" { print $2 } ' $t `
VEND=`awk -F= ' $1=="T5" { print $2 } ' $t `
prods=./tmp1/prods
cliok=./tmp1/cliok
cfact=./tmp1/cfact
cguia=./tmp1/cguia
docvt=./tmp1/docvt.f
#DESD=01/09/2022
#HAST=30/09/2022
appl/sh/cliok |  awk -F^ '{ print $1"^"$3"^"$2 } ' > $cliok
./ihd/bin/ihmq docvt h00"^ " h01"^" h03"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05"^" d06"^" d07"^" d08"^" |

awk -F^ ' {
for (i=1;i<NF;i++) if(i==2) printf "%10.10s^",trail($i)
else printf "%s^",trail($i)
print ""
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
' -  | 


awk -F"^" ' BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'");clie="'"$CLIE"'" }
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
FILENAME==ARGV[1] { cliok[$1]=$3; next }
$6+0==0 { next }
clie!="" && substr(cliok[substr($4,1,6)],1,lentgh(clie)!=clie { next } 
{ rd=rdat($2)}
rd < desd { next }
rd > hast { next }
{ print } ' $cliok -  >$docvt

awk -F^ ' 
FILENAME==ARGV[1] { cliok[$1]=$3; next }
{c[$1"^"$4"^"$6] +=$10; k[$1"^"$4"^"$6] +=$11;v[$1"^"$4"^"$6] +=$12; dtv[$1"^"$4"^"$6] =$1"^"$2"^"substr($3,6)"^"cliok[substr($4,1,6)]"^"$6+0 } 
END { 
for (i in k) printf "%s^%s^%.1f^%.0f\n", dtv[i],c[i],k[i],v[i]

} ' $cliok $docvt | sort -n -t^ -k5 | awk -F^ ' NR==1 {ant=$5+0;file="'"$cguia"'"; print >file ; next}
$5+0-ant > 30 { file=file".f" }
ant!=$5-1 { print $0"^*" >file ;ant=$5+0; next }
{
ant=$5+0
print>file  
} ' 




# |  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 | 

appl/sh/prods |  awk -F^ '{ print $1"^"$4 } ' > $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 |


# |  1 H00 identi 06 15   0 | 2 H01 codigo 10 00   6 | 3 H02 rutcli 13 00  16 |
# |  4 H03 identi 40 00  29 | 5 H04 girocl 25 00  69 | 6 H05 direcc 40 00  94 |
# |  7 H06 comuna 25 00 134 | 8 H07 ciudad 25 00 159 | 9 H08 telefo 25 00 184 |
# | 10 H09 cortec 20 00 209 |11 H10 atenci 04 55 229 |12 H11 fopago 20 00 233 |
# | 13 H12 contac 30 00 253 |14 H13 cargoe 30 00 283 |15 H14 telefo 30 00 313 |
# | 16 H15 emailc 40 00 343 |

# 17 D00 codigp  6   51 | 18 D01 precio  10  01 | 

awk -F^ '
FILENAME==ARGV[1] { cliok[$1]=$2; alias[$1]=$3; next }
FILENAME==ARGV[2] { prods[$1]=$2; next }
$4+0==15 { next }
{c[$4"^"$7] +=$10; k[$4"^"$7] +=$11;v[$4"^"$7] +=sprintf("%.0f",$12); dtv[$4"^"$7] =alias[substr($4,1,6)]; gdd[$4"^"$6+0]=1 }
END {
for (i in v)
{
	printf "%s^%s^%s^%.0f^%.1f^%.0f",dtv[i],cliok[substr(i,1,6)],prods[substr(i,8)],c[i],k[i],v[i]
	#for(j in gdd) if(substr(i,1,6)==substr(j,1,6)) printf "%s-",substr(j,12)
	print ""
}

} ' $cliok $prods $docvt | sort > $cfact

awk -F"^" '
FILENAME == "'"$cguia"'" {
dc[NR]=1
fac[NR]=$0
x=NR
next
}
FILENAME == "'"$cguia.f"'" {
if(FNR >x) x=FNR
gui[FNR]=$0
dc[FNR]=1
}
END {
for (i=1; i<=x; i++)
printf "%s^^%s\n",fac[i],gui[i]
} ' $cguia $cguia.f > $docvt.tt



for i in $cguia $cguia.f
do
awk -F"^" ' BEGIN { 
T["00"]="FOLIO^CT"
T["01"]="FECHA^CT"
T["02"]="VENDEDOR^LT"
T["03"]="CLIENTE^LT"
if("'"$i"'"=="'"$cguia"'") T["04"]="FACT^RT"
else T["04"]="GUIA^RT"
T["05"]="CANT^RT"
T["06"]="KILOS^RN1"
T["07"]="VALORES^RN"
T["08"]="VS^CT"
tx=10
	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
		st[i] +=$i
	}
printf "\n"
}
END {
	printf "TOTAL^"
	for (i=2;i<=tx-2;i++)
		printf "%s^",tt[i]
print ""
} ' $i |
awk -F"^" ' BEGIN {ttop("Correlativo de F/ y G/");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-4; 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"
if ($(NF-1)=="*" && NR >2 && "'"$CLIE"'"=="") bgcolor="#DD0000"
if (substr($4,1,4)=="F/ G") bgcolor="#DDDD00"
 boton($1,al[1])
 for (i=2; i<NF-1 ; 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=\"submit\" style=\"width: 120; text-align:%s\" value=\"%s\" name=\"docvtH00\"></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>"
if("'"$i"'"=="'"$cguia"'") tti="FACTURAS"
else { tti="GUIAS"; print "<br>"}
printf "<H1><font face=\"Arial\" size=\"5\">CORRELATIVO DE %s - %s</font></H1>\n",tti,"'"$DESD"' AL '"$HAST"'"
print "<form method=\"post\" action=\"../cgi-bin/docvt.cgi\" name=billform>"
print "<center>"
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#999999\" height=\"50\">"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
print "</table>"
print "</center>"
print "</form>"
print "</body>"
print "</html>"
} ' -

done


sh cfactu
