pordr=./tmpI/pordr
ihdm pordr h00"^" h01"^" h02"^" h03"^" h04"^" h05"^"h06"^"h26 d00"^" d02"^" d03"^" d04> $pordr

# |  1 H00 filenu 06 01   0 | 2 H01 typeor 02 51   6 | 3 H02 dateor 10 00   8 |
# |  4 H03 branch 02 52  18 | 5 H04 ordnum 06 01  20 | 6 H05 invnum 06 01  26 |
# |  7 H06 codigo 06 53  32 | 8 H07 associ 03 54  38 | 9 H08 layawa 30 00  41 |
# | 10 H09 namess 50 00  71 |11 H10 emaill 50 00 121 |12 H11 financ 30 00 171 |
# | 13 H12 addres 50 00 201 |14 H13 cities 20 00 251 |15 H14 codeco 15 00 271 |
# | 16 H15 celpho 15 00 286 |17 H16 zipcod 15 00 301 |18 H17 taxess 08 01 316 |
# | 19 H18 otherp 15 00 324 |20 H19 inters 40 00 339 |21 H20 deldat 10 00 379 |
# | 22 H21 totalo 10 00 389 |23 H22 deposi 10 01 399 |24 H23 balanc 10 00 409 |
# | 25 H24 delive 10 00 419 |26 H25 delive 10 00 429 |27 H26 statu  02 55 439 |
# | 28 H27 dateor 10 15 441 |29 H28 datein 10 15 451 |

# 30 D00 barcod  6   56 | 31 D01 itemss  60  00 | 32 D02 cantid  6   01 | 
# 33 D03 pricel  10  01 | 34 D04 pricef  10  01 | 35 D05 amount  10  01 | 
#
items=./tmpI/items
ihdm items h00"^" h08"^" h09 > $items

# |  1 H00 fileno 06 00   0 | 2 H01 barcod 20 00   6 | 3 H02 suppli 35 00  26 |
# |  4 H03 catego 35 00  61 | 5 H04 modelo 20 00  96 | 6 H05 subcat 35 00 116 |
# |  7 H06 typefu 20 00 151 | 8 H07 finish 20 00 171 | 9 H08 descri 60 00 191 |
# | 10 H09 costos 10 00 251 |11 H10 margin 06 00 261 |12 H11 prlist 10 00 267 |
# | 13 H12 status 02 55 277 |

#
tdocc=./tmpI/tdocc
ihdm tdocc h00"^" h01 > $tdocc

# |  1 H00 codigo 02 00   0 | 2 H01 descri 25 00   2 | 3 H02 utiliz 256 11  27 |

bodeg=./tmpI/bodeg
ihdm bodeg h00"^" h01 > $bodeg

# |  1 H00 codigo 02 00   0 | 2 H01 identi 20 00   2 | 3 H02 ubicac 30 00  22 |
# |  4 H03 telefo 20 00  52 | 5 H04 fax    20 00  72 | 6 H05 operad 15 00  92 |

FI=03/01/2014
FF=03/31/2014
awk -F^ ' BEGIN { fi=parf("'"$FI"'");ff=parf("'"$FF"'") }
FILENAME==ARGV[1] {desc[$1]=$2;cst[$1]=3;next }
function parf(xx) { return substr(xx,7,4)substr(xx,1,2)substr(xx,4,2) }
{ fe=parf($3)}
fe >ff { next }
FILENAME==ARGV[2] {
	it[$7]=1
	if($2=="04") sig=-1
	else sig=1
	if($2=="01")
	{
		tpc[$7] +=$8
		tpv[$7] += $8*$9
	}
	else
	{
		ttc[$7] += sig*$8
		ttv[$7] += sig*$8*$9
		tbc[$4$7] += sig*$8
		tbv[$4$7] += sig*$8*$9
	}
	if(fe <fi)
	{
		tac[$4$7] += sig*$8
		tav[$4$7] += sig*$8*$9
	}
	else 
	{
		tcc[$4$2$7] += sig*$8
		tcv[$4$2$7] += sig*$8*$9
	}
}

END {
		#for (i in tpc) print i,tpc[i],tpv[i]
		#for (i in ttc) print i,ttc[i],ttv[i]
		#for (i in tac) print i,tac[i],tav[i]
		#for (i in tcc) print i,tcc[i],tcv[i]
		print "var item = new Array()"
		for (i in it)
		{
			printf "item[\"%s\"]=\"",i
			for (j=1;j<6;j++)
			printf "%d^",tbc[sprintf("%.2d",j)i],tbv[sprintf("%.2d",j)i]
			printf "%d^",ttc[i],ttv[i]
			printf "%d\"\n",tpc[i],tpv[i]
		}
		#for (i in it)
		#{
			#printf "%s^",i
			#for (j=1;j<6;j++)
			#printf "%d^%.2f^",tbc[sprintf("%.2d",j)i],tbv[sprintf("%.2d",j)i]
			#printf "%d^%.2f^",ttc[i],ttv[i]
			#printf "%d^%.2f^\n",tpc[i],tpv[i]
		#}
}

 ' $items $pordr >../js/stock.js
exit
000001^03^03/01/2014^01^      ^      ^001926^2     ^0.00      ^0.00      
000001^03^03/01/2014^01^      ^      ^002621^1     ^58.00     ^58.00     
000001^03^03/01/2014^01^      ^      ^002624^1     ^100.99    ^100.99    
000001^03^03/01/2014^01^      ^      ^002270^1     ^194.99    ^194.99    
000001^03^03/01/2014^01^      ^      ^002447^1     ^134.99    ^134.99    
000001^03^03/01/2014^01^      ^      ^002455^1     ^259.98    ^259.98    
000001^03^03/01/2014^01^      ^      ^002457^1     ^259.98    ^259.98    
000001^03^03/01/2014^01^      ^      ^002460^2     ^259.98    ^259.98    
000001^03^03/01/2014^01^      ^      ^002620^2     ^339.98    ^339.98    
000001^03^03/01/2014^01^      ^      ^002627^2     ^274.98    ^274.98    

