fix one-time charge quantities &
[freeside.git] / FS / FS / cust_pkg.pm
index 121d491..5d449ac 100644 (file)
@@ -2,6 +2,7 @@ package FS::cust_pkg;
 
 use strict;
 use vars qw(@ISA $disable_agentcheck $DEBUG);
+use Scalar::Util qw( blessed );
 use List::Util qw(max);
 use Tie::IxHash;
 use FS::UID qw( getotaker dbh );
@@ -21,6 +22,7 @@ use FS::reg_code;
 use FS::part_svc;
 use FS::cust_pkg_reason;
 use FS::reason;
+use FS::UI::Web;
 
 # need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
 # setup }
@@ -126,6 +128,8 @@ inherits from FS::Record.  The following fields are currently supported:
 =item manual_flag - If this field is set to 1, disables the automatic
 unsuspension of this package when using the B<unsuspendauto> config file.
 
+=item quantity - If not set, defaults to 1
+
 =back
 
 Note: setup, bill, adjourn, susp, expire and cancel are specified as UNIX timestamps;
@@ -300,12 +304,17 @@ Calls
 =cut
 
 sub replace {
-  my( $new, $old, %options ) = @_;
+  my $new = shift;
+
+  my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
+              ? shift
+              : $new->replace_old;
+
+  my $options = 
+    ( ref($_[0]) eq 'HASH' )
+      ? shift
+      : { @_ };
 
-  # We absolutely have to have an old vs. new record to make this work.
-  if (!defined($old)) {
-    $old = qsearchs( 'cust_pkg', { 'pkgnum' => $new->pkgnum } );
-  }
   #return "Can't (yet?) change pkgpart!" if $old->pkgpart != $new->pkgpart;
   return "Can't change otaker!" if $old->otaker ne $new->otaker;
 
@@ -330,8 +339,8 @@ sub replace {
   my $dbh = dbh;
 
   foreach my $method ( qw(adjourn expire) ) {  # How many reasons?
-    if ($options{'reason'} && $new->$method && $old->$method ne $new->$method) {
-      my $error = $new->insert_reason( 'reason' => $options{'reason'},
+    if ($options->{'reason'} && $new->$method && $old->$method ne $new->$method) {
+      my $error = $new->insert_reason( 'reason' => $options->{'reason'},
                                        'date'   => $new->$method,
                                      );
       if ( $error ) {
@@ -356,7 +365,7 @@ sub replace {
   }
 
   my $error = $new->SUPER::replace($old,
-                                   $options{options} ? ${options{options}} : ()
+                                   $options->{options} ? $options->{options} : ()
                                   );
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -756,15 +765,29 @@ Useful for billing metered services.
 
 sub last_bill {
   my $self = shift;
-  if ( $self->dbdef_table->column('last_bill') ) {
-    return $self->setfield('last_bill', $_[0]) if @_;
-    return $self->getfield('last_bill') if $self->getfield('last_bill');
-  }    
+  return $self->setfield('last_bill', $_[0]) if @_;
+  return $self->getfield('last_bill') if $self->getfield('last_bill');
   my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
                                                   'edate'  => $self->bill,  } );
   $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;
 }
 
+=item last_cust_pkg_reason
+
+Returns the most recent FS::reason associated with the package.
+
+=cut
+
+sub last_cust_pkg_reason {
+  my $self = shift;
+  qsearchs( {
+              'table' => 'cust_pkg_reason',
+              'hashref' => { 'pkgnum' => $self->pkgnum, },
+              'extra_sql'=> "AND date <= ". time,
+              'order_by' => 'ORDER BY date DESC LIMIT 1',
+           } );
+}
+
 =item last_reason
 
 Returns the most recent FS::reason associated with the package.
@@ -772,13 +795,8 @@ Returns the most recent FS::reason associated with the package.
 =cut
 
 sub last_reason {
-  my $self = shift;
-  my $cust_pkg_reason = qsearchs( {
-                                    'table' => 'cust_pkg_reason',
-                                   'hashref' => { 'pkgnum' => $self->pkgnum, },
-                                   'extra_sql'=> 'ORDER BY date DESC LIMIT 1',
-                                 } );
-  qsearchs ( 'reason', { 'reasonnum' => $cust_pkg_reason->reasonnum } )
+  my $cust_pkg_reason = shift->last_cust_pkg_reason;
+  $cust_pkg_reason->reason
     if $cust_pkg_reason;
 }
 
@@ -1319,6 +1337,18 @@ sub attribute_since_sqlradacct {
 
 }
 
+=item quantity
+
+=cut
+
+sub quantity {
+  my( $self, $value ) = @_;
+  if ( defined($value) ) {
+    $self->setfield('quantity', $value);
+  }
+  $self->getfield('quantity') || 1;
+}
+
 =item transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
 
 Transfers as many services as possible from this package to another package.
@@ -1551,27 +1581,71 @@ sub cancel_sql {
   "cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0";
 }
 
-=item search_sql HREF
+=item search_sql HASHREF
+
+(Class method)
 
-Returns a qsearch hash expression to search for parameters specified in HREF.
+Returns a qsearch hash expression to search for parameters specified in HASHREF.
 Valid parameters are
 
 =over 4
+
 =item agentnum
-=item magic - /^(active|inactive|suspended|cancell?ed)$/
-=item status - /^(active|inactive|suspended|one-time charge|inactive|cancell?ed)$/
+
+=item magic
+
+active, inactive, suspended, cancel (or cancelled)
+
+=item status
+
+active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
+
 =item classnum
-=item pkgpart - list specified how?
-=item setup     - arrayref of beginning and ending epoch date
-=item last_bill - arrayref of beginning and ending epoch date
-=item bill      - arrayref of beginning and ending epoch date
-=item adjourn   - arrayref of beginning and ending epoch date
-=item susp      - arrayref of beginning and ending epoch date
-=item expire    - arrayref of beginning and ending epoch date
-=item cancel    - arrayref of beginning and ending epoch date
-=item query - /^(pkgnum/APKG_pkgnum)$/
-=item cust_fields - a value suited to passing to FS::UI::Web::cust_header
-=item CurrentUser - specifies the user for agent virtualization
+
+=item pkgpart
+
+list specified how?
+
+=item setup
+
+arrayref of beginning and ending epoch date
+
+=item last_bill
+
+arrayref of beginning and ending epoch date
+
+=item bill
+
+arrayref of beginning and ending epoch date
+
+=item adjourn
+
+arrayref of beginning and ending epoch date
+
+=item susp
+
+arrayref of beginning and ending epoch date
+
+=item expire
+
+arrayref of beginning and ending epoch date
+
+=item cancel
+
+arrayref of beginning and ending epoch date
+
+=item query
+
+pkgnum or APKG_pkgnum
+
+=item cust_fields
+
+a value suited to passing to FS::UI::Web::cust_header
+
+=item CurrentUser
+
+specifies the user for agent virtualization
+
 =back
 
 =cut