ShippedQty: keep_matches option
[kivitendo-erp.git] / SL / DATEV.pm
index 9f8719b..c39ba34 100644 (file)
@@ -312,7 +312,7 @@ sub export {
 
   die 'no format set!' unless $self->has_format;
 
-   if ($self->format == DATEV_FORMAT_CSV) {
+  if ($self->format == DATEV_FORMAT_CSV) {
     $result = $self->csv_export;
   } elsif ($self->format == DATEV_FORMAT_KNE) {
     $result = $self->kne_export;
@@ -368,12 +368,20 @@ sub generate_datev_data {
   $main::lxdebug->enter_sub();
 
   my ($self, %params)   = @_;
-  my $fromto            = $params{from_to};
+  my $fromto            = $params{from_to} // '';
   my $progress_callback = $params{progress_callback} || sub {};
 
   my $form     =  $main::form;
 
   my $trans_id_filter = '';
+  my $ar_department_id_filter = '';
+  my $ap_department_id_filter = '';
+  my $gl_department_id_filter = '';
+  if ( $form->{department_id} ) {
+    $ar_department_id_filter = " AND ar.department_id = ? ";
+    $ap_department_id_filter = " AND ap.department_id = ? ";
+    $gl_department_id_filter = " AND gl.department_id = ? ";
+  }
 
   if ( $self->{trans_id} ) {
     # ignore dates when trans_id is passed so that the entire transaction is
@@ -400,6 +408,7 @@ sub generate_datev_data {
          t.rate AS taxrate, t.taxdescription,
          'ar' as table,
          tc.accno AS tax_accno, tc.description AS tax_accname,
+         ar.department_id,
          ar.notes
        FROM acc_trans ac
        LEFT JOIN ar          ON (ac.trans_id    = ar.id)
@@ -410,6 +419,7 @@ sub generate_datev_data {
        WHERE (ar.id IS NOT NULL)
          AND $fromto
          $trans_id_filter
+         $ar_department_id_filter
          $filter
 
        UNION ALL
@@ -422,6 +432,7 @@ sub generate_datev_data {
          t.rate AS taxrate, t.taxdescription,
          'ap' as table,
          tc.accno AS tax_accno, tc.description AS tax_accname,
+         ap.department_id,
          ap.notes
        FROM acc_trans ac
        LEFT JOIN ap        ON (ac.trans_id  = ap.id)
@@ -432,6 +443,7 @@ sub generate_datev_data {
        WHERE (ap.id IS NOT NULL)
          AND $fromto
          $trans_id_filter
+         $ap_department_id_filter
          $filter
 
        UNION ALL
@@ -444,6 +456,7 @@ sub generate_datev_data {
          t.rate AS taxrate, t.taxdescription,
          'gl' as table,
          tc.accno AS tax_accno, tc.description AS tax_accname,
+         gl.department_id,
          gl.notes
        FROM acc_trans ac
        LEFT JOIN gl      ON (ac.trans_id  = gl.id)
@@ -453,11 +466,17 @@ sub generate_datev_data {
        WHERE (gl.id IS NOT NULL)
          AND $fromto
          $trans_id_filter
+         $gl_department_id_filter
          $filter
 
        ORDER BY trans_id, acc_trans_id|;
 
-  my $sth = prepare_execute_query($form, $self->dbh, $query);
+  my @query_args;
+  if ( $form->{department_id} ) {
+    push(@query_args, ($form->{department_id}) x 3);
+  }
+
+  my $sth = prepare_execute_query($form, $self->dbh, $query, @query_args);
   $self->{DATEV} = [];
 
   my $counter = 0;
@@ -1155,7 +1174,7 @@ sub _format_accno {
 sub csv_export_for_tax_accountant {
   my ($self) = @_;
 
-  $self->_get_transactions(from_to => $self->fromto);
+  $self->generate_datev_data(from_to => $self->fromto);
 
   foreach my $transaction (@{ $self->{DATEV} }) {
     foreach my $entry (@{ $transaction }) {
@@ -1183,7 +1202,7 @@ sub csv_export_for_tax_accountant {
     taxkey            => { 'text' => $::locale->text('Taxkey'), },
     tax_accname       => { 'text' => $::locale->text('Tax Account Name'), },
     tax_accno         => { 'text' => $::locale->text('Tax Account'), },
-    transdate         => { 'text' => $::locale->text('Invoice Date'), },
+    transdate         => { 'text' => $::locale->text('Transdate'), },
     vcnumber          => { 'text' => $::locale->text('Customer/Vendor Number'), },
   );
 
@@ -1447,6 +1466,25 @@ Returns a list of errors that occured. If no errors occured, the export was a su
 Exports data. You have to have set L<exporttype> and L<format> or an error will
 occur. OBE exports are currently not implemented.
 
+=item csv_export_for_tax_accountant
+
+Generates up to four downloadable csv files containing data about sales and
+purchase invoices, and their respective payments:
+
+Example:
+  my $startdate = DateTime->new(year => 2012, month =>  1, day =>  1);
+  my $enddate   = DateTime->new(year => 2012, month => 12, day => 31);
+  SL::DATEV->new(from => $startdate, to => $enddate)->csv_export_for_tax_accountant;
+  # {
+  #   'download_token' => '1488551625-815654-22430',
+  #   'filenames' => [
+  #                    'Zahlungen Kreditorenbuchungen 2012-01-01 - 2012-12-31.csv',
+  #                    'Kreditorenbuchungen 2012-01-01 - 2012-12-31.csv',
+  #                    'Zahlungen Debitorenbuchungen 2012-01-01 - 2012-12-31.csv',
+  #                    'Debitorenbuchungen 2012-01-01 - 2012-12-31.csv'
+  #                  ]
+  # };
+
 =back
 
 =head1 ATTRIBUTES