#!/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 APPLDIR DATADIR PATH 

wrkor=./tmp/wrkor
./appl/sh/wrkor > $wrkor
# |  1 H00 filenu 06 01   0 | 2 H01 dateor 10 00   6 | 3 H02 cotiza 06 01  16 |
# |  4 H03 fechco 10 00  22 | 5 H04 cusdat 512 11  32 | 6 H05 obrdat 256 11 544 |
# |  7 H06 observ 256 11 800 | 8 H07 ordenc 20 01 1056 | 9 H08 fechae 10 00 1076 |
# | 10 H09 assoc  03 52 1086 |11 H10 respon 03 54 1089 |12 H11 statu  02 53 1092 |

# 13 D00 itemmm  2   00 | 14 D01 itemss  500 12 | 15 D02 textos  2   16 | 
# 16 D03 adicio  500 15 | 17 D04 cantid  6   01 | 


cotiz=./tmp/cotiz
./appl/sh/cotiz > $cotiz

# |  1 H00 filenu 06 01   0 | 2 H01 dateor 10 00   6 | 3 H02 codigo 05 51  16 |
# |  4 H03 cusdat 512 11  21 | 5 H04 obras  07 59 533 | 6 H05 obrdat 256 11 540 |
# |  7 H06 obrass 07 15 796 | 8 H07 condxs 02 55 803 | 9 H08 cndxv1 256 11 805 |
# | 10 H09 observ 256 11 1061 |11 H10 totalo 10 01 1317 |12 H11 deposi 10 01 1327 |
# | 13 H12 balanc 10 01 1337 |14 H13 assoc  03 52 1347 |15 H14 statu  02 53 1350 |

# 16 D00 produc  6   54 | 17 D01 itemss  500 12 | 18 D02 textos  2   16 | 
# 19 D03 adicio  500 15 | 20 D04 costos  10  01 | 21 D05 cantid  6   01 | 
# 22 D06 pricel  10  01 | 23 D07 pricef  10  01 | 24 D08 amount  10  01 | 
# 25 D09 checko  1   17 | 

clien=./tmp/clien
./ihd/bin/ihmq clien h00"^" h01"^" h02"^" h13"^" > $clien

# |  1 H00 regist 05 15   0 | 2 H01 rut    13 07   5 | 3 H02 razsoc 50 00  18 |
# |  4 H03 giro   40 00  68 | 5 H04 direc1 60 00 108 | 6 H05 comuna 25 00 168 |
# |  7 H06 ciudad 20 00 193 | 8 H07 region 30 00 213 | 9 H08 telefo 25 00 243 |
# | 10 H09 wsite  50 00 268 |11 H10 corree 60 00 318 |12 H11 contac 50 00 378 |
# | 13 H12 correo 60 00 428 |14 H13 observ 512 11 488 |

# 15 D00 obra    30  00 | 16 D01 direcc  50  00 | 17 D02 contac  30  00 |
# 18 D03 telefo  50  00 | 19 D04 correo  40  00 |

awk -F"^" ' BEGIN { ret=sprintf("%c",13) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
FILENAME==ARGV[1] && substr($12,1,2)=="99" { next } # OT NULA
FILENAME==ARGV[1] && ant!=$1 { fot[$1]=$2;cot[$1]=$3;ant=$1 }
FILENAME==ARGV[1] { valot[$1] +=$20*$21;next }
FILENAME==ARGV[2] && $1!=ant { ant=$1;fcot[$1]=$2;cli[$1]=substr($3,1,5);valc[$1]=$13}  
FILENAME==ARGV[2] { next}
ant!=$1 {
	clii[$1]=$2
	rsoc[$1]=trail($3)
	ind=index($4,ret)
	inf=$4
	while(ind)
	{
		inf=substr(inf,1,ind-1)"\\r"substr(inf,ind+1)
		ind=index(inf,ret)
	}
		
	infc[$1]=trail(inf)
ant=$1
}
END {
print "var ot=new Array()"
print "var cot=new Array()"
print "var cli=new Array()"
for (i in fot) print "ot["i+0"]=\""fot[i]","cot[i]+0","sprintf("%d",valot[i]*1.19)"\""
for(i in fcot) print "cot["i+0"]=\""fcot[i]","trail(valc[i])","cli[i]+0"\""
for(i in clii) print "cli["i+0"]=\""trail(clii[i])","rsoc[i]","infc[i]"\""
} ' $wrkor $cotiz $clien >../js/factu.js
cat ../ctact
