% NGA invoice LaTeX class
% Copyright 2022 by Philippe P. PEBAY philippe.pebay@ng-analytics.com
%
\def\ngainv@version{1.0}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{nga-invoice}[Invoice\ngainv@version]
\LoadClass[11pt]{article}
\RequirePackage[margin=1in]{geometry}
\RequirePackage{datatool}
\RequirePackage{multicol}
\RequirePackage{xcolor}
\RequirePackage{tabularx}
\RequirePackage{graphicx}
\RequirePackage{longtable}
\RequirePackage{dcolumn}
\RequirePackage{fancyhdr}
\RequirePackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan,
    pdftitle={NGA Invoice},
    pdfpagemode=FullScreen,
}

% internal commands
\renewcommand{\rmdefault}{\sfdefault} 
\newcolumntype{d}{D{.}{.}{2}}
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\newcommand*{\ngainv@invoiceid}{Invoice: \ngainv@invoiceidnum}
\newcommand*{\ngainv@invoicepoid}{P.O.: \ngainv@poid}
\newcommand*{\ngainv@invoicedate}{Issue date: \ngainv@date}
\newcommand*{\ngainv@invoicebillperiod}{Billing period: \ngainv@billperiod}
\newcommand*{\ngainv@invoiceterms}{Terms: \ngainv@terms}
\newcommand*{\ngainv@invoicedue}{{\color{red}\fbox{Due date: \ngainv@due}}}
\newcommand*{\ngainv@invoicetotal}{\ngainv@total}

% user commands
\newcommand*{\inputCSVfile}[1]{\gdef\ngainv@inputfile{#1}}
\newcommand*{\invoiceid}[1]{\gdef\ngainv@invoiceidnum{#1}}
\newcommand{\fromaddress}[1]{\long\gdef\ngainv@fromaddress{#1}}
\newcommand{\poid}[1]{\gdef\ngainv@poid{#1}}
\newcommand{\billperiod}[1]{\gdef\ngainv@billperiod{#1}}
\newcommand{\invoicedate}[1]{\gdef\ngainv@date{#1}}
\newcommand{\poterms}[1]{\gdef\ngainv@terms{#1}}
\newcommand{\invoicedue}[1]{\gdef\ngainv@due{#1}}
\newcommand{\invoicetotal}[1]{\gdef\ngainv@total{#1}}
\newcommand{\toaddress}[1]{\gdef\ngainv@toaddress{#1}}
\newcommand*{\ngainv@labtotal}{0}

% Invoice header and footer
\AtBeginDocument{
\fromaddress{%
30N Gould St., Ste 5912\\
Sheridan WY 82801\\ U.S.A.\\[3pt]
\href{mailto:info@ng-analytics.com}{\texttt{info@ng-analytics.com}}\\
%\textbf{Phone:}
+1 (307) 461-4716}
}
%
\pagestyle{fancy}
\parindent=0pt
\lhead{\hspace{15mm}\includegraphics[height=11mm]{../data/NGAlogo.jpg}}
\chead{\huge \textbf{INVOICE}\\[-6mm]}
\rhead{}
\lfoot{}
\cfoot{\href{https://www.ng-analytics.com}{www.ng-analytics.com}
\rfoot{\sffamily\thepage}
}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\columnsep}{14pt}
\setlength{\headheight}{24pt}
\thispagestyle{fancy}
\sffamily
\newcommand*{\printinvoice}{%
\begin{multicols}{3}
\includegraphics[height=4mm]{../data/NGAtext.jpg}\\
\ngainv@fromaddress
\vfill\null
\columnbreak
{\bfseries\ngainv@invoiceid}\\
\ngainv@invoicepoid\\
\ngainv@invoicebillperiod\\
\ngainv@invoicedate\\
\ngainv@invoiceterms\\[2pt]
\ngainv@invoicedue\\
\vfill\null
\columnbreak
{\bfseries Bill to:\\}
\ngainv@toaddress
\end{multicols}

% invoice body
\vspace{15mm}
\DTLsetseparator{;}
\DTLloaddb{invoice}{\ngainv@inputfile}
\setlength{\extrarowheight}{2pt}
\begin{tabularx}{\textwidth}{|X|c|d|d|d|}
\hline
\vphantom{\Huge H}\bfseries Description (cf. hourly report for details) &
\bfseries Category & 
\multicolumn{1}{c|}{\bfseries Quantity} &
\multicolumn{1}{c|}{\bfseries Unit Price} &
\multicolumn{1}{c|} {\bfseries Amount}\\[6pt]\hline
&&&&\\[-40pt]
\DTLforeach{invoice}{%
\description=Description,\category=Category,\quantity=Quantity,\unitvalue=UnitValue,\amount=Amount}{\\%
\description & \category & \quantity & \$\unitvalue & \$\amount}\\[4pt]\hline
\multicolumn{4}{|r} {\vphantom{\Huge H}\bfseries TOTAL AMOUNT DUE:} &
\multicolumn{1}{B{.}{.}{2}|}{\$\ngainv@invoicetotal}\\[6pt]
\hline
\end{tabularx}
\vfill
\begin{center}
Please send {\bfseries electronic payment only} to:\\[2pt]
\begin{tabular}{|p{31mm}l|}
\hline
\multicolumn{2}{|l|}
{{\bfseries Bank of the West} 2287 Second Street, Livermore, CA 94550,
U.S.A.}\\
Account holder: & {\bfseries NexGen Analytics LC}\\
Routing number: & {\bfseries 121100782}\\
Account number: & {\bfseries 050897958}\\
\hline
\end{tabular}
\end{center}

% end of invoice
}
\endinput