Erfolgsrechnung: von pos_eur auf pos_er gelegt.
[kivitendo-erp.git] / bin / mozilla / ap.pl
index b2e5819..1070c67 100644 (file)
@@ -32,7 +32,8 @@
 #======================================================================
 
 use POSIX qw(strftime);
-use List::Util qw(sum);
+use List::Util qw(max sum);
+use List::UtilsBy qw(sort_by);
 
 use SL::AP;
 use SL::FU;
@@ -41,6 +42,7 @@ use SL::IS;
 use SL::PE;
 use SL::ReportGenerator;
 use SL::DB::Default;
+use SL::DB::PurchaseInvoice;
 
 require "bin/mozilla/arap.pl";
 require "bin/mozilla/common.pl";
@@ -187,6 +189,25 @@ sub create_links {
   $main::lxdebug->leave_sub();
 }
 
+sub _sort_payments {
+  my @fields   = qw(acc_trans_id gldate datepaid source memo paid AR_paid paid_project_id);
+  my @payments =
+    grep { $_->{paid} != 0 }
+    map  {
+      my $idx = $_;
+      +{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
+    } (1..$::form->{paidaccounts});
+
+  @payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;
+
+  $::form->{paidaccounts} = max scalar(@payments), 1;
+
+  foreach my $idx (1 .. scalar(@payments)) {
+    my $payment = $payments[$idx - 1];
+    $::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
+  }
+}
+
 sub form_header {
   $main::lxdebug->enter_sub();
 
@@ -197,6 +218,8 @@ sub form_header {
 
   $main::auth->assert('general_ledger');
 
+  $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
+
   $form->{title_} = $form->{title};
   $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
 
@@ -372,6 +395,8 @@ sub form_header {
 
   $form->{totalpaid} = 0;
 
+  _sort_payments();
+
   if ( $form->{'paid_'. $form->{paidaccounts}} ) {
     $form->{paidaccounts}++;
   }
@@ -559,6 +584,7 @@ sub post_payment {
   my $locale   = $main::locale;
 
   $main::auth->assert('general_ledger');
+  $form->mtime_ischanged('ap');
 
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
@@ -607,6 +633,7 @@ sub post {
   my $locale   = $main::locale;
 
   $main::auth->assert('general_ledger');
+  $form->mtime_ischanged('ap');
 
   my ($inline) = @_;