%------------------------------------------------------------------
% generic letter template based on scrlttr2
%
% Author: Karsten Hilbert
% Author: karsten.hilbert@gmx.net
% License: GPL v2 or later
%
% requires pdflatex to be run with -recorder option
%------------------------------------------------------------------
% documentclass with options for *all* packages:
\documentclass[
	version=last,							% use latest scrlttr2 code
	BCOR=0mm,								% keine BindeCORrektur
	paper=portrait,							% not landscape
	paper=a4,								% set paper size
	fontsize=11pt,							% set fontsize early such that recalctypearea is not necessary
	foldmarks=true,							% switch on foldmarks
	foldmarks=HPV,							% select ALL foldmarks (Horz/Punch/Vert)
	enlargefirstpage=on,					% allow for more text on first page, if possible
	firsthead=true,							% do show Sender information on first page
	fromalign=left,							% center Sender information at top of first page
	fromrule=afteraddress,					% horiz rule after Sender address at top of first page
	fromurl=false,							% explizit in der Fußzeile
	fromemail=false,						% explizit in der Fußzeile
	fromfax=false,							% explizit in der Fußzeile
	fromphone=false,						% explizit in der Fußzeile
	frommobilephone=false,					% explizit in der Fußzeile
	fromlogo=true,							% at top of page, if available
	symbolicnames=true,						% use MarvoSym symbols for <from*> labelling rather than textual labels
	locfield=wide,							% make available lots of space in the location field
	headsepline=true,						% do show line below page header
	footsepline=true,						% do show line above page footer
	pagenumber=footright					% where to place page number by default
]{scrlttr2}


% package loading:
\usepackage{scrlayer-scrpage}				% header and footer control
\usepackage[ngerman]{babel}					% Deutsch und Trennung
\usepackage[utf8]{inputenc}					% input is UTF8
\usepackage[T1]{fontenc}					% fonts are T1
\usepackage{textcomp}						% symbols for textmode escaping, does not mix with "ucs", unless utf8x used
\usepackage{marvosym}						% symbols: Handy, Telefon, E-Mail
\usepackage{lmodern}						% sans serif Latin Modern
\usepackage{array}							% improved table column styles
\usepackage{graphicx}						% enhanced graphics loading (logo and signature)
\usepackage[space,extendedchars]{grffile}	% better access to graphics files
\usepackage[export]{adjustbox}				% improved options for \includegraphics
\usepackage{ragged2e}						% improved alignment
\usepackage{lastpage}						% easy access to page number of last page
\usepackage{embedfile}						% store data files in PDF (patient, praxis)
%\usepackage[abspath]{currfile}				% generically refer to LaTeX input file
\usepackage{calc}							% \widthof (for signature)
\usepackage{microtype}						% micro-adjustments to typesetting
% debugging
$<if_debugging:://%::>$\usepackage{interfaces}				% provides \papergraduate
$<if_debugging:://%::>$\usepackage{showkeys}				% print labels (anchors and stuff) as margin notes
$<if_debugging:://%::>$\usepackage[noframe]{showframe}		% show page margins as boxes
$<if_debugging:://%::>$\usepackage[verbose]{layout}			% generate a page with the current layout
$<if_debugging:://%::>$\usepackage[pass]{geometry}			% nicely display geometry in log file
$<if_debugging:://%::>$\usepackage{afterpage}				% add stuff after next page bread
% load last:
\usepackage{hyperref}						% aktive URLs, needs to be loaded last, most of the time


% package setup:
% - debugging
\listfiles																% list all input files used
\errorcontextlines 10000												% list lots of context lines with errors
\RequirePackage[l2tabu, orthodox]{nag}									% show unorthodox/faulty/obsolete uses
\hypersetup{debug=true}													% extra debugging log messages
% %\tracingtabu=3														% trace tabu tables if using tabu package
$<if_debugging:://%::>$\usetikz{basic}									% needed for \papergraduate from package interfaces
$<if_debugging:://%::>$\overfullrule=2cm								% mark overfull horizontal boxes
$<if_debugging:://%::>$\LoadLetterOption{visualize}						% visualize letter title page layout
$<if_debugging:://%::>$\showfields{head,foot,address,location,refline}	% show srlttr2 fields
$<if_debugging:://%::>$\geometry{pass,verbose}							% put geometry into log file
$<if_debugging:://%::>$\renewcommand\ShowFrameLinethickness{0.25pt}		% configure showframe
$<if_debugging:://%::>$\renewcommand*\ShowFrameColor{\color{red}}		% configure showframe

% - PDF metadata
\hypersetup{%
	pdftitle = {Patient: $<name::%(firstnames)s %(lastnames)s::>$, $<date_of_birth::%d.%m.%Y::>$},
	pdfauthor = {$<current_provider::::>$, $<praxis::%(branch)s, %(praxis)s::>$},
	pdfsubject = {Begleitbrief},
	pdfproducer = {GNUmed $<client_version::::>$, Template $<form_name_long::::>$, $<form_version::::>$ [$<form_version_internal::::>$, $<form_last_modified::::>$]},
	pdfdisplaydoctitle = true,
	pdfprintscaling = None
}

% - base font: sans serif Latin Modern
\renewcommand*\familydefault{\sfdefault}

%------------------------------------------------------------------
\begin{document}


% letter options and settings:
%\LoadLetterOption{DIN}				% suitable for C4/C5/C6/C6-lang
% - Adressfeld etwas weiter oben (per Faltmarkenverschiebung)
\LoadLetterOption{DINmtext}			% suitable for C6/C6-lang only


% - sender details
\setkomavar{fromname}{%
	$<current_provider_name::%(title)s %(firstnames)s %(lastnames)s::>$, FA für $<current_provider_external_id::Fachgebiet//Ärztekammer::50>$
}
\setkomavar{fromaddress}{%
	\small
	$2<ph_cfg::argumentsdivider//#2#//%% <%(name)s> set to [%(value)s]::>2$
	$<praxis::%(praxis)s, %(branch)s::120>$$2<if_not_empty::$<praxis_address::%(street)s::>$#2#\\#2#::>2$
	$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
	$3<if_not_empty::$<praxis_address::%(street)s::>$#3#\href{http://nominatim.openstreetmap.org/search/$2<url_escape::$<praxis_address::%(country)s::>$::>2$/$2<url_escape::$<praxis_address::%(urb)s::>$::>2$/$2<url_escape::$<praxis_address::%(street)s::>$::>2$/$2<url_escape::$<praxis_address::%(number)s::>$::>2$?limit=3}{$<praxis_address::%(street)s %(number)s::>$$2<if_not_empty::$<praxis_address::%(subunit)s::>$#2# (%s)#2#::>2$$<praxis_address::, %(postcode)s %(urb)s::>$}#3#%% no praxis address available::>3$
	$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$
	$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
}
\setkomavar{fromlogo}{%
	$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
	$2<ph_cfg::argumentsdivider//#2#//%% <%(name)s> set to [%(value)s]::>2$
	$1<ph_cfg::argumentsdivider//#1#//%% <%(name)s> set to [%(value)s]::>1$
	$3<if_not_empty::$1<data_snippet::praxis-logo#1#%s#1#image/png#1#.png::>1$#3#$2<if_not_empty::$1<praxis_comm::web::>1$#2#\href{http://%s}{$1<data_snippet::praxis-logo#1#\includegraphics[width=20mm]{%s}#1#image/png#1#.png::250>1$}#2#$1<data_snippet::praxis-logo#1#\includegraphics[width=20mm]{%s}#1#image/png#1#.png::250>1$::>2$#3#$2<if_not_empty::$1<praxis_scan2pay::fmt=txt::>1$#2#\href{https://www.scan2pay.info}{\includegraphics[width=15mm]{$1<praxis_scan2pay::fmt=qr::>1$}} %% no praxis logo available#2#% no praxis logo available::>2$::>3$
	$1<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>1$
	$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
	$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$
}
% explicit backaddress, not calculated from <from*> values
\setkomavar{backaddress}{$<current_provider_firstnames::::1>$.$<current_provider_lastnames::::>$\\$<praxis_address::%(street)s %(number)s\\%(postcode)s %(urb)s::60>$}
\setkomavar{location}{%
	\begin{tabular}[t]{cc}%
		Patient & Absender \\
		$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
		\includegraphics[width=30mm]{$<patient_mcf::fmt=qr//%s::>$} & $3<if_not_empty::$1<praxis_address::%(street)s::>1$#3#\href{http://nominatim.openstreetmap.org/search/$2<url_escape::$1<praxis_address::%(country)s::>1$::>2$/$2<url_escape::$1<praxis_address::%(urb)s::>1$::>2$/$2<url_escape::$1<praxis_address::%(street)s::>1$::>2$/$2<url_escape::$1<praxis_address::%(number)s::>1$::>2$?limit=3}{\includegraphics[width=30mm]{$1<praxis_mcf::fmt=qr//%s::>1$}}#3#\includegraphics[width=30mm]{$1<praxis_mcf::fmt=qr//%s::>1$} %% no praxis address available::>3$
		$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$
	\end{tabular}%
}
% not needed:
%\setkomavar{fromurl}{}
%\setkomavar{fromemail}{}
%\setkomavar{fromfax}{}
%\setkomavar{fromphone}{}
%\setkomavar{frommobilephone}{}


% - reference line
\setkomavar{myref}{$2<free_text::"Unser Zeichen" (z.B. "$1<current_provider_name::%(firstnames)s::1>1$$1<current_provider_name::%(lastnames)s::1>1$" für $1<current_provider_name::%(firstnames)s %(lastnames)s::>1$)//$1<current_provider_name::%(firstnames)s::1>1$$1<current_provider_name::%(lastnames)s::1>1$::40>2$}
\setkomavar{yourref}{$2<free_text::"Ihr Zeichen"::40>2$}
\setkomavar{yourmail}{$2<free_text::"Ihr Schreiben vom"::20>2$}
\setkomavar{date}{$<today::%d.%B %Y::50>$}
\setkomavar{place}{$<praxis_address::%(urb)s::120>$}


% - Subject, namely patient data
\setkomavar{subject}[]{%
	$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
	$3<if_not_empty::$2<free_text::Betreff (Patientenstammdaten werden hinzugefügt)::120>2$#3#%s\\#3#%% no additional Subject by user::>3$
	$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$
	$2<ph_cfg::argumentsdivider//#2#//%% <%(name)s> set to [%(value)s]::>2$
	Patient: $<title::::>$ $<firstname::::>$ $<lastname::::1>$\textsc{$<lastname::::2-99>$} (geb $<date_of_birth::%d.%B %Y::>$)$2<if_not_empty::$<adr_street::home::>$#2#\\#2#::>2$
	$2<if_not_empty::$<adr_street::home::>$#2#Adresse: $<adr_street::home::>$ $<adr_number::home::>$, $<adr_postcode::home::>$ $<adr_location::home::>$#2#%% no address available::>2$
	$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
}


% - signature
\setkomavar{signature}{%
	$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
	$3<if_not_empty::$2<data_snippet::autograph-$<current_provider_lastnames::::>$_$<current_provider_firstnames::::>$//\includegraphics[width=30mm]{%s}//image/png//.png::250>2$#3#%s\\#3#%% no signature file available::>3$
	$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$
	\rule{\widthof{\tiny{}(Der Unterzeichner haftet nicht für unsignierte Änderungen des Inhalts.)}}{.1pt}\\
	$2<ph_cfg::argumentsdivider//#2#//%% <%(name)s> set to [%(value)s]::>2$
	$2<if_not_empty::$<current_provider_name::%(title)s::>$#2#$<current_provider_name::%(title)s %(firstnames)s %(lastnames)s::>$\\#2#$<current_provider_name::%(firstnames)s %(lastnames)s::>$\\::>2$
	$2<if_not_empty::$<current_provider_external_id::Fachgebiet//Ärztekammer::50>$#2#FA für %s\\#2#%% no speciality available::>2$
	$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
	{\tiny{}(Der Unterzeichner haftet nicht für unsignierte Änderungen des Inhalts.)}
}
\renewcommand*{\raggedsignature}{\raggedright}
\makeatletter
\@setplength{sigbeforevskip}{1.8cm}							% Definiere vertikalen Abstand vor der Unterschrift
\makeatother


% - headings starting on second page
\lehead{$2<if_not_empty::$<title::::>$//%s //::>2$$<lastname::::1>$\textsc{$<lastname::::2-99>$}, $<firstname::::>$ ($<date_of_birth::%d.%B %Y::>$)}
\lohead{$2<if_not_empty::$<title::::>$//%s //::>2$$<lastname::::1>$\textsc{$<lastname::::2-99>$}, $<firstname::::>$ ($<date_of_birth::%d.%B %Y::>$)}
\cehead{}
\cohead{}
\rehead{Seite \thepage/\pageref{LastPage}}
\rohead{Seite \thepage/\pageref{LastPage}}


% - footer on first page
\setkomavar{firstfoot}{%
	\rule{\textwidth}{.3pt}
	\parbox[t]{\textwidth}{%
		\tiny
		\begin{tabular}[t]{ll}%
			\Telefon{}       & $<praxis_comm::workphone::60>$\\
			\FAX{}           & $<praxis_comm::fax::60>$\\
			\Email{}         & \href{mailto:$<praxis_comm::email::60>$}{$<praxis_comm::email::60>$}\\
			$2<ph_cfg::argumentsdivider//##//%% <%(name)s> set to [%(value)s]::>2$
			\ComputerMouse{} & $2<if_not_empty::$<praxis_comm::web::>$##\href{http://%s}{$<praxis_comm::web::60>$}##%% no praxis web address available::>2$
			$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
		\end{tabular}%
		\hfill
		\begin{tabular}[t]{ll}%
			$2<ph_cfg::argumentsdivider//##//%% <%(name)s> set to [%(value)s]::>2$
			\multicolumn{2}{l}{$2<if_not_empty::$<current_provider_external_id::Fachgebiet//Ärztekammer::50>$##FA für %s##::>2$}\\
			$2<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>2$
			BSNR       & $<praxis_id::KV-BSNR//KV//%(value)s::25>$\\
			LANR       & $<current_provider_external_id::KV-LANR//KV::25>$\\
			\multicolumn{2}{l}{GNUmed $<client_version::::>$ (\href{http://www.gnumed.org}{www.gnumed.org})}
		\end{tabular}%
		\hfill
		\begin{tabular}[t]{ll}%
			\multicolumn{2}{l}{$<praxis_id::Bankname//Bank//%(value)s::60>$}\\
			IBAN       & $<praxis_id::IBAN//Bank//%(value)s::30>$\\
			BIC        & $<praxis_id::BIC//Bank//%(value)s::30>$\\
			\multicolumn{2}{l}{Vorlage $<form_name_long::::60>$ v$<form_version::::20>$}
		\end{tabular}%
	}
}


% - footer starting on second page
\lefoot{{\tiny{}$<current_provider::::>$, $<praxis::%(praxis)s: %(branch)s::120>$}}
\lofoot{{\tiny{}$<current_provider::::>$, $<praxis::%(praxis)s: %(branch)s::120>$}}
\cefoot{}
\cofoot{}
\refoot{{\tiny{}$<today::%d.%B %Y::50>$}}
\rofoot{{\tiny{}$<today::%d.%B %Y::50>$}}


%------------------------------------------------------------------
% start letter by setting receiver data
% FIXME: use if_empty because empty lines are not allowed
\begin{letter}{%
	$<receiver_name::::>$\\
	$<receiver_street::::>$\ $<receiver_number::::>$\ $<receiver_subunit::::>$\\
	$<receiver_postcode::::>$\ $<receiver_location::::>$\\
	$<receiver_country::::>$
}


% debugging
$<if_debugging:://%::>$\papergraduate											% show coordinate system on first page
$<if_debugging:://%::>$\afterpage{\AddToShipoutPicture*{\ShowFramePicture}}		% show "normal" page layout starting with second page


% define opening (Anrede)
\opening{%
	$2<free_text::Anrede, z.B. "Sehr geehrte/geehrter $<receiver_name::::>$,"//Sehr geehrte Kollegin $<receiver_name::::>$,::140>2$%
}


% the actual content
\selectlanguage{ngerman}					% if German
$2<free_text::Der eigentliche Brieftext (in LaTeX)::>2$


% define closing of letter (Grußformel)
% multiple lines with \\, no empty lines
\closing{%
	$2<free_text::Grußformel, z.B. "Mit kollegialen Grüßen,"//Mit kollegialen Grüßen,::140>2$%
}

% no Post Scriptum
%\ps

% Anlagen
%\setkomavar*{enclseparator}[Anlage(n)]		% title for "enclosures" area
$3<ph_cfg::argumentsdivider//#3#//%% <%(name)s> set to [%(value)s]::>3$
$3<if_not_empty::$2<free_text::Liste von Anlagen::300>2$#3#\encl{%s}#3#%\encl{}::>3$
$3<ph_cfg::argumentsdivider//DEFAULT//%% <%(name)s> set to [%(value)s]::>3$

% no Carbon Copies
%\setkomavar*{ccseparator}[Verteiler]
% multiple lines with \\, no empty lines
%\cc{%
%}

\end{letter}
%------------------------------------------------------------------
% embed data into PDF for easier processing:

% VCF of creator
\IfFileExists{$<praxis_vcf::::>$}{%
	\embedfile[
		desc=Praxis - digital business card (sender),
		mimetype=text/vcf,
		ucfilespec=praxis.vcf
	]{$<praxis_vcf::::>$}
}{\typeout{[$<praxis_vcf::::>$] not found}}

% VCF of patient
\IfFileExists{$<patient_vcf::::>$}{%
	\embedfile[
		desc=Patient - digital business card,
		mimetype=text/vcf,
		ucfilespec=patient.vcf
	]{$<patient_vcf::::>$}
}{\typeout{[$<patient_vcf::::>$] (patient VCF) not found}}

%------------------------------------------------------------------

% debugging
$<if_debugging:://%::>$\layout				% show page layout page

\end{document}
