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