

#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 |

FECI=2022-07-01
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"^" h03"^" h04"^" h05"^" h06"^" h07"^" h08"^" h09"^" h10"^" h11"^" d00"^" d01"^" d02"^" d03"^" d04"^" d05^ > $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 002 057  25 |
# |  6 H05 respons 004 052  27 | 7 H06 status  002 054  31 | 8 H07 observa 256 012  33 | 9 H08 tradici 004 015 289 |10 H09 vegetar 004 015 293 |
# | 11 H10 dulce   004 015 297 |12 H11 totalne 010 015 301 |

# | 13 D00 product 004 55 | 14 D01 descrip 004 56 | 15 D02 precio  006 01 | 16 D03 cantida 004 01 | 17 D04 totalli 007 01 | 
# | 18 D05 indicac 040 00 | 

#cat $pedidos
produccion=./tmp/produccion
ihmq produccion h00"^" h01"^" h02"^" h03"^" h04"^" h05"^" d00"^" d01"^" d02"^" d03"^" d04^ > $produccion

# |  1 H00 registe 005 015   0 | 2 H01 rproduc 006 001   5 | 3 H02 creatio 010 008  11 | 4 H03 respons 004 052  21 | 5 H04 status  002 054  25 |
# |  6 H05 observa 256 012  27 |

# |  7 D00 product 004 55 |  8 D01 descrip 004 56 |  9 D02 cantida 004 01 | 10 D03 operari 004 57 | 11 D04 horario 005 06 | 

TIT=RPHEADER/pedidos
echo $pedidos

awk -F^ ' BEGIN { feci = "'"$FECI"'" }
FILENAME==ARGV[1] && $3 < feci { z[$7]=1;x[$7] += $9; tpa+=$9 ;  next }
FILENAME==ARGV[1] && $3== feci { z[$7]=1;xp[$7] += $9; tpp+=$9 ; next }

FILENAME==ARGV[2] && $3< feci { z[$9]=1;x[$9] -= $12; tpa-=$12 ; next }
FILENAME==ARGV[2] && $3== feci { z[$9]=1;vl[$9] += $12; tvl+=$12 ; next }

FILENAME==ARGV[3] && $3< feci { z[$13]=1;x[$13] -= $16; tpa-=$16 ; next }
FILENAME==ARGV[3] && $3== feci { z[$13]=1;vp[$13] += $16; tvp+=$16 ;next }
END {
#for (i in op) print i"^"op[i]
for (i in z) printf "%s %3d %3d %3d %3d %3d\n", i,x[i],xp[i],vl[i],vp[i],x[i]+xp[i]-vl[i]-vp[i]
printf "%s %3d %3d %3d %3d %3d\n", "TTTT",tpa,tpp,tvl,tvp,tpa+tpp-tvl-tvp
} 

' $produccion $ventalocal $pedidos
exit
awk -F^ '
FILENAME==ARGV[1] { pr[$1]=trail($3); ctg[$1]=substr($6,4);ct[$1]=substr($6,1,2); cat[ct[$1]]=0;next }
NF==2 { op[$1]=$2;next }
$1+0 > reg { reg=$1+0}
item[$2]=="" { item[$2]=ct[$2] ; it[ct[$2]"^"cat[ct[$2]]++]=$2; print "zz^"ct[$2]"^"ctg[$2]| "sort -u" }
$2!=ant { it[++z]=$2;ant=$2 }
{
ctt[ct[$2]] +=$3
rg[$1"^"$2"^"ct[$2]] =$3
}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {
print reg,"z="z
for (i =0; i<= reg; i++)
{
	printf "%s^",op[i]
	for(k in ct) if(ctt[ct[k]])
	{
		print "ctt="ctt[ct[k]]
		for(j=1;j<=z;j++) printf "%s^",rg[i"^"it[j]"^"ct[it[j]]]
		printf "%s^",ctt[ct[k]]
	}
	print ""
}
} ' $productosventa  -


exit

	



awk -F^ ' BEGIN { header="'"$TIT"'" }
FILENAME==ARGV[1] { pr[$1]=trail($3); ctg[$1]=substr($6,4);ct[$1]=substr($6,1,2); cat[ct[$1]]=0;next }
FILENAME==ARGV[2] { if($2+0) ru[$1]=1; else ru[$1]=0 ;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; print "zz^"ct[$7]"^"ctg[$7]| "sort -u" }
{
	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
}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {
		
	printf "1^%s^LT^1^1\n","RUTA/ZONA" > header
	printf "1^%s^CT^1^1\n","PEDIDO" > header
	printf "1^%s^LT^1^1\n","CLIENTE" > header
		for(j in cat) if(cat[j])
		{
			for (i=0;i<cat[j];i++) printf "1^%s^RN^1^1\n",pr[it[j"^"i]] > header
			printf "1^%s^RN^1^1\n","UND" > header
			printf "1^%s^RN^1^1\n","NETO" > header
		}
	printf  "1^%s^RT^1^1\n","TUNDS" > header
	printf  "1^%s^RT^1^1\n","TNETO" > header
	printf  "1^%s^RT^1^1\n","IVA" > header
	printf  "1^%s^RT^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
		feci=substr("'"$FECI"'",9)substr("'"$FECI"'",5,4)substr("'"$FECI"'",1,4)
		print "z0^"feci
}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
 ' $productosventa $clientes $pedidos | sort |
awk -F^ '
{ print }
$2=="ZZ" { print "\n" }
END {
}
 '  | tee tmp/horneo > ../../xls/lg/pedhoriz.dat
 cat  ../../xls/lg/pedhoriz.dat
