ensure invoice line items are delivered in line number order
[freeside.git] / FS / FS / cust_main.pm
index 946495c..b9c2d66 100644 (file)
@@ -2068,15 +2068,14 @@ sub bill {
   # & generate invoice database.
   ###
 
-  my( $total_setup, $total_recur ) = ( 0, 0 );
+  my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
   my %tax;
   my %taxlisthash;
   my %taxname;
   my @precommit_hooks = ();
 
-  foreach my $cust_pkg (
-    qsearch('cust_pkg', { 'custnum' => $self->custnum } )
-  ) {
+  my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } );
+  foreach my $cust_pkg (@cust_pkgs) {
 
     #NO!! next if $cust_pkg->cancel;  
     next if $cust_pkg->getfield('cancel');  
@@ -2242,6 +2241,17 @@ sub bill {
   
         if ( $setup != 0 || $recur != 0 ) {
   
+          unless ($postal_charge) {
+            $postal_charge = 1;  # try only once
+            my $postal_pkg = $self->charge_postal_fee();
+            if ( $postal_pkg && !ref( $postal_pkg ) ) {
+              $dbh->rollback if $oldAutoCommit;
+              return "can't charge postal invoice fee for customer ".
+                $self->custnum. ": $postal_pkg";
+            }
+            push @cust_pkgs, $postal_pkg if $postal_pkg;
+          }
+
           warn "    charges (setup=$setup, recur=$recur); adding line items\n"
             if $DEBUG > 1;
           my $cust_bill_pkg = new FS::cust_bill_pkg {
@@ -4764,6 +4774,33 @@ sub charge {
 
 }
 
+#=item charge_postal_fee
+#
+#Applies a one time charge this customer.  If there is an error,
+#returns the error, returns the cust_pkg charge object or false
+#if there was no charge.
+#
+#=cut
+#
+# This should be a customer event.  For that to work requires that bill
+# also be a customer event.
+
+sub charge_postal_fee {
+  my $self = shift;
+
+  my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
+  return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
+
+  my $cust_pkg = new FS::cust_pkg ( {
+    'custnum'  => $self->custnum,
+    'pkgpart'  => $pkgpart,
+    'quantity' => 1,
+  } );
+
+  my $error = $cust_pkg->insert;
+  $error ? $error : $cust_pkg;
+}
+
 =item cust_bill
 
 Returns all the invoices (see L<FS::cust_bill>) for this customer.
@@ -5007,7 +5044,7 @@ Returns a hex triplet color string for this customer's status.
 =cut
 
 use vars qw(%statuscolor);
-tie my %statuscolor, 'Tie::IxHash',
+tie %statuscolor, 'Tie::IxHash',
   'prospect'  => '7e0079', #'000000', #black?  naw, purple
   'active'    => '00CC00', #green
   'inactive'  => '0000CC', #blue
@@ -5167,6 +5204,7 @@ sub cancel_sql {
     AND 0 = ( $select_count_pkgs AND $recurring_sql
                   AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
             )
+    AND 0 = (  $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
   ";
 
 }
@@ -5222,15 +5260,24 @@ Available options are:
 
 =over 4
 
-=item unapplied_date - set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
+=item unapplied_date
+
+set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting (or is it?), probably a bad idea for event triggering)
+
+=item total
+
+(unused.  obsolete?)
+set to true to remove all customer comparison clauses, for totals
 
-=item total - set to true to remove all customer comparison clauses, for totals
+=item where
 
-=item where - WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
+(unused.  obsolete?)
+WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
 
-=item join - JOIN clause (typically used with the total option)
+=item join
 
-=item 
+(unused.  obsolete?)
+JOIN clause (typically used with the total option)
 
 =back