FECI=$1

#FECI=`awk -F= ' /H00/ { print $2 } ' $1`
#FECF=`awk -F= ' /H01/ { print $2 } ' $1`
#CATG=`awk -F= ' /H02/ { print $2 } ' $1`
#ZONA=`awk -F= ' /H03/ { print $2 } ' $1`
clientes=./tmp/clientes
ihmq clientes h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12"^" h13"^" h14"^" h15"^" h16"^" h17^ > $clientes

# |  1 H00 registe 005 015   0 | 2 H01 rutclie 013 000   5 | 3 H02 cliente 020 000  18 | 4 H03 razonso 050 000  38 | 5 H04 giro    050 000  88 |
# |  6 H05 direcci 060 000 138 | 7 H06 region  002 051 198 | 8 H07 comuna  030 002 200 | 9 H08 ciudad  030 000 230 |10 H09 zonarut 030 000 260 |
# | 11 H10 telefon 030 000 290 |12 H11 website 030 000 320 |13 H12 mailemp 030 000 350 |14 H13 contact 030 000 380 |15 H14 mailcon 030 000 410 |
# | 16 H15 observa 256 011 440 |17 H16 status  002 052 696 |18 H17 creatio 010 008 698 |

productosventa=./tmp/productosventa
ihmq productosventa h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" h12"^" h13"^" h14"^" h15"^" h16^ > $productosventa

# |  1 H00 registe 004 015   0 | 2 H01 barcode 015 000   4 | 3 H02 skucode 015 000  19 | 4 H03 longdes 040 000  34 | 5 H04 shortde 025 000  74 |
# |  6 H05 categor 002 051  99 | 7 H06 unidadm 002 052 101 | 8 H07 ultimop 005 053 103 | 9 H08 ultimop 010 001 108 |10 H09 preciov 010 001 118 |
# | 11 H10 preciov 010 001 128 |12 H11 stockmi 010 001 138 |13 H12 stockcr 010 001 148 |14 H13 statusp 002 054 158 |15 H14 creatio 010 008 160 |
# | 16 H15 stockac 010 015 170 |17 H16 valorac 015 015 180 |

salecondition=./tmp/salecondition
ihmq salecondition h00"^" h01"^" h02"^" h03"^" h04^ > $salecondition

# |  1 H00 registe 002 015   0 | 2 H01 codigo  002 000   2 | 3 H02 identif 030 000   4 | 4 H03 descrip 040 000  34 | 5 H04 creatio 010 008  74 |

ventalocal=./tmp/ventalocal
ihmq ventalocal h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" h06"^" h07"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05^ > $ventalocal

# |  1 H00 registe 005 015   0 | 2 H01 pedidon 005 001   5 | 3 H02 creatio 010 008  10 | 4 H03 salesco 002 051  20 | 5 H04 documen 006 001  22 |
# |  6 H05 respons 002 052  28 | 7 H06 status  002 054  30 | 8 H07 observa 128 012  32 |

# |  9 D00 product 004 55 | 10 D01 descrip 004 56 | 11 D02 precio  005 01 | 12 D03 cantida 004 01 | 13 D04 totalli 006 01 | 
# | 14 D05 indicac 020 00 | 

pedidos=./tmp/pedidos
ihmq pedidos h00"^" h01"^" h02"^"$FECI h03"^" h04"^"  h06"^" d00"^" d02"^" d03"^" d04"^" | sort -t^ -k7 >$pedidos


# |  1 H00 registe 005 015   0 | 2 H01 pedidon 005 001   5 | 3 H02 creatio 010 008  10 | 4 H03 cliente 005 051  20 | 5 H04 zonarut 030 000  25 |
# |  6 H05 respons 004 052  55 | 7 H06 status  002 054  59 | 8 H07 observa 256 012  61 | 9 H08 tradici 004 015 317 |10 H09 vegetar 004 015 321 |
# | 11 H10 dulce   004 015 325 |12 H11 totalne 010 015 329 |

# | 13 D00 product 004 55 | 14 D01 descrip 004 56 | 15 D02 precio  005 01 | 16 D03 cantida 004 01 | 17 D04 totalli 006 01 | 
# | 18 D05 indicac 040 00 | 
#cat $pedidos
vtaped=./tmp/vtaped

#00012^00012^2022-07-01^00042|PRAT 636 LOCAL 2    ^03|SANTIAGO CENTRO               ^00|VIGENTE             ^0011^1190  ^2   ^2380   ^
produccion=./tmp/produccion
TIT=RPHEADER/pedidos

awk -F^ ' BEGIN { header="'"$TIT"'";may=-1 }
FILENAME==ARGV[1] { pr[$1]=trail($3); ctg[$1]=trail(substr($6,4));ct[$1]=substr($6,1,2); cct[ct[$1]]=ctg[$1];cat[ct[$1]]=0;next }
FILENAME==ARGV[2] { if($2+0) ru[$1]=1; else ru[$1]=0 ;next }
$1=="VL" { vp[$2] +=$4;vl[$1"^"$2]+=$3;vl[$2]+=$3;vl[$1] +=$3;vl[ct[$2]] +=$3 ;next}
FILENAME==ARGV[3] && NF==2{ op[$1]=substr($2,1,5)"^"substr($2,7);next }
FILENAME==ARGV[3] && item[$2]=="" { item[$2]=ct[$2] ; it[ct[$2]"^"cat[ct[$2]]++]=$2 } #print "zz^"ct[$2]"^"ctg[$2]| "sort -u" }
FILENAME==ARGV[3] { prd[$1"^"$2]+=$3;if($1 >may) may=$1;prd[$2]+=$3;prd[$1] +=$3;prd["ZZ^"ct[$2]] +=$3 ;next}
doc[$1]=="" { ruta[$1]=trail(substr($5,4));doc[$1]=trail(substr($5,4))"^"$2;cll[$1]=trail(substr($4,7));bn[$1]=ru[sprintf("%5.5s",$4)]}
item[$7]=="" { item[$7]=ct[$7] ; it[ct[$7]"^"cat[ct[$7]]++]=$7 }
{
	und[doc[$1]"^"$7] +=$9
	und[doc[$1]] +=$9
	und[doc[$1]"^"ct[$7]] +=$9

	val[doc[$1]] +=$10
	val[doc[$1]"^"ct[$7]] +=$10

	und[ruta[$1]"^"$7] += $9
	und[ruta[$1]"^"ct[$7]] += $9
	und[ruta[$1]] += $9
	val[ruta[$1]"^"ct[$7]] += $10
	val[ruta[$1]] += $10
	und[$7] += $9
	und[ct[$7]] += $9
	val[ct[$7]] += $10
	undt +=$9
	valt +=$10
}
END {
		may++
		produc="'"$produccion.p"'"
		#produc="/dev/tty"
		printf "VL^^LOCAL^",op[w] > produc
		pwt=0
		for(j in cat) if(cat[j])
		{
			prw=0
			ppw=0
			for (i=0;i<cat[j];i++) {prw +=vl[it[j"^"i]] ;ppw +=vp[it[j"^"i]]; printf "%s^",vl[it[j"^"i]] >produc }
			printf "%s^%s^",prw,ppw > produc
			pwt +=prw
			ppt +=ppw
		}
		print pwt"^"ppt > produc

		for(w=0;w<may;w++)
		{
			pwt=0
			if(w) printf "Z2^%s^",op[w] > produc
			else printf "Z1^^REMANENTE ANTERIOR&nbsp;^",op[w] > produc
			for(j in cat) if(cat[j])
			{
				prw=0
				for (i=0;i<cat[j];i++) {prw +=prd[w"^"it[j"^"i]] ; printf "%s^",prd[w"^"it[j"^"i]] >produc }
				printf "%s^^",prw > produc
				pwt +=prw
			}
			print pwt"^" > produc
		}
		printf "Z3^^TOTAL DEL DIA^",op[w] >produc
		pwt=0
		for(j in cat) if(cat[j])
		{
			prw=0
			for (i=0;i<cat[j];i++) { printf "%s^",prd[it[j"^"i]] >produc ;prw +=prd[it[j"^"i]] }
			printf "%s^^",prw >produc
			pwt +=prw
		}
		print pwt"^" >produc
		printf "Z3^^REMANENTE DEL DIA^",op[w] >produc
		pwt=0
		for(j in cat) if(cat[j])
		{
			prw=0
			for (i=0;i<cat[j];i++)  printf "%s^",0 >produc 
			printf "%s^^",0 >produc
			pwt +=prw
		}
		print pwt"^" >produc
		printf "1^%s^CT^3^1\n","ID DEL PEDIDO" > header
		for(j in cat) if(cat[j])
		{
			printf "1^%s^CT^%d^1\n",cct[j],cat[j]+2 > header
		}
	printf "1^%s^CT^4^1\n","TOTALES DEL DIA" > header
	printf "2^%s^LT^1^1\n","RUTA/ZONA" > header
	printf "2^%s^CT^1^1\n","PEDIDO" > header
	printf "2^%s^LT^1^1\n","CLIENTE" > header
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "2^%s^RN^1^1\n",pr[it[j"^"i]] > header
			printf "2^%s^RN^1^1\n","UND" > header
			printf "2^%s^RN^1^1\n","NETO" > header
		}
	printf  "2^%s^RN^1^1\n","TUNDS" > header
	printf  "2^%s^RN^1^1\n","TNETO" > header
	printf  "2^%s^RN^1^1\n","IVA" > header
	printf  "2^%s^RN^1^1^\n","TOTAL" > header
	printf " RUTA/ZONA^PEDIDO^CLIENTES^"
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "%s^",pr[it[j"^"i]]
			printf "%s^%s^","UND","NETO"
		}
	print  "TUNDS^TNETO^IVA^TOTAL^"
	for( cl in doc) 
	{
		printf "%s^%s^",doc[cl],cll[cl]
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "%s^",und[doc[cl]"^"it[j"^"i]]
			printf "%s^%s^",und[doc[cl]"^"j],val[doc[cl]"^"j]
		}
		if(bn[cl])
		{
			tb=sprintf("%.0f",val[doc[cl]]*1.19)
			ti=tb-val[doc[cl]]
			
		}
		else { tb=val[doc[cl]]; ti=0}
		printf "%s^%s^%s^%s^\n",und[doc[cl]],val[doc[cl]],ti,tb
		tiv[ruta[cl]] += ti
	}
	for (zo in tiv)
	{
		printf "%s^ZZ^TOTAL RUTA^",zo
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "%s^",und[zo"^"it[j"^"i]]
			printf "%s^%s^",und[zo"^"j],val[zo"^"j]
		}
		printf "%s^%s^",und[zo],val[zo]
		printf "%s^%s^\n",tiv[zo],val[zo]+tiv[zo]
		tivv +=tiv[zo]
	}
	printf "Z0^ZZ^TODAS LAS RUTAS^"
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "%s^",und[it[j"^"i]]
			printf "%s^%s^",und[j],val[j]
		}
		for (i=0;i<z;i++) printf "%s^",und[it[i]]
		printf "%s^%s^%s^%s^\n",undt,valt,tivv,valt+tivv
}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
 ' $productosventa $clientes $produccion.op $pedidos | sort| 
awk -F^ ' #BEGIN  { feci=substr("'"$FECI"'",9)substr("'"$FECI"'",5,4)substr("'"$FECI"'",1,4); print "z0^"feci }
{ print }
#substr($2,1,2)=="ZZ" { print "\n" }
 '  - $produccion.p | tee tmp/horneo > ../../xls/lg/pedhoriz.dat
cat  ../../xls/lg/pedhoriz.dat
