{% autoescape off %}
{% load creme_core_tags creme_latex %}

{% with source=object.source currency=object.currency %}
{% with billing_address=object.billing_address source_address=source.billing_address %}

\documentclass[french,11pt]{article}
\usepackage[french]{babel}
\usepackage[french]{layout}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{units}
\usepackage{bera}
\usepackage{graphicx}
\usepackage{textcase}
\usepackage{fancyhdr}
\usepackage{fp}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{color}
\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}

\geometry{verbose,tmargin=4em,bmargin=8em,lmargin=6em,rmargin=6em}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}

\thispagestyle{fancy}
\pagestyle{fancy}
\setlength{\parindent}{0pt}

\renewcommand{\headrulewidth}{0pt}
\definecolor{hydarkblue}{rgb}{0,0.38,0.62}
\definecolor{hylightblue}{rgb}{0.30,0.33,0.37}
\newcolumntype{M}[1]{>{\raggedleft}p{#1}}

\def\SourceName{%
{{source|latex_escape}}
}

\def\EditDate{%
{{object.issuing_date|date:'d-m-Y'}}
}

\def\TargetName{% 
{{object.target|latex_escape}}
}

\def\Number{%
{% if object.number %} {{object.number|latex_escape}} {% else %} Numéro non généré {% endif %}
}

\def\DocumentName{% 
{{object.entity_type}}
}
\newcommand{\HyHeader}[1]{
  \centering{#1}
}

\newcommand{\HySourceName}[1]{
{\Large\textbf{#1{% templatetag closevariable %}
}


\newcommand{\HyDocName}[1]{
\color{hydarkblue}{% templatetag openvariable %}\Huge\textbf{\MakeTextUppercase{#1}{% templatetag closevariable %}}
}

\newcommand{\HyBillingHeader}[1]{

        \begin{tabular}{| c | c |}
    {% block doc_header  %}
        \hline Numéro & {#1} \\
        \hline Date & {\EditDate}\\
        \hline Réglement & {{object.payment_type|default:'À 30 jours'}} \\
    {% endblock %}
        \hline 
        \end{tabular}

}

{% comment %}
\def\TargetAddress{%
{{billing_address.address|latex_escape}} \\
{{billing_address.city|latex_escape}} {{billing_address.po_box|latex_escape}} \\
}
{% endcomment %}

{% block footer %}
\cfoot{ \footnotesize{ SIRET : {{source.siret}} - NAF : {{source.naf}} -  RCS : {{source.rcs}} - Numéro TVA : {{source.tvaintra}} } }
{% endblock %}

\begin{document}


{% block source_info %}
\begin{tabular}{@{}p{0.5\linewidth}M{0.5\linewidth{% templatetag closevariable %}
\HySourceName{\SourceName} & \HyDocName{\DocumentName} \tabularnewline
{{source_address.address|latex_escape}} &  \tabularnewline
{{source_address.city|latex_escape}} {{source_address.po_box|latex_escape}} &  \tabularnewline
\end{tabular}
{% endblock %}

\vspace{1cm}

{% block target_info %}
\begin{tabular}{@{}p{10cm} l }
\multirow{3}{*} {\HyBillingHeader{\Number}} & \textbf{\TargetName} \\
 & {{billing_address.address|latex_escape}} \\
 & {{billing_address.city|latex_escape}} {{billing_address.po_box|latex_escape}} \\
\end{tabular}
{% endblock %}

\vspace{2cm}


{% block lines %}
\begin{longtable}
   {|p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}|}
   \hline
    \HyHeader{Description} &  \HyHeader{Qté} &  \HyHeader{PU HT} &  \HyHeader{Montant HT}  \endhead
   \hline
   \endfoot
\hline
\multicolumn{3}{c} {} \tabularnewline \cline{3-4}
\multicolumn{2}{c|} {} & \centering{ Total HT } & \centering{ {{object.total_no_vat|format_amount:currency|latex_escape}} } \tabularnewline \cline{3-4}
\multicolumn{2}{c|} {} & \centering{ TVA } & \centering{ {{object.total_vat|sub:object.total_no_vat|format_amount:currency|latex_escape}} } \tabularnewline \cline{3-4}
\multicolumn{2}{c|} {} & \centering{ TTC } & \centering{ {{object.total_vat|format_amount:currency|latex_escape}} } \tabularnewline \cline{3-4}
\endlastfoot

{% for line in object.iter_all_lines %}
   \hline
   \centering{ {{line.related_item|default:line.on_the_fly_item|latex_escape}} } & \centering{ {{line.quantity}} } &  \centering{ {{line.unit_price|format_amount:currency|latex_escape}} } & \centering{ {{line.get_raw_price|format_amount:currency|latex_escape}} } \tabularnewline
{% endfor %}
\end{longtable}
{% endblock %}

\vspace{3cm}

{% block bank_info %}
{% with pinfo=object.payment_info %}
{% if pinfo %}
Vous pouvez régler par chéque ou par virement bancaire sur le compte suivant : \\
\begin{center}

\begin{tabular}{| c | c | c | c | c |  }
\hline
Banque & Guichet & Numéro de Compte & Clé RIB & Domiciliation \\
\hline
 {{pinfo.bank_code|latex_escape}} & {{pinfo.counter_code|latex_escape}}  & {{pinfo.account_number|latex_escape}} & {{pinfo.rib_key|latex_escape}}   & {{pinfo.banking_domiciliation|latex_escape}}  \\
\hline
\end{tabular}
\end{center}
{% endif %}
{% endwith %}
{% endblock %}

{% block payment_notice %}{% endblock %}

\end{document}

{% endwith %}
{% endwith %}
{% endautoescape %}
