summaryrefslogtreecommitdiff
path: root/FS/FS/Report/Tax/All.pm
blob: 26dbf5f0fe611867cc3146a80d3dbaaa1727e5d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package FS::Report::Tax::All;

use strict;
use vars qw($DEBUG);
use FS::Record qw(dbh qsearch qsearchs group_concat_sql);
use FS::Report::Tax::ByName;
use Date::Format qw( time2str );

use Data::Dumper;

$DEBUG = 0;

=item report OPTIONS

Constructor. Generates a tax report using the internal tax rate system,
showing all taxes, broken down by tax name and country.

Required parameters:
- beginning, ending: the date range as Unix timestamps.

Optional parameters:
- debug: sets the debug level.  1 will warn the data collected for the report;
2 will also warn all of the SQL statements.

=cut

# because there's not yet a "DBIx::DBSchema::View"...

sub report {
  my $class = shift;
  my %opt = @_;

  $DEBUG ||= $opt{debug};

  my($beginning, $ending) = @opt{'beginning', 'ending'};

  # figure out which reports we need to run
  my @taxname_and_country = qsearch({
      table     => 'cust_main_county',
      select    => 'country, taxname',
      hashref   => {
        tax => { op => '>', value => '0' }
      },
      order_by  => 'GROUP BY country, taxname ORDER BY country, taxname',
  });
  my @table;
  foreach (@taxname_and_country) {
    my $taxname = $_->taxname || 'Tax';
    my $country = $_->country;
    my $report = FS::Report::Tax::ByName->report(
      %opt,
      taxname     => $taxname,
      country     => $country,
      total_only  => 1,
    );
    # will have only one total row (should be only one row at all)
    my ($total_row) = grep { $_->{total} } $report->table;
    $total_row->{total} = 0; # but in this context it's a detail row
    $total_row->{taxname} = $taxname;
    $total_row->{country} = $country;
    $total_row->{label} = "$country - $taxname";
    push @table, $total_row;
  }
  my $self = bless {
    'opt'   => \%opt,
    'table' => \@table,
  }, $class;

  $self;
}

sub opt {
  my $self = shift;
  $self->{opt};
}

sub data {
  my $self = shift;
  $self->{data};
}

# sub fetchall_array...

sub table {
  my $self = shift;
  @{ $self->{table} };
}

sub title {
  my $self = shift;
  my $string = '';
  if ( $self->{opt}->{agentnum} ) {
    my $agent = qsearchs('agent', { agentnum => $self->{opt}->{agentnum} });
    $string .= $agent->agent . ' ';
  }
  $string .= 'Tax Report: '; # XXX localization
  if ( $self->{opt}->{beginning} ) {
    $string .= time2str('%h %o %Y ', $self->{opt}->{beginning});
  }
  $string .= 'through ';
  if ( $self->{opt}->{ending} and $self->{opt}->{ending} < 4294967295 ) {
    $string .= time2str('%h %o %Y', $self->{opt}->{ending});
  } else {
    $string .= 'now';
  }
  $string .= ' - all taxes';
  return $string;
}

1;