\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{rechnung}
\RequirePackage{ifthen}
\RequirePackage{color}
\RequirePackage{eurosym}
\newboolean{@german}
\setboolean{@german}{false}
\DeclareOption{german}{\setboolean{@german}{true}}
\DeclareOption{english}{\setboolean{@german}{false}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}
\ExecuteOptions{german}
\ProcessOptions
\LoadClass[a4paper]{letter}

\newcommand*{\@myref}{}
\newcommand*{\@yref}{}
\newcommand*{\@ymail}{}
\newcommand*{\@subject}{}
\newcommand*{\@billnumber}{}
\newcommand*{\@termsofpayment}{}
\newcommand*{\@letterhead}{}
\newcommand*{\@addressinwindow}{}
\newcommand*{\@footblock}{}
\newcommand*{\myref}[1]{\renewcommand*{\@myref}{\myrefname:\quad~#1}}
\newcommand*{\yref}[1]{\renewcommand*{\@yref}{\yrefname:\quad~#1}}
\newcommand*{\ymail}[1]{\renewcommand*{\@ymail}{\ymailname:\quad~#1 ..??..}}
\newcommand*{\subject}[1]{\renewcommand*{\@subject}{\subjectname:\quad~#1}}
\newcommand*{\billnumber}[1]{\renewcommand*{\@billnumber}{#1}}
\newcommand*{\termsofpayment}[1]{\renewcommand*{\@termsofpayment}{\textbf{\termsofpaymentname:} \quad\parbox[t]{.8\textwidth}{#1}}}
\newcommand*{\letterhead}[1]{\renewcommand*{\@letterhead}{#1}}
\newcommand*{\addressinwindow}[1]{\renewcommand*{\@addressinwindow}{#1}}
\newcommand*{\footblock}[1]{\renewcommand*{\@footblock}{#1}}

\newcommand{\termsofpaymentname}{Zahlungsbedingungen}

\newcommand{\lettergerman}{%
   \def\myrefname{\textsl{Unser Zeichen}}
   \def\ymailname{\underline{Ihre Nachricht vom}}
   \def\yrefname{\underline{Ihr Zeichen}}
   \def\subjectname{}}

\newcommand{\letterenglish}{}
\ifthenelse{\boolean{@german}}
   {\RequirePackage{german}\lettergerman}{\letterenglish}

\setlength{\hoffset}{-1in}
\setlength{\voffset}{-1in}
\setlength{\topmargin}{6mm}
\setlength{\marginparwidth}{0mm}
\setlength{\oddsidemargin}{25mm}
\setlength{\evensidemargin}{25mm}
\setlength{\textwidth}{160mm}
\setlength{\textheight}{200mm}
\setlength{\headheight}{30mm}
\setlength{\headsep}{15mm}

\newdimen\addressfieldwidth
\setlength{\addressfieldwidth}{70mm}
\newdimen\addressfieldheight
\setlength{\addressfieldheight}{45mm}

\newdimen\leftfield
\setlength{\leftfield}{90mm}
\newdimen\rightfield
\setlength{\rightfield}{70mm}

\newcommand{\billname}{Rechnung}

\let\name\@undefined
\let\fromname\@undefined
\let\address\@undefined
\let\fromaddress\@undefined
\let\location\@undefined
\let\fromlocation\@undefined
\let\telephone\@undefined
\let\telephonenum\@undefined

\def\ps@firstpage{\def\@oddhead{%
    \parbox[b][\headheight][b]{\textwidth}{\@letterhead}}
  \def\@oddfoot{\@footblock} \def\@evenhead{} \def\@evenfoot{}}

\def\ps@headings{\def\@oddhead{\parbox{\textwidth}{%
       {\usefont{OT1}{cmdh}{m}{n}\myname}\\[8pt]
       \normalfont\slshape \headtoname\ \ignorespaces\toname
          \hfill \@date \hfill \pagename\ \thepage\\[-8pt]
          \rule{\textwidth}{.4pt}}}
    \let\@evenhead=\@oddhead
    \def\@oddfoot{} \let\@evenfoot=\@oddfoot}
\ps@headings

\newcommand{\to@label}[2]{\parbox[][35mm][c]{\addressfieldwidth}{%
                     #1\\[1ex] #2}}
\newcommand\myreturn{}


\newcommand{\refbox}{\setbox0\vbox{\makebox[\textwidth][1]{%
   }
   \ifthenelse{\lengthtest{\@ymail >\z@}}{\\ \@ymail .---. \hfill\\}{}
   \ifthenelse{\lengthtest{\@yref >\z@}}{\\ \@yref \hfill\\}{}
   \ifthenelse{\lengthtest{\@myref >\z@}}{\\ \@myref \hfill\\}{}
   \ifthenelse{\lengthtest{\@subject >\z@}}{\\[3ex] \@subject \hfill\\}{}}
   \ifthenelse{\lengthtest{\ht0 >\z@}}{\vspace{1.5\parskip} \box0}{}}

\def\opening#1{\thispagestyle{firstpage}
    \parbox[b][\addressfieldheight][t]{\addressfieldwidth}{{\normalfont\fontsize{8}{9.6pt}\ifx\@empty\@addressinwindow $\;$\else \@addressinwindow\fi} \\[-2.5mm]\rule{\addressfieldwidth}{.5pt} \to@label\toname\toaddress}\\[6.5mm] \hspace*{-20mm}\rule{9mm}{.5pt}\\ \par
    {\large\bfseries \billname}\vspace*{1em}

    \setlength{\fboxsep}{1.5ex}
    \fbox{%
      \parbox{.75\textwidth}{%
        \raisebox{.8ex}[-.8ex]{\colorbox{white}{Bitte bei Zahlungen und R\"uckfragen angeben}}\\
        \hspace*{1.5ex}\textbf{Rechnungs--Nr.:} \@billnumber \null\hspace{\fill} \textbf{Datum:} \@date
      }
    }
    \par

    \vspace{3\parskip}}

\long\def\closing#1{\par\nobreak\vspace{\parskip}
    \stopbreaks \noindent
    \@termsofpayment\\[3\medskipamount]
    \parbox{\indentedwidth}{
    \ignorespaces #1}\par}

\let\@texttop=\relax


