#!/bin/sh

# disable filename globbing
t=./tmp1/tf
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 `

cliok=./tmp1/cliok
docvt=./tmp1/docvt
cguia=./tmp1/cguia
cfactu=./tmp1/cfactu
#020366^01/09/2022 17:45^5006|pablo muņoz              ^000026^AGUSTIN ROMERO ZUŅIGA                   ^732   ^000002^KILOS ^892.5   ^1      ^10      ^8925      ^
sort -t^ -k6,6 -k2,2 $docvt.f -o $docvt.f
awk -F^ ' BEGIN { file="'"$docvt"'"}
FILENAME==ARGV[1] { corte=$1; next } #print "corte="$1 >"/dev/tty"; next }
FILENAME == ARGV[2] {cliok[$1]=$3"^"$2; 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) }
$4+0==15 { next }
#{print corte,$6,$7+0 }
$6+0 < corte && $7+0==0 {
printf "%10.10s^",$2 >file".fa"
printf "%s^",$4 >file".fa"
printf "%s^",$6 >file".fa"
print "" >file".fa"
clf[$4]=rdat($2)+0
if(ant1!=$6) fcf[$4]=fcf[$4]""trail($6)"-"
ant1=$6
next
}
$6+0 < corte {
printf "%10.10s^",$2 >file".fb"
printf "%s^",$4 >file".fb"
printf "%s^",$6 >file".fb"
printf "%s^",$7 >file".fb"
printf "%s^",$9 >file".fb"
printf "%s^",$11 >file".fb"
printf "%s^",$12 >file".fb"
print "" >file".fb"
faca[$4]=1
if(ant2!=$6) fmes[$4]=fmes[$4]trail($6)"-"
kmes[$4]+=$11
pmes[$4]+=$12
ant2=$6
next 
}
clf[$4]!="" && clf[$4]>=rdat($2)+0 { 
printf "%10.10s^",$2  >file".fc"
printf "%s^",$4 >file".fc"
printf "%s^",$6 >file".fc"
printf "%s^",$7 >file".fc"
printf "%s^",$9 >file".fc"
printf "%s^",$11 >file".fc"
printf "%s^",$12 >file".fc"
print fcf[$4] >file".fc"
faca[$4]=1
if(ant3!=$6) gmes[$4]=gmes[$4]trail($6)"-"
fmes[$4]=fcf[$4]
kmes[$4]+=$11
pmes[$4]+=$12
ant3=$6
next
}
{
printf "%10.10s^",$2 >file".fd"
printf "%s^",$4 >file".fd"
printf "%s^",$6 >file".fd"
printf "%s^",$7 >file".fd"
printf "%s^",$9 >file".fd"
printf "%s^",$11 >file".fd"
printf "%s^",$12 >file".fd"
print ""  >file".fd"
faca[$4]=1
if(ant4!=$6) gper[$4]=gper[$4]trail($6)"-"
kper[$4]+=$11
pper[$4]+=$12
ant4=$6
}
END {

for (i in fmes)
{
	x1=split(gmes[i],a,"-")
	z1=""
	for (j=1;j<x1;j++)
	{
		z1=z1""a[j]" "
		if(!(j%5)) z1=z1"&nbsp;<br>&nbsp;"
	}
	#print "gmes="substr(z1,length(z1)-2) >"/dev/tty"
	if(substr(z1,length(z1)-2)=="sp;") z1=substr(z1,1,length(z1)-17)
	gmes[i]=z1
	#print "gmes="z1
	x1=split(fmes[i],a,"-")
        z1=""
        for (j=1;j<x1;j++)
        {
                z1=z1""a[j]" "
                if(!(j%5)) z1=z1"&nbsp;<br>&nbsp;"
        }
	#print "fmes="substr(z1,length(z1)-2) >"/dev/tty"
	if(substr(z1,length(z1)-2)=="sp;") z1=substr(z1,1,length(z1)-17)
        fmes[i]=z1
        #print "fmes="z1

}
for (i in gper)
{
        x1=split(gper[i],a,"-")
        z1=""
        for (j=1;j<x1;j++)
        {
                z1=z1""a[j]" "
                if(!(j%5)) z1=z1"&nbsp;<br>&nbsp;"
        }
	#print "gper="substr(z1,length(z1)-2) >"/dev/tty"
	if(substr(z1,length(z1)-2)=="sp;") z1=substr(z1,1,length(z1)-17)
        gper[i]=z1
        #print "gper="i,z1 >"/dev/tty"
}

for (i in fmes) if(kmes[i]>0) f1[i]=sprintf ("%s^%s^%s^%.2f^%s^",gmes[i],fmes[i],kmes[i],pmes[i]/kmes[i],pmes[i])
else f1[i]=sprintf ("%s^%s^%s^%.2f^%s^",gmes[i],fmes[i],kmes[i],"sss",pmes[i])
for (i in gper) if(kper[i]>0) f2[i]=sprintf ("%s^%s^%.2f^%s^",gper[i],kper[i],pper[i]/kper[i],pper[i])
else f2[i]=sprintf ("%s^%s^%.2f^%s^",gper[i],kper[i],"ssss",pper[i])
f3="^^^^^"
for (i in faca) if(f1[i]=="") printf "%s^%s%s\n",cliok[i],f3,f2[i] >"'"$docvt.tt"'"
else if(f2[i]=="") printf "%s^%s%s\n",cliok[i],f1[i],f3 >"'"$docvt.tt"'"
else printf "%s^%s%s\n",cliok[i],f1[i],f2[i]  >"'"$docvt.tt"'"

} ' $cguia.c $cliok $docvt.f
sort $docvt.tt -o $docvt.tt
awk -F"^" ' BEGIN { 
T["00"]="CLIENTE^LT"
T["01"]="RUT^LT"
T["02"]="N&deg; GUIAS^LT"
T["03"]="N&deg; FACTURAS^LT"
T["04"]="KILOS^RN1"
T["05"]="PRECIO^RN2"
T["06"]="TOTAL^RN"
T["07"]="N&deg; GUIAS^LT"
T["08"]="KILOS^RN1"
T["09"]="PRECIO^RN2"
T["10"]="TOTAL^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 "%s^",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 "%s^",st[i]
print ""
	printf "TOTAL^"
	for (i=2;i<=tx-1;i++)
		printf "%s^",tt[i]
print ""
} ' $docvt.tt |
awk -F"^" ' BEGIN {ttop("Produccion de Terminado");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
header()
}
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"
# boton($1,al[1])
 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=\"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 header()
{
printf "<tr><td bgcolor=\"%s\" colspan=\"7\"><p align=\"center\"><font color=\"#FFFF00\" face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p></td>","#003399","FACTURADO EN EL PERIODO"
printf "<td bgcolor=\"%s\" colspan=\"7\"><p align=\"center\"><font color=\"#FFFF00\" face=\"Arial\" size=\"4\">&nbsp;%s&nbsp;</font></p></td></tr>","#003399","POR FACTURAR EN EL PERIODO"
#printf "<td bgcolor=\"%s\" colspan=\"4\" align=\"center\">POR FACTURAR</td><tr>","#003399"
}


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=\"center\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",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\" onblur=\"self.close()\">"
#print "<p align=\"center\"><img border=\"0\" src=\"../../logol.jpg\" width=\"200\" height=\"97\"></p>"
printf "<H1>FACTURACION</H1>\n","'"$1"'"
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 "<form method=\"post\" action=\"../\" name=lform>"
printf "<input type=\"submit\" value=\"Continuar\" name=\"B1\">\n"
print "</body>"
print "</html>"
} ' -
