agent-specific sales/credit/receipts summary
[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 # NEEDS TO BE AGENTNUM-capable
160 sub canceled { #active
161   my( $self, $speriod, $eperiod, $agentnum ) = @_;
162   $self->scalar_sql("
163     SELECT COUNT(*)
164       FROM cust_pkg
165         LEFT JOIN cust_main USING ( custnum )
166       WHERE 0 = ( SELECT COUNT(*)
167                     FROM cust_pkg
168                     WHERE cust_pkg.custnum = cust_main.custnum
169                       AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
170                 )
171         AND cust_pkg.cancel > $speriod AND cust_pkg.cancel < $eperiod
172   ");
173 }
174  
175 # NEEDS TO BE AGENTNUM-capable
176 sub newaccount { #newaccount
177   my( $self, $speriod, $eperiod, $agentnum ) = @_;
178   $self->scalar_sql("
179      SELECT COUNT(*) FROM cust_pkg
180      WHERE cust_pkg.custnum = cust_main.custnum
181      AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
182      AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
183      AND cust_pkg.setup > $speriod AND cust_pkg.setup < $eperiod
184   ");
185 }
186  
187 # NEEDS TO BE AGENTNUM-capable
188 sub suspended { #suspended
189   my( $self, $speriod, $eperiod, $agentnum ) = @_;
190   $self->scalar_sql("
191      SELECT COUNT(*) FROM cust_pkg
192      WHERE cust_pkg.custnum = cust_main.custnum
193      AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
194      AND 0 = ( SELECT COUNT(*) FROM cust_pkg
195                WHERE cust_pkg.custnum = cust_main.custnum
196                AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
197              )
198      AND cust_pkg.susp > $speriod AND cust_pkg.susp < $eperiod
199   ");
200 }
201
202 sub in_time_period_and_agent {
203   my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
204   my $table = @_ ? shift().'.' : '';
205   my $sql = "${table}_date >= $speriod AND ${table}_date < $eperiod";
206   $sql .= " AND agentnum = $agentnum"
207     if $agentnum;
208   $sql;
209 }
210
211 sub scalar_sql {
212   my( $self, $sql ) = ( shift, shift );
213   my $sth = dbh->prepare($sql) or die dbh->errstr;
214   $sth->execute
215     or die "Unexpected error executing statement $sql: ". $sth->errstr;
216   $sth->fetchrow_arrayref->[0] || 0;
217 }
218
219 =back
220
221 =head1 BUGS
222
223 Documentation.
224
225 =head1 SEE ALSO
226
227 =cut
228
229 1;
230