Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / Report / Tax / All.pm
1 package FS::Report::Tax::All;
2
3 use strict;
4 use vars qw($DEBUG);
5 use FS::Record qw(dbh qsearch qsearchs group_concat_sql);
6 use FS::Report::Tax::ByName;
7 use Date::Format qw( time2str );
8
9 use Data::Dumper;
10
11 $DEBUG = 0;
12
13 =item report OPTIONS
14
15 Constructor. Generates a tax report using the internal tax rate system,
16 showing all taxes, broken down by tax name and country.
17
18 Required parameters:
19 - beginning, ending: the date range as Unix timestamps.
20
21 Optional parameters:
22 - debug: sets the debug level.  1 will warn the data collected for the report;
23 2 will also warn all of the SQL statements.
24
25 =cut
26
27 # because there's not yet a "DBIx::DBSchema::View"...
28
29 sub report {
30   my $class = shift;
31   my %opt = @_;
32
33   $DEBUG ||= $opt{debug};
34
35   my($beginning, $ending) = @opt{'beginning', 'ending'};
36
37   # figure out which reports we need to run
38   my @taxname_and_country = qsearch({
39       table     => 'cust_main_county',
40       select    => 'country, taxname',
41       hashref   => {
42         tax => { op => '>', value => '0' }
43       },
44       order_by  => 'GROUP BY country, taxname ORDER BY country, taxname',
45   });
46   my @table;
47   foreach (@taxname_and_country) {
48     my $taxname = $_->taxname || 'Tax';
49     my $country = $_->country;
50     my $report = FS::Report::Tax::ByName->report(
51       %opt,
52       taxname     => $taxname,
53       country     => $country,
54       total_only  => 1,
55     );
56     # will have only one total row (should be only one row at all)
57     my ($total_row) = grep { $_->{total} } $report->table;
58     $total_row->{total} = 0; # but in this context it's a detail row
59     $total_row->{taxname} = $taxname;
60     $total_row->{country} = $country;
61     $total_row->{label} = "$country - $taxname";
62     push @table, $total_row;
63   }
64   my $self = bless {
65     'opt'   => \%opt,
66     'table' => \@table,
67   }, $class;
68
69   $self;
70 }
71
72 sub opt {
73   my $self = shift;
74   $self->{opt};
75 }
76
77 sub data {
78   my $self = shift;
79   $self->{data};
80 }
81
82 # sub fetchall_array...
83
84 sub table {
85   my $self = shift;
86   @{ $self->{table} };
87 }
88
89 sub title {
90   my $self = shift;
91   my $string = '';
92   if ( $self->{opt}->{agentnum} ) {
93     my $agent = qsearchs('agent', { agentnum => $self->{opt}->{agentnum} });
94     $string .= $agent->agent . ' ';
95   }
96   $string .= 'Tax Report: '; # XXX localization
97   if ( $self->{opt}->{beginning} ) {
98     $string .= time2str('%h %o %Y ', $self->{opt}->{beginning});
99   }
100   $string .= 'through ';
101   if ( $self->{opt}->{ending} and $self->{opt}->{ending} < 4294967295 ) {
102     $string .= time2str('%h %o %Y', $self->{opt}->{ending});
103   } else {
104     $string .= 'now';
105   }
106   $string .= ' - all taxes';
107   return $string;
108 }
109
110 1;