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

vtaped=./tmp/vtaped
FECI=2022-07-01
FECF=2022-07-01
rm -f $pedidos.*
>$pedidos.clpra
awk -F^ ' BEGIN { feci="'"$FECI"'";fecf="'"$FECF"'"
f1=substr(feci,9)+0;f2=substr(fecf,9)+0
for (i=f1;i<=f2;i++) fc[sprintf("%8.8s%.2d",feci,i)]=i-f1+0
for (i in fc) print fc[i]"^"i >"'"$pedidos.f"'"
}
FILENAME==ARGV[1] { catg[$1]=substr($6,1,2) ; next } 
FILENAME==ARGV[2] { zona[$1]=substr($10,1,2);if($2+0) ccl[$1]=1; else ccl[$1]=0;next } 
substr($3,1,7)!=substr(feci,1,7) || $3 >fecf || substr($7,1,2)!="00" { next }
"'"$ZONA"'"!="" && zona[substr($4,1,5)]!="'"$ZONA"'" { next }
"'"$CATG"'"!="" && catg[$13]!="'"$CATG"'" { next }
$3 <feci {
	cli=substr($4,1,5)
 clpra[cli"^"$13]+=$16
 vlpra[cli"^"$13]+=$17

 clpra[cli]+=$16
 vlpra[cli]+=$17

 clpra[$13]+=$16
 vlpra[$13]+=$17

 clpra["Z"]+=$16
 vlpra["Z"]+=$17

 clprt[cli"^"$13]+=$16
 vlprt[cli"^"$13]+=$17

 clprt[cli]+=$16
 vlprt[cli]+=$17

 clprt[$13]+=$16
 vlprt[$13]+=$17

 clprt["Z"]+=$16
 vlprt["Z"]+=$17

next
}
{
 cli=substr($4,1,5)
 cl[cli]=ccl[cli]"^"trail(substr($4,7))"^"trail(substr($5,4))
 clprp[fc[$3]"^"cli"^"$13]+=$16
 vlprp[fc[$3]"^"cli"^"$13]+=$17

 clprp[cli"^"$13]+=$16
 vlprp[cli"^"$13]+=$17

 clprp[fc[$3]"^"cli]+=$16
 vlprp[fc[$3]"^"cli]+=$17

 clprp[fc[$3]"^"$13]+=$16
 vlprp[fc[$3]"^"$13]+=$17

 clprp[cli]+=$16
 vlprp[cli]+=$17

 clprp[$13]+=$16
 vlprp[$13]+=$17

 clprp[fc[$3]]+=$16
 vlprp[fc[$3]]+=$17

 clprp["Z"]+=$16
 vlprp["Z"]+=$17

 clprt[cli"^"$13]+=$16
 vlprt[cli"^"$13]+=$17

 clprt[cli]+=$16
 vlprt[cli]+=$17

 clprt[$13]+=$16
 vlprt[$13]+=$17

 clprt["Z"]+=$16
 vlprt["Z"]+=$17

}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {
for (i in cl) print i"^"cl[i] >"'"$clientes.cl"'"
for (i in clpra) print i"^"clpra[i]"^"vlpra[i] >"'"$pedidos.clpra"'"
for (i in clprt) print i"^"clprt[i]"^"vlprt[i] >"'"$pedidos.clprt"'"
for (i in clprp) print i"^"clprp[i]"^"vlprp[i] >"'"$pedidos.clprp"'"
} ' $productosventa $clientes $pedidos

awk -F^ ' NF==5 ' $pedidos.clprp | sort -t^ -k3 | 

awk -F^ '
FILENAME==ARGV[1] { cl[$1]=$2"^"$4"^"$3; next }
FILENAME==ARGV[2] { itv[$1]=trail($3) ; next }
it[$3]=="" { it[$3] = 1 ;rit[0+zz++]=$3 }
{rg[$2"^"$3] = $4; tt[$2]+=$5;rg[$2] += $4
split(cl[$2],ac,"^")
rg[ac[2]"^"$3] +=$4; tr[ac[2]]+=$5;rg[ac[2]] += $4;if(ac[1]+0==0) te[ac[2]"^"$3] +=$5
rg["ZZ^"$3] +=$4; tr["ZZ"]+=$5;rg["ZZ"] += $4;if(ac[1]+0==0) te["ZZ"]+=$5  }
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {
	printf " RUTA/ZONA^CLIENTE^"
	for (j=0;j<zz;j++)  printf "%s^",itv[rit[j]]
	printf "TT/U^NETO^IVA^BRUTO^\n"
	for (i in tt)
	{
		
		printf "%s^",substr(cl[i],3)
		for (j=0;j<zz;j++)  printf "%s^",rg[i"^"rit[j]]
		printf "%s^%s^",rg[i],tt[i]
		if(substr(cl[i],1,1)+0)
		{
			t2=sprintf("%.0f",tt[i]*1.19)
			ti=t2-tt[i]
			printf "%s^%s^\n",ti,t2
		}
			else printf "^%s^\n",tt[i]
	}
	for (i in tr)
	{
		printf "%s^ZZ==========^",i
		for (j=0;j<zz;j++)  printf "%s^",rg[i"^"rit[j]]
		printf "i=%s^%s^%s^",i,rg[i],tr[i]
			t2=sprintf("%.0f",(tr[i]-te[i])*1.19+te[i])
			ti=t2-tr[i]
			printf "%s^%s^\n",ti,t2
	}
} ' $clientes.cl $productosventa - | sort | awk -F^ ' {print }
$2=="ZZ" { print "\n" } ' > ../../xls/lg/pedhoriz.dat
cat  ../../xls/lg/pedhoriz.dat
exit 
sort -n $pedidos.f -o $pedidos.f

#head $pedidos.*
awk -F^ ' BEGIN { printf "00000^^^^^^" }
FILENAME==ARGV[1] {fe[$1]=$2;dias++;printf "%s^^",$2;next}
ff=="" {print "";ff=1}
FILENAME==ARGV[2] {
if(NF==3) {clpra[$1]=$2"^"$3}
if(NF==4) {clpra[$1"^"$2]=$3"^"$4;cl[$1]=1;pr[$2]=1}
}
FILENAME==ARGV[3] {
if(NF==3) {clprt[$1]=$2"^"$3}
if(NF==4) {clprt[$1"^"$2]=$3"^"$4;cl[$1]=1;pr[$2]=1}
}
FILENAME==ARGV[4] {
if(NF==3) {clprp[$1]=$2"^"$3}
if(NF==4) {clprp[$1"^"$2]=$3"^"$4}
if(NF==5) {clprp[$1"^"$2"^"$3]=$4"^"$5;cl[$2]=1;pr[$3]=1}
}
FILENAME==ARGV[5] {cli[$1]=trail($3)}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {

for (i in cl)
{
	for(j in pr) if(clpra[i"^"j] || clprt[i"^"j])
	{ 
		printf "%s^%s^",cli[i],j
		if(clpra[i"^"j]) printf "%s^",clpra[i"^"j]
		else printf "^^"
		for(k=0;k<dias;k++) if(clprp[k"^"i"^"j]) printf "%s^",clprp[k"^"i"^"j]
		else printf "^^"
		if(clprp[i"^"j]) printf "%s^",clprp[i"^"j]
		else printf "^^"
		printf "%s\n",clprt[i"^"j]
	}
	printf "%s^TT^",cli[i]
	if(clpra[i]) printf "%s^",clpra[i]
	else printf "^^"
	for(k=0;k<dias;k++) if(clprp[k"^"i]) printf "%s^",clprp[k"^"i]
	else printf "^^"
	if(clprp[i]) printf "%s^",clprp[i]
	else printf "^^"
	printf "%s\n",clprt[i]
}
	for(j in pr) if(clpra[j] || clprt[j])
        {
		
                printf "ZZZ^%s^",j
                if(clpra[j]) printf "%s^",clpra[j]
                else printf "^^"
                for(k=0;k<dias;k++) if(clprp[k"^"j]) printf "%s^",clprp[k"^"j]
                else printf "^^"
                if(clprp[j]) printf "%s^",clprp[j]
                else printf "^^"
                printf "%s\n",clprt[j]
        }
        printf "ZZZ^TTT^"
        if(clpra["Z"]) printf "%s^",clpra["Z"]
        else printf "^^"
        for(k=0;k<dias;k++) if(clprp[k]) printf "%s^",clprp[k]
        else printf "^^"
        if(clprp["Z"]) printf "%s^",clprp["Z"]
        else printf "^^"
        printf "%s\n",clprt["Z"]
}

'  $pedidos.f $pedidos.clpra $pedidos.clprt $pedidos.clprp  $clientes | sort > $vtaped
awk -F^ ' BEGIN { pr["TT"]="^TOTAL CLIENTE" ;pr["TTT"]="^TOTAL GENERAL" }
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
FILENAME==ARGV[1] {pr[$1]=trail($3)"^"trail($4);next}
a1!=$1 {if(a1!="") printf "\n\n";a1=$1;xll=0 }
FNR==1 { printf "%s%s^^%s\n",$0,"DEL PERIODO","ACUMULADO" ; next }
{
	
	if($1=="ZZZ") printf "^%s^",pr[$2]
	else 
	{
		if(xll==0 || $2=="TT") printf "%s^%s^",$1,pr[$2]
		else printf "^%s^",pr[$2]
	}
	for (i=3;i<NF;i++) printf "%s^",$i
	printf "%s\n",$i
	xll=1

}
' $productosventa $vtaped > ../../xls/lg/vtacli.dat
{
if [ "$CATG" = "" ]
then
echo "CATEGORIA: TODAS"
else
ihmq categoriasproductos h00^$CATG h01^ | awk -F^ ' { print "CATEGORIA: "$2 } '  
fi
if [ "$ZONA" = "" ]
then
echo "RUTA           : TODAS"
else
ihmq rutazona  h00^$ZONA h02^ | awk -F^ ' { printf "RUTA          : %s\n",$2 } '  
fi
echo $FECF
} >> ../../xls/lg/vtacli.dat
cat ../reportepedidosrp
#rm -f $clientes $productosventa $salecondition $ventalocal $pedidos* $vtaped
