#======================================================================
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;
use SL::PE;
use SL::ReportGenerator;
use SL::DB::Default;
+use SL::DB::PurchaseInvoice;
require "bin/mozilla/arap.pl";
require "bin/mozilla/common.pl";
$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();
$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');
$form->{totalpaid} = 0;
+ _sort_payments();
+
if ( $form->{'paid_'. $form->{paidaccounts}} ) {
$form->{paidaccounts}++;
}
my $locale = $main::locale;
$main::auth->assert('general_ledger');
+ $form->mtime_ischanged('ap');
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
my $locale = $main::locale;
$main::auth->assert('general_ledger');
+ $form->mtime_ischanged('ap');
my ($inline) = @_;