#!/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
DESD=01/01/2010
HAST=31/01/2010
CCST=3
rm -f ./tmpV/*
t=./tmpV/tt
splash> $t
FI=`awk -F= ' $1=="T1" { print $2 } ' $t `
FF=`awk -F= ' $1=="T2" { print $2 } ' $t `
awk ' BEGIN {
print " var a=new Array()"
printf "a[0]=\"%s\"\n","'"$FI"'"
printf "a[1]=\"%s\"\n","'"$FF"'"
} ' > ../js/ventas.js

order=./tmpV/order
ihdm order h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12"^" h13"^" h14"^" h15"^" h16"^" h17"^" h18"^" h19"^" h20"^" h21"^" h22"^" h23"^" h24"^" h25"^" h26"^" h27"^" h28"^" h29"^" h30"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05 > $order

# |  1 H00 filenu 06 01   0 | 2 H01 typeor 02 51   6 | 3 H02 dateor 10 00   8 |
# |  4 H03 branch 02 00  18 | 5 H04 ordnum 06 01  20 | 6 H05 invnum 06 01  26 |
# |  7 H06 codigo 06 52  32 | 8 H07 associ 03 58  38 | 9 H08 layawa 30 00  41 |
# | 10 H09 namess 50 00  71 |11 H10 emaill 50 00 121 |12 H11 financ 30 00 171 |
# | 13 H12 addres 50 00 201 |14 H13 cities 20 00 251 |15 H14 codeco 15 00 271 |
# | 16 H15 celpho 15 00 286 |17 H16 zipcod 15 00 301 |18 H17 taxess 08 01 316 |
# | 19 H18 otherp 15 00 324 |20 H19 inters 40 00 339 |21 H20 deldat 10 01 379 |
# | 22 H21 totalo 10 01 389 |23 H22 deposi 10 01 399 |24 H23 balanc 10 01 409 |
# | 25 H24 delive 10 00 419 |26 H25 delive 10 00 429 |27 H26 statu  02 57 439 |
# | 28 H27 dateor 10 15 441 |29 H28 datein 10 15 451 |

# 30 D00 barcod  6   55 | 31 D01 itemss  6   54 | 32 D02 cantid  6   01 | 
# 33 D03 pricel  6   56 | 34 D04 pricef  10  01 | 35 D05 amount  10  01 | 

tdocs=./tmpV/tdocs
ihdm tdocs h00"^" h01"^" h02 > $tdocs

# |  1 H00 codigo 02 00   0 | 2 H01 descri 25 00   2 | 3 H02 utiliz 256 11  27 |

assoc=./tmpV/assoc
ihdm assoc h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12 > $assoc

# |  1 H00 codigo 03 01   0 | 2 H01 identi 50 00   3 | 3 H02 namess 20 00  53 |
# |  4 H03 lastna 25 00  73 | 5 H04 addres 30 00  98 | 6 H05 addres 20 00 128 |
# |  7 H06 cities 20 00 148 | 8 H07 state  02 55 168 | 9 H08 zipcod 10 01 170 |
# | 10 H09 emaill 50 00 180 |11 H10 celpho 15 00 230 |12 H11 otherp 15 00 245 |
# | 13 H12 social 20 15 260 |
statu=./tmpV/statu
ihdm statu h00"^" h01 > $statu

# |  1 H00 codigo 02 00   0 | 2 H01 status 15 00   2 |

items=./tmpV/items
ihdm items h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12 > $items

# |  1 H00 fileno 06 00   0 | 2 H01 barcod 20 00   6 | 3 H02 suppli 35 00  26 |
# |  4 H03 catego 35 00  61 | 5 H04 modelo 20 00  96 | 6 H05 subcat 35 00 116 |
# |  7 H06 typefu 20 00 151 | 8 H07 finish 20 00 171 | 9 H08 descri 60 00 191 |
# | 10 H09 costos 10 00 251 |11 H10 margin 06 00 261 |12 H11 prlist 10 00 267 |
# | 13 H12 status 02 55 277 |

fpago=./tmpV/fpago
ihdm fpago h00"^" h01 > $fpago

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

#FI=02/01/2014
#FF=02/28/2014
awk -F"^" ' BEGIN {
fi=parf("'"$FI"'")
ff=parf("'"$FF"'")
}
function parf(xx) { return substr(xx,7,4)substr(xx,1,2)substr(xx,4,2) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[1] { tdoc[$1]=$2 ; next }
FILENAME==ARGV[2] { asoc[$1]=trail($2) ; next }
FILENAME==ARGV[3] { stat[$1]=$2 ; next }
FILENAME==ARGV[4] { item[$1]=$9 ; cst[$1]=$10 ; next }
FILENAME==ARGV[5] { fpag[$1]=$2 ; next }
$27=="99" { next }
{ fe=parf($3) }
x[$1]=="" {
x[$1]=$1"^"$5"^"$2"^"$3"^"$4"^"asoc[$8]"^"trail($10)
rg=split($31,py,"|")
for (i=2;i<rg;i +=7) if(py[i]!="")
{
	fd=parf(py[i+1])
	if(!(fd<fi||fd>ff))
	{
		tp[py[i]] +=py[i+6]
		if(fe <fi ) pg["A"py[i]] +=py[i+6]
		else pg["B"py[i]] +=py[i+6]
		print x[$1]"^"trail(fpag[py[i]])"^"py[i+1]"^"asoc[py[i+5]]"^"py[i+6]"^" >"'"$order".p'"
	}
}
}
fe < fi || fe > ff { next }
{
printf "%s^",$1
printf "%s^",trail($5)
printf "%s^",trail(tdoc[$2])
printf "%s^",$3
printf "%s^",$4
printf "%s^",trail(asoc[$8])
printf "%s^",trail($10)
printf "%s^",trail($18)
printf "%s^",trail($21)
printf "%s^",trail(item[$32])
printf "%s^",trail($34)
printf "%s^",trail($36)
printf "%s^",trail($37)
printf "%s^",trail($31)
printf "%s^",cst[$32]
xc[$32]+=$34
xv[$32]+=$37
print ""
}
END {
for (i in xc) if(xc[i]) printf "%s^%s^%d^%.2f^%.2f\n", i,trail(item[i]),xc[i],xv[i],xv[i]/xc[i] >"./tmpV/orderi"
else printf "%s^%s^%d^%.2f^%.2f\n", i,trail(item[i]),xc[i],xv[i],"" >"./tmpV/orderi"
for (i in tp)
printf "%s^%.2f^%.2f^%.2f^\n",fpag[i],pg["A"i],pg["B"i],tp[i] >"'"$order"'p"
}

' $tdocs $assoc $statu $items $fpago $order > $order.1
awk -F"^" ' BEGIN {
fi=parf("'"$FI"'")
ff=parf("'"$FF"'")
}
function parf(xx) { return substr(xx,7,4)substr(xx,1,2)substr(xx,4,2) }
x[$1]=="" {
x[$1]=$1"^"$2"^"$3"^"$4"^"$5"^"$6"^"$7"^"$8"^"$9"^"
rg=split($14,py,"|")
for (i=2;i<rg;i +=7)
{
	fe=parf(py[i+1])
	if(fe <= ff) pg[$1]+=py[i+6]
}
}
{
qt[$1] +=$11
nt[$1] +=$12*$11
ct[$1] +=$15*$11
}
END {
for (i in x)
{
print x[i]"^"qt[i]"^"nt[i]"^"pg[i]"^"nt[i]-pg[i]"^"ct[i]"^"
}
 }
' $order.1 | sort -t"^" -k4.7,4 -k4,4 >$order.2
awk -F"^" ' 
{
for(i=1;i<9;i++) printf "%s^",$i
printf "%s^",$11
printf "%s^",$12
tax=sprintf("%.2f^",$8*($9+$12)/100+.0005)
printf "%.2f^",$9
printf "%.2f^",tax
printf "%.2f^",$12 +tax +$9
printf "%.2f^",$13
printf "%.2f^",$12 +tax +$9-$13
printf "%.2f^",$15
print ""
x["TOTAL"]++
pc["TOTAL"] +=$11
nt["TOTAL"] +=$12
tx["TOTAL"] +=tax
de["TOTAL"] += $9
am["TOTAL"] +=sprintf("%.2f",$12 + tax +$9)
py["TOTAL"] +=$13
bl["TOTAL"] +=$14
ct["TOTAL"] +=$15
x[$6" "]++
pc[$6" "] +=$11
nt[$6" "] +=$12
tx[$6" "] +=tax
de[$6" "] += $9
am[$6" "] +=sprintf("%.2f",$12 + tax+$9)
py[$6" "] +=$13
bl[$6" "] +=$14
ct[$6" "] +=$15
}
END {
	for (i in x) printf "%s^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^%.2f^\n",i,x[i],pc[i],nt[i],ct[i],nt[i]-ct[i],100*(1-ct[i]/nt[i]),100*(nt[i]/ct[i]-1),de[i],tx[i],am[i],py[i],am[i]-py[i] >"./tmpV/orderv"
} ' $order.2  >$order.3
sort $order"v" -o $order"v"
sort $order"p" -o $order"p"
sort -t"^" -n -k4 -r $order"i" -o $order"i"
awk -F"^" ' BEGIN { 
T["00"]="SELLER^LT"
T["01"]="ORDERS^RN"
T["02"]="PCS^RN"
T["03"]="NET SALE^RN2"
T["04"]="NET COST^RN2"
T["05"]="PROFIT^RN2"
T["06"]="MARGIN^RN2"
T["07"]="MARKUP^RN2"
T["08"]="DELIVERY^RN2"
T["09"]="SALE TAX^RN2"
T["10"]="TOTAL AMOUNT^RN2"
T["11"]="PAID^RN2"
T["12"]="BALANCE^RN2"
tx=13
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
	#printf "TOTAL^"
	#for (i=2;i<=tx;i++)
		#printf "%.2f^",tt[i]
#print ""
} ' $order""v |
awk -F"^" ' BEGIN {
ttop("General Sales '"$1"'");bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
 for (i=1; i<NF; i+=2)
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelt($i,al[(i+1)/2])
	}
print "</tr>"
next
}
$1=="TOTAL" { for (i=1; i<NF; i++) if(i==1) boton($i,al[i])
else labelf($i,al[i],tp[i])
print "</tr>"
}
$1!="TOTAL" {
 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 {
print "</table>"
# foot()
printf "<script language=\"Javascript\" src=\"../js/vende.js\"> </script>\n"
 } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"button\" style=\"width: 150; text-align:%s\" value=\"%s\" name=\"T02\" OnClick=\"vende(this.value,%c%s - %s%c)\"></p>\n",alg[alig],value,39,"'"$FI"'","'"$FF"'",39

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=\"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 labelt(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">&nbsp;<b>%s</b>&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>"
printf "<H3><font face=\"Arial\">%s<br>%s - %s</H3>\n",value,"'"$FI"'", "'"$FF"'"
print "<form method=\"post\" action=\"../cgi-bin/order.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 "<table><tr>"
labels("SALDOS","L")
labels("DEBITOS","R")
labels("CREDITOS","R")
labels("DIFERENCIA","R")
print "</tr>"
print "<tr>"
labels("ANTERIOR","L")
label(cta[4],"R","N")
label(cta[5],"R","N")
label(cta[6],"R","N")
print "</tr>"
print "<tr>"
labels("DEL PERIODO","L")
label(cta[7],"R","N")
label(cta[8],"R","N")
label(cta[9],"R","N")
print "</tr>"
print "<tr>"
labels("ACTUAL","L")
label(cta[10],"R","N")
label(cta[11],"R","N")
label(cta[12],"R","N")
print "</tr>"
print "</center>"
print "</form>"
print "</body>"
print "</html>"
} ' - 
} > ./error/vents.err
cat ./error/vents.err
./ventap.cgi $FI $FF
./ventai.cgi $FI $FF
