freeside/FS/FS prepay_credit.pm,1.5,1.6 cust_main.pm,1.170,1.171 cust_pay.pm,1.36,1.37 agent.pm,1.9,1.10

ivan ivan at pouncequick.420.am
Sat Mar 12 06:32:23 PST 2005


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory pouncequick:/tmp/cvs-serv9831/FS/FS

Modified Files:
	prepay_credit.pm cust_main.pm cust_pay.pm agent.pm 
Log Message:
- bring prepaid support into this century (close: Bug#1124)
- finally get rid of fs_signup (everything is in fs_selfservice now) (Bug#413)
- organize main menu sysadmin section so it is slightly less confusing

Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.170
retrieving revision 1.171
diff -u -d -r1.170 -r1.171
--- cust_main.pm	19 Jan 2005 00:57:11 -0000	1.170
+++ cust_main.pm	12 Mar 2005 14:31:47 -0000	1.171
@@ -175,7 +175,7 @@
 
 =item ship_fax - phone (optional)
 
-=item payby - I<CARD> (credit card - automatic), I<DCRD> (credit card - on-demand), I<CHEK> (electronic check - automatic), I<DCHK> (electronic check - on-demand), I<LECB> (Phone bill billing), I<BILL> (billing), I<COMP> (free), or I<PREPAY> (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
+=item payby - I<CARD> (credit card - automatic), I<DCRD> (credit card - on-demand), I<CHEK> (electronic check - automatic), I<DCHK> (electronic check - on-demand), I<LECB> (Phone bill billing), I<BILL> (billing), I<COMP> (free), or I<PREPAY> (special billing type: applies a payment from a prepaid card - see L<FS::prepay_credit> - and sets billing type to I<BILL>)
 
 =item payinfo - card number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
 
@@ -271,20 +271,28 @@
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my $amount = 0;
+  my $prepay_credit = '';
   my $seconds = 0;
   if ( $self->payby eq 'PREPAY' ) {
     $self->payby('BILL');
-    my $prepay_credit = qsearchs(
+    $prepay_credit = qsearchs(
       'prepay_credit',
       { 'identifier' => $self->payinfo },
       '',
       'FOR UPDATE'
     );
-    warn "WARNING: can't find pre-found prepay_credit: ". $self->payinfo
-      unless $prepay_credit;
-    $amount = $prepay_credit->amount;
+    unless ( $prepay_credit ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "Invalid prepaid card: ". $self->payinfo;
+    }
     $seconds = $prepay_credit->seconds;
+    if ( $prepay_credit->agentnum ) {
+      if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "prepaid card not valid for agent ". $self->agentnum;
+      }
+      $self->agentnum($prepay_credit->agentnum);
+    }
     my $error = $prepay_credit->delete;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -321,15 +329,18 @@
     return "No svc_acct record to apply pre-paid time";
   }
 
-  if ( $amount ) {
-    my $cust_credit = new FS::cust_credit {
+  if ( $prepay_credit && $prepay_credit->amount ) {
+    my $cust_pay = new FS::cust_pay {
       'custnum' => $self->custnum,
-      'amount'  => $amount,
+      'paid'    => $prepay_credit->amount,
+      #'_date'   => #date the prepaid card was purchased???
+      'payby'   => 'PREP',
+      'payinfo' => $prepay_credit->identifier,
     };
-    $error = $cust_credit->insert;
+    $error = $cust_pay->insert;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
-      return "inserting credit (transaction rolled back): $error";
+      return "inserting prepayment (transaction rolled back): $error";
     }
   }
 

Index: cust_pay.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_pay.pm,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- cust_pay.pm	27 Dec 2004 10:19:25 -0000	1.36
+++ cust_pay.pm	12 Mar 2005 14:31:47 -0000	1.37
@@ -357,7 +357,7 @@
 
   $self->_date(time) unless $self->_date;
 
-  $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
+  $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP)$/ or return "Illegal payby";
   $self->payby($1);
 
   #false laziness with cust_refund::check

Index: agent.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/agent.pm,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- agent.pm	29 Jan 2005 12:34:10 -0000	1.9
+++ agent.pm	12 Mar 2005 14:31:47 -0000	1.10
@@ -274,6 +274,7 @@
 
 =cut
 
+#false laziness w/prepay_credit::generate
 sub generate_reg_codes {
   my( $self, $num, $pkgparts ) = @_;
 
@@ -324,6 +325,22 @@
   $sth->execute($self->agentnum) or die $sth->errstr;
   $sth->fetchrow_arrayref->[0];
 }
+
+=item num_prepay_credit
+
+Returns the number of unused prepaid cards for this agent.
+
+=cut
+
+sub num_prepay_credit {
+  my $self = shift;
+  my $sth = dbh->prepare(
+    "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
+  ) or die dbh->errstr;
+  $sth->execute($self->agentnum) or die $sth->errstr;
+  $sth->fetchrow_arrayref->[0];
+}
+
 
 =back
 

Index: prepay_credit.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/prepay_credit.pm,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- prepay_credit.pm	5 Aug 2003 00:20:45 -0000	1.5
+++ prepay_credit.pm	12 Mar 2005 14:31:47 -0000	1.6
@@ -2,8 +2,8 @@
 
 use strict;
 use vars qw( @ISA );
-#use FS::Record qw( qsearch qsearchs );
-use FS::Record qw();
+use FS::Record qw(qsearchs dbh);
+use FS::agent;
 
 @ISA = qw(FS::Record);
 
@@ -37,8 +37,8 @@
 
 =head1 DESCRIPTION
 
-An FS::table_name object represents an pre--paid credit, such as a pre-paid
-"calling card".  FS::prepay_credit inherits from FS::Record.  The following
+An FS::prepay_credit object represents a pre-paid card.  FS::prepay_credit
+inherits from FS::Record.  The following
 fields are currently supported:
 
 =over 4
@@ -107,13 +107,75 @@
   $self->ut_numbern('prepaynum')
   || $self->ut_alpha('identifier')
   || $self->ut_money('amount')
-  || $self->utnumbern('seconds')
+  || $self->ut_numbern('seconds')
+  || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
   || $self->SUPER::check
   ;
 
 }
 
+=item agent
+
+Returns the agent (see L<FS::agent>) for this prepaid card, if any.
+
+=cut
+
+sub agent {
+  my $self = shift;
+  qsearchs('agent', { 'agentnum' => $self->agentnum } );
+}
+
 =back
+
+=head1 SUBROUTINES
+
+=over 4
+
+=item generate NUM TYPE HASHREF
+
+Generates the specified number of prepaid cards.  Returns an array reference of
+the newly generated card identifiers, or a scalar error message.
+
+=cut
+
+#false laziness w/agent::generate_reg_codes
+sub generate {
+  my( $num, $type, $hashref ) = @_;
+
+  my @codeset = ();
+  push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
+  push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my @cards = ();
+  for ( 1 ... $num ) {
+    my $prepay_credit = new FS::prepay_credit {
+      'identifier' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
+      %$hashref,
+    };
+    my $error = $prepay_credit->check || $prepay_credit->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "(inserting prepay_credit) $error";
+    }
+    push @cards, $prepay_credit->identifier;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  \@cards;
+
+}
 
 =head1 BUGS
 




More information about the freeside-commits mailing list