\newif\ifpo@colour
\DeclareOption{colour}{\po@colourtrue}
\DeclareOption{color}{\po@colourtrue}
\DeclareOption{nocolour}{\po@colourfalse}
\DeclareOption{nocolor}{\po@colourfalse}
\ExecuteOptions{colour}
\ProcessOptions\relax

\RequirePackage{mdwtab}
\ifpo@colour
  \RequirePackage{colour, mtcolour}
\fi

\newcount\po@total
\newcount\po@unit
\newcount\po@qty

\def\poinit{\global\po@total\z@}
\def\poline{\global\po@qty\@ne\global\po@unit\z@}
\def\poqty#1{\global\po@qty#1\relax}
\def\pounit#1{\po@split\pounit@i#1..\q@delim}
\def\po@split#1#2.#3.#4\q@delim{%
  \count@#2%
  \multiply\count@100%
  \ifx.#3.\else%
    \count\tw@#3%
    \advance\count@\count\tw@%
  \fi%
  #1%
}
\def\pounit@i{\global\po@unit\count@}
\def\po@@tot{%
  \count@\po@unit%
  \multiply\count@\po@qty%
}
\def\po@@vat{%
  \count@\po@total%
  \multiply\count@175%
  \advance\count@500%
  \divide\count@1000%
}
\def\poadd#1{%
  \csname po@@#1\endcsname%
  \global\advance\po@total\count@%
}
\def\po@@unit{\count@\po@unit}
\def\po@@total{\count@\po@total}
\def\q@delim{\q@delim}
\def\pocash#1{%
  \csname po@@#1\endcsname%
  \count\tw@\count@%
  \divide\count\tw@100%
  \multiply\count\tw@100%
  \advance\count@-\count\tw@%
  \divide\count\tw@100%
  \relax\the\count\tw@%
  .%
  \expandafter\po@lead\the\count@\q@delim%
}
\def\po@lead#1#2\q@delim{\ifx\q@delim#2\q@delim0#1\else#1#2\fi}
\ifpo@colour
  \def\altcolour{%
    \ifmod{\value{tabrow}}{2}{0}{\rowcolour{white}}{\rowcolour[grey]{0.95}}%
  }
\else
  \let\altcolour\@empty
\fi
\def\po{%
  \small%
  \poinit%
  \begin{tabular}[C]{|?>{\altcolour}l|>{\raggedright}m{1.6in}|r|r|r|}%
    \hlx{hv}%
    \bfseries Part&%
    \bfseries Description&%
    $\vcenter{\bfseries\halign
      {&\strut\hfil##\cr Unit\cr price (\textsterling)\cr}}$&%
    \bfseries Qty&%
    $\vcenter{\bfseries\halign
      {&\strut\hfil##\cr Total\cr price (\textsterling)\cr}}$\\%
    \hlx{vh}%
}
\def\poitem{\hlx{v}\@testopt\poitem@i\@ne}
\def\poitem@i[#1]#2#3#4{%
    #2&%
    #3&%
    \pounit{#4}\pocash{unit}&%
    \poqty{#1}\the\po@qty&%
    \pocash{tot}\poadd{tot}%
    \\%
    \hlx{v+}%
}
\def\posep{\hlx{hv}}
\def\endpo{%
    \hlx{h?{\global\c@tabrow\z@}v[1-3]}%
    \multicolumn{4}{|l|}{Total excl. VAT}&\pocash{total}\\%
    \hlx{v[1-3]v[1-3]}%
    \multicolumn{4}{|l|}{VAT (@17.5\%)}&\pocash{vat}\poadd{vat}\\%
    \hlx{v[1-3]hv[1-3]}%
    \multicolumn{4}{|l|}{Total incl. VAT}&\pocash{total}\\%
    \hlx{v[1-3]h}%
  \end{tabular}%
}
\endinput
