1 package FS::Report::Table::Monthly;
4 use vars qw( @ISA $expenses_kludge );
9 @ISA = qw( FS::Report::Table );
15 FS::Report::Table::Monthly - Tables of report data, indexed monthly
19 use FS::Report::Table::Monthly;
21 my $report = new FS::Report::Table::Monthly (
22 'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
29 my $data = $report->data;
37 Returns a hashref of data (!! describe)
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'};
52 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
54 push @{$data{label}}, "$smonth/$syear";
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;
62 foreach my $item ( @{$self->{'items'}} ) {
63 push @{$data{$item}}, $self->$item($speriod, $eperiod, $agentnum);
72 sub invoiced { #invoiced
73 my( $self, $speriod, $eperiod, $agentnum ) = @_;
77 LEFT JOIN cust_main USING ( custnum )
78 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
82 sub netsales { #net sales
83 my( $self, $speriod, $eperiod, $agentnum ) = @_;
85 my $credited = $self->scalar_sql("
86 SELECT SUM(cust_credit_bill.amount)
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')
93 #horrible local kludge
94 my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
95 SELECT SUM(cust_bill_pkg.setup)
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 _%'
105 $self->invoiced($speriod,$eperiod,$agentnum) - $credited - $expenses;
110 sub receipts { #cashflow
111 my( $self, $speriod, $eperiod, $agentnum ) = @_;
113 my $refunded = $self->scalar_sql("
116 LEFT JOIN cust_main USING ( custnum )
117 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
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)
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 _%'
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 _%'";
136 $self->payments($speriod, $eperiod, $agentnum) - $refunded - $expenses;
140 my( $self, $speriod, $eperiod, $agentnum ) = @_;
144 LEFT JOIN cust_main USING ( custnum )
145 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
150 my( $self, $speriod, $eperiod, $agentnum ) = @_;
154 LEFT JOIN cust_main USING ( custnum )
155 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
159 #these should be auto-generated
161 my( $self, $speriod, $eperiod, $agentnum ) = @_;
162 $speriod = $self->_subtract_11mo($speriod);
163 $self->invoiced($speriod, $eperiod, $agentnum);
167 my( $self, $speriod, $eperiod, $agentnum ) = @_;
168 $speriod = $self->_subtract_11mo($speriod);
169 $self->netsales($speriod, $eperiod, $agentnum);
173 my( $self, $speriod, $eperiod, $agentnum ) = @_;
174 $speriod = $self->_subtract_11mo($speriod);
175 $self->receipts($speriod, $eperiod, $agentnum);
179 my( $self, $speriod, $eperiod, $agentnum ) = @_;
180 $speriod = $self->_subtract_11mo($speriod);
181 $self->payments($speriod, $eperiod, $agentnum);
185 my( $self, $speriod, $eperiod, $agentnum ) = @_;
186 $speriod = $self->_subtract_11mo($speriod);
187 $self->credits($speriod, $eperiod, $agentnum);
190 #not being too bad with the false laziness
191 use Time::Local qw(timelocal);
193 my($self, $time) = @_;
194 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
196 if ( $mon < 0 ) { $mon+=12; $year--; }
197 timelocal($sec,$min,$hour,$mday,$mon,$year);
200 # NEEDS TO BE AGENTNUM-capable
201 sub canceled { #active
202 my( $self, $speriod, $eperiod, $agentnum ) = @_;
206 LEFT JOIN cust_main USING ( custnum )
207 WHERE 0 = ( SELECT COUNT(*)
209 WHERE cust_pkg.custnum = cust_main.custnum
210 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
212 AND cust_pkg.cancel > $speriod AND cust_pkg.cancel < $eperiod
216 # NEEDS TO BE AGENTNUM-capable
217 sub newaccount { #newaccount
218 my( $self, $speriod, $eperiod, $agentnum ) = @_;
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
228 # NEEDS TO BE AGENTNUM-capable
229 sub suspended { #suspended
230 my( $self, $speriod, $eperiod, $agentnum ) = @_;
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 )
239 AND cust_pkg.susp > $speriod AND cust_pkg.susp < $eperiod
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"
253 my( $self, $sql ) = ( shift, shift );
254 my $sth = dbh->prepare($sql) or die dbh->errstr;
256 or die "Unexpected error executing statement $sql: ". $sth->errstr;
257 $sth->fetchrow_arrayref->[0] || 0;