add a "pre-report" page to this report/graph as requested by lewis/wtxs, also add...
[freeside.git] / FS / FS / Report / Table / Monthly.pm
1 package FS::Report::Table::Monthly;
2
3 use strict;
4 use vars qw( @ISA $expenses_kludge );
5 use Time::Local;
6 use FS::UID qw( dbh );
7 use FS::Report::Table;
8
9 @ISA = qw( FS::Report::Table );
10
11 $expenses_kludge = 0;
12
13 =head1 NAME
14
15 FS::Report::Table::Monthly - Tables of report data, indexed monthly
16
17 =head1 SYNOPSIS
18
19   use FS::Report::Table::Monthly;
20
21   my $report = new FS::Report::Table::Monthly (
22     'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
23     'start_month' => 4,
24     'start_year'  => 2000,
25     'end_month'   => 4,
26     'end_year'    => 2020,
27   );
28
29   my $data = $report->data;
30
31 =head1 METHODS
32
33 =over 4
34
35 =item data
36
37 Returns a hashref of data (!! describe)
38
39 =cut
40
41 sub data {
42   my $self = shift;
43
44   my $smonth = $self->{'start_month'};
45   my $syear = $self->{'start_year'};
46   my $emonth = $self->{'end_month'};
47   my $eyear = $self->{'end_year'};
48   my $agentnum = $self->{'agentnum'};
49
50   my %data;
51
52   while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
53
54     push @{$data{label}}, "$smonth/$syear";
55
56     my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
57     push @{$data{speriod}}, $speriod;
58     if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
59     my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
60     push @{$data{eperiod}}, $eperiod;
61   
62     foreach my $item ( @{$self->{'items'}} ) {
63       push @{$data{$item}}, $self->$item($speriod, $eperiod, $agentnum);
64     }
65
66   }
67
68   \%data;
69
70 }
71
72 sub invoiced { #invoiced
73   my( $self, $speriod, $eperiod, $agentnum ) = @_;
74   $self->scalar_sql("
75     SELECT SUM(charged)
76       FROM cust_bill
77         LEFT JOIN cust_main USING ( custnum )
78       WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
79   );
80 }
81
82 sub netsales { #net sales
83   my( $self, $speriod, $eperiod, $agentnum ) = @_;
84
85   my $credited = $self->scalar_sql("
86     SELECT SUM(cust_credit_bill.amount)
87       FROM cust_credit_bill
88         LEFT JOIN cust_bill USING ( invnum  )
89         LEFT JOIN cust_main USING ( custnum )
90     WHERE ".  $self->in_time_period_and_agent($speriod, $eperiod, $agentnum, 'cust_bill')
91   );
92
93   #horrible local kludge
94   my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
95     SELECT SUM(cust_bill_pkg.setup)
96       FROM cust_bill_pkg
97         LEFT JOIN cust_bill USING ( invnum  )
98         LEFT JOIN cust_main USING ( custnum )
99         LEFT JOIN cust_pkg  USING ( pkgnum  )
100         LEFT JOIN part_pkg  USING ( pkgpart )
101       WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum, 'cust_bill'). "
102         AND LOWER(part_pkg.pkg) LIKE 'expense _%'
103   ");
104
105   $self->invoiced($speriod,$eperiod,$agentnum) - $credited - $expenses;
106 }
107
108 #deferred revenue
109
110 sub receipts { #cashflow
111   my( $self, $speriod, $eperiod, $agentnum ) = @_;
112
113   my $refunded = $self->scalar_sql("
114     SELECT SUM(refund)
115       FROM cust_refund
116         LEFT JOIN cust_main USING ( custnum )
117       WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
118   );
119
120   #horrible local kludge that doesn't even really work right
121   my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
122     SELECT SUM(cust_bill_pay.amount)
123       FROM cust_bill_pay
124         LEFT JOIN cust_bill USING ( invnum  )
125         LEFT JOIN cust_main USING ( custnum )
126     WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum, 'cust_bill_pay'). "
127     AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
128               WHERE cust_bill.invnum = cust_bill_pkg.invnum
129               AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
130               AND cust_pkg.pkgpart = part_pkg.pkgpart
131               AND LOWER(part_pkg.pkg) LIKE 'expense _%'
132             )
133   ");
134   #    my $expenses_sql2 = "SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay, cust_bill_pkg, cust_bill, cust_pkg, part_pkg WHERE cust_bill_pay.invnum = cust_bill.invnum AND cust_bill.invnum = cust_bill_pkg.invnum AND cust_bill_pay._date >= $speriod AND cust_bill_pay._date < $eperiod AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum AND cust_pkg.pkgpart = part_pkg.pkgpart AND LOWER(part_pkg.pkg) LIKE 'expense _%'";
135   
136   $self->payments($speriod, $eperiod, $agentnum) - $refunded - $expenses;
137 }
138
139 sub payments {
140   my( $self, $speriod, $eperiod, $agentnum ) = @_;
141   $self->scalar_sql("
142     SELECT SUM(paid)
143       FROM cust_pay
144         LEFT JOIN cust_main USING ( custnum )
145       WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
146   );
147 }
148
149 sub credits {
150   my( $self, $speriod, $eperiod, $agentnum ) = @_;
151   $self->scalar_sql("
152     SELECT SUM(amount)
153       FROM cust_credit
154         LEFT JOIN cust_main USING ( custnum )
155       WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
156   );
157 }
158
159 #these should be auto-generated
160 sub invoiced_12mo {
161   my( $self, $speriod, $eperiod, $agentnum ) = @_;
162   $speriod = $self->_subtract_11mo($speriod);
163   $self->invoiced($speriod, $eperiod, $agentnum);
164 }
165
166 sub netsales_12mo {
167   my( $self, $speriod, $eperiod, $agentnum ) = @_;
168   $speriod = $self->_subtract_11mo($speriod);
169   $self->netsales($speriod, $eperiod, $agentnum);
170 }
171
172 sub receipts_12mo {
173   my( $self, $speriod, $eperiod, $agentnum ) = @_;
174   $speriod = $self->_subtract_11mo($speriod);
175   $self->receipts($speriod, $eperiod, $agentnum);
176 }
177
178 sub payments_12mo {
179   my( $self, $speriod, $eperiod, $agentnum ) = @_;
180   $speriod = $self->_subtract_11mo($speriod);
181   $self->payments($speriod, $eperiod, $agentnum);
182 }
183
184 sub credits_12mo {
185   my( $self, $speriod, $eperiod, $agentnum ) = @_;
186   $speriod = $self->_subtract_11mo($speriod);
187   $self->credits($speriod, $eperiod, $agentnum);
188 }
189
190 #not being too bad with the false laziness
191 use Time::Local qw(timelocal);
192 sub _subtract_11mo {
193   my($self, $time) = @_;
194   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
195   $mon -= 11;
196   if ( $mon < 0 ) { $mon+=12; $year--; }
197   timelocal($sec,$min,$hour,$mday,$mon,$year);
198 }
199
200 # NEEDS TO BE AGENTNUM-capable
201 sub canceled { #active
202   my( $self, $speriod, $eperiod, $agentnum ) = @_;
203   $self->scalar_sql("
204     SELECT COUNT(*)
205       FROM cust_pkg
206         LEFT JOIN cust_main USING ( custnum )
207       WHERE 0 = ( SELECT COUNT(*)
208                     FROM cust_pkg
209                     WHERE cust_pkg.custnum = cust_main.custnum
210                       AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
211                 )
212         AND cust_pkg.cancel > $speriod AND cust_pkg.cancel < $eperiod
213   ");
214 }
215  
216 # NEEDS TO BE AGENTNUM-capable
217 sub newaccount { #newaccount
218   my( $self, $speriod, $eperiod, $agentnum ) = @_;
219   $self->scalar_sql("
220      SELECT COUNT(*) FROM cust_pkg
221      WHERE cust_pkg.custnum = cust_main.custnum
222      AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
223      AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
224      AND cust_pkg.setup > $speriod AND cust_pkg.setup < $eperiod
225   ");
226 }
227  
228 # NEEDS TO BE AGENTNUM-capable
229 sub suspended { #suspended
230   my( $self, $speriod, $eperiod, $agentnum ) = @_;
231   $self->scalar_sql("
232      SELECT COUNT(*) FROM cust_pkg
233      WHERE cust_pkg.custnum = cust_main.custnum
234      AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
235      AND 0 = ( SELECT COUNT(*) FROM cust_pkg
236                WHERE cust_pkg.custnum = cust_main.custnum
237                AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
238              )
239      AND cust_pkg.susp > $speriod AND cust_pkg.susp < $eperiod
240   ");
241 }
242
243 sub in_time_period_and_agent {
244   my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
245   my $table = @_ ? shift().'.' : '';
246   my $sql = "${table}_date >= $speriod AND ${table}_date < $eperiod";
247   $sql .= " AND agentnum = $agentnum"
248     if $agentnum;
249   $sql;
250 }
251
252 sub scalar_sql {
253   my( $self, $sql ) = ( shift, shift );
254   my $sth = dbh->prepare($sql) or die dbh->errstr;
255   $sth->execute
256     or die "Unexpected error executing statement $sql: ". $sth->errstr;
257   $sth->fetchrow_arrayref->[0] || 0;
258 }
259
260 =back
261
262 =head1 BUGS
263
264 Documentation.
265
266 =head1 SEE ALSO
267
268 =cut
269
270 1;
271