Revision 960124c0
Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt
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 |
|
... | ... | |
373 | 385 |
foreach $item (@taxaccounts) { |
374 | 386 |
$taxamount = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})); |
375 | 387 |
$totaltax += $taxamount; |
376 |
$form->{amount}{ $form->{id} }{$item} -= $taxamount;
|
|
388 |
$form->{amount}{$item . "_" . $form->{"${item}_tax_id"}}{amount} -= $taxamount;
|
|
377 | 389 |
} |
378 | 390 |
} else { |
379 |
map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
|
|
391 |
map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
|
|
380 | 392 |
} |
381 | 393 |
} else { |
382 |
map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
|
|
394 |
map { $form->{amount}{$_ . "_" . $form->{"${_}_tax_id"}}{amount} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
|
|
383 | 395 |
} |
384 | 396 |
|
385 | 397 |
$amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor; |
... | ... | |
390 | 402 |
$expensediff += ($amount - $linetotal); |
391 | 403 |
|
392 | 404 |
# add amount to expense |
393 |
$form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -= $linetotal; |
|
405 |
my $expense_key = $form->{"expense_accno_$i"} . "_" . $form->{"expense_accno_tax_id_$i"}; |
|
406 |
$form->{amount}{$expense_key}{amount} -= $linetotal; |
|
407 |
$form->{amount}{$expense_key}{accno} = $form->{"expense_accno_$i"}; |
|
408 |
$form->{amount}{$expense_key}{tax_id} = $form->{"expense_accno_tax_id_$i"}; |
|
394 | 409 |
|
395 |
$lastexpenseaccno = $form->{"expense_accno_$i"};
|
|
410 |
$last_expense_accno_tax_id_key = $expense_key;
|
|
396 | 411 |
|
397 | 412 |
# adjust and round sellprice |
398 | 413 |
$form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces); |
... | ... | |
500 | 515 |
$netamount = $amount; |
501 | 516 |
|
502 | 517 |
foreach $item (split / /, $form->{taxaccounts}) { |
503 |
$amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate}; |
|
504 |
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2); |
|
518 |
my $key = $item . "_" . $form->{"${item}_tax_id"}; |
|
519 |
$amount = $form->{amount}{$key}{amount} * $form->{exchangerate}; |
|
520 |
$form->{amount}{$key}{amount} = $form->round_amount($amount, 2); |
|
505 | 521 |
|
506 |
$amount = $form->{amount}{ $form->{id} }{$item} * -1;
|
|
522 |
$amount = $form->{amount}{$key}{amount} * -1;
|
|
507 | 523 |
$tax += $amount; |
508 | 524 |
$netamount -= $amount; |
509 | 525 |
} |
... | ... | |
516 | 532 |
# in the sales invoice case rounding errors only have to be corrected for |
517 | 533 |
# income accounts, it is enough to add the total rounding error to one of |
518 | 534 |
# the income accounts, with the one assigned to the last row being used |
519 |
# (lastinventoryaccno)
|
|
535 |
# (last_inventory_accno_tax_id_key)
|
|
520 | 536 |
|
521 | 537 |
# in the purchase invoice case rounding errors may be split between |
522 | 538 |
# inventory accounts and expense accounts. After rounding, an error of 1 |
523 | 539 |
# cent is introduced if the total rounding error exceeds 0.005. The total |
524 | 540 |
# error is made up of $invoicediff and $expensediff, however, so if both |
525 | 541 |
# values are below 0.005, but add up to a total >= 0.005, correcting |
526 |
# lastinventoryaccno and lastexpenseaccno separately has no effect after
|
|
542 |
# last_inventory_accno_tax_id_key and last_expense_accno_tax_id_key separately has no effect after
|
|
527 | 543 |
# rounding. This caused bug 1579. Therefore when the combined total exceeds |
528 | 544 |
# 0.005, but neither do individually, the account with the larger value |
529 | 545 |
# shall receive the total rounding error, and the next time it is rounded |
530 | 546 |
# the 1 cent correction will be introduced. |
531 | 547 |
|
532 |
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno;
|
|
533 |
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno;
|
|
548 |
$form->{amount}{$last_inventory_accno_tax_id_key}{amount} -= $invoicediff if $last_inventory_accno_tax_id_key;
|
|
549 |
$form->{amount}{$last_expense_accno_tax_id_key}{amount} -= $expensediff if $last_expense_accno_tax_id_key;
|
|
534 | 550 |
|
535 | 551 |
if ( (abs($expensediff)+abs($invoicediff)) >= 0.005 and abs($expensediff) < 0.005 and abs($invoicediff) < 0.005 ) { |
536 | 552 |
|
537 | 553 |
# in total the rounding error adds up to 1 cent effectively, correct the |
538 | 554 |
# larger of the two numbers |
539 | 555 |
|
540 |
if ( abs($form->{amount}{ $form->{id} }{$lastinventoryaccno}) > abs($form->{amount}{ $form->{id} }{$lastexpenseaccno}) ) {
|
|
556 |
if ( abs($form->{amount}{$last_inventory_accno_tax_id_key}{amount}) > abs($form->{amount}{$last_expense_accno_tax_id_key}{amount}) ) {
|
|
541 | 557 |
# $invoicediff has already been deducted, now also deduct expensediff |
542 |
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $expensediff;
|
|
558 |
$form->{amount}{$last_inventory_accno_tax_id_key}{amount} -= $expensediff;
|
|
543 | 559 |
} else { |
544 | 560 |
# expensediff has already been deducted, now also deduct invoicediff |
545 |
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $invoicediff;
|
|
561 |
$form->{amount}{$last_expense_accno_tax_id_key}{amount} -= $invoicediff;
|
|
546 | 562 |
}; |
547 | 563 |
}; |
548 | 564 |
|
... | ... | |
552 | 568 |
$netamount = $amount; |
553 | 569 |
|
554 | 570 |
foreach my $item (split / /, $form->{taxaccounts}) { |
555 |
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2); |
|
556 |
$amount = $form->round_amount( $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1, 2); |
|
557 |
$paiddiff += $amount - $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1; |
|
558 |
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount * -1, 2); |
|
559 |
$amount = $form->{amount}{ $form->{id} }{$item} * -1; |
|
560 |
$tax += $amount; |
|
571 |
my $key = $item . "_" . $form->{"${item}_tax_id"}; |
|
572 |
$form->{amount}{$key}{amount} = $form->round_amount($form->{amount}{$key}{amount}, 2); |
|
573 |
$amount = $form->round_amount( $form->{amount}{$key}{amount} * $form->{exchangerate} * -1, 2); |
|
574 |
$paiddiff += $amount - $form->{amount}{$key}{amount} * $form->{exchangerate} * -1; |
|
575 |
$form->{amount}{$key}{amount} = $form->round_amount($amount * -1, 2); |
|
576 |
$amount = $form->{amount}{$key}{amount} * -1; |
|
577 |
$tax += $amount; |
|
561 | 578 |
} |
562 | 579 |
} |
563 | 580 |
|
564 |
$form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax; |
|
565 |
|
|
566 |
|
|
567 |
$form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0; |
|
568 |
|
|
569 | 581 |
# record acc_trans transactions |
570 | 582 |
my $taxdate = $form->{tax_point} || $form->{deliverydate} || $form->{invdate}; |
571 |
foreach my $trans_id (keys %{ $form->{amount} }) { |
|
572 |
foreach my $accno (keys %{ $form->{amount}{$trans_id} }) { |
|
573 |
$form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2); |
|
574 |
|
|
583 |
foreach my $accno_tax_id_key (keys %{ $form->{amount} }) { |
|
584 |
$form->{amount}{$accno_tax_id_key}{amount} = $form->round_amount($form->{amount}{$accno_tax_id_key}{amount}, 2); |
|
585 |
|
|
586 |
|
|
587 |
next if $payments_only || !$form->{amount}{$accno_tax_id_key}{amount}; |
|
588 |
my $amount = $form->{amount}{$accno_tax_id_key}{amount}; |
|
589 |
my $accno = $form->{amount}{$accno_tax_id_key}{accno}; |
|
590 |
my $tax_id = $form->{amount}{$accno_tax_id_key}{tax_id}; |
|
591 |
|
|
592 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link) |
|
593 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, |
|
594 |
(SELECT taxkey |
|
595 |
FROM tax |
|
596 |
WHERE id = ?), |
|
597 |
?, |
|
598 |
?, |
|
599 |
(SELECT link FROM chart WHERE accno = ?))|; |
|
600 |
@values = (conv_i($form->{id}), $accno, $amount, |
|
601 |
conv_date($form->{invdate}), $tax_id, $project_id, $tax_id, $accno); |
|
602 |
do_query($form, $dbh, $query, @values); |
|
603 |
} |
|
575 | 604 |
|
576 |
next if $payments_only || !$form->{amount}{$trans_id}{$accno}; |
|
605 |
my $ap_amount = $netamount + $tax; |
|
606 |
my $ap_accno = $form->{AP}; |
|
607 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link) |
|
608 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, |
|
609 |
(SELECT taxkey_id |
|
610 |
FROM taxkeys |
|
611 |
WHERE chart_id= (SELECT id |
|
612 |
FROM chart |
|
613 |
WHERE accno = ?) |
|
614 |
AND startdate <= ? |
|
615 |
ORDER BY startdate DESC LIMIT 1), |
|
616 |
?, |
|
617 |
(SELECT tax_id |
|
618 |
FROM taxkeys |
|
619 |
WHERE chart_id= (SELECT id |
|
620 |
FROM chart |
|
621 |
WHERE accno = ?) |
|
622 |
AND startdate <= ? |
|
623 |
ORDER BY startdate DESC LIMIT 1), |
|
624 |
(SELECT link FROM chart WHERE accno = ?))|; |
|
625 |
@values = (conv_i($form->{id}), $ap_accno, $ap_amount, |
|
626 |
conv_date($form->{invdate}), $ap_accno, conv_date($taxdate), $project_id, $ap_accno, conv_date($taxdate), $ap_accno); |
|
627 |
do_query($form, $dbh, $query, @values); |
|
577 | 628 |
|
578 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link) |
|
579 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, |
|
580 |
(SELECT taxkey_id |
|
581 |
FROM taxkeys |
|
582 |
WHERE chart_id= (SELECT id |
|
583 |
FROM chart |
|
584 |
WHERE accno = ?) |
|
585 |
AND startdate <= ? |
|
586 |
ORDER BY startdate DESC LIMIT 1), |
|
587 |
?, |
|
588 |
(SELECT tax_id |
|
589 |
FROM taxkeys |
|
590 |
WHERE chart_id= (SELECT id |
|
591 |
FROM chart |
|
592 |
WHERE accno = ?) |
|
593 |
AND startdate <= ? |
|
594 |
ORDER BY startdate DESC LIMIT 1), |
|
595 |
(SELECT link FROM chart WHERE accno = ?))|; |
|
596 |
@values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno}, |
|
597 |
conv_date($form->{invdate}), $accno, conv_date($taxdate), $project_id, $accno, conv_date($taxdate), $accno); |
|
598 |
do_query($form, $dbh, $query, @values); |
|
599 |
} |
|
600 |
} |
|
629 |
$form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0; |
|
601 | 630 |
|
602 | 631 |
# deduct payment differences from paiddiff |
603 | 632 |
for my $i (1 .. $form->{paidaccounts}) { |
... | ... | |
609 | 638 |
|
610 | 639 |
# force AP entry if 0 |
611 | 640 |
|
612 |
$form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0;
|
|
641 |
$ap_amount = $form->{paid} if $ap_amount == 0;
|
|
613 | 642 |
|
614 | 643 |
my %already_cleared = %{ $params{already_cleared} // {} }; |
615 | 644 |
|
... | ... | |
637 | 666 |
: 'f'; |
638 | 667 |
|
639 | 668 |
# record AP |
640 |
if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
|
|
669 |
if ($ap_amount != 0) {
|
|
641 | 670 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, cleared, tax_id, chart_link) |
642 | 671 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, |
643 | 672 |
(SELECT taxkey_id |
Auch abrufbar als: Unified diff
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.