\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{kiviletter}[2019/10/10 Letter Layouts for Kivitendo]

\RequirePackage{expl3}
\RequirePackage{iftex}
% Schriftart, Eingabelayout der Tastatur
\ifPDFTeX
\RequirePackage[utf8]{inputenc}% Nur notwendig, wenn Basis älter als TL2018
\RequirePackage[T1]{fontenc}
\else
\RequirePackage{fontspec}
\fi

\RequirePackage{xltabular}
\RequirePackage{booktabs}
\PassOptionsToPackage{table}{xcolor}

\RequirePackage{xcolor}
\RequirePackage{graphicx}

\ifPDFTeX
\RequirePackage{eurosym}
\DeclareUnicodeCharacter{20AC}{\euro}
\fi

\RequirePackage[fromlogo,fromalign=right]{scrletter}
\LoadLetterOption{DIN}

\usepackage{geometry}
\geometry{left=\useplength{firstheadhpos}, right=\useplength{firstheadhpos}}

\ExplSyntaxOn
%Scratch variables
\int_new:N \l_kivi_tmp_int
\bool_new:N \l_kivi_tmp_bool
\ExplSyntaxOff

\newsavebox{\shippingAddressBox}


\DeclareNewLayer[
foreground,
hoffset=\useplength{toaddrhpos},
voffset=\dimexpr\useplength{toaddrvpos}+\useplength{toaddrheight}+\baselineskip,
contents={\usebox\shippingAddressBox}
]{kivitendo.shippingaddress}
\DeclareNewPageStyleByLayers{kivitendo.letter.first}{kivitendo.shippingaddress}

\setkomavar{backaddress}{\firma\ $\cdot$ \strasse\ $\cdot$ \ort}
\@setplength{locwidth}{6cm}

\ExplSyntaxOn
\dim_new:N \g_kivi_tab_pos_dim
\dim_gset:Nn \g_kivi_tab_pos_dim {3.5ex}
\dim_new:N \g_kivi_tab_id_dim
\dim_gset:Nn \g_kivi_tab_id_dim {4em}
\dim_new:N \g_kivi_tab_num_dim
\dim_gset:Nn \g_kivi_tab_num_dim {4em}
\dim_new:N \g_kivi_tab_price_dim
\dim_gset:Nn \g_kivi_tab_price_dim {4em}
\dim_new:N \g_kivi_tab_desc_dim

\dim_new:N \g_kivi_tabcolsep_dim
\dim_gset:Nn \g_kivi_tabcolsep_dim {.5\tabcolsep}
\newcommand*{\CalcTabCols}{
	\dim_gset:Nn \g_kivi_tab_desc_dim {\textwidth-\g_kivi_tab_pos_dim -\g_kivi_tab_id_dim-\g_kivi_tab_num_dim - 2\g_kivi_tab_price_dim - 10\g_kivi_tabcolsep_dim}
}

\newcolumntype{P}{>{\arraybackslash}p{\g_kivi_tab_price_dim}}

\newenvironment{PricingTabular}{
	\begingroup
	\setlength{\tabcolsep}{\g_kivi_tabcolsep_dim}
	\CalcTabCols
	\longtable{@{}p{\g_kivi_tab_pos_dim}p{\g_kivi_tab_id_dim}p{\g_kivi_tab_desc_dim}>{\raggedleft\arraybackslash}p{\g_kivi_tab_num_dim}*2{P<{\,\currency}}@{}}
	% Tabellenkopf
	\toprule
	\bfseries\position & \bfseries\artikelnummer & \bfseries\bezeichnung & \bfseries\menge &\multicolumn{1}{P}{\bfseries\einzelpreis}&\multicolumn{1}{P}{\bfseries\gesamtpreis}\\
	\midrule
	\endhead
	\midrule
	\multicolumn{6}{@{}r@{}}{\weiteraufnaechsterseite}\\
	\endfoot
}{
	\endlongtable
	\endgroup
}

\ExplSyntaxOff


\setkomavar{location}{
	\begin{flushright}
		\bfseries
		\LARGE
		\usekomavar{title}
%		\angebot
%		\par
%		\medskip
%		\large\nr ~<%quonumber%>
	\end{flushright}

	\medskip
	\begin{tabularx}{\linewidth}{@{}l<{:}>{\raggedright\arraybackslash}X@{}}
		\datum&\usekomavar{date}\\%%<%transdate%>\\
		\kundennummer&\usekomavar{customer}\\%<%customernumber%>\\
		\ansprechpartner&\usekomavar{fromname}\\%<%employee_name%>\\
		\textTelefon&\usekomavar{fromphone}\\%<%employee_tel%>\\
		\textEmail&\usekomavar{fromemail}\\%<%employee_email%>
	\end{tabularx}
}


\renewcommand*{\raggedsignature}{\raggedright}

\endinput
