About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account

Exercise 13: Creating an Invoice for a Customer (invoice.sty) using CSV Files (Solution)

This is a solution to the CSV part of Exercise 13. I've used the letter class file for my solution and used an empty \opening{} to ensure the recipient's address is displayed.

You need the sample files people.csv, booklist.csv, country-codes.csv, ordergroups.csv and orders.csv.

\documentclass[12pt]{letter}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

\usepackage[a4paper]{geometry}
\usepackage[british]{babel}

\usepackage{datatool}

\usepackage{invoice}

\DTLloaddb{orders}{orders.csv}
\DTLloaddb{ordergroups}{ordergroups.csv}
\DTLloaddb{books}{booklist.csv}
\DTLloaddb{countries}{country-codes.csv}
\DTLloaddb{people}{people.csv}

\renewcommand*{\Fees}{Products}
\renewcommand*{\UnitRate}{Price}
\renewcommand*{\Count}{Quantity}
\renewcommand*{\Activity}{Product}

\DTLassignfirstmatch{ordergroups}{id}{2}{%
  \CustomerId=customerid,%
  \OrderDiscount=discount,%
  \Postage=postage%
}
\xDTLassignfirstmatch{people}{id}{\CustomerId}{%
  \Title=title,%
  \Forenames=forenames,%
  \Surname=surname,%
  \AddressI=address1,%
  \AddressII=address2,%
  \Town=town,%
  \County=county,%
  \Postcode=postcode,%
  \CountryCode=country%
}
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}

\begin{document}

\begin{letter}{\DTLifnullorempty{\Title}{}{\Title\ }%
  \Forenames\ \Surname\\%
  \AddressI\\%
  \DTLifnullorempty{\AddressII}{}{\AddressII\\}%
  \Town\\%
  \DTLifnullorempty{\County}{}{\County\\}%
  \Postcode\\%
  \CountryName}

  \opening{}

  \begin{invoice}{\pounds}{0}
   \ProjectTitle{Book Order}%
   \DTLforeach*[\equal{\OrderGroupId}{2}]{orders}%
   {%
     \OrderGroupId=groupid,%
     \BookId=bookid,%
     \OrderQuantity=quantity%
   }%
   {%
     \xDTLassignfirstmatch{books}{id}{\BookId}%
     {%
       \BookTitle=title,%
       \BookAuthor=author,%
       \BookFormat=format,%
       \BookPrice=price%
     }%
     \Fee{\BookTitle\newline 
     by \BookAuthor\ (\BookFormat)}{\BookPrice}{\OrderQuantity}%
   }%
   \EBC{Postage and Packaging}{\Postage}%
   \Discount{Promotion}{\OrderDiscount}%
  \end{invoice}

\end{letter}

\end{document}

Download invoice-csv.tex or invoice-csv.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs