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 | 

FECI=2022-07-05
FECF=2022-07-12
rm -f $pedidos.*
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"'"
}
substr($3,1,7)!=substr(feci,1,7) || $3 >fecf || substr($7,1,2)!="00" { next }
$3 <feci {
	cli=substr($4,1,5)
 clpra[cli"^"$13]+=$16
 vlpra[cli"^"$13]+=$17
 clprt[cli"^"$13]+=$16
 vlprt[cli"^"$13]+=$17

 cpra[$13]+=$16
 vpra[$13]+=$17
 cprt[$13]+=$16
 vprt[$13]+=$17

 tcta+=$16
 tvta+=$17
 tctt+=$16
 tvtt+=$17
next
}
{
 cli=substr($4,1,5)
 clprp[fc[$3]"^"cli"^"$13]+=$16
 vlprp[fc[$3]"^"cli"^"$13]+=$17
 clprs[cli"^"$13]+=$16
 vlprs[cli"^"$13]+=$17
 clprt[cli"^"$13]+=$16
 vlprt[cli"^"$13]+=$17
 cprp[fc[$3]"^"cli]+=$16
 vprp[fc[$3]"^"cli]+=$17

 cprp[$13]+=$16
 vprp[$13]+=$17
 cprt[$13]+=$16
 vprt[$13]+=$17

 cptp[fc[$3]]+=$16
 vptp[fc[$3]]+=$17

 tptp+=$16
 tvtp+=$17
 tptt+=$16
 tvtt+=$17
}
END {
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 cpra) print i"^"cpra[i]"^"vpra[i] >"'"$pedidos.cpra"'"
for (i in cprt) print i"^"cprt[i]"^"vprt[i] >"'"$pedidos.cprt"'"
for (i in clprp) print i"^"clprp[i]"^"vlprp[i] >"'"$pedidos.clprp"'"
for (i in clprs) print i"^"clprs[i]"^"vlprs[i] >"'"$pedidos.clprs"'"
for (i in cprp) print i"^"cprp[i]"^"vprp[i] >"'"$pedidos.cprp"'"
for (i in cptp) print i"^"cptp[i]"^"cptp[i] >"'"$pedidos.cptp"'"
} ' $pedidos
sort -n $pedidos.f -o $pedidos.f
awk -F^ ' BEGIN { printf "00000^^^^" }
FILENAME==ARGV[1] {fe[$1]=$2;dias++;printf "%s^^",$2;next}
ff=="" { print "" ; ff=1 }
FILENAME==ARGV[2] { 
pr[$2]=1
cl[$1]=1
clpra[$1"^"$2]=$3"^"$4
}
FILENAME==ARGV[3] {
pr[$2]=1
cl[$1]=1
clprs[$1"^"$2]=$3"^"$4
}
FILENAME==ARGV[4] {
pr[$2]=1
cl[$1]=1
clprt[$1"^"$2]=$3"^"$4
}
FILENAME==ARGV[5] { clprp[$1"^"$2"^"$3]=$4"^"$5}
FILENAME==ARGV[6] { cprt[$1"^"$2]=$3"^"$4}
FILENAME==ARGV[7] { cli[$1]=trail($3)}
FILENAME==ARGV[8] { pr[$1]=trail($3)"^"trail($4) }
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^%s^",cli[i],j,pr[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(clprs[i"^"j]) printf "%s^",clprs[i"^"j]
		else printf "^^"
		printf "%s\n",clprt[i"^"j]
	}
}
}

'  $pedidos.f $pedidos.clpra $pedidos.clprs $pedidos.clprt $pedidos.clprp $pedidos.cprt $clientes $productosventa | sort >../../xls/lg/vtacli.dat

more ../../xls/lg/vtacli.dat

