#!/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
if [ A = A ]
then
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 `
VEND=`awk -F= ' $1=="T5" { print $2 } ' $t `
SALD=`awk -F= ' $1=="T6" { print $2 } ' $t `
FPAG=`awk -F= ' $1=="T7" { print $2 } ' $t `
fi

cliok=./tmp/cliok
ihdm cliok h00"^" h01 > $cliok

venta=./tmp/venta
vent=./tmp/vent
docvt=./tmp/docvt
./appl/sh/docvt >$docvt

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

prods=./tmp/prods
./appl/sh/prods > $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 |

formp=./tmp/formp
./appl/sh/formp > $formp

# |  1 H00 codigo 02 00   0 | 2 H01 identi 20 00   2 |

awk -F"^" '
FILENAME==ARGV[1] { pr[$1]=$4; next }
FILENAME==ARGV[2] { fp[$1]=$2; next }
FILENAME==ARGV[3] { cli[$1+0]=trail($2); next }
ant!=$1 {jj=1;ant=$1}
{ 
x="13,14,1,2,4,15,16,17,19,18,20,21,22"
z=split(x,a,",")
for (i=1;i<=z;i++) if(i==4||i==5)
{
	z1=$(a[i])
	if(i==1) { z1=cli[b[1]+0] }
	if(i==5) { split(z1,b,"|"); z1=b[2] }
	split(z1,b," ")
	printf "%s^",trail(b[1])
}
else if(i==7) printf "%s^",trail(pr[$(a[i])])
else if(i==3) printf "%s^%.2d^",trail($(a[i])),jj++
else if(i==2) printf "%20.20s^",trail($(a[i]))
else if(i==1) printf "%s^",cli[$(a[i])+0]
else printf "%s^",trail($(a[i]))

print""
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }

 ' $prods $formp $cliok $docvt > $vent

SALD=1
awk -F"^" '
FILENAME==ARGV[1] { cli[$1+0]=trail($2); next }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
{x=split(trail($11),a,"|") }
length(a[2])&& $1!=ant {
for (i=2;i<x;i+=5)
if(a[i]!="") {

        split($4,b,"|")
        split(b[2],c," ")

printf "%s^",cli[a[i]+0]
printf "%-20.20s^",a[i+1]
printf "%s^",$1
printf "%.2d^%10.10s^%s^",i/5 +1,$2,c[1]
printf "%s^ABONO A CTACTE^PESOS^^^^",a[i+2]
printf "%d^",a[i+3]
printf "%s^",a[i+4]
print ""
}

ant=$1
} ' $cliok $docvt >> $vent
>$venta.0
#sort -t"^" -k1,1 -k5.6,5  -k5.3,5 -k5,5 $vent -o $vent
awk  -F"^" '
function rdat(rec) { return substr(rec,7,4)""substr(rec,4,2)""substr(rec,1,2) }
BEGIN { desd=rdat("'"$DESD"'");hast=rdat("'"$HAST"'") }
"'"$CLIE"'"!="" && index($1,"'"$CLIE"'")==0 { next }
"'"$VEND"'"!="" && index($6,"'"$VEND"'")==0 { next }
"'"$FPAG"'"!="" && index($14,"'"$FPAG"'")==0 { next }
{ rd=rdat($5) }
#{ print $1,"'"$CLIE"',$6,"'"$VEND"'<br>"}<br>"}
#{ print rd, desd "<br>" ; print "<br>" } 
rd > hast { next }
rd < desd { ent[$1] +=$12 ;rec[$1]+=$13 ;next }
{ print }
END { lin="000000^00^"sprintf("%.2d%s",substr(desd,7)-1,substr("'"$DESD"'",3))"^^^SALDO ANT^^"
 if ("'"$SALD"'"!="") for (i in ent) if(ent[i]!=rec[i]) printf "%s^^%s^^%s^%s^SL\n",i,lin,ent[i],rec[i] >"'"$venta.0"'"
}
' $vent > $venta.2
if [ a = A ]
then
cat $venta.2 $venta.0 |
awk -F"^" '
{  for (i=1;i<=NF;i++) if (i!=5)
		{
			if(i==1) printf "%s^",$5
			printf "%s^",$i
		}
		printf "\n"
} ' -  | sort -t"^" -k1,1 -k4.6,4  -k4.3,4 -k4,4 > $venta
#sort -t"^" -k5,6 -k3.7 -k3.4 -k3.1  $venta $venta.0 -o $venta
fi 
sort -t"^" -k1,1 -k5.7,5.3 -k5.4,5.6 -k5.1,6  $venta.2 $venta.0 -o $venta
#awk ' { print $0 "<br>" } ' $venta
awk -F"^" ' BEGIN { 
T["00"]="CLIENTE^LT"
T["01"]="REFERENCIA^LT"
T["02"]="VENTA^CT"
T["03"]="LI^CT"
T["04"]="FECHA^CT"
T["05"]="VENDEDOR^LT"
T["06"]="DOCUM^CT"
T["07"]="PRODUCTO^LT"
T["08"]="UNIDS^LT"
T["09"]="CANT^RN"
T["10"]="PRECIO^RN"
T["11"]="VALOR^RN"
T["12"]="PAGADO^RN"
T["13"]="FP^CT"
T["14"]="SALDO^RN"
tx=16
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
ant!=$1 { if (ant!="") totalp(ant)
	for (i=1;i<tx;i++) tt0[i]=0
l1 +=ll
ll=0
sl1 +=sl
sl=0
ant=$1 }
{
	if($9=="PESOS") vl= 1
	else vl=1
	for (i=1;i<tx-1;i++)
	{
		if ($3=="00")
			if(i==17 || i==18) printf "^"
			else printf "%s^",$i
		else
		{
			printf "%s^",$i
			if (vl==1)
			{
				tt[i] +=$i
				tt0[i] +=$i
			}
		}
	}
	sl +=$12-$13
ll++
printf "%.0f^\n",sl
}
function totalp(cli)
{
	printf "%s^%d^^^TOTAL^",cli,ll
	tt0[6]=0
	tt0[7]=0
	tt0[11]=0
	for (i=6;i<tx-1;i++)
	printf "%.2f^",tt0[i]
	printf "%.0f^^\n",sl
}
END {
	totalp($1)
	
	printf "TOTAL^%d^^^GENERAL^^",ll+l1
	tt[6]=0
	tt[7]=0
	tt[11]=0
	for (i=7;i<tx-1;i++) printf "%.2f^",tt[i]
	printf "%.0f^^\n",sl1 +sl
} ' $venta |
awk -F"^" ' BEGIN {ttop("Cuenta Corriente de Clientes");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF-1; 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 { for (i=1; i<NF-1; i++) labelf($i,al[i],tp[i])
print "</tr>"
}
"'"$RESU"'"!="" { next }
index($0,"TOTAL")==0 {
if ($(NF-1)=="mi") bgcolor="#00FF00"
if ($(NF-1)=="ma") bgcolor="#FF0000"
 for (i=1;i<NF;i++) if(i==3) if($i=="000000") label($i,al[i])
 else boton($i,al[i],tp[i])
else label($i,al[i],tp[i])
print "</tr>"
if (NR%2==0 ) bgcolor="#E1E1E1"
else bgcolor="#FFFFFF"
}
END { foot() } 
function boton(value,alig,typ)
{
	print "<td bgcolor=\""bgcolor"\">"
printf "<p><input type=\"submit\" style=\"width: 80; 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\" style=\"padding: 0px\">\n",bgcolor
if(substr(tip,1,1)=="N") printf "<p align=\"%s\" style=\"font-family: Arial; font-size: 9pt\">&nbsp;%s&nbsp;</p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\" style=\"font-family: Arial; font-size: 9pt;\">&nbsp;%s&nbsp;</p>\n",alg[alig],value
print "</td>"
}
function labelf(value,alig,tip)
{
if(substr(tip,1,1)=="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\"style=\"font-family: Arial; font-size: 9pt; color:#ffffff\" ;\">&nbsp;%s&nbsp;</p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\" style=\"font-family: Arial; font-size: 9pt; color:#ffffff;\" ;\">&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\"><br><b>Cuenta Corrientes Clientes: %s al %s</b>\n","'"$DESD"'","'"$HAST"'"
#printf "<H1>Cuenta Corriente: %s al %s</H1>\n","'"$DESD"'","'"$HAST"'"
if ("'"$VEND"'"!="") 
printf "<H2>Vendedor: %s </H2>\n","'"$VEND"'"
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>"
} ' - 
} 2> ./error/ctacte.err
awk ' {  print ; print "<br>" } '  ./error/ctacte.err
