don't run my local expenses kludge by default; horrible performance
[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 (
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
49   my %data;
50
51   while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
52
53     push @{$data{label}}, "$smonth/$syear";
54
55     my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
56     if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
57     my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
58   
59     foreach my $item ( @{$self->{'items'}} ) {
60       push @{$data{$item}}, $self->$item($speriod, $eperiod);
61     }
62
63   }
64
65   \%data;
66
67 }
68
69 sub invoiced { #invoiced
70   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
71   $self->scalar_sql("
72     SELECT SUM(charged) FROM cust_bill
73     WHERE ". $self->in_time_period($speriod, $eperiod)
74   );
75 }
76
77 sub netsales { #net sales
78   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
79
80   my $credited = $self->scalar_sql("
81     SELECT SUM(cust_credit_bill.amount)
82     FROM cust_credit_bill, cust_bill
83     WHERE cust_bill.invnum = cust_credit_bill.invnum
84       AND ".  $self->in_time_period($speriod, $eperiod, 'cust_bill')
85   );
86
87   #horrible local kludge
88   my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
89     SELECT SUM(cust_bill_pkg.setup)
90     FROM cust_bill_pkg, cust_bill, cust_pkg, part_pkg
91     WHERE cust_bill.invnum = cust_bill_pkg.invnum
92     AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill'). "
93     AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
94     AND cust_pkg.pkgpart = part_pkg.pkgpart
95     AND LOWER(part_pkg.pkg) LIKE 'expense _%'
96   ");
97
98   $self->invoiced($speriod,$eperiod)-$credited-$expenses;
99 }
100
101 #deferred revenue
102
103 sub receipts { #cashflow
104   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
105
106   #cashflow
107   my $paid = $self->scalar_sql("
108     SELECT SUM(paid) FROM cust_pay
109     WHERE ". $self->in_time_period($speriod, $eperiod)
110   );
111
112   my $refunded = $self->scalar_sql("
113     SELECT SUM(refund) FROM cust_refund
114     WHERE ". $self->in_time_period($speriod, $eperiod)
115   );
116
117   #horrible local kludge that doesn't even really work right
118   my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
119     SELECT SUM(cust_bill_pay.amount)
120     FROM cust_bill_pay, cust_bill
121     WHERE cust_bill_pay.invnum = cust_bill.invnum
122     AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill_pay'). "
123     AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
124               WHERE cust_bill.invnum = cust_bill_pkg.invnum
125               AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
126               AND cust_pkg.pkgpart = part_pkg.pkgpart
127               AND LOWER(part_pkg.pkg) LIKE 'expense _%'
128             )
129   ");
130   #    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 _%'";
131   
132   $paid-$refunded-$expenses;
133 }
134
135 sub credits {
136   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
137   $self->scalar_sql("
138     SELECT SUM(amount) FROM cust_credit
139     WHERE ". $self->in_time_period($speriod, $eperiod)
140   );
141 }
142
143 sub in_time_period {
144   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
145   my $table = @_ ? shift().'.' : '';
146   "${table}_date >= $speriod AND ${table}_date < $eperiod";
147 }
148
149 sub scalar_sql {
150   my( $self, $sql ) = ( shift, shift );
151   my $sth = dbh->prepare($sql) or die dbh->errstr;
152   $sth->execute
153     or die "Unexpected error executing statement $sql: ". $sth->errstr;
154   $sth->fetchrow_arrayref->[0] || 0;
155 }
156
157 =back
158
159 =head1 BUGS
160
161 Documentation.
162
163 =head1 SEE ALSO
164
165 =cut
166
167 1;
168