c18106461718063bbadd78afc8cf1850a6aba3f5
[freeside.git] / FS / FS / Report / Table / Daily.pm
1 package FS::Report::Table::Daily;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Report::Table;
6 use FS::Conf;
7 use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
8 use Date::Format qw( time2str );
9
10 @ISA = qw( FS::Report::Table );
11
12 =head1 NAME
13
14 FS::Report::Table::Daily - Tables of report data, indexed daily
15
16 =head1 SYNOPSIS
17
18   use FS::Report::Table::Daily;
19
20   my $report = new FS::Report::Table::Daily (
21     'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
22     'start_month' => 4,
23     'start_year'  => 2000,
24     'end_month'   => 4,
25     'end_year'    => 2020,
26     'start_day'   => 2,
27     'end_day'     => 27,
28     #opt
29     'agentnum'    => 54
30     'cust_classnum' => [ 1,2,4 ],
31     'params'      => [ [ 'paramsfor', 'item_one' ], [ 'item', 'two' ] ], # ...
32     'remove_empty' => 1, #collapse empty rows, default 0
33     'item_labels' => [ ], #useful with remove_empty
34   );
35
36   my $data = $report->data;
37
38 =head1 METHODS
39
40 =over 4
41
42 =item data
43
44 Returns a hashref of data (!! describe)
45
46 =cut
47
48 sub data {
49   my $self = shift;
50
51   my $sday = $self->{'start_day'};
52   my $smonth = $self->{'start_month'};
53   my $syear = $self->{'start_year'};
54   my $eday = $self->{'end_day'};
55   my $emonth = $self->{'end_month'};
56   my $eyear = $self->{'end_year'};
57   my $agentnum = $self->{'agentnum'};
58   my $cust_classnum = $self->{'cust_classnum'} || [];
59   $cust_classnum = [ $cust_classnum ] if !ref($cust_classnum);
60
61   my %data;
62
63   my $sdate = timelocal(0,0,0,$sday,$smonth-1,$syear);
64   my $edate = timelocal(0,0,0,$eday,$emonth-1,$eyear);
65
66   my $conf = FS::Conf->new;
67   my $date_format = $conf->config('date_format') || '%d/%m/%Y';
68
69   #warn "daily range $sdate $edate\n";
70
71   # XXX: use date_format config for the labels since we have day in the labels now?
72   while ( $sdate < $edate ) {
73     push @{$data{label}}, time2str($date_format, $sdate);
74
75     my $speriod = $sdate;
76
77     #ala part_pkg->add_freq, to deal with local DST.  DateTime also a good idea
78     my ($mday,$mon,$year) = (localtime($sdate) )[3,4,5];
79     $sdate = timelocal_nocheck(0,0,0,$mday+1,$mon,$year);
80
81     my $eperiod = $sdate;
82
83     push @{$data{speriod}}, $speriod;
84     push @{$data{eperiod}}, $eperiod;
85
86     my $col = 0;
87     my @items = @{$self->{'items'}};
88     my $i;
89     for ( $i = 0; $i < scalar(@items); $i++ ) {
90           my $item = $items[$i];
91           my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
92           push @param, 'cust_classnum' => $cust_classnum if @$cust_classnum;
93           my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
94           push @{$data{data}->[$col++]}, $value;
95     }
96   }
97
98   #these need to get generalized, sheesh
99   $data{'items'}       = $self->{'items'};
100   $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
101   $data{'colors'}      = $self->{'colors'};
102   $data{'links'}       = $self->{'links'} || [];
103
104   if ( $self->{'remove_empty'} ) {
105
106     my $col = 0;
107     #these need to get generalized, sheesh
108     my @newitems = ();
109     my @newlabels = ();
110     my @newdata = ();
111     my @newcolors = ();
112     my @newlinks = ();
113     foreach my $item ( @{$self->{'items'}} ) {
114
115       if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
116         push @newitems,  $data{'items'}->[$col];
117         push @newlabels, $data{'item_labels'}->[$col];
118         push @newdata,   $data{'data'}->[$col];
119         push @newcolors, $data{'colors'}->[$col];
120         push @newlinks,  $data{'links'}->[$col];
121       }
122
123       $col++;
124     }
125
126     $data{'items'}       = \@newitems;
127     $data{'item_labels'} = \@newlabels;
128     $data{'data'}        = \@newdata;
129     $data{'colors'}      = \@newcolors;
130     $data{'links'}       = \@newlinks;
131
132   }
133
134   \%data;
135
136 }
137
138 =back
139
140 =head1 BUGS
141
142 Documentation.
143
144 =head1 SEE ALSO
145
146 =cut
147
148 1;