#!/bin/sh

# disable filename globbing

{
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin
export APPLDIR DATADIR PATH HOME
order=./tmpV/order
awk -F"^" ' BEGIN { 
xx=split("'"$1"'",b," ")
if (xx==4) key=b[1]
if (xx==5) key=b[1]" "b[2]
ref=b[xx-2]" "b[xx-1]" "b[xx]
T["00"]="ITEM #^RT"
T["01"]="DESCRIPCION^LT"
T["02"]="PIECES^RN"
T["03"]="AMOUNT^RN2"
T["04"]="AVERAGE^RN2"
T["05"]="VENDOR^LT"
T["06"]="CUSTOMER^LT"
T["07"]="TAX^RN2"
T["08"]="PCS^RN"
T["09"]="NET^RN2"
T["10"]="TAX^RN2"
T["11"]="DEL FEE^RN2"
T["12"]="AMOUNT^RN2"
tx=05
	for (i=0;i<tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
key=="TOTAL" || key==$6 {
	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"i" |
awk -F"^" ' BEGIN {
ttop("Libro Mayor '"$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++) 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/vendi.js\"> </script>\n"
 } 
function boton(value,alig)
{
	print "<td bgcolor=\"#E1E1E1\">"
printf "<p><input type=\"button\" style=\"width: 80; text-align:%s\" value=\"%s\" name=\"%s\" OnClick=\"vendi(this.name,%c%s - %s%c)\"></p>\n",alg[alig],value,$2,39,"'"$1"'","'"$2"'",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\"><br>Ranked Sales by Items</H3>\n"
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>"
} ' - 
} 2>./error/vents.err
cat ./error/vents.err


exit

002571^25240 ^SALES ORDER^12/01/2013^01^Angel^Maria Reyna^8.25^Ashley Softgoods 7500155 Laf Loveseat Salsa^1^349.00^349.00^
002571^25240 ^SALES ORDER^12/01/2013^01^Angel^Maria Reyna^8.25^Ashley Softgoods 7500156 Raf Loveseat Salsa^1^350.00^350.00^
002571^25240 ^SALES ORDER^12/01/2013^01^Angel^Maria Reyna^8.25^Montage Furniture Services Furniture Protection Combo Lite 5^1^179.00^179.00^
002572^25241 ^SALES ORDER^12/01/2013^01^HOUSE CREDIT^Maria Reyna^0^Progressive Processing Fee^1^25.00^25.00^
002573^25242 ^SALES ORDER^12/01/2013^01^Angel^Adela Sicre^8.25^Crown Mark, Inc 2185-OAK Table^1^198.00^198.00^
002573^25242 ^SALES ORDER^12/01/2013^01^Angel^Adela Sicre^8.25^Crown Mark, Inc 2185S-OAK Chairs^4^50.00^200.00^
002574^25243 ^SALES ORDER^12/01/2013^01^Alondra^Maria Rios^8.25^Corsicana Bedding, Inc. 8610FS-1050 Queen Mattress Harmony G^1^749.00^749.00^
002574^25243 ^SALES ORDER^12/01/2013^01^Alondra^Maria Rios^8.25^Corsicana Bedding, Inc. CAFE-6050 Queen Foundation Café^1^100.00^100.00^
002574^25243 ^SALES ORDER^12/01/2013^01^Alondra^Maria Rios^8.25^Mantua MFG.CO I6066RR Queen/King-Cal.King RAILS^1^0.00^0.00^
002578^25247 ^SALES ORDER^12/02/2013^01^Nora Nevarez^Adaiberto Paz^8.25^Coaster Co. 100678 Bar^1^1398.00^1398.00^
