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 (
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 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
57 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
59 foreach my $item ( @{$self->{'items'}} ) {
60 push @{$data{$item}}, $self->$item($speriod, $eperiod);
69 sub invoiced { #invoiced
70 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
72 SELECT SUM(charged) FROM cust_bill
73 WHERE ". $self->in_time_period($speriod, $eperiod)
77 sub netsales { #net sales
78 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
80 my $credited = $self->scalar_sql("
81 SELECT SUM(cust_credit_bill.amount)
82 FROM cust_credit_bill, cust_bill
83 WHERE cust_bill.invnum = cust_credit_bill.invnum
84 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill')
87 #horrible local kludge
88 my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
89 SELECT SUM(cust_bill_pkg.setup)
90 FROM cust_bill_pkg, cust_bill, cust_pkg, part_pkg
91 WHERE cust_bill.invnum = cust_bill_pkg.invnum
92 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill'). "
93 AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
94 AND cust_pkg.pkgpart = part_pkg.pkgpart
95 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
98 $self->invoiced($speriod,$eperiod)-$credited-$expenses;
103 sub receipts { #cashflow
104 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
107 my $paid = $self->scalar_sql("
108 SELECT SUM(paid) FROM cust_pay
109 WHERE ". $self->in_time_period($speriod, $eperiod)
112 my $refunded = $self->scalar_sql("
113 SELECT SUM(refund) FROM cust_refund
114 WHERE ". $self->in_time_period($speriod, $eperiod)
117 #horrible local kludge that doesn't even really work right
118 my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
119 SELECT SUM(cust_bill_pay.amount)
120 FROM cust_bill_pay, cust_bill
121 WHERE cust_bill_pay.invnum = cust_bill.invnum
122 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill_pay'). "
123 AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
124 WHERE cust_bill.invnum = cust_bill_pkg.invnum
125 AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
126 AND cust_pkg.pkgpart = part_pkg.pkgpart
127 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
130 # 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 _%'";
132 $paid-$refunded-$expenses;
136 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
138 SELECT SUM(amount) FROM cust_credit
139 WHERE ". $self->in_time_period($speriod, $eperiod)
144 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
145 my $table = @_ ? shift().'.' : '';
146 "${table}_date >= $speriod AND ${table}_date < $eperiod";
150 my( $self, $sql ) = ( shift, shift );
151 my $sth = dbh->prepare($sql) or die dbh->errstr;
153 or die "Unexpected error executing statement $sql: ". $sth->errstr;
154 $sth->fetchrow_arrayref->[0] || 0;