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

\newif\if@kivi@infobox
\DeclareOption{reffields}{\@kivi@infoboxfalse}
\DeclareOption{infobox}{\@kivi@infoboxtrue}
\@kivi@infoboxtrue

\newif\if@kivi@faketable
\DeclareOption{faketable}{\@kivi@faketabletrue}

\ProcessOptions\relax


\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{a4paper,left=\useplength{toaddrhpos}, right=\useplength{toaddrhpos}}

\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 {5em}
\dim_new:N \g_kivi_tab_price_dim
\dim_gset:Nn \g_kivi_tab_price_dim {5em}
\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}{>{\raggedleft\arraybackslash}p{\g_kivi_tab_price_dim}}

%\if@kivi@faketable
\RequirePackage{environ}
\seq_new:N \l_kivi_PricingTable_seq
\newcommand{\FakeTable}[1]{
%	\tl_set:No \l_tmpa_tl {\BODY}
	\seq_set_split:Nnn \l_kivi_PricingTable_seq {\tabularnewline} {#1}
	\seq_map_inline:Nn \l_kivi_PricingTable_seq {
	\tabular{@{}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}}@{}}
	##1
	\endtabular
	}
	}
}
%\else
\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
}
%\fi

\ExplSyntaxOff

\if@kivi@infobox
\newkomavar{locationtitle}
\setkomavar{location}{
	\begin{flushright}
		\bfseries
		\LARGE
		\usekomavar{locationtitle}
	\end{flushright}

	\medskip
	\begin{tabularx}{\linewidth}{@{}l<{:}>{\raggedright\arraybackslash}X@{}}
		\datum&\usekomavar{date}\\
		\kundennummer&\usekomavar{customer}\\
		\ansprechpartner&\usekomavar{fromname}\\
		\ifkomavarempty{fromphone}{}{\textTelefon&\usekomavar{fromphone}\\}
		\ifkomavarempty{fromemail}{}{\textEmail&\usekomavar{fromemail}\\}
	\end{tabularx}
}
\removereffields

\ExplSyntaxOn
\AtBeginLetter{
	\ifkomavarempty{title}{}{
		\exp_args:Nno\setkomavar{locationtitle}{\scr@title@var}
		\setkomavar{title}{}
	}
}
\ExplSyntaxOff
\fi



\renewcommand*{\raggedsignature}{\raggedright}

\endinput
