Mahnungsbericht: Dokumente nur für ersten Eintrag eines Mahnlaufs anzeigen
[kivitendo-erp.git] / bin / mozilla / dn.pl
index c6c520e..9422da3 100644 (file)
@@ -38,8 +38,10 @@ use SL::IS;
 use SL::DN;
 use SL::DB::Department;
 use SL::DB::Dunning;
+use SL::File;
 use SL::Helper::Flash qw(flash);
 use SL::Locale::String qw(t8);
+use SL::Presenter::FileObject;
 use SL::ReportGenerator;
 
 require "bin/mozilla/common.pl";
@@ -84,6 +86,8 @@ sub edit_config {
   $form->{title}      = $locale->text('Edit Dunning Process Config');
   $form->{callback} ||= build_std_url("action=edit_config");
 
+  setup_dn_edit_config_action_bar();
+
   $form->header();
   print $form->parse_html_template("dunning/edit_config");
 
@@ -101,10 +105,14 @@ sub add {
 
   DN->get_config(\%myconfig, \%$form);
 
+  $form->get_lists("departments" => "ALL_DEPARTMENTS");
+
   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
-  $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{all_departments} && scalar @{ $form->{all_departments} || [] };
+  $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
 
   $form->{title}    = $locale->text('Start Dunning Process');
+
+  setup_dn_add_action_bar();
   $form->header();
 
   print $form->parse_html_template("dunning/add");
@@ -150,7 +158,7 @@ sub show_invoices {
                                           'no_html'         => 1,
                                           'no_opendocument' => 1,);
 
-  $::request->layout->add_javascripts("kivi.Dunning.js");
+  setup_dn_show_invoices_action_bar();
   $form->header();
   print $form->parse_html_template("dunning/show_invoices");
 
@@ -203,7 +211,7 @@ sub save_dunning {
 
   my $saved_language_id = $form->{language_id};
 
-  if ($form->{groupinvoices}) {
+  if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
     my %dunnings_for;
 
     for my $i (1 .. $form->{rowcount}) {
@@ -217,9 +225,11 @@ sub save_dunning {
 
       push @{ $level }, { "row"                    => $i,
                           "invoice_id"             => $form->{"inv_id_$i"},
+                          "credit_note"            => $form->{"credit_note_$i"},
                           "customer_id"            => $form->{"customer_id_$i"},
                           "language_id"            => $form->{"language_id_$i"},
                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+                          "print_invoice"          => $form->{"include_invoice_$i"},
                           "email"                  => $form->{"email_$i"}, };
     }
 
@@ -242,6 +252,7 @@ sub save_dunning {
                       "customer_id"            => $form->{"customer_id_$i"},
                       "language_id"            => $form->{"language_id_$i"},
                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
+                      "print_invoice"          => $form->{"include_invoice_$i"},
                       "email"                  => $form->{"email_$i"}, } ];
       if (!$form->{force_lang}) {
         $form->{language_id} = @{$level}[0]->{language_id};
@@ -307,6 +318,7 @@ sub search {
 
   $form->{title}    = $locale->text('Dunnings');
 
+  setup_dn_search_action_bar();
   $form->header();
 
   print $form->parse_html_template("dunning/search");
@@ -357,6 +369,7 @@ sub show_dunning {
     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
     'customername'        => { 'text' => $locale->text('Customername') },
+    'departmentname'      => { 'text' => $locale->text('Department') },
     'language'            => { 'text' => $locale->text('Language') },
     'invnumber'           => { 'text' => $locale->text('Invnumber') },
     'transdate'           => { 'text' => $locale->text('Invdate') },
@@ -367,6 +380,7 @@ sub show_dunning {
     'fee'                 => { 'text' => $locale->text('Total Fees') },
     'interest'            => { 'text' => $locale->text('Interest') },
     'salesman'            => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
+    'documents'           => { 'text' => $locale->text('Documents')  , 'visible' => $::instance_conf->get_doc_storage ? 1 : 0 },
   );
 
   $report->set_columns(%column_defs);
@@ -433,6 +447,19 @@ sub show_dunning {
       $row->{language} = { };
     }
 
+    if ($::instance_conf->get_doc_storage && $first_row_for_dunning) {
+      my @files  = SL::File->get_all_versions(object_id   => $ref->{dunning_id},
+                                              object_type => 'dunning',
+                                              file_type   => 'document',);
+      if (scalar @files) {
+        my $html          = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
+        my $text          = join "\n",   map { $_->file_name                              } @files;
+        $row->{documents} = { 'raw_data' => $html, data => $text };
+      } else {
+        $row->{documents} = { };
+      }
+    }
+
     push @{ $current_dunning_rows }, $row;
 
     $previous_dunning_id   = $ref->{dunning_id};
@@ -449,7 +476,7 @@ sub show_dunning {
 
   $report->set_options_from_form();
 
-  $::request->layout->add_javascripts("kivi.Dunning.js");
+  setup_dn_show_dunning_action_bar();
   $report->generate_with_headers();
 
   $main::lxdebug->leave_sub();
@@ -525,6 +552,11 @@ sub print_multiple {
     $form->{dunning_id} = $dunning_id;
     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
     DN->print_dunning(\%myconfig, $form, $dunning_id);
+
+    # print original dunned invoices, if they where printed on dunning run
+    my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
+    DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
+
     $i++;
   }
   $form->{language_id} = $saved_language_id;
@@ -559,4 +591,85 @@ sub dispatcher {
 
   $::form->error($::locale->text('No action defined.'));
 }
+
+sub setup_dn_add_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Search'),
+        submit    => [ '#form', { action => "show_invoices" } ],
+        accesskey => 'enter',
+      ],
+    );
+  }
+}
+
+sub setup_dn_show_invoices_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Create'),
+        submit    => [ '#form', { action => "save_dunning" } ],
+        checks    => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
+        accesskey => 'enter',
+        only_once => 1,
+      ],
+    );
+  }
+}
+
+sub setup_dn_search_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Search'),
+        submit    => [ '#form', { action => "show_dunning" } ],
+        accesskey => 'enter',
+      ],
+    );
+  }
+}
+
+sub setup_dn_show_dunning_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Print'),
+        submit    => [ '#form', { action => "print_multiple" } ],
+        checks    => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
+        accesskey => 'enter',
+      ],
+
+      action => [
+        t8('Delete'),
+        submit  => [ '#form', { action => "delete" } ],
+        checks  => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
+        confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
+      ],
+    );
+  }
+}
+
+sub setup_dn_edit_config_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Save'),
+        submit    => [ '#form', { action => "save" } ],
+        accesskey => 'enter',
+      ],
+    );
+  }
+}
+
 # end of main