#!/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
Enc=../bolet/Enc1020
bol1=../bolet/bol1
awk -F^ ' BEGIN {
det1="  <th class=\"%s\" width=40 style=\"text-align:center;vertical-align:middle\">%s&nbsp;</th>\n"
det2="  <th class=\"%s\" width=90>%s&nbsp;</th>\n"
det3="  <th class=\"%s\" width=378 style=\"text-align:left\">&nbsp;%s</th>\n"
det4="  <th class=\"%s\" width=90 style=\"vertical-align:bottom\">%s&nbsp;</th>\n"
det5="  <th class=\"%s\" width=102 style=\"vertical-align:bottom\">%s&nbsp;</th>\n"
}
FILENAME== ARGV[1] { enc[$1]=$2 ; next }

NF >1 { printf $2,enc[$1] ; next }
{ print }
END {
	x=enc["LineasdeDetalle"]

for (i=1;i<=x;i++)
{
	print "<tr>"
	if (i==x) print"   <tr style=\"border-bottom:1.0pt solid #A6A6A6;\">"
	ss=sprintf("%.2d",i)
	if(i%2) itemlin="itemlin"
	else itemlin="itemlinc"
	printf det1,itemlin,enc["NroLinDet"ss]
	printf det2,itemlin,punto(enc["QtyItem"ss],1)
	printf det3,itemlin,enc["DescripcionItem"ss]
	printf det4,itemlin,punto(enc["PrcItem"ss],0)
	printf det5,itemlin,punto(enc["MontoItem"ss],0)
	print "</tr>"
}
print "<table width=550 center cellspacing=0 style=\"border-collapse:collapse;\">"
print "<tr>"
print "  <th width=28>&nbsp;</th>"
print "  <th width=550>&nbsp;</th>"
print "  <th width=165>&nbsp;</th>"
print "</tr>"
print "<tr>"
print "  <th> </th>"
printf "  <th><img src=http://buin.homelinux.com/tcpdf/39/%s.php></img>\n",enc["Folio"]
	print "Timbre Electr&oacute;nico SII"
print "<br>Res. Nº 80 de 22/08/2014 Verifique documento: www.sii.cl"
print "</th>"
print "  <th> "
print " <table width=192 cellspacing=0 style=\"border-collapse:collapse;\">"
print "  <tr>"
print "  <th class=\"itemlinc\" width=90 style=\"border-top:1.0pt solid #A6A6A6;\">NETO&nbsp;</th>"
printf "  <th class=\"itemlinc\" width=65 style=\"border-top:1.0pt solid #A6A6A6;\"><b>%s&nbsp;</th>",punto(enc["MntNeto"],0)
print "  </tr>"
print "<tr>"
print "  <th class=\"itemlin\" width=90>IVA&nbsp;</th>"
printf "  <th class=\"itemlin\" width=102><b>%s&nbsp;</th>",punto(enc["IVA"],0)
print "</tr>"
print "<tr>"
print "  <th class=\"itemlinc\" width=90 style=\"border-bottom:1.0pt solid #A6A6A6;\">TOTAL&nbsp;</th>"
printf "  <th class=\"itemlinc\" width=102 style=\"border-bottom:1.0pt solid #A6A6A6;\"><b>%s&nbsp;</th>",punto(enc["MntTotal"],0)
print "</th>"
print "</tr>"
print "</table>"
print "</body>"
print "<table>"
print "<tr>"

}
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>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else 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
}

' $Enc $bol1
