#!/bin/sh

# disable filename globbing
echo
{
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin
export APPLDIR DATADIR PATH HOME
DESD=`awk -F= ' $1=="repofH01" { print $2 } ' $1`
HAST=`awk -F= ' $1=="repofH02" { print $2 } ' $1`
CLIE=`awk -F= ' $1=="repofH03" { print $2 } ' $1`
VEND=`awk -F= ' $1=="repofH03" { print $2 } ' $1`
STAT=`awk -F= ' $1=="repofH04" { print $2 } ' $1`
EXCE=`awk -F= ' $1=="repofH05" { print $2 } ' $1`


TZ=America/Santiago
T=./tmp/T$$
#DESD=`date +%d/%m/%Y`
STX=`./appl/sh/statf | awk -F^ ' "'"$STAT"'" == $1 { print $2 } ' - `
[ "$STX" = "" ] && STX="TODAS"
ctact=./tmp/ctact
./appl/sh/ctact > $ctact

# |  1 H00 intern 06 15   0 | 2 H01 factur 06 01   6 | 3 H02 fechaf 10 00  12 |
# |  4 H03 montof 10 01  22 | 5 H04 cotiza 06 01  32 | 6 H05 fechac 10 00  38 |
# |  7 H06 montoc 10 01  48 | 8 H07 wrorde 06 01  58 | 9 H08 fechao 10 00  64 |
# | 10 H09 montoo 10 01  74 |11 H10 rutcli 13 00  84 |12 H11 razons 50 00  97 |
# | 13 H12 infocl 512 11 147 |14 H13 gestio 512 11 659 |15 H14 fechae 10 00 1171 |
# | 16 H15 fechap 10 00 1181 |17 H16 assocc 03 51 1191 |18 H17 status 02 52 1194 |


awk -F^ ' BEGIN {
STT="'"$STAT"'"
STX="'"$STX"'"
desd=fec("'"$DESD"'")
hast=fec("'"$HAST"'")
clie="'"$CLIE"'"
vend="'"$VEND"'"
}
vend!="" && vend!=substr($17,1,3) { next }
fec($15) < desd { next }
fec($15) > hast { next }
function fec(ff) {return substr(ff,7)substr(ff,4,2)substr(ff,1,2)}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }

STT!="" && STT!= " " && STT!=substr($18,1,2) { next }
{ print $1"^"$2"^"$3"^"$4"^"trail($11)"^"trail($12)"^"trail($15)"^"trail($16)"^"trail(substr($17,5))  }
' $ctact | sort -t^ -k7.6,7 -k7.3,7 -k7  >$ctact.1
awk -F^ ' BEGIN {
printf "%s;","FACTURA"
printf "%s;","FCH_EMIS"
printf "%s;","MONTO"
printf "%s;","RUT"
printf "%s;","RAZ_SOC"
printf "%s;","FEC_VENC"
printf "%s;","FEC_PAGO"
printf "%s\n","RESPONSABLE"

}
{
printf "%s;",$2
printf "%s;",$3
printf "%s;",$4
printf "%s;",$5
printf "%s;",$6
printf "%s;",$7
printf "%s;",$8
printf "%s;",$9
printf "%s\n",$10
} ' $ctact.1 > ../../xls/es/factu.csv

if [ "$EXCE" = SI ]
then
	cat ../fact1
	exit
fi


awk -F"^" ' BEGIN { 
T["00"]="ID^CT"
T["01"]=" FACT^RT"
T["02"]="FECHA^CT"
T["03"]="MONTO^RN"
T["04"]="RUT^RT"
T["05"]="RAZON SOCIAL^LT"
T["06"]="VENCMTO^CT"
T["07"]="F.E.P.^CT"
T["08"]="RESPONSABLE^LT"
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
		tt0[i] +=$i
	}
printf "\n"
}
END {
        printf "TOTAL^"
        for (i=2;i<=tx;i++)
                printf "%.2f^",tt[i]
print ""

} ' $ctact.1 |
awk -F"^" ' BEGIN {ttop("Facturas de Clientes");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  {
 labelf($1,al[i],tp[i])
 for (i=2; i<NF-1; i++) if(tp[i]=="N") labelf($i,al[i],tp[i])
else labelf("",al[i],tp[i])
print "</tr>"
}
index($0,"TOTAL")==0 {
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.value,%c%s al %s%c)\"></p>\n",alg[alig],value,39,"'"$DESD"'","'"$HAST"'",39
printf "<p><input type=\"submit\" style=\"width: 120; text-align:%s\" value=\"%s\" name=\"ctactH00\"></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>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else 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") if(value <0) printf "<p align=\"%s\"><font face=\"Arial\" size=\"2\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"2\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font face=\"Arial\" size=\"2\">&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\" size=\"2\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">&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.png\" width=\"200\" height=\"97\"></p>"
printf "<H2>Facturas de Clientes del %s al %s en Estado: %s</H1>\n","'"$DESD"'","'"$HAST"'","'"$STX"'"
print "<form method=\"post\" action=\"../../cgi-bin/ctact.cgi\" name=billform>"
print "</H2><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 "<b>"
print NR-2" Registros informados<br>Emitido: "
dd < system("date +%Y-%m-%dT%T")
print dd
print "</center>"

print "</form>"
print "</body>"
print "</html>"
printf "<script language=\"Javascript\" src=\"../js/mayor.js\"> </script>\n"
} ' -

} 2>error/mayor.err
#cat error/regas.err
