#!/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
tpdoc=tmp/tpdoc
factu=tmp/factu
t=tmp/tt
splash> $t
T1=`awk -F= ' $1=="T1" { print $2 } ' $t `

T2=`ihdm perio h00"^" h01"^" | egrep ^$T1 `
ihdm factu h04"^" h03"^" h00"^" h02"^" h01"^" h05"^" h06"^" h09"^" h10"^" h11"^" h12"^" | sort>$factu

# |  1 H00 folio1 05 15   0 | 2 H01 nrodoc 10 00   5 | 3 H02 tipodo 02 57  15 |
# |  4 H03 fechae 10 08  17 | 5 H04 period 02 56  27 | 6 H05 rutemi 11 00  29 |
# |  7 H06 razons 50 00  40 | 8 H07 tasaim 05 02  90 | 9 H08 cuenta 05 55  95 |
# | 10 H09 mnexen 10 02 100 |11 H10 mnneto 10 02 110 |12 H11 mnivas 10 02 120 |
# | 13 H12 mntota 10 02 130 |14 H13 ivauso 04 02 140 |15 H14 ivamor 10 02 144 |
# | 16 H15 cinano 02 00 154 |17 H16 otroim 02 00 156 |18 H17 tasaim 05 02 158 |
# | 19 H18 mnimpt 10 02 163 |
awk -F"^" ' $1=="'"$T1"'" {
print substr($0,4)
} ' $factu  |sort -t^ -k3,3 -k5,5 -k1,1 -k2,2 | tee tmp/LC |
awk -F"^" ' a=="" { a=$5 ;a1=$6}
a!=$5 { printf "ST^^^^%s^%s^%d^%d^%d^%d^\n",a,a1,a2,a3,a4,a5
a=$5;a1=$6;a2=0;a3=0;a4=0;a5=0 }
{
print
a2+=$7
a3+=$8
a4+=$9
a5+=$10
} END {
 printf "ST^^^^%s^%s^%d^%d^%d^%d^\n",a,a1,a2,a3,a4,a5
} ' - | 
awk -F"^" ' BEGIN { 
T["00"]="FECHA^CT"
T["01"]="DOCUM^CT"
T["02"]="TP^CT"
T["03"]="FACTURA^RT"
T["04"]="R.U.T.^RT"
T["05"]="RAZON SOCIAL^LT"
T["06"]="MONTO&nbsp;<BR>&nbsp;EXENTO^RN"
T["07"]="MONTO&nbsp;<br>NETO^RN"
T["08"]="I.V.A.^RN"
T["09"]="MONTO&nbsp;<br>BRUTO^RN"
T["11"]="CC^CT"
T["12"]="SALDO^RN"
tx=11
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{ mul=1 }
$1=="ST" { print ; next }
$3=="61" { mul= -1 }
{
	for (i=1;i<=tx-1;i++)
	{
		printf "%s^",$i
		tt[i] +=$i*mul
		tp[$3""i] +=$i*mul
	}
printf "\n"
		td[$3]++
}
END {
	while ((getline < "'"$tpdoc"'") > 0) ds[$1]=$2
	for (j in td)
	{
		printf "TOTAL^^%s^^%3d^%s^",j,td[j],ds[j]
		for (i=7;i<=tx-1;i++)
		printf "%s^",tp[j""i]
		tdc +=td[j]
print ""
	}
	printf "TOTAL^^%s^^%s^%s^","TT",tdc,"DOCUMENTOS TOTALES"
	for (i=7;i<=tx-1;i++)
		printf "%s^",tt[i]
print ""
	} ' - | tee ../xls/lcom |
awk -F"^" ' BEGIN {
split("'"$T2"'",a); per=a[2]
ttop("Libro de Compras ..."per);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" || $1=="ST" { for (i=1; i<NF; i++) labelf($i,al[i],tp[i])
print "</tr>"
next
}
$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") if(value <0) printf "<p align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&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],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(value=="0" && 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>"
while ((getline < "./tmp/planc.1") >0 ) if($1=="'"$1"'") split($0,cta)
printf "<H3>Libro de Compras<br>Per&iacute;odo: %s</H3>\n",per
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()
{
if (a = A)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "</body>"
print "</html>"
} ' - 
