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'};
51 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
53 push @{$data{label}}, "$smonth/$syear";
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;
61 foreach my $item ( @{$self->{'items'}} ) {
62 push @{$data{$item}}, $self->$item($speriod, $eperiod);
71 sub invoiced { #invoiced
72 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
74 SELECT SUM(charged) FROM cust_bill
75 WHERE ". $self->in_time_period($speriod, $eperiod)
79 sub netsales { #net sales
80 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
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')
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 _%'
100 $self->invoiced($speriod,$eperiod) - $credited - $expenses;
105 sub receipts { #cashflow
106 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
108 my $refunded = $self->scalar_sql("
109 SELECT SUM(refund) FROM cust_refund
110 WHERE ". $self->in_time_period($speriod, $eperiod)
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 _%'
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 _%'";
128 $self->payments($speriod, $eperiod) - $refunded - $expenses;
132 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
134 SELECT SUM(paid) FROM cust_pay
135 WHERE ". $self->in_time_period($speriod, $eperiod)
140 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
142 SELECT SUM(amount) FROM cust_credit
143 WHERE ". $self->in_time_period($speriod, $eperiod)
148 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
149 my $table = @_ ? shift().'.' : '';
150 "${table}_date >= $speriod AND ${table}_date < $eperiod";
154 my( $self, $sql ) = ( shift, shift );
155 my $sth = dbh->prepare($sql) or die dbh->errstr;
157 or die "Unexpected error executing statement $sql: ". $sth->errstr;
158 $sth->fetchrow_arrayref->[0] || 0;