Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 675fb784

Von Tamino Steinert vor fast 2 Jahren hinzugefügt

  • ID 675fb784a71641256f6be4ccbd76b2710ad595d5
  • Vorgänger 75cb7791
  • Nachfolger 624bfec3

InvoiceItem: Buchen auf einem Konto mit verschiedenen Steuersätzen

Nutze Kontonummer und SteuerId als Schlüssel für die
Hashtabelle ($form->{amount}) und entferne die Ebene Belegnummer.
Speicher die Kontonummer und (wenn vorhanden) SteuerId für die Buchung.
Wenn keine SteureId vorhanden, dann suche den Steuerschlüssel und
Steuer über die Kontonummer.

Unterschiede anzeigen:

SL/IR.pm
96 96

  
97 97
  my ($query, $sth, @values, $project_id);
98 98
  my ($allocated, $taxrate, $taxamount, $taxdiff, $item);
99
  my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
99
  my ($amount, $linetotal, $last_inventory_accno_tax_id_key, $last_expense_accno_tax_id_key);
100 100
  my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
101 101
  my $exchangerate = 0;
102 102
  my ($basefactor, $baseqty, @taxaccounts, $totaltax);
......
176 176
    $allocated   = 0;
177 177
    $taxrate     = 0;
178 178

  
179
    # preset tax_id and accno for all taxaccounts
180
    foreach $item (@taxaccounts) {
181
      my $accno  = $item;
182
      my $tax_id = $form->{"${item}_tax_id"};
183
      $form->{amount}{$item . "_" . $tax_id}{tax_id} = $tax_id;
184
      $form->{amount}{$item . "_" . $tax_id}{accno}  = $item;
185
    }
186

  
179 187
    $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
180 188
    (my $fxsellprice = $form->{"sellprice_$i"}) =~ /\.(\d+)/;
181 189
    my $dec = length $1;
......
213 221
          foreach $item (@taxaccounts) {
214 222
            $taxamount =
215 223
              $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
216
            $taxdiff                              += $taxamount;
217
            $form->{amount}{ $form->{id} }{$item} -= $taxamount;
224
            $taxdiff += $taxamount;
225
            $form->{amount}{$item . "_" . $form->{"${item}_tax_id"}}{amount} -= $taxamount;
218 226
          }
219
          $form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
227
          my $first_taxaccno = $taxaccounts[0];
228
          $form->{amount}{ $first_taxaccno . "_" . $form->{"${first_taxaccno}_tax_id"} }{amount} += $taxdiff;
220 229

  
221 230
        } else {
222
          map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
231
          map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
223 232
        }
224 233

  
225 234
      } else {
226
        map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
235
        map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
227 236
      }
228 237

  
229 238
      # add purchase to inventory, this one is without the tax!
......
234 243
      # this is the difference for the inventory
235 244
      $invoicediff += ($amount - $linetotal);
236 245

  
237
      $form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -= $linetotal;
246
      my $inventory_key = $form->{"inventory_accno_$i"} . "_" . $form->{"expense_accno_tax_id_$i"};
247
      $form->{amount}{$inventory_key}{amount} -= $linetotal;
248
      $form->{amount}{$inventory_key}{accno}   = $form->{"inventory_accno_$i"};
249
      $form->{amount}{$inventory_key}{tax_id}  = $form->{"expense_accno_tax_id_$i"};
238 250

  
239 251
      # adjust and round sellprice
240 252
      $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
241 253

  
242
      $lastinventoryaccno = $form->{"inventory_accno_$i"};
254
      $last_inventory_accno_tax_id_key = $inventory_key;
243 255

  
244 256
      next if $payments_only;
245 257

  
......
375 387
          foreach $item (@taxaccounts) {
376 388
            $taxamount = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"}));
377 389
            $totaltax += $taxamount;
378
            $form->{amount}{ $form->{id} }{$item} -= $taxamount;
390
            $form->{amount}{$item . "_" . $form->{"${item}_tax_id"}}{amount} -= $taxamount;
379 391
          }
380 392
        } else {
381
          map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
393
          map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
382 394
        }
383 395
      } else {
384
        map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
396
        map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
385 397
      }
386 398

  
387 399
      $amount    = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
......
392 404
      $expensediff += ($amount - $linetotal);
393 405

  
394 406
      # add amount to expense
395
      $form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -= $linetotal;
407
      my $expense_key = $form->{"expense_accno_$i"} . "_" . $form->{"expense_accno_tax_id_$i"};
408
      $form->{amount}{$expense_key}{amount} -= $linetotal;
409
      $form->{amount}{$expense_key}{accno}   = $form->{"expense_accno_$i"};
410
      $form->{amount}{$expense_key}{tax_id}  = $form->{"expense_accno_tax_id_$i"};
396 411

  
397
      $lastexpenseaccno = $form->{"expense_accno_$i"};
412
      $last_expense_accno_tax_id_key = $expense_key;
398 413

  
399 414
      # adjust and round sellprice
400 415
      $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
......
502 517
    $netamount = $amount;
503 518

  
504 519
    foreach $item (split / /, $form->{taxaccounts}) {
505
      $amount                               = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
506
      $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
520
      my $key = $item . "_" . $form->{"${item}_tax_id"};
521
      $amount = $form->{amount}{$key}{amount} * $form->{exchangerate};
522
      $form->{amount}{$key}{amount} = $form->round_amount($amount, 2);
507 523

  
508
      $amount     = $form->{amount}{ $form->{id} }{$item} * -1;
524
      $amount     = $form->{amount}{$key}{amount} * -1;
509 525
      $tax       += $amount;
510 526
      $netamount -= $amount;
511 527
    }
......
518 534
    # in the sales invoice case rounding errors only have to be corrected for
519 535
    # income accounts, it is enough to add the total rounding error to one of
520 536
    # the income accounts, with the one assigned to the last row being used
521
    # (lastinventoryaccno)
537
    # (last_inventory_accno_tax_id_key)
522 538

  
523 539
    # in the purchase invoice case rounding errors may be split between
524 540
    # inventory accounts and expense accounts. After rounding, an error of 1
525 541
    # cent is introduced if the total rounding error exceeds 0.005. The total
526 542
    # error is made up of $invoicediff and $expensediff, however, so if both
527 543
    # values are below 0.005, but add up to a total >= 0.005, correcting
528
    # lastinventoryaccno and lastexpenseaccno separately has no effect after
544
    # last_inventory_accno_tax_id_key and last_expense_accno_tax_id_key separately has no effect after
529 545
    # rounding. This caused bug 1579. Therefore when the combined total exceeds
530 546
    # 0.005, but neither do individually, the account with the larger value
531 547
    # shall receive the total rounding error, and the next time it is rounded
532 548
    # the 1 cent correction will be introduced.
533 549

  
534
    $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno;
535
    $form->{amount}{ $form->{id} }{$lastexpenseaccno}   -= $expensediff if $lastexpenseaccno;
550
    $form->{amount}{$last_inventory_accno_tax_id_key}{amount} -= $invoicediff if $last_inventory_accno_tax_id_key;
551
    $form->{amount}{$last_expense_accno_tax_id_key}{amount}   -= $expensediff if $last_expense_accno_tax_id_key;
536 552

  
537 553
    if ( (abs($expensediff)+abs($invoicediff)) >= 0.005 and abs($expensediff) < 0.005 and abs($invoicediff) < 0.005 ) {
538 554

  
539 555
      # in total the rounding error adds up to 1 cent effectively, correct the
540 556
      # larger of the two numbers
541 557

  
542
      if ( abs($form->{amount}{ $form->{id} }{$lastinventoryaccno}) > abs($form->{amount}{ $form->{id} }{$lastexpenseaccno}) ) {
558
      if ( abs($form->{amount}{$last_inventory_accno_tax_id_key}{amount}) > abs($form->{amount}{$last_expense_accno_tax_id_key}{amount}) ) {
543 559
        # $invoicediff has already been deducted, now also deduct expensediff
544
        $form->{amount}{ $form->{id} }{$lastinventoryaccno}   -= $expensediff;
560
        $form->{amount}{$last_inventory_accno_tax_id_key}{amount} -= $expensediff;
545 561
      } else {
546 562
        # expensediff has already been deducted, now also deduct invoicediff
547
        $form->{amount}{ $form->{id} }{$lastexpenseaccno}   -= $invoicediff;
563
        $form->{amount}{$last_expense_accno_tax_id_key}{amount}   -= $invoicediff;
548 564
      };
549 565
    };
550 566

  
......
554 570
    $netamount = $amount;
555 571

  
556 572
    foreach my $item (split / /, $form->{taxaccounts}) {
557
      $form->{amount}{ $form->{id} }{$item}  = $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
558
      $amount                                = $form->round_amount( $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1, 2);
559
      $paiddiff                             += $amount - $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1;
560
      $form->{amount}{ $form->{id} }{$item}  = $form->round_amount($amount * -1, 2);
561
      $amount                                = $form->{amount}{ $form->{id} }{$item} * -1;
562
      $tax                                  += $amount;
573
      my $key = $item . "_" . $form->{"${item}_tax_id"};
574
      $form->{amount}{$key}{amount}  = $form->round_amount($form->{amount}{$key}{amount}, 2);
575
      $amount                        = $form->round_amount( $form->{amount}{$key}{amount} * $form->{exchangerate} * -1, 2);
576
      $paiddiff                     += $amount - $form->{amount}{$key}{amount} * $form->{exchangerate} * -1;
577
      $form->{amount}{$key}{amount}  = $form->round_amount($amount * -1, 2);
578
      $amount                        = $form->{amount}{$key}{amount} * -1;
579
      $tax                          += $amount;
563 580
    }
564 581
  }
565 582

  
566
  $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
567

  
568

  
569
  $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0;
570

  
571 583
  # record acc_trans transactions
572 584
  my $taxdate = $form->{tax_point} || $form->{deliverydate} || $form->{invdate};
573
  foreach my $trans_id (keys %{ $form->{amount} }) {
574
    foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
575
      $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
576

  
585
  foreach my $accno_tax_id_key (keys %{ $form->{amount} }) {
586
    $form->{amount}{$accno_tax_id_key}{amount} = $form->round_amount($form->{amount}{$accno_tax_id_key}{amount}, 2);
587

  
588

  
589
    next if $payments_only || !$form->{amount}{$accno_tax_id_key}{amount};
590
    my $amount = $form->{amount}{$accno_tax_id_key}{amount};
591
    my $accno  = $form->{amount}{$accno_tax_id_key}{accno};
592
    my $tax_id = $form->{amount}{$accno_tax_id_key}{tax_id};
593

  
594
    $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link)
595
                VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
596
                (SELECT taxkey
597
                 FROM tax
598
                 WHERE id = ?),
599
                ?,
600
                ?,
601
                (SELECT link FROM chart WHERE accno = ?))|;
602
    @values = (conv_i($form->{id}), $accno, $amount,
603
               conv_date($form->{invdate}), $tax_id, $project_id, $tax_id, $accno);
604
    do_query($form, $dbh, $query, @values);
605
  }
577 606

  
578
      next if $payments_only || !$form->{amount}{$trans_id}{$accno};
607
  my $ap_amount = $netamount + $tax;
608
  my $ap_accno  = $form->{AP};
609
  $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link)
610
              VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
611
              (SELECT taxkey_id
612
               FROM taxkeys
613
               WHERE chart_id= (SELECT id
614
                                FROM chart
615
                                WHERE accno = ?)
616
               AND startdate <= ?
617
               ORDER BY startdate DESC LIMIT 1),
618
              ?,
619
              (SELECT tax_id
620
               FROM taxkeys
621
               WHERE chart_id= (SELECT id
622
                                FROM chart
623
                                WHERE accno = ?)
624
               AND startdate <= ?
625
               ORDER BY startdate DESC LIMIT 1),
626
              (SELECT link FROM chart WHERE accno = ?))|;
627
  @values = (conv_i($form->{id}), $ap_accno, $ap_amount,
628
             conv_date($form->{invdate}), $ap_accno, conv_date($taxdate), $project_id, $ap_accno, conv_date($taxdate), $ap_accno);
629
  do_query($form, $dbh, $query, @values);
579 630

  
580
      $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link)
581
                  VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
582
                  (SELECT taxkey_id
583
                   FROM taxkeys
584
                   WHERE chart_id= (SELECT id
585
                                    FROM chart
586
                                    WHERE accno = ?)
587
                   AND startdate <= ?
588
                   ORDER BY startdate DESC LIMIT 1),
589
                  ?,
590
                  (SELECT tax_id
591
                   FROM taxkeys
592
                   WHERE chart_id= (SELECT id
593
                                    FROM chart
594
                                    WHERE accno = ?)
595
                   AND startdate <= ?
596
                   ORDER BY startdate DESC LIMIT 1),
597
                  (SELECT link FROM chart WHERE accno = ?))|;
598
      @values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno},
599
                 conv_date($form->{invdate}), $accno, conv_date($taxdate), $project_id, $accno, conv_date($taxdate), $accno);
600
      do_query($form, $dbh, $query, @values);
601
    }
602
  }
631
  $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0;
603 632

  
604 633
  # deduct payment differences from paiddiff
605 634
  for my $i (1 .. $form->{paidaccounts}) {
......
611 640

  
612 641
  # force AP entry if 0
613 642

  
614
  $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0;
643
  $ap_amount = $form->{paid} if $ap_amount == 0;
615 644

  
616 645
  my %already_cleared = %{ $params{already_cleared} // {} };
617 646

  
......
639 668
                    :                                                                                'f';
640 669

  
641 670
    # record AP
642
    if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
671
    if ($ap_amount != 0) {
643 672
      $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, cleared, tax_id, chart_link)
644 673
                  VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
645 674
                          (SELECT taxkey_id

Auch abrufbar als: Unified diff