epic-ts
[kivitendo-erp.git] / bin / mozilla / dn.pl
index 01658ab..e418fa2 100644 (file)
 #
 #======================================================================
 
-use POSIX;
+use POSIX qw(strftime);
 
 use SL::IS;
 use SL::PE;
 use SL::DN;
+use SL::DB::Dunning;
+use SL::Helper::Flash;
+use SL::Locale::String qw(t8);
 use SL::ReportGenerator;
 
 require "bin/mozilla/common.pl";
@@ -251,7 +254,8 @@ sub save_dunning {
 
   $form->{language_id} = $saved_language_id;
 
-  if($form->{DUNNING_PDFS}) {
+  if (scalar @{ $form->{DUNNING_PDFS} }) {
+    $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
   }
 
@@ -297,7 +301,7 @@ sub search {
 
   $form->get_lists("customers"   => "ALL_CUSTOMERS",
                    "departments" => "ALL_DEPARTMENTS");
-  $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
+  $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
 
   DN->get_config(\%myconfig, \%$form);
 
@@ -472,6 +476,29 @@ sub print_dunning {
   $main::lxdebug->leave_sub();
 }
 
+sub delete {
+  $main::auth->assert('dunning_edit');
+
+  my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
+
+  if (!scalar @dunning_ids) {
+    $::form->error($::locale->text('No dunnings have been selected for printing.'));
+  }
+
+  my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
+
+  SL::DB::Dunning->new->db->with_transaction(sub {
+    for my $dunning (@$dunnings) {
+      SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
+      $dunning->delete;
+    }
+  });
+
+  flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
+
+  search();
+}
+
 sub print_multiple {
   $main::lxdebug->enter_sub();
 
@@ -498,6 +525,7 @@ sub print_multiple {
     if (!$form->{force_lang}) {
       $form->{language_id} = $language_ids[$i];
     }
+    $form->{dunning_id} = $dunning_id;
     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
     DN->print_dunning(\%myconfig, $form, $dunning_id);
     $i++;
@@ -505,7 +533,7 @@ sub print_multiple {
   $form->{language_id} = $saved_language_id;
 
   if (scalar @{ $form->{DUNNING_PDFS} }) {
-    $form->{dunning_id} = strftime("%Y%m%d", localtime time);
+    $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
 
     if ($form->{media} eq 'printer') {
@@ -524,4 +552,14 @@ sub continue {
   call_sub($main::form->{nextsub});
 }
 
+sub dispatcher {
+  foreach my $action (qw(delete print_multiple)) {
+    if ($::form->{"action_${action}"}) {
+      call_sub($action);
+      return;
+    }
+  }
+
+  $::form->error($::locale->text('No action defined.'));
+}
 # end of main