#!/bin/sh

# disable filename globbing

{
BASE=`pwd`
HOME=$BASE
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR HOME

FECI=`awk -F= ' /H00/ { print $2 } ' $1`
FECI=`awk -F= ' /H01/ { print $2 } ' $1`
ZONA=`awk -F= ' /H02/ { print $2 } ' $1`
#FECI=2022-07-05
#ZONA=""
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"^"$ZONA 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 | 
vtaped=./tmp/vtaped

#00012^00012^2022-07-01^00042|PRAT 636 LOCAL 2    ^03|SANTIAGO CENTRO               ^00|VIGENTE             ^0011^1190  ^2   ^2380   ^
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 {
		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]
	}
	if("'"$ZONA"'"=="")
	{
		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 $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" }
 '  - > $pedidos.d
ZON=`awk -F^ ' NR==2 { print $1 } ' $pedidos.d`
set pedidos $pedidos.d $FECI $ZON
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($2=="TUNDS" || $2=="TNETO"|| $2=="IVA"|| $2=="TOTAL") clr[T[$1+0]]="tl"
	else if($2=="UND"|| $2=="NETO") clr[T[$1+0]]="ng"
	else clr[T[$1+0]]="wh"
	if(row=="")  row=$1+0
	next
}
function labelf(value,style,colspan,rowspan,k)
{
alg["L"]="left"
alg["C"]="center"
alg["R"]="right"
align="align=\""alg[substr(style,1,1)]"\""
printf "<th colspan=\"%s\" rowspan=\"%s\" bgcolor=\"%s\">",colspan,rowspan,"#003399"
printf "&nbsp;%s&nbsp;",value
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,cll)
{
align="align=\""alg[substr(style,1,1)]"\""
type=substr(style,2,1)
if($2=="ZZ") printf "<td id=\"tt\" %s>\n",align
else if($1=="Z3"|| $1=="VL") printf "<td id=\"tt\" %s>\n",align
else printf "<td id=\"%s\" %s>",clr[cll],align
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 "<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 "<style>"
print "#hornear {"
print "  font-size: 15px;"
print "  border-radius: 3px;"
print "  height: 25px;"
print "  width: 550px;"
print "  font-weight: bold;"
print "  color: black;"
print "</style>"

print "<head>"
print "<center>"
print "<header id=\"hornear\" align=\"center\" >"
if("'"$ZONA"'"=="") zona="TODAS"
else zona="'"$ZON"'"
printf "PEDIDOS DEL DIA: %s &nbsp; ZONA:%s</header>\n","'"$3"'",zona

	print "<table id=\"customers\">"
	row=2
	tit(1)
next
}
function tit(k)
{
	for (j=1;j<row+1;j++)
	{

		print "<tr>"
		zz=T[j]+1
		if(k==2 && j==2) zz -=4
		for(i=1;i<zz;i++)
		{
			styl[i]=ST[j""i];lab=TI[j""i];csp=CSP[j""i];rsp=RSP[j""i]
			if(k==2)
			{
				if(j==1 && i==1) { print "<td  style=\"background-color: #F2A9F2\"></td>";lab="PRODUCCION";csp=2 }
				if(j==2 && i==1) {  print "<td  style=\"background-color: #F2A9F2\"></td>";i=2}
				if(j==2 && i==2) { lab="HORA";csp=1 }
				if(j==2 && i==3) { lab="OPERARIO";csp=1 }
				if(j==1 && i==zz-1) { lab="TUNDS";csp=1;rsp=2 }
				if (lab=="NETO") lab="" #print "<td  style=\"background-color: #F2A9F2\"></td>"
				labelf(lab,ST[j""i],csp,rsp,k)
			}
			else labelf(lab,ST[j""i],csp,rsp,k)
		}
		print "</tr>"
	}
}
{
	if($1=="Z0") print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"
	if($1=="Z1") {k=2;print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"; tit(2) }
		if($1=="VL") print "<tr><td style=\"background-color: #F2A9F2\"><br></td></tr>"
	print "<tr>"
	for(i=1;i<NF;i++)
	{
		if(i==1 && k==2) print "<td style=\"background-color: #F2A9F2\"></td>"
		else if($i=="VL") print "<td style=\"background-color: #F2A9F2\"></td>"
		else
		{
			rg=$i
			if($i=="z0") exit
			if($i=="Z0") rg=""
			if($i=="Z1") rg=""
			if($i=="Z2") rg=""
			if($i=="ZZ") rg=""
			label(rg,styl[i],i)
		}
	}
	print "</tr>"
}
END {
print "</table>"
}
' RPHEADER/$1 $2
} 2>error/hornear
cat error/hornear
