rm VEN/* VentasH INVE CUSTO
awk -F"^" '
FILENAME==ARGV[1] { o[$1]=1 }
FILENAME==ARGV[2]  && o[$1]==1 { c[$7]=1 ; print >"VentasH" }
FILENAME==ARGV[3]  && o[$1]==1 { i[$3]=1 }
FILENAME==ARGV[4] { if(i[$1]==1) printf "%.6d^%s\n",++y,$0 >"INVE" }
FILENAME==ARGV[5] { if(c[$1]==1) printf "%.6d^%s\n",++z,$0 >"CUSTO"  }
END {

} ' Payments.csv Orders Items.csv Inventory.csv Customers.csv

awk -F"^" '
function parf(a0) { split(a0,a9,"-"); return a9[2]"/"a9[1]"/"a9[3] }
function nume(a0) { split(a0,a9,","); return a9[1]"."a9[2] }
FILENAME==ARGV[1] { cus[$2]=$1 ;cs[$2]=$0; next }
FILENAME==ARGV[2] { inv[$2]=$1 ; desc[$2]=$7" "$8" "$45" "$9" - "$10; prc[$2]=nume($16); next }
FILENAME==ARGV[3] { aso[$2]=$1 ; next }

FILENAME==ARGV[4] {
zz[$1]=1
file="VEN/"$1
if ($4=="Layaway") print "H0105" > file
else print "H0103" > file
print "H02"parf($6)> file
print "H0301"> file
print "H05"$1> file
print "H06"cus[$7]> file
split(cs[$7],cc)
a=""
if (cc[8]!="") a=" "cc[8]"."
print "H09"cc[7]a" "cc[9]c[10]> file
print "H12"cc[10]" "cc[11]> file
print "H13"cc[13]> file
print "H16"cc[14]"-"cc[15]> file
print "H15"cc[55]> file
print "H18"cc[17]> file
print "H07"aso[$11]> file
print "H17"nume($15)> file
printf "H20%.2f\n",nume($18)> file
print "H21"sprintf("%.2f",nume($12)+nume($13)+nume(18))> file
if($19!="")
print "H24"parf($19)> file
if($4=="Void") print "H2699"> file
else print "H2600"> file
print "H27"parf($6)> file
close(file)
next
}
zz[$1]!=1 { next }
ant!=$1 && ant!="" {
close(file)
d=0
}
{
file="VEN/"$1
	printf "D%.2d00%s\n",d,inv[$3] >>file
	printf "D%.2d01%s\n",d,desc[$3]>>file
	printf "D%.2d02%s\n",d,$4>>file
	printf "D%.2d03%.2f\n",d,prc[$3]>>file
	printf "D%.2d04%.2f\n",d,nume($5)>>file
	printf "D%.2d05%.2f\n",d,$4*nume($5)>>file
	printf "D%.2d06%.2d\n",d++,1 >>file
	#print $1"-"inv[$3]" "desc[$3]" "prc[$3]
	ant=$1
}


 ' CUSTO INVE assoc VentasH Items.csv
sh awp
> ../data/order/H
> ../data/order/HEAD
> ../data/order/DOC
echo -n "    0     0"> ../data/order/INFO

for i in VEN/*; do ../ihd/bin/ihmu order ../../import/$i -N1; done

exit
