FECI=`awk -F= ' /H01/ { print $2 } ' $1`
CATG=`awk -F= ' /H02/ { print $2 } ' $1`
rm -f tmp/*
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 | 


vtaloc=./tmp/vtaloc
#FECI=2022-07-02
FECF=$FECI
rm -f $ventalocal.*
>$ventalocal.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 >"'"$ventalocal.f"'"
}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
FILENAME==ARGV[1] { catg[$1]=substr($6,1,2) ; next }
substr($3,1,7)!=substr(feci,1,7) || $3 >fecf || substr($7,1,2)!="00" { next }
$3 <feci {
 fp=substr($4,1,2)

 clpra[$9]+=$12
 vlpra[$9]+=$13

 clpra[fp"^"$9]+=$12
 vlpra[fp"^"$9]+=$13

 clpra[fp]+=$12
 vlpra[fp]+=$13

 clpra["Z"]+=$12
 vlpra["Z"]+=$13

 clpra[fp"^Z"]+=$12
 vlpra[fp"^Z"]+=$13

 clprt[fp"^"$9]+=$12
 vlprt[fp"^"$9]+=$13

 clprt[$9]+=$12
 vlprt[$9]+=$13

 clprt[fp]+=$12
 vlprt[fp]+=$13

 clprt[fp"^Z"]+=$12
 vlprt[fp"^Z"]+=$13

 clprt["Z"]+=$12
 vlprt["Z"]+=$13

next
}
{
 fp=substr($4,1,2)

 clprp[fc[$3]"^"fp"^"$9]+=$12
 vlprp[fc[$3]"^"fp"^"$9]+=$13

 clprp[fp"^"$9]+=$12
 vlprp[fp"^"$9]+=$13

 clprp[fc[$3]"^"fp]+=$12
 vlprp[fc[$3]"^"fp]+=$13

 clprp[fc[$3]"^"$9]+=$12
 vlprp[fc[$3]"^"$9]+=$13

 clprp[fp]+=$12
 vlprp[fp]+=$13

 clprp[$9]+=$12
 vlprp[$9]+=$13

 clprp[fc[$3]]+=$12
 vlprp[fc[$3]]+=$13

 clprp["Z"]+=$12
 vlprp["Z"]+=$13

 clprp[fp"^Z"]+=$12
 vlprp[fp"^Z"]+=$13

 clprt[fp"^"$9]+=$12
 vlprt[fp"^"$9]+=$13

 clprt[fp]+=$12
 vlprt[fp]+=$13

 clprt[$9]+=$12
 vlprt[$9]+=$13

 clprt[fp"^Z"]+=$12
 vlprt[fp"^Z"]+=$13

 clprt["Z"]+=$12
 vlprt["Z"]+=$13


}
END {
for (i in clpra) print i"^"clpra[i]"^"vlpra[i] >"'"$ventalocal.clpra"'"
for (i in clprt) print i"^"clprt[i]"^"vlprt[i] >"'"$ventalocal.clprt"'"
for (i in clprp) print i"^"clprp[i]"^"vlprp[i] >"'"$ventalocal.clprp"'"
} ' $productosventa  $ventalocal
sort -n $ventalocal.f -o $ventalocal.f

#head $ventalocal.*
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}
}
FILENAME==ARGV[5] {cli[$1]=trail($3);vl[++z]=$1}
function trail(txt) {while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt)}
END {
if ("a"=="A")
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])
        {
		if(j!="Z")
		{
                printf "ZZZ^%s^",j
		for(i=1;i<=z;i++) if(clpra[vl[i]"^"j]) printf "%s^",clpra[vl[i]"^"j]
                else printf "^^"
		if(clpra[j]) printf "%s^",clpra[j]
                else printf "^^"
		for(i=1;i<=z;i++) if(clprp[vl[i]"^"j]) printf "%s^",clprp[vl[i]"^"j]
                else printf "^^"
                if(clprp[j]) printf "%s^",clprp[j]
                else printf "^^"
		for(i=1;i<=z;i++) if(clprt[vl[i]"^"j]) printf "%s^",clprt[vl[i]"^"j]
                else printf "^^"
                printf "%s\n",clprt[j]
		}
        }
        printf "ZZZ^TTT^"
		for(i=1;i<=z;i++) if(clpra[vl[i]"^Z"]) printf "%s^",clpra[vl[i]"^Z"]
		else printf "^^"
		if(clpra["Z"]) printf "%s^",clpra["Z"]
		else printf "^^"
		for(i=1;i<=z;i++) if(clprp[vl[i]"^Z"]) printf "%s^",clprp[vl[i]"^Z"]
		else printf "^^"
		if(clprp["Z"]) printf "%s^",clprp["Z"]
		else printf "^^"
		for(i=1;i<=z;i++) if(clprt[vl[i]"^Z"]) printf "%s^",clprt[vl[i]"^Z"]
		else printf "^^"
		printf "%s\n",clprt["Z"]

}

'  $ventalocal.f $ventalocal.clpra $ventalocal.clprt $ventalocal.clprp  $salecondition | sort > $vtaloc
awk -F^ ' BEGIN { pr["TT"]="^ZZ" ;pr["TTT"]="^ZZ" }
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}
FNR==1 { printf "%s%s^^%s\n",$0,"DEL PERIODO","ACUMULADO" ; next }

a1!=$1 {if(a1!="") printf "\n\n";a1=$1;xll=0 }
FNR==1 { printf "%s^^%s\n",$0,"DEL PERIODO","ACUMULADO" ; next }
{

        if($1=="ZZZ") printf "^%s^",pr[$2]
        else
        {
                if(xll==0 || $2=="TTT") 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 $vtaloc | awk -F^ ' { for (i=2;i<=NF;i++) printf "%s^",$i ; print "" } ' -  > $vtaloc""al


set basetit $vtaloc"al"
awk -F^ ' BEGIN {alg["L"]="left"; alg["C"]="center"; alg["R"]="right" }
FILENAME==ARGV[1] {
	T[$1+0]++
	TI[$1+0""T[$1+0]]=$2
	ST[$1+0""T[$1+0]]=$3
	CSP[$1+0""T[$1+0]]=$4
	RSP[$1+0""T[$1+0]]=$5
	if(row=="")  row=$1+0
	next
}
function labelf(value,style,colspan,rowspan)
{
alg["L"]="left"
alg["C"]="center"
alg["R"]="right"
adfec=""
if(i>2 && j==1) adfec=" AL '"$FECI"'"
align="align=\""alg[substr(style,1,1)]"\""
printf "<th colspan=\"%s\" rowspan=\"%s\" bgcolor=\"%s\">",colspan,rowspan,"#003399"
if(type=="N") printf "<align=\"%s\">&nbsp;%s&nbsp;</font>\n",alg[alig],punto(value,substr(tip,2))
else printf "%s",value""adfec
print "</th>"
}
function boton(value)
{
        print "<td>"
        printf "<input id=\"submit\" type=\"submit\" center value=\"%s\" name=\"T02\">\n",value
        print "</td>"
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
function label(value,style)
{
align="align=\""alg[substr(style,1,1)]"\""
type=substr(style,2,1)
if($2=="ZZ") printf "<td id=\"tt\" %s>\n",align
else printf "<td %s>",align,sty
if(type=="T") printf "&nbsp;%s&nbsp;",trail(value)
else printf "&nbsp;%s&nbsp;",punto(value,0)
print "</td>"
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
        if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
FNR==1{
print "<!DOCTYPE html>"
print "<html lang=\"en-US\">"
print "<head>"
print "<meta charset=\"UTF-8\">"
print "<meta http-equiv=\"Content-Type\" content=\"text/html\">"
print "<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">"
print "<meta name=\"ProgId\" content=\"Inhedo Form Generator\">"
print "<link rel=\"stylesheet\" href=\"../css/repo.css\">"
print "<center>"
print "<header id=\"titulo\" align=\"center\" >"
printf "VENTA LOCAL DIARIA y ACUMULADA AL: %s</header>\n","'"$FECI"'"

	print "<table id=\"customers\">"
	for (j=1;j<3;j++)
	{
		print "<tr>"
		for(i=1;i<T[j]+1;i++) labelf(TI[j""i],ST[j""i],CSP[j""i],RSP[j""i])
		print "</tr>"
	}
next
}
{
	print "<tr>"
	for(i=1;i<NF;i++)
	{
		rg=$i
		if(i<3) styl[i]="LT"
		else styl[i]="RN"
		if($i=="Z0") rg=""
		if($i=="ZZ") rg="TOTAL"
		label(rg,styl[i])
	}
	print "</tr>"
}
	
' RPHEADER/$1 $2
