This commit was generated by cvs2svn to compensate for changes in r3241,
[freeside.git] / FS / FS / Report / Table / Monthly.pm
1 package FS::Report::Table::Monthly;
2
3 use strict;
4 use vars qw( @ISA );
5 use Time::Local;
6 use FS::UID qw( dbh );
7 use FS::Report::Table;
8
9 @ISA = qw( FS::Report::Table );
10
11 =head1 NAME
12
13 FS::Report::Table::Monthly - Tables of report data, indexed monthly
14
15 =head1 SYNOPSIS
16
17   use FS::Report::Table::Monthly;
18
19   my $report = new FS::Report::Table (
20     'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
21     'start_month' => 4,
22     'start_year'  => 2000,
23     'end_month'   => 4,
24     'end_year'    => 2020,
25   );
26
27   my $data = $report->data;
28
29 =head1 METHODS
30
31 =over 4
32
33 =item data
34
35 Returns a hashref of data (!! describe)
36
37 =cut
38
39 sub data {
40   my $self = shift;
41
42   my $smonth = $self->{'start_month'};
43   my $syear = $self->{'start_year'};
44   my $emonth = $self->{'end_month'};
45   my $eyear = $self->{'end_year'};
46
47   my %data;
48
49   while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
50
51     push @{$data{label}}, "$smonth/$syear";
52
53     my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
54     if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
55     my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
56   
57     foreach my $item ( @{$self->{'items'}} ) {
58       push @{$data{$item}}, $self->$item($speriod, $eperiod);
59     }
60
61   }
62
63   \%data;
64
65 }
66
67 sub invoiced { #invoiced
68   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
69   $self->scalar_sql("
70     SELECT SUM(charged) FROM cust_bill
71     WHERE ". $self->in_time_period($speriod, $eperiod)
72   );
73 }
74
75 sub netsales { #net sales
76   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
77
78   my $credited = $self->scalar_sql("
79     SELECT SUM(cust_credit_bill.amount)
80     FROM cust_credit_bill, cust_bill
81     WHERE cust_bill.invnum = cust_credit_bill.invnum
82       AND ".  $self->in_time_period($speriod, $eperiod, 'cust_bill')
83   );
84
85   #horrible local kludge
86   my $expenses = $self->scalar_sql("
87     SELECT SUM(cust_bill_pkg.setup)
88     FROM cust_bill_pkg, cust_bill, cust_pkg, part_pkg
89     WHERE cust_bill.invnum = cust_bill_pkg.invnum
90     AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill'). "
91     AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
92     AND cust_pkg.pkgpart = part_pkg.pkgpart
93     AND LOWER(part_pkg.pkg) LIKE 'expense _%'
94   ");
95
96   $self->invoiced($speriod,$eperiod)-$credited-$expenses;
97 }
98
99 #deferred revenue
100
101 sub receipts { #cashflow
102   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
103
104   #cashflow
105   my $paid = $self->scalar_sql("
106     SELECT SUM(paid) FROM cust_pay
107     WHERE ". $self->in_time_period($speriod, $eperiod)
108   );
109
110   my $refunded = $self->scalar_sql("
111     SELECT SUM(refund) FROM cust_refund
112     WHERE ". $self->in_time_period($speriod, $eperiod)
113   );
114
115   #horrible local kludge that doesn't even really work right
116   my $expenses = $self->scalar_sql("
117     SELECT SUM(cust_bill_pay.amount)
118     FROM cust_bill_pay, cust_bill
119     WHERE cust_bill_pay.invnum = cust_bill.invnum
120     AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill_pay'). "
121     AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
122               WHERE cust_bill.invnum = cust_bill_pkg.invnum
123               AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
124               AND cust_pkg.pkgpart = part_pkg.pkgpart
125               AND LOWER(part_pkg.pkg) LIKE 'expense _%'
126             )
127   ");
128   #    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 _%'";
129   
130   $paid-$refunded-$expenses;
131 }
132
133 sub credits {
134   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
135   $self->scalar_sql("
136     SELECT SUM(amount) FROM cust_credit
137     WHERE ". $self->in_time_period($speriod, $eperiod)
138   );
139 }
140
141 sub in_time_period {
142   my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
143   my $table = @_ ? shift().'.' : '';
144   "${table}_date >= $speriod AND ${table}_date < $eperiod";
145 }
146
147 sub scalar_sql {
148   my( $self, $sql ) = ( shift, shift );
149   my $sth = dbh->prepare($sql) or die dbh->errstr;
150   $sth->execute
151     or die "Unexpected error executing statement $sql: ". $sth->errstr;
152   $sth->fetchrow_arrayref->[0] || 0;
153 }
154
155 =back
156
157 =head1 BUGS
158
159 Documentation.
160
161 =head1 SEE ALSO
162
163 =cut
164
165 1;
166