1 package FS::Report::Table::Monthly;
9 @ISA = qw( FS::Report::Table );
13 FS::Report::Table::Monthly - Tables of report data, indexed monthly
17 use FS::Report::Table::Monthly;
19 my $report = new FS::Report::Table (
20 'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
27 my $data = $report->data;
35 Returns a hashref of data (!! describe)
42 my $smonth = $self->{'start_month'};
43 my $syear = $self->{'start_year'};
44 my $emonth = $self->{'end_month'};
45 my $eyear = $self->{'end_year'};
49 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
51 push @{$data{label}}, "$smonth/$syear";
53 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
54 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
55 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
57 foreach my $item ( @{$self->{'items'}} ) {
58 push @{$data{$item}}, $self->$item($speriod, $eperiod);
67 sub invoiced { #invoiced
68 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
70 SELECT SUM(charged) FROM cust_bill
71 WHERE ". $self->in_time_period($speriod, $eperiod)
75 sub netsales { #net sales
76 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
78 my $credited = $self->scalar_sql("
79 SELECT SUM(cust_credit_bill.amount)
80 FROM cust_credit_bill, cust_bill
81 WHERE cust_bill.invnum = cust_credit_bill.invnum
82 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill')
85 #horrible local kludge
86 my $expenses = $self->scalar_sql("
87 SELECT SUM(cust_bill_pkg.setup)
88 FROM cust_bill_pkg, cust_bill, cust_pkg, part_pkg
89 WHERE cust_bill.invnum = cust_bill_pkg.invnum
90 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill'). "
91 AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
92 AND cust_pkg.pkgpart = part_pkg.pkgpart
93 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
96 $self->invoiced($speriod,$eperiod)-$credited-$expenses;
101 sub receipts { #cashflow
102 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
105 my $paid = $self->scalar_sql("
106 SELECT SUM(paid) FROM cust_pay
107 WHERE ". $self->in_time_period($speriod, $eperiod)
110 my $refunded = $self->scalar_sql("
111 SELECT SUM(refund) FROM cust_refund
112 WHERE ". $self->in_time_period($speriod, $eperiod)
115 #horrible local kludge that doesn't even really work right
116 my $expenses = $self->scalar_sql("
117 SELECT SUM(cust_bill_pay.amount)
118 FROM cust_bill_pay, cust_bill
119 WHERE cust_bill_pay.invnum = cust_bill.invnum
120 AND ". $self->in_time_period($speriod, $eperiod, 'cust_bill_pay'). "
121 AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
122 WHERE cust_bill.invnum = cust_bill_pkg.invnum
123 AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
124 AND cust_pkg.pkgpart = part_pkg.pkgpart
125 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
128 # 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 _%'";
130 $paid-$refunded-$expenses;
134 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
136 SELECT SUM(amount) FROM cust_credit
137 WHERE ". $self->in_time_period($speriod, $eperiod)
142 my( $self, $speriod, $eperiod ) = ( shift, shift, shift );
143 my $table = @_ ? shift().'.' : '';
144 "${table}_date >= $speriod AND ${table}_date < $eperiod";
148 my( $self, $sql ) = ( shift, shift );
149 my $sth = dbh->prepare($sql) or die dbh->errstr;
151 or die "Unexpected error executing statement $sql: ". $sth->errstr;
152 $sth->fetchrow_arrayref->[0] || 0;