#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
 BASE=`pwd`
{
rm -f ../js/orderp.js
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
T=/tmp/T$$
T1=/tmp/T1$$
T2=/tmp/T2$$
items=./tmp/items
splash >$T
awk -F= ' BEGIN { ret=sprintf("%c",13) }
$0=="" { next }
$1=="T00" { next }
substr($1,1,5)=="order" && $2=="" { next }
/DOCorder/ { next }
{ wh="" }
substr($1,1,5)=="order" && index($0,ret) >0 {
printf "%s",$0
        while (wh=="")
        {
                if (getline <= 0) wh=1
                if(substr($1,1,5)=="order") 
                if (index($0,ret)==0)  { printf "\n%s\n",$0;wh=1 }
                else printf "%s",$0
                else  printf "%s",$0
        }
}
wh=="" && $2!="" && $1!="T00" ' $T > $T1
QRY=`awk -F= ' $1== "B1" {print $2} ' $T1 `
if [ "$QRY" = Consultar ]
then
	COMMAND=`awk -F= ' BEGIN { printf "./ihd/bin/ihmq order " }
	$1!="B1" {
	if (NR==1 && substr($1,length($1)-1) !="00") printf "H00^ "
	if (substr($1,6,1)=="D") printf "D%s^%s ",substr($1,length($1)-1),adv($2)
	else printf "H%s^%s ",substr($1,length($1)-1),adv($2)
}
function adv(txt) { while (substr(txt,1,1)==" ") txt=substr(txt,2); return (txt) }
END {
if (NR==1) print "H00^ "
print "" } ' $T1 `
$COMMAND | sort -t\^ -k2 -k1 -u  >$T2
NF=`awk ' END { print NR } ' $T2`
if [ $NF = 1 ]
then
		REG=`awk -F"^" ' { print $1 } ' $T2`
		./orderget.cgi $REG
	else
	./orderlist.cgi $T $T2
	fi
fi
if [ "$QRY" = Actualizar -o "$QRY" = Print ]
then
TYPE=`awk -F= ' $1== "orderH01" {print $2} ' $T1 `
QUOT=`awk -F= ' $1== "orderH00" {print $2} ' $T1 `
ORDE=`awk -F= ' $1== "orderH04" {print $2} ' $T1 `
INVO=`awk -F= ' $1== "orderH05" {print $2} ' $T1 `
date=`date +%m/%d/%Y`
if [ "$INVO" = "" -a "$TYPE" = 03 ]
then
	awk ' { printf "orderH05=%.6d\n",$1+1 } END { print $1+1 >"INVOICE" } ' INVOICE >>$T1
	echo orderH28=$date >>$T1
	#echo orderH26=50 >>$T1
fi
if [ "$ORDE" = "" -a "$TYPE" = 02 ]
then
	awk ' { printf "orderH04=%.6d\n",$1+1 } END { print $1+1 >"ORDER" } ' ORDER >>$T1
	echo orderH27=$date >>$T1
fi
if [ "$ORDE" = "" -a "$TYPE" = 04 ]
then
	awk ' { printf "orderH04=%.6d\n",$1+1 } END { print $1+1 >"ORDER" } ' ORDER >>$T1
	echo orderH27=$date >>$T1
fi
	COMMAND=`awk -F= ' END { printf "./ihd/bin/ihmu order %s -N1\n","'"$T2"'" } ' `
	awk -F= '
	/orderH/ { printf "H%s%s\n",substr($1,length($1)-1),substr($0,10) }
	/orderD/ { printf "D%s%s\n",substr($1,length($1)-3),substr($0,12) }
	' $T1 > $T2
        awk -F= ' $1=="orderH00" { y=$2 }
                  $2=="NEWFOTO>>>" { x=$1 }
                  $1=="FOTO" {
                                split($2,ph,".")
                                shell=sprintf("rm -f ../IMAG/order/%s%s.%s;mv -f \"../upload/uploads/%s\" ../IMAG/order/%s%s.%s\n",x,y,ph[2],$2,x,y,ph[2])
                                system(shell)
                                printf "%s%s%s.%s\n",substr(x,6),x,y,ph[2] } ' $T1 >> $T2
echo -n " " >>$T2
	flag=`$COMMAND`
	#echo flag=:$flag:
[ "$flag" = "" ] || echo orderH00=$flag >>$T1
if [ "$QRY" = Print ]
then
#cat $T1
if [ A = A ]
then
#echo TYPE=$TYPE
FILE=../Quotation.htm0
$APPLDIR/sh/items " " >$items
$APPLDIR/sh/assoc " " | 
awk -F"^" ' BEGIN {
tdoc["01"]="QUOTATION"
tdoc["02"]="SALES ORDER"
tdoc["03"]="INVOICE"
tdoc["04"]="SPECIAL ORDER"
tdoc["05"]="LAYAWAY"
}
FILENAME== ARGV[1] { r=split($1,b,"="); fld[b[1]]=b[2]
if (substr($1,6,1)=="D") xxl=sprintf("%d",substr($1,7,2)/2+1)
next }
FILENAME== ARGV[2] { assoc[$1]=$2; next }
FILENAME== ARGV[3] { items[$1]=$9; next }
ww=="" {
fld["orderH000"]=fld["orderH00"]
if(fld["orderH01"]=="02"||fld["orderH01"]=="04") { fld["orderH000"]=fld["orderH04"];fld["orderH02"]=fld["orderH27"]}
if(fld["orderH01"]=="03") { fld["orderH000"]=fld["orderH05"];fld["orderH00"]=fld["orderH04"];fld["orderH02"]=fld["orderH28"]}
if(fld["orderH10"]!="") fld["orderH10"]="e-Mail: "fld["orderH10"]
if(fld["orderH15"]!="") fld["orderH15"]="Cell Phone: "fld["orderH15"]
if(fld["orderH19"]!="") fld["orderH19"]="Landmark: "fld["orderH19"]
if(fld["orderH29"]!="")
{
	split(fld["orderH29"],del,"|")
	fld["orderH291"]=del[1]
	fld["orderH292"]=del[2]
	fld["orderH293"]=del[3]
	fld["orderH294"]=del[4]
	if(del[5]!="") fld["orderH295"]="Cell Phone: "del[5]
	if(del[6]!="") fld["orderH296"]="Landmark: "del[6]
}
else
{
	fld["orderH291"]=fld["orderH09"]
	fld["orderH292"]=fld["orderH12"]
	fld["orderH293"]=fld["orderH13"]
	fld["orderH294"]=fld["orderH16"]
	fld["orderH295"]=fld["orderH15"]
	fld["orderH296"]=fld["orderH19"]
}
fld["orderH07"]=assoc[fld["orderH07"]]
fld["orderQOI"]="QUOTED "
fld["orderQQQ"]="Quotation "
fld["orderQQ0"]="Quoted"
fld["orderSHP"]="DELIVERY ADDRESS TO"
if (fld["orderH01"]=="03") fld["orderQQQ"]="Order "
if (fld["orderH01"]!="01")
{
	fld["orderQOI"]="SOLD "
	fld["orderSHP"]="SHIPPED TO"
	fld["orderQQ0"]="Sold"
	 FOOT=1
}
fld["orderH01"]=tdoc[fld["orderH01"]]
fld["orderFILE"]=fld["orderH01"]" - "fld["orderH000"] 
dd=-1
ww=1
}
NF >1 { 
printf $3,fld["order"$1]
next
}
{ print }
END {
	if(xxl+0<9) xxl=9
	for (j=0;j<xxl+0;j++)
	{
	while ((getline <"../Quotation.htm1") >0)
	if (NF >1)
	{
		if($1=="02") DD=sprintf("orderD%.2d",++dd)
		if($1=="00") printf $3,items[fld[DD""$1]]
		else
		if($2=="N" && fld[DD""$1]!=0 ) printf $3,"$"punto(fld[DD""$1],2)
		else printf $3,fld[DD""$1]
	}
	else print
	close ("../Quotation.htm1")
	}
	while ((getline <"../Quotation.htm2") >0)
	{
		if ($1=="FOOTER")
		{
			if (FOOT==1)
			while ((getline footer <"../Quotation.htmf") >0) print footer
		}
		else 
		if (NF >1)
		if(fld[$1]!=0) printf $3,"$"punto(fld[$1],2)
		else printf $3,""
		else print
	}

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

' $T1 - $items $FILE >../DOCS/$TYPE$QUOT.html
awk ' BEGIN {
print "<html>"
print "<script language=\"Javascript\">"
printf " window.open (\"../DOCS/%s.html\",\"_blank\")\n","'"$TYPE$QUOT"'"
print " var Backlen=history.length;   history.go(-Backlen)"
 
print "</script>"
print "<head>"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
print "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=../order.htm\">"
print "</head>"
print "</body>"
print "</head>"

} ' >../orderp
fi
fi
[ "$QRY" = Actualizar ] && cat ../order
[ "$QRY" = Print ] && cat ../orderp
fi
 } 2> ./error/order.err
cat ./error/order.err
rm $T $T1 $T2
