1 package SL::DB::Helper::SalesPurchaseInvoice;
6 use parent qw(Exporter);
7 our @EXPORT = qw(get_tax_and_amount_by_tax_chart_id);
9 sub get_tax_and_amount_by_tax_chart_id {
12 my $ARAP = $self->is_sales ? 'AR' : 'AP';
13 my ($tax_and_amount_by_tax_id, $total);
15 foreach my $transaction (@{ $self->transactions }) {
16 next if $transaction->chart_link =~ m/(^${ARAP}$|paid)/;
18 my $tax_or_netamount = $transaction->chart_link =~ m/tax/ ? 'tax'
19 : $transaction->chart_link =~ m/(${ARAP}_amount|IC_cogs)/ ? 'netamount'
21 if ($tax_or_netamount eq 'netamount') {
22 $tax_and_amount_by_tax_id->{ $transaction->tax->chart_id }->{$tax_or_netamount} ||= 0;
23 $tax_and_amount_by_tax_id->{ $transaction->tax->chart_id }->{$tax_or_netamount} += $transaction->amount;
24 # die "Invalid state" unless $tax_and_amount_by_tax_id->{ $transaction->tax->chart_id }->{tax_id} == 0
25 $tax_and_amount_by_tax_id->{ $transaction->tax->chart_id }->{tax_id} = $transaction->tax_id;
26 } elsif ($tax_or_netamount eq 'tax') {
27 $tax_and_amount_by_tax_id->{ $transaction->chart_id }->{$tax_or_netamount} ||= 0;
28 $tax_and_amount_by_tax_id->{ $transaction->chart_id }->{$tax_or_netamount} += $transaction->amount;
30 die "Invalid chart link at: " . $transaction->chart_link unless $tax_or_netamount;
33 $total += $transaction->amount;
35 die "Invalid calculated amount. Calc: $total Amount: " . abs($self->amount) if abs($total) - abs($self->amount) > 0.001;
36 return $tax_and_amount_by_tax_id;
51 SL::DB::Helper::SalesPurchaseInvoice - Helper functions for Sales or Purchase bookings (mirrored)
53 Delivers the booked amounts split by net amount and tax amount for one ar or ap transaction
54 as persisted in the table acc_trans.
55 Should be rounding or calculation error prone because all values are already computed before
56 the values are written in the acc_trans table.
58 That is the main purpose for this helper class.
63 =item C<get_tax_and_amount_by_tax_chart_id>
65 Iterates over all transactions for one distinct ar or ap transaction (trans_id in acc_trans) and
66 groups the amounts in relation to distinct tax (tax.id) and net amounts (sums all bookings with
67 _cogs or _amount chart links).
68 Returns a hashref with the chart_id of the tax entry as key like this:
73 'netamount' => '1748.32',
82 C<tax_id> is the id of the used tax. C<tax> ist the amount of tax booked for the whole transaction.
83 C<netamount> is the netamount booked with this tax.
84 TODO: Please note the hash key chart_id may not be unique but the entry tax_id is always unique.
86 As additional safety method the functions dies if the calculated sums do not match the
87 the whole amount of the transaction with an accuracy of two decimal places.
97 Jan Büren E<lt>jan@kivitendo.deE<gt>