#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
{
 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
venta=./tmp/venta
cliok=./tmp/cliok
imput=/tmp/imput
>$imput
ff=`date +%d/%m/%Y`
ff=`cat tmp/FEC`
awk -F"^" ' BEGIN {
m["01"]="Enero"
m["02"]="Febrero"
m["03"]="Marzo"
m["04"]="Abril"
m["05"]="Mayo"
m["06"]="Junio"
m["07"]="Julio"
m["08"]="Agosto"
m["09"]="Septiembre"
m["10"]="Octubre"
m["11"]="Noviembre"
m["12"]="Diciembre"
m1[1]="un"
m1[2]="dos"
m1[3]="tres"
m1[4]="cuatro"
m1[5]="cinco"
m1[6]="seis"
m1[7]="siete"
m1[8]="ocho"
m1[9]="nueve"
m1[10]="diez"
m1[11]="once"
m1[12]="doce"
m1[13]="trece"
m1[14]="catorce"
m1[15]="quince"
m2[1]="dieci"
m2[2]="veinte"
m2[3]="treinta"
m2[4]="cuarenta"
m2[5]="cincuenta"
m2[6]="sesenta"
m2[7]="setenta"
m2[8]="ochenta"
m2[9]="noventa"
pap[2]="KILOS DE PAPA PICADA CALIBRE 9mm"
pap[4]="KILOS DE PAPA PICADA CALIBRE 12mm"
pap[6]="KILOS DE PAPA PELADA ENTERA"
pap[8]="KILOS DE PAPA CORTE ESTOFADO"
pap[10]="KILOS DE PAPA PICADA EN CUBOS"
pap[12]="KILOS DE PAPA PICADA CORTE 6"
split("'"$ff"'",dd,"/")
getline <"./FACT"
FACT=$0
}
FILENAME==ARGV[1] { cl[$1]=$0 ; next }
index($1,"'"$1"'")==0 { next }
{
split(cl[$1],cli)
printf ":8G0"
for (i=0;i<12;i++) printf "\n"
printf "%75s\n\r",$1
printf "%10s%.2d     %-20s %d\n\n","",dd[1],m[dd[2]],dd[3]-2000
printf "H0000000\n" >"'"$imput"'"
printf "H01%.6d\n",FACT++ >"'"$imput"'"
printf "%.6d\n",FACT >"./FACT"
printf "H02%.2d/%.2d/%d\n",dd[1],dd[2],dd[3] >"'"$imput"'"
printf "H03%s\n",cli[3] >"'"$imput"'"
printf "H04%s\n",cli[2] >"'"$imput"'"
printf "H05%s\n",cli[4] >"'"$imput"'"
printf "%12s%-59.59s%-20.20s\n\n\r","",cli[3],cli[2]
printf "%12s%-59.59s%-20.20s\n\n\r","",cli[5],cli[6]
printf "%12s%-59.59s%-20.20s\n\n\r","",cli[4],cli[7]
printf "%12s%-59.59s%-20.20s\n\n\r","","   CONTADO",""
printf "\n\n\n\n"
tk=0
for (j=2;j<14;j +=2) if ($j+0 >0) 
{
	printf "H%.2d%d\n",j/2+5,$j >"'"$imput"'"
	tk+=$j
	printf "%10s     ",punto($j,0)
	printf "%-35s",pap[j]
	printf "%22s",punto($(j+1)/1.19/$j,0)
	printf "%13s",punto($(j+1)/1.19,0)
	printf "\n\n\r"
	lin+=2
}
if(tk) printf "H12%s\n",tk >"'"$imput"'"
if ($14+$15)
{
	
	if($14+0)
	{
		printf "H13%s",$14 >"'"$imput"'"
		sm="MALLA "
		if($14+0 >1) sm=" MALLAS "
		printf "%10s     ",punto($14,0)
		printf "%-35s",sm"DE PAPAS"
		printf "%22s",punto($17/1.19/$14,0)
		printf "%13s\n",punto($17/1.19,0)
	}
	if($15+0)
	{
		sc="SACO "
		printf "H14%s",$15 >"'"$imput"'"
		if($15+0 >1) sc=" SACOS "
		printf "%10s     ",punto($15,0)
		printf "%-35s",sc"DE PAPAS"
		printf "%22s",punto($17/1.19/$15,0)
		printf "%13s\n",punto($17/1.19,0)
	}
	printf "\n\r"
	lin+=2
}
if($16+0) printf "H15%s\n",$16 >"'"$imput"'"
gu=$19
tt=$18+$17
printf "\n\r"
rg=42
gui=""
while (length(gu) >rg)
{
	printf "%15s%-42.42s\n\r","",gu
	gui=gui""sprintf ("%-42.42s\r",gu)
	lin++
	gu=substr(gu,rg+1)
}
gui=gui""sprintf ("%s",gu)
printf "%15s%-42.42s\n\r","",gu
printf "H16%s\n",gui >"'"$imput"'"
for (j=lin;j<21;j++) print ""
	nt=sprintf("%.0f",tt/1.19)
	tt=sprintf("%.0f",tt)	
	mont="    "mon(tt)
lmo=length(mont)
if (lmo>55)
while (substr(mont,lmo,1)!=" " || lmo >55)  lmo--

iv=tt-nt
printf "   %-55.55s%3s%23s\n\r",substr(mont,1,lmo),"",punto(nt)
printf "H17%.0f\n",nt >"'"$imput"'"
printf "       %-60s\n\n%64s%20s\n\n\r\n",substr(mont,lmo+1),"",punto(iv)
printf "%64s%20s\n\n\r\f","",punto(tt)
printf "H18%.0f\n",iv >"'"$imput"'"
printf "H19%.0f\n\n",tt >"'"$imput"'"

}
function mon (value)
{
	v0=sprintf ("%9.0f",value)
	v1=substr(v0,1,3)+0
	v2=substr(v0,4,3)+0
	v3=substr(v0,7,3)+0
	z=""
	if (v1)
	{
		if (v1>1) z=mon1(v1)" millones "
		else z="un millon "
	}
	if (v2)
	{
		if (v2>1) z=z""mon1(v2)" mil "
		else z=z"mil "
	}
	if (v3) z=z""mon1(v3)
	if(substr(z,length(z))==" ")
	z=z""
	else z=z" "
	return toupper(substr(z,1,1))substr(z,2)
}
function mon1 (mont)
{
	monto=sprintf ("%3d",mont)
	x1=substr(monto,1,1)+0
	x2=substr(monto,2,1)+0
	x3=substr(monto,3,1)+0
	zz=""
	if(x1) 
	{
		p2=m1[x1]
		if (x1==2) p2="do"
		if (x1==3) p2="tre"
		if (x1==7) p2="sete"
		if (x1==9) p2="nove"
		if (x1==1)
		{
			 if (x3+x2) zz="ciento "
			else zz="cien "
		}
		else zz=p2"cientos "
		if (x1==5) zz="quinientos "
	}
	p1=x2*10+x3
	if (p1<16) zz=zz""m1[p1]
	else
	{
		if (p1<20) zz=zz""m2[x2]""m1[x3]" "
		else
		{
			if (p1>19 && !x3)  zz=zz""m2[x2]" "
			if (p1>20 && p1<30)  zz=zz"veinti"
			if (p1>29 && x3)  zz=zz""m2[x2]" y "
			zz=zz""m1[x3]
		}
	}
	return zz
} 
function punto (dat,dec)
{
	if (dat+0==0) return ""
	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 pun
} ' $cliok $venta.fac |lp -dGeneric-text-only
./ihd/bin/ihmu ffact $imput -N1 >/dev/null
} 2>./error/fact1.err
cat ./error/fact1.err
echo "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">
<meta name=\"GENERATOR\" content=\"Inhedo 4gt 1.0\">
<meta name=\"ProgId\" content=\"Inhedo Form Generator\">
</head>
<body>
<script language=\"Javascript\">
self.close()
</script>
</body>
</head>
</html>"
