XLSX format for spreadsheet download, #17971
[freeside.git] / FS / FS / cust_statement.pm
index cd3e7ce..45fae1c 100644 (file)
@@ -60,6 +60,10 @@ Creates a new record.  To add the record to the database, see L<"insert">.
 Note that this stores the hash reference, not a distinct copy of the hash it
 points to.  You can ask the object for a copy with the I<hash> method.
 
+Pass "statementnum => 'ALL'" to create a temporary statement that includes 
+all of the customer's invoices.  This statement can't be inserted and won't
+set the statementnum field on any invoices.
+
 =cut
 
 sub new { FS::Record::new(@_); }
@@ -165,7 +169,16 @@ Returns the associated invoices (cust_bill records) for this statement.
 
 sub cust_bill {
   my $self = shift;
-  qsearch('cust_bill', { 'statementnum' => $self->statementnum } );
+  # we use it about a thousand times, let's cache it
+  $self->{Hash}->{cust_bill} ||= [
+    qsearch('cust_bill', { 
+        $self->statementnum eq 'ALL' ?
+          ('custnum' => $self->custnum) :
+          ('statementnum' => $self->statementnum)
+    } )
+  ];
+
+  @{ $self->{Hash}->{cust_bill} }
 }
 
 sub _aggregate {
@@ -180,6 +193,18 @@ sub _aggregate {
   @agg;
 }
 
+sub _total {
+  my( $self, $method ) = ( shift, shift );
+
+  my $total = 0;
+
+  foreach my $cust_bill ( $self->cust_bill ) {
+    $total += $cust_bill->$method( @_ );
+  }
+
+  $total;
+}
+
 =item cust_bill_pkg
 
 Returns the line items (see L<FS::cust_bill_pkg>) for all associated invoices.
@@ -221,20 +246,29 @@ sub cust_bill_pkg_pkgnum { shift->_aggregate('cust_bill_pkg_pkgnum', @_); }
 
 =item tax
 
-Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
+Returns the total tax amount for all assoicated invoices.0
 
 =cut
 
-sub tax {
-  my $self = shift;
+=item charged
 
-  my $total = 0;
+Returns the total amount charged for all associated invoices.
 
-  foreach my $cust_bill ( $self->cust_bill ) {
-    $total += $cust_bill->tax;
-  }
+=cut
 
-  $total;
+=item owed
+
+Returns the total amount owed for all associated invoices.
+
+=cut
+
+sub tax     { shift->_total('tax',     @_); }
+sub charged { shift->_total('charged', @_); }
+sub owed    { shift->_total('owed',    @_); }
+
+#don't show previous info
+sub previous {
+  ( 0 ); # 0, empty list
 }
 
 =back