Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
~rladams/+junk/LedgerInvoicingExample
#!/bin/sh
# Example from http://bazaar.launchpad.net/~rladams/+junk/LedgerInvoicingExample/view/head:/CreateInvoice.sh
# Pass the invoice # as the first and only argument
[ -n "$1" ] || { echo 'Specify an invoice number.' ; exit -1 ; }
# Load config file to override defaults
CUSTOMIZATION_FILE="~/.`basename \"${0}\" .sh`.rc"
[ -f "${CUSTOMIZATION_FILE}" ] && . "${CUSTOMIZATION_FILE}"
# Project directory, each file is named for a project and contains the customer name/address
: ${PROJSRC:="./Projects"}
# Where to put the final pdf
: ${PDFDST:="."}
# Ledger path
: ${LEDGER_CMD:="./ledger"}
# Temporary storage
: ${TMPDIR:=`mktemp -d /tmp/${0}.XXXXXX`}
# Setup the latex document properties
: ${LATEX_HEADER:="
\documentclass[10pt,letterpaper]{article}
\usepackage[letterpaper,includeheadfoot,top=0.5in,bottom=0.5in,left=0.75in,right=0.75in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\hypersetup{colorlinks,linkcolor=blue}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{multicol}
\usepackage{invoice}
\fancyhead[L]{\Huge \bf Invoice\# $1}
\begin{document}
"}
# Company logo here
: ${LATEX_COMPANY:="
\begin{raggedleft}
\bf \large Bob Smith Smithing Inc. \\\\ \rm
123 Boudine Blvd \\\\
Somewhere, AK 12344 \\\\
800-555-1212 \\\\
\end{raggedleft}
"}
# Any notes at the end go here, raw latex
: ${LATEX_NOTES:=""}
# Footer to close the doc
: ${LATEX_FOOTER:="\end{document}"}
# Grab the invoice description from the payee matching the invoice number that haven't cleared
DESC=`"${LEDGER_CMD}" -U -l "$1" -F "%P" reg ^AR:PendingInvoices`
PROJ=`"${LEDGER_CMD}" -U -l "$1" -r -F "%a" reg ^AR:PendingInvoices | sed 's/^AR:Projects://'`
# This file is a latex fragment, very simple, just end each line with \\
[ -f "${PROJSRC}/${PROJ}" ] || { echo "Need customer bill to data in ${PROJSRC}/${PROJ}" ; exit -1 ; }
# Grab all non-cleared fee items from hours for the project
LATEX_FEES=`"${LEDGER_CMD}" -F 'Fee{%D%|%P}{%T}{%t}\n' -VEU -t '(v/(v/a))' -T '(v/a)' -d 'a>=0' reg ^AR:Projects:${PROJ} | sed 's/^Fee/\\\\Fee/g'`
# Now output to the tex file
cat <<EOF > "${TMPDIR}/${1}.tex"
$LATEX_HEADER
$LATEX_COMPANY
\bigskip \bigskip \bigskip \bigskip \bigskip \bigskip
\begin{raggedright}
\large \bf Bill To: \\ \rm \bigskip
`cat "${PROJSRC}/${PROJ}"`
\end{raggedright}
\bigskip \bigskip \bigskip
\begin{invoice}{US\\$}{0}
\ProjectTitle{${PROJ}: ${DESC} }
% each line of the project ledger item, by payee. get hours &
% values, divide to find rate
$LATEX_FEES
\end{invoice}
$LATEX_NOTES
$LATEX_FOOTER
EOF
pdflatex -output-directory "${TMPDIR}" "${TMPDIR}/${1}.tex"
pdflatex -output-directory "${TMPDIR}" "${TMPDIR}/${1}.tex"
mv -b "${TMPDIR}/${1}.pdf" "${PDFDST}/${1}.pdf"
# export DEBUG=TRUE to skip this step
[ -z "${DEBUG}" ] && rm -rf "${TMPDIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.