#!/bin/sh

# disable filename globbing
PER="15"
BAS=`pwd`
cd ..
BASE=`pwd`
rm $BAS/H/* $factu
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR

factu=$BAS/tmp/factu
ihdm factu h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12"^" h13"^" h14"^" h15"^" h16"^" h17"^" h18  | awk -F"^" ' $5=="'"$PER"'" ' > $factu

# |  1 H00 folio1 05 15   0 | 2 H01 nrodoc 10 00   5 | 3 H02 tipodo 02 57  15 |
# |  4 H03 fechae 10 08  17 | 5 H04 period 02 56  27 | 6 H05 rutemi 11 00  29 |
# |  7 H06 razons 50 00  40 | 8 H07 tasaim 05 02  90 | 9 H08 cuenta 05 55  95 |
# | 10 H09 mnexen 10 02 100 |11 H10 mnneto 10 02 110 |12 H11 mnivas 10 02 120 |
# | 13 H12 mntota 10 02 130 |14 H13 ivauso 04 02 140 |15 H14 ivamor 10 02 144 |
# | 16 H15 cinano 02 00 154 |17 H16 otroim 02 00 156 |18 H17 tasaim 05 02 158 |
# | 19 H18 mnimpt 10 02 163 |
sort -t"^" -k4 -o $factu $factu
awk -F"^" 'BEGIN { getline <"./data/vouch/INFO"; reg=substr($0,1,5)+0 }
function rut(rr) {
split(rr,a,"-")
l=length(a[1])
return substr(a[1],1,l-6)"."substr(a[1],l-5,3)"."substr(a[1],l-2)"-"a[2]
}
function fec(rr) { split(rr,a,"-"); return a[3]"/"a[2]"/"a[1] }
{
file=sprintf("impu/H/F%.3d",NR)
print "H01"++reg > file
print "H0230/12/2015" > file
print "H03"$3 > file
print "H04"$7" F/"$2 > file
print "H05RAN" > file
print "H0630/12/2015" > file
print "D0000"$9 > file
print "D0001"$9 > file
print "D0002"$7 > file
print "D0003"$11 > file
print "D0005"rut($6) > file
print "D0006"$2 > file
print "D0007"fec($4) > file
print "D00081" > file

print "D010011510" > file
print "D010111510" > file
print "D0102"$7 > file
print "D0103"$12 > file
print "D0105"rut($6) > file
print "D0106"$2 > file
print "D0107"fec($4) > file
print "D01081" > file

print "D020021140" > file
print "D020121140" > file
print "D0202"$7 > file
print "D0204"$13 > file
print "D0205"rut($6) > file
print "D0206"$2 > file
print "D0207"fec($4) > file
print "D02081" > file
print " " > file
close(file)
} ' $factu
for i in $BAS/H/*
do
./ihd/bin/ihmu vouch $i -N1

done
#rm $BAS/H/* $factu
