commit fd2e0902abd98a3bea5166bf7fafd04fd7039f97
Author: Martin Helmling <martin.helmling@octosoft.eu>
Date:   Thu Dec 29 12:20:49 2016 +0100

    Dateimanagement: PDF Buchungsjournal mit Anhängen der Buchungen
    
    Optinal kann beim Erzeugen eines PDF alle an den einzelnen Buchungen
    angehängte PDF-Belege/Dokumente angehängt werden.
    In der HTML/CSV/PDF-Tabelle gibt es eine weitere Spalte über die Anzahl der Belege.
    Somit kann gesehen werden wo noch Belege fehlen.
    
    Die Spalte wird implizit dazugeschaltet wenn "Beleg" in der Liste miterscheinen soll.
    
    Die zusätzlichen Methoden ist in einem Helper ausgelagert.

diff --git a/SL/Helper/GlAttachments.pm b/SL/Helper/GlAttachments.pm
new file mode 100644
index 000000000..aee71a483
--- /dev/null
+++ b/SL/Helper/GlAttachments.pm
@@ -0,0 +1,101 @@
+#
+# Helper for General Ledger Reports
+#
+# 1. Fetch the Count of PDF-Documents of one item of a General Ledger Report
+# 2. Append the contents of all items of a General Ledger Report
+
+package SL::Helper::GlAttachments;
+
+use strict;
+
+use Exporter 'import';
+our @EXPORT_OK = qw(count_gl_attachments append_gl_pdf_attachments);
+our %EXPORT_TAGS = (
+  all => \@EXPORT_OK,
+);
+use SL::File;
+
+my %gl_types = (
+  'ar' => 'invoice',
+  'ap' => 'purchase_invoice',
+  'gl' => 'gl_transaction',
+);
+
+#
+# Fetch the Count of PDF-Documents with are related to the $id parameter
+# The parameter $gltype may be 'ar','ap' or 'gl'.
+#
+sub count_gl_pdf_attachments {
+  my ($self,$id,$gltype) = @_;
+  return SL::File->get_all_count(object_id   => $id,
+                                 object_type => $gl_types{$gltype},
+                                 mime_type   => 'application/pdf',
+                                 );
+}
+
+# Append the contents of all PDF-Documents to the base $content
+# This Method is only used in SL/Reportgenerator.pm if the $form->{GD} array is set.
+# The elements of the array need the two elements $ref->{type},$ref->{id}
+#
+sub append_gl_pdf_attachments {
+  my ($self,$form,$content) = @_;
+  my @filelist;
+  foreach my $ref (@{ $form->{GL} }) {
+    my @files = SL::File->get_all(object_id   => $ref->{id},
+                                  object_type => $gl_types{$ref->{type}},
+                                  mime_type   => 'application/pdf',
+                                 );
+    push @filelist, $_->get_file for @files;
+  }
+  return $self->merge_pdfs(file_names => \@filelist , inp_content => $content );
+}
+
+1;
+
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+SL::Helper::GlAttachments - Helper for General Ledger Reports
+
+=head1 SYNOPSIS
+
+   $self->count_gl_pdf_attachments($ref->{id},$ref->{type});
+   $self->append_gl_pdf_attachments($form,$base_content);
+
+
+=head1 DESCRIPTION
+
+Helper for General Ledger Reports
+
+1. Fetch the Count of PDF-Documents of one item of a General Ledger Report
+
+2. Append the contents of all items of a General Ledger Report
+
+
+=head1 METHODS
+
+=head2 C<count_gl_pdf_attachments>
+
+count_gl_pdf_attachments($id,$type);
+
+Fetch the Count of PDF-Documents with are related to the $id parameter
+The parameter $type may be 'ar','ap' or 'gl'.
+
+=head2 C<append_gl_pdf_attachments>
+
+append_gl_pdf_attachments($form,$content);
+
+Append the contents of all PDF-Documents to the base $content
+This Method is only used in SL/Reportgenerator.pm if the $form->{GD} array is set.
+The elements of the array need the two elements $ref->{type},$ref->{id}
+
+=head1 AUTHOR
+
+Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
+
+
+=cut
+
diff --git a/SL/ReportGenerator.pm b/SL/ReportGenerator.pm
index 2e12a2acc..30517a199 100644
--- a/SL/ReportGenerator.pm
+++ b/SL/ReportGenerator.pm
@@ -7,6 +7,8 @@ use Text::CSV_XS;
 #use PDF::Table;
 
 use strict;
+use SL::Helper::GlAttachments qw(append_gl_pdf_attachments);
+use SL::Helper::CreatePDF     qw(merge_pdfs);
 
 # Cause locales.pl to parse these files:
 # parse_html_template('report_generator/html_report')
@@ -656,6 +658,11 @@ sub generate_pdf_content {
 
   my $content = $pdf->stringify();
 
+  $main::lxdebug->message(LXDebug->DEBUG2(),"addattachments ?? =".$form->{report_generator_addattachments}." GL=".$form->{GL});
+  if ( $form->{report_generator_addattachments} eq 'yes' && $form->{GL}) {
+    $content = $self->append_gl_pdf_attachments($form,$content);
+  }
+
   my $printer_command;
   if ($pdfopts->{print} && $pdfopts->{printer_id}) {
     $form->{printer_id} = $pdfopts->{printer_id};
diff --git a/bin/mozilla/gl.pl b/bin/mozilla/gl.pl
index fc8a162e6..c2a2f02d5 100644
--- a/bin/mozilla/gl.pl
+++ b/bin/mozilla/gl.pl
@@ -48,6 +48,7 @@ use SL::ReportGenerator;
 use SL::DBUtils qw(selectrow_query selectall_hashref_query);
 use SL::Webdav;
 use SL::Locale::String qw(t8);
+use SL::Helper::GlAttachments qw(count_gl_attachments);
 
 require "bin/mozilla/common.pl";
 require "bin/mozilla/reportgenerator.pl";
@@ -411,7 +412,7 @@ sub generate_report {
 
   my @columns = qw(
     gldate         transdate        id             reference      description
-    notes          source           debit          debit_accno
+    notes          source   doccnt  debit          debit_accno
     credit         credit_accno     debit_tax      debit_tax_accno
     credit_tax     credit_tax_accno projectnumbers balance employee
   );
@@ -451,6 +452,7 @@ sub generate_report {
   $form->{l_datesort} = 'Y';
   $form->{l_debit_tax_accno}  = 'Y';
   $form->{l_balance}          = $form->{accno} ? 'Y' : '';
+  $form->{l_doccnt}           = $form->{l_source} ? 'Y' : '';
 
   my %column_defs = (
     'id'               => { 'text' => $locale->text('ID'), },
@@ -458,6 +460,7 @@ sub generate_report {
     'gldate'           => { 'text' => $locale->text('Booking Date'), },
     'reference'        => { 'text' => $locale->text('Reference'), },
     'source'           => { 'text' => $locale->text('Source'), },
+    'doccnt'           => { 'text' => $locale->text('Document Count'), },
     'description'      => { 'text' => $locale->text('Description'), },
     'notes'            => { 'text' => $locale->text('Notes'), },
     'debit'            => { 'text' => $locale->text('Debit'), },
@@ -493,7 +496,8 @@ sub generate_report {
   $report->set_columns(%column_defs);
   $report->set_column_order(@columns);
 
-  $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir));
+  $form->{l_attachments} = 'Y';
+  $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir l_attachments));
 
   $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
 
@@ -542,6 +546,10 @@ sub generate_report {
     my $row = { };
     map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
 
+    if ( $form->{l_doccnt} ) {
+      $row->{doccnt}->{data} = SL::Helper::GlAttachments->count_gl_pdf_attachments($ref->{id},$ref->{type});
+    }
+
     my $sh = "";
     if ($form->{balance} < 0) {
       $sh = " S";
diff --git a/bin/mozilla/reportgenerator.pl b/bin/mozilla/reportgenerator.pl
index 5e39df1c1..2304e492d 100644
--- a/bin/mozilla/reportgenerator.pl
+++ b/bin/mozilla/reportgenerator.pl
@@ -64,12 +64,15 @@ sub report_generator_export_as_pdf {
   $form->{copies} = max $myconfig{copies} * 1, 1;
 
   my $allow_font_selection = 1;
+  my $allow_attachments    = 0;
   eval { require PDF::API2; };
   $allow_font_selection = 0 if ($@);
+  $allow_attachments    = 1 if $form->{report_generator_hidden_l_attachments};
 
   $form->{title} = $locale->text('PDF export -- options');
   $form->header();
   print $form->parse_html_template('report_generator/pdf_export_options', { 'HIDDEN'               => \@form_values,
+                                                                            'ALLOW_ATTACHMENTS'    => $allow_attachments,
                                                                             'ALLOW_FONT_SELECTION' => $allow_font_selection, });
 
   $main::lxdebug->leave_sub();
diff --git a/locale/de/all b/locale/de/all
index 52973e76d..af182ec99 100644
--- a/locale/de/all
+++ b/locale/de/all
@@ -992,6 +992,7 @@ $self->{texts} = {
   'Do you want to set the account number "#1" to "#2" and the name "#3" to "#4"?' => 'Soll die Kontonummer "#1" zu "#2" und den Name "#3" zu "#4" geändert werden?',
   'Do you want to store the existing onhand values into a new warehouse?' => 'M&ouml;chten Sie die vorhandenen Mengendaten in ein Lager &uuml;bertragen?',
   'Document'                    => 'Dokument',
+  'Document Count'              => 'Anz. PDF Belege',
   'Document Project (database ID)' => 'Projektnummer des Belegs (Datenbank-ID)',
   'Document Project (description)' => 'Projektnummer des Belegs (Beschreibung)',
   'Document Project (number)'   => 'Projektnummer des Belegs',
@@ -1287,6 +1288,7 @@ $self->{texts} = {
   'Export Stammdaten'           => 'Export Stammdaten',
   'Export as CSV'               => 'Als CSV exportieren',
   'Export as PDF'               => 'Als PDF exportieren',
+  'Export as PDF with attachments' => 'Als PDF mit Anhängen exportieren',
   'Export date'                 => 'Exportdatum',
   'Export date from'            => 'Exportdatum von',
   'Export date to'              => 'Exportdatum bis',
@@ -3682,7 +3684,7 @@ $self->{texts} = {
   'found'                       => 'Gefunden',
   'found_br'                    => 'Gef.',
   'from (time)'                 => 'von',
-  'general_ledger_list'         => 'buchungsjournal',
+  'general_ledger_list'         => 'Buchungsjournal',
   'generate cb/ob transactions for selected charts' => 'Buchungen erstellen',
   'gobd-#1-#2.zip'              => 'gobd-#1-#2.zip',
   'h'                           => 'h',
diff --git a/templates/webpages/report_generator/pdf_export_options.html b/templates/webpages/report_generator/pdf_export_options.html
index 444c0b600..5b2f68f3f 100644
--- a/templates/webpages/report_generator/pdf_export_options.html
+++ b/templates/webpages/report_generator/pdf_export_options.html
@@ -14,6 +14,7 @@
 
   <input type="hidden" name="report_generator_pdf_options_set" value="1">
   <input type="hidden" name="report_generator_dispatch_to" value="">
+  <input type="hidden" name="report_generator_addattachments" value="">
 
   <table>
    <tr>
@@ -134,12 +135,18 @@
 [%- ELSE %]
   <p>
    <input type="hidden" name="action" value="report_generator_dispatcher">
-   <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_pdf')" value="[% 'Export as PDF' | $T8 %]">
+   <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_export_as_pdf','')" value="[% 'Export as PDF' | $T8 %]">
+[%- IF ALLOW_ATTACHMENTS %]
+   <input type="submit" class="submit"
+      onclick="submit_report_generator_form('report_generator_export_as_pdf','yes')"
+      value="[% 'Export as PDF with attachments' | $T8 %]">
+[%- END %]
    <input type="submit" class="submit" onclick="submit_report_generator_form('report_generator_back')" value="[% 'Back' | $T8 %]">
   </p>
  <script type="text/javascript"><!--
-      function submit_report_generator_form(nextsub) {
+      function submit_report_generator_form(nextsub,att) {
         document.report_generator_form.report_generator_dispatch_to.value = nextsub;
+        document.report_generator_form.report_generator_addattachments.value = att;
         document.report_generator_form.submit();
       } // -->
  </script>
