ed1ba32d2857101dc61fe20b0b1c49e9149f9e67
[kivitendo-erp.git] / SL / DB / Helper / SalesPurchaseInvoice.pm
1 package SL::DB::Helper::SalesPurchaseInvoice;
2
3 use strict;
4 use utf8;
5
6 use parent qw(Exporter);
7 our @EXPORT = qw(get_tax_and_amount_by_tax_chart_id);
8
9 sub get_tax_and_amount_by_tax_chart_id {
10   my ($self) = @_;
11
12   my $ARAP = $self->is_sales ? 'AR' : 'AP';
13   my ($tax_and_amount_by_tax_id, $total);
14
15   foreach my $transaction (@{ $self->transactions }) {
16     next if $transaction->chart_link =~ m/(^${ARAP}$|paid)/;
17
18     my $tax_or_netamount = $transaction->chart_link =~ m/tax/            ? 'tax'
19                          : $transaction->chart_link =~ m/(${ARAP}_amount|IC_cogs)/ ? 'netamount'
20                          : undef;
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;
29     } else {
30       die "Invalid chart link at: " . $transaction->chart_link unless $tax_or_netamount;
31     }
32     $total ||= 0;
33     $total  += $transaction->amount;
34   }
35   die "Invalid calculated amount" if abs($total) - abs($self->amount) > 0.001;
36   return $tax_and_amount_by_tax_id;
37 }
38
39
40
41 1;
42
43 __END__
44
45 =pod
46
47 =encoding utf8
48
49 =head1 NAME
50
51 SL::DB::Helper::SalesPurchaseInvoice - Helper functions for Sales or Purchase bookings (mirrored)
52
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.
57
58 That is the main purpose for this helper class.
59 =head1 FUNCTIONS
60
61 =over 4
62
63 =item C<get_tax_and_amount_by_tax_chart_id>
64
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:
69
70  '775' => {
71     'tax_id'    => 777
72     'tax'       => '332.18',
73     'netamount' => '1748.32',
74   },
75
76  '194' => {
77     'tax_id'    => 378,
78     'netamount' => '20',
79     'tax'       => '1.4'
80   }
81
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.
85
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.
88
89 =back
90
91 =head1 BUGS
92
93 Nothing here yet.
94
95 =head1 AUTHOR
96
97 Jan Büren E<lt>jan@kivitendo.deE<gt>
98
99 =cut