Anzahlungs-Rg.: Steuer der Anzahlungs-Rgs. nicht auf das Forderungskto buchen …
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Fri, 10 Dec 2021 15:12:51 +0000 (16:12 +0100)
committerJan Büren <jan@kivitendo.de>
Mon, 14 Feb 2022 12:54:08 +0000 (13:54 +0100)
… und Betrag der Anzahlungs-Rgs. mit Steuerschlüssel 0 buchen

SL/IS.pm

index e5ba049..c667c0f 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1082,6 +1082,7 @@ SQL
     }
   }
 
+  my $iap_amounts;
   if ($form->{type} eq 'final_invoice') {
     my $invoices_for_advance_payment = $self->_get_invoices_for_advance_payment($form->{convert_from_ar_ids} || $form->{id});
     if (scalar @$invoices_for_advance_payment > 0) {
@@ -1110,15 +1111,24 @@ SQL
         }
 
         # VAT for invoices for advance payment is booked on payment of these. So do not book this VAT for final invoice.
-        # Collect VAT of invoices for advance payment.
+        # And book the amount of the invoices for advance payment with taxkey 0 (see below).
+        # Collect amounts and VAT of invoices for advance payment.
+
         # Set sellprices to fxsellprices for items, because
         # the PriceTaxCalculator sets fxsellprice from sellprice before calculating.
         $_->sellprice($_->fxsellprice) for @{$invoice_for_advance_payment->items};
         my %pat = $invoice_for_advance_payment->calculate_prices_and_taxes;
 
         foreach my $tax_chart_id (keys %{ $pat{taxes_by_chart_id} }) {
-          my $tax_accno = SL::DB::Chart->new(id => $tax_chart_id)->load->accno;
-          $form->{amount}{ $form->{id} }{$tax_accno} -= $pat{taxes_by_chart_id}->{$tax_chart_id};
+          my $tax_accno = SL::DB::Chart->load_cached($tax_chart_id)->accno;
+          $form->{amount}{ $form->{id} }{$tax_accno}  -= $pat{taxes_by_chart_id}->{$tax_chart_id};
+          $form->{amount}{ $form->{id} }{$form->{AR}} += $pat{taxes_by_chart_id}->{$tax_chart_id};
+        }
+
+        foreach my $amount_chart_id (keys %{ $pat{amounts} }) {
+          my $amount_accno = SL::DB::Chart->load_cached($amount_chart_id)->accno;
+          $iap_amounts->{$amount_accno}                 += $pat{amounts}->{$amount_chart_id}->{amount};
+          $form->{amount}{ $form->{id} }{$amount_accno} -= $pat{amounts}->{$amount_chart_id}->{amount};
         }
       }
     }
@@ -1237,6 +1247,17 @@ SQL
     }
   }
 
+  # Book the amount of the invoices for advance payment with taxkey 0 (see below).
+  if ($form->{type} eq 'final_invoice' && $iap_amounts) {
+    foreach my $accno (keys %$iap_amounts) {
+      $query =
+        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, tax_id, taxkey, project_id, chart_link)
+        VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT id FROM tax WHERE taxkey=0), 0, ?, (SELECT link FROM chart WHERE accno = ?))|;
+      @values = (conv_i($form->{id}), $accno, $iap_amounts->{$accno}, conv_date($form->{invdate}), conv_i($project_id), $accno);
+      do_query($form, $dbh, $query, @values);
+    }
+  }
+
   # deduct payment differences from diff
   for my $i (1 .. $form->{paidaccounts}) {
     if ($form->{"paid_$i"} != 0) {