Auftrags-Controller: Einheiten per Select ändern können und sellprice anpassen.
[kivitendo-erp.git] / bin / mozilla / ir.pl
index 86f78a8..6bcba9b 100644 (file)
@@ -36,7 +36,9 @@ use SL::IR;
 use SL::IS;
 use SL::PE;
 use SL::DB::Default;
+use SL::DB::PurchaseInvoice;
 use List::Util qw(max sum);
+use List::UtilsBy qw(sort_by);
 
 require "bin/mozilla/io.pl";
 require "bin/mozilla/invoice_io.pl";
@@ -258,6 +260,7 @@ sub form_header {
   my %TMPL_VAR = ();
   my @custom_hiddens;
 
+  $TMPL_VAR{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $form->{id})->load if $form->{id};
   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
@@ -349,6 +352,25 @@ sub form_header {
   $main::lxdebug->leave_sub();
 }
 
+sub _sort_payments {
+  my @fields   = qw(acc_trans_id gldate datepaid source memo paid AR_paid);
+  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_footer {
   $main::lxdebug->enter_sub();
 
@@ -391,6 +413,8 @@ sub form_footer {
   }
 
   # payments
+  _sort_payments();
+
   my $totalpaid = 0;
   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
   $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
@@ -648,6 +672,7 @@ sub post_payment {
 
   $main::auth->assert('vendor_invoice_edit');
 
+  $form->mtime_ischanged('ap') ;
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
   for my $i (1 .. $form->{paidaccounts}) {
     if ($form->{"paid_$i"}) {
@@ -707,6 +732,7 @@ sub post {
 
   $main::auth->assert('vendor_invoice_edit');
 
+  $form->mtime_ischanged('ap');
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
   $form->isblank("invdate",   $locale->text('Invoice Date missing!'));