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 # NEEDS TO BE AGENTNUM-capable
160 sub canceled { #active
161 my( $self, $speriod, $eperiod, $agentnum ) = @_;
165 LEFT JOIN cust_main USING ( custnum )
166 WHERE 0 = ( SELECT COUNT(*)
168 WHERE cust_pkg.custnum = cust_main.custnum
169 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
171 AND cust_pkg.cancel > $speriod AND cust_pkg.cancel < $eperiod
175 # NEEDS TO BE AGENTNUM-capable
176 sub newaccount { #newaccount
177 my( $self, $speriod, $eperiod, $agentnum ) = @_;
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
187 # NEEDS TO BE AGENTNUM-capable
188 sub suspended { #suspended
189 my( $self, $speriod, $eperiod, $agentnum ) = @_;
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 )
198 AND cust_pkg.susp > $speriod AND cust_pkg.susp < $eperiod
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"
212 my( $self, $sql ) = ( shift, shift );
213 my $sth = dbh->prepare($sql) or die dbh->errstr;
215 or die "Unexpected error executing statement $sql: ". $sth->errstr;
216 $sth->fetchrow_arrayref->[0] || 0;