add cust_credit_bill relating multiple invoices to credits
authorjeff <jeff>
Sat, 1 Sep 2001 21:52:20 +0000 (21:52 +0000)
committerjeff <jeff>
Sat, 1 Sep 2001 21:52:20 +0000 (21:52 +0000)
FS/FS/cust_bill.pm
FS/FS/cust_credit.pm
FS/FS/cust_credit_bill.pm [new file with mode: 0644]
FS/FS/cust_main.pm
FS/MANIFEST
bin/fs-setup
httemplate/docs/upgrade8.html
httemplate/edit/cust_credit_bill.cgi [new file with mode: 0755]
httemplate/edit/process/cust_credit_bill.cgi [new file with mode: 0755]
httemplate/view/cust_main.cgi

index 4ffb8e2..4c1617f 100644 (file)
@@ -11,6 +11,7 @@ use FS::cust_bill_pkg;
 use FS::cust_credit;
 use FS::cust_pay;
 use FS::cust_pkg;
+use FS::cust_credit_bill;
 
 @ISA = qw( FS::Record );
 
@@ -204,8 +205,8 @@ sub cust_bill_pkg {
 =item cust_credit
 
 Returns a list consisting of the total previous credited (see
-L<FS::cust_credit>) for this customer, followed by the previous outstanding
-credits (FS::cust_credit objects).
+L<FS::cust_credit>) and unapplied for this customer, followed by the previous
+outstanding credits (FS::cust_credit objects).
 
 =cut
 
@@ -233,6 +234,19 @@ sub cust_pay {
   ;
 }
 
+=item cust_credited
+
+Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
+
+=cut
+
+sub cust_credited {
+  my $self = shift;
+  sort { $a->_date <=> $b->_date }
+    qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
+  ;
+}
+
 =item tax
 
 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
@@ -251,7 +265,8 @@ sub tax {
 =item owed
 
 Returns the amount owed (still outstanding) on this invoice, which is charged
-minus all payments (see L<FS::cust_pay>).
+minus all payments (see L<FS::cust_pay>) and applied credits
+(see L<FS::cust_credit_bill>).
 
 =cut
 
@@ -259,7 +274,8 @@ sub owed {
   my $self = shift;
   my $balance = $self->charged;
   $balance -= $_->paid foreach ( $self->cust_pay );
-  $balance;
+  $balance -= $_->amount foreach ( $self->cust_credited );
+  $balance = sprintf( "%.2f", $balance);
 }
 
 =item print_text [TIME];
@@ -349,6 +365,12 @@ sub print_text {
   push @buf,['',''];
 
   #credits
+  foreach ( $self->cust_credited ) {
+    push @buf,[
+      "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
+      $money_char. sprintf("%10.2f",$_->amount)
+    ];
+  }
   foreach ( @cr_cust_credit ) {
     push @buf,[
       "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
@@ -441,7 +463,7 @@ sub print_text {
 
 =head1 VERSION
 
-$Id: cust_bill.pm,v 1.8 2001-08-03 20:34:28 jeff Exp $
+$Id: cust_bill.pm,v 1.9 2001-09-01 21:52:19 jeff Exp $
 
 =head1 BUGS
 
index d6de27c..ec3a7ce 100644 (file)
@@ -6,6 +6,7 @@ use FS::UID qw( getotaker );
 use FS::Record qw( qsearch qsearchs );
 use FS::cust_main;
 use FS::cust_refund;
+use FS::cust_credit_bill;
 
 @ISA = qw( FS::Record );
 
@@ -132,10 +133,25 @@ sub cust_refund {
   ;
 }
 
+=item cust_credit_bill
+
+Returns all application to invoices (see L<FS::cust_credit_bill>) for this
+credit.
+
+=cut
+
+sub cust_credit_bill {
+  my $self = shift;
+  sort { $a->_date <=> $b->_date }
+    qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
+  ;
+}
+
 =item credited
 
 Returns the amount of this credit that is still outstanding; which is
-amount minus all refunds (see L<FS::cust_refund>).
+amount minus all refunds (see L<FS::cust_refund>) and applications to
+invoices (see L<FS::cust_credit_bill>).
 
 =cut
 
@@ -143,14 +159,15 @@ sub credited {
   my $self = shift;
   my $amount = $self->amount;
   $amount -= $_->refund foreach ( $self->cust_refund );
-  $amount;
+  $amount -= $_->amount foreach ( $self->cust_credit_bill );
+  sprintf( "%.2f", $amount );
 }
 
 =back
 
 =head1 VERSION
 
-$Id: cust_credit.pm,v 1.8 2001-08-26 05:06:19 ivan Exp $
+$Id: cust_credit.pm,v 1.9 2001-09-01 21:52:19 jeff Exp $
 
 =head1 BUGS
 
diff --git a/FS/FS/cust_credit_bill.pm b/FS/FS/cust_credit_bill.pm
new file mode 100644 (file)
index 0000000..be3d548
--- /dev/null
@@ -0,0 +1,146 @@
+package FS::cust_credit_bill;
+
+use strict;
+use vars qw( @ISA );
+use FS::UID qw( getotaker );
+use FS::Record qw( qsearch qsearchs );
+use FS::cust_main;
+use FS::cust_refund;
+use FS::cust_credit;
+use FS::cust_bill;
+
+@ISA = qw( FS::Record );
+
+=head1 NAME
+
+FS::cust_credit_bill - Object methods for cust_credit_bill records
+
+=head1 SYNOPSIS
+
+  use FS::cust_credit_bill;
+
+  $record = new FS::cust_credit_bill \%hash;
+  $record = new FS::cust_credit_bill { 'column' => 'value' };
+
+  $error = $record->insert;
+
+  $error = $new_record->replace($old_record);
+
+  $error = $record->delete;
+
+  $error = $record->check;
+
+=head1 DESCRIPTION
+
+An FS::cust_credit_bill object represents application of a credit (see
+L<FS::cust_credit>) to a customer bill (see L<FS::cust_bill>).  FS::cust_credit
+inherits from FS::Record.  The following fields are currently supported:
+
+=over 4
+
+=item crednum - primary key; credit being applied 
+
+=item invnum - invoice to which credit is applied (see L<FS::cust_bill>)
+
+=item amount - amount of the credit applied
+
+=item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
+L<Time::Local> and L<Date::Parse> for conversion functions.
+
+=back
+
+=head1 METHODS
+
+=over 4
+
+=item new HASHREF
+
+Creates a new cust_credit_bill.  To add the cust_credit_bill to the database,
+see L<"insert">.
+
+=cut
+
+sub table { 'cust_credit_bill'; }
+
+=item insert
+
+Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
+If there is an error, returns the error, otherwise returns false.
+
+=item delete
+
+Currently unimplemented.
+
+=cut
+
+sub delete {
+  return "Can't unapply credit!"
+}
+
+=item replace OLD_RECORD
+
+Application of credits may not be modified.
+
+=cut
+
+sub replace {
+  return "Can't modify application of credit!"
+}
+
+=item check
+
+Checks all fields to make sure this is a valid credit application.  If there
+is an error, returns the error, otherwise returns false.  Called by the insert
+and replace methods.
+
+=cut
+
+sub check {
+  my $self = shift;
+
+  my $error =
+    $self->ut_number('crednum')
+    || $self->ut_number('invnum')
+    || $self->ut_numbern('_date')
+    || $self->ut_money('amount')
+  ;
+  return $error if $error;
+
+  return "Unknown credit"
+    unless my $cust_credit = 
+      qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
+
+  return "Unknown invoice"
+    unless my $cust_bill =
+      qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
+
+  $self->_date(time) unless $self->_date;
+
+  return "Cannot apply more than remaining value of credit memo"
+    unless $self->amount <= $cust_credit->credited;
+
+  return "Cannot apply more than remaining value of invoice"
+    unless $self->amount <= $cust_bill->owed;
+
+  ''; #no error
+}
+
+=back
+
+=head1 VERSION
+
+$Id: cust_credit_bill.pm,v 1.1 2001-09-01 21:52:19 jeff Exp $
+
+=head1 BUGS
+
+The delete method.
+
+=head1 SEE ALSO
+
+L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base
+documentation.
+
+=cut
+
+1;
+
index 31f6d5e..382cc49 100644 (file)
@@ -28,6 +28,7 @@ use FS::part_referral;
 use FS::cust_main_county;
 use FS::agent;
 use FS::cust_main_invoice;
+use FS::cust_credit_bill;
 use FS::prepay_credit;
 
 @ISA = qw( FS::Record );
@@ -1398,7 +1399,7 @@ sub rebuild_fuzzyfiles {
 
 =head1 VERSION
 
-$Id: cust_main.pm,v 1.23 2001-09-01 20:11:07 ivan Exp $
+$Id: cust_main.pm,v 1.24 2001-09-01 21:52:20 jeff Exp $
 
 =head1 BUGS
 
index fa51b21..a8c080b 100644 (file)
@@ -13,6 +13,7 @@ FS/agent_type.pm
 FS/cust_bill.pm
 FS/cust_bill_pkg.pm
 FS/cust_credit.pm
+FS/cust_credit_bill.pm
 FS/cust_main.pm
 FS/cust_main_county.pm
 FS/cust_main_invoice.pm
index 2cba840..8fbc67e 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -Tw
 #
-# $Id: fs-setup,v 1.45 2001-09-01 20:11:07 ivan Exp $
+# $Id: fs-setup,v 1.46 2001-09-01 21:52:20 jeff Exp $
 
 #to delay loading dbdef until we're ready
 BEGIN { $FS::Record::setup_hack = 1; }
@@ -317,6 +317,18 @@ sub tables_hash_hack {
       'index' => [ ['custnum'] ],
     },
 
+    'cust_credit_bill' => {
+      'columns' => [
+        'crednum',  'int', '', '',
+        'invnum',  'int', '', '',
+        '_date',    @date_type,
+        'amount',   @money_type,
+      ],
+      'primary_key' => 'crednum',
+      'unique' => [ [] ],
+      'index' => [ ['invnum'] ],
+    },
+
     'cust_main' => {
       'columns' => [
         'custnum',  'int',  '',     '',
index 3edc878..b35194d 100644 (file)
@@ -70,6 +70,15 @@ CREATE TABLE svc_forward (
   dst varchar(80),
   PRIMARY KEY (svcnum)
 );
+
+CREATE TABLE cust_credit_bill (
+  crednum int not null,
+  invnum int not null,
+  _date timestamp not null,
+  amount decimal(10,2) not null,
+  PRIMARY KEY (crednum)
+);
+
 ALTER TABLE svc_acct ADD domsvc integer NOT NULL;
 ALTER TABLE svc_domain ADD catchall integer NULL;
 ALTER TABLE part_svc ADD svc_acct__domsvc integer NULL;
@@ -84,6 +93,7 @@ ALTER TABLE part_svc ADD svc_forward__dst integer NULL;
 ALTER TABLE part_svc ADD svc_forward__dst_flag char(1) NULL;
 ALTER TABLE cust_main ADD referral_custnum integer NULL;
 CREATE INDEX cust_main3 ON cust_main ( referral_custnum );
+CREATE INDEX cust_credit_bill2 ON cust_credit_bill ( invnum );
 </pre>
 
   <li>If you are using PostgreSQL, apply the following changes to your database:
diff --git a/httemplate/edit/cust_credit_bill.cgi b/httemplate/edit/cust_credit_bill.cgi
new file mode 100755 (executable)
index 0000000..df495a6
--- /dev/null
@@ -0,0 +1,81 @@
+<%
+#<!-- $Id: cust_credit_bill.cgi,v 1.1 2001-09-01 21:52:20 jeff Exp $ -->
+
+use strict;
+use vars qw( $cgi $query $custnum $invnum $otaker $p1 $crednum $_date $amount $reason $cust_credit );
+use Date::Format;
+use CGI;
+use CGI::Carp qw(fatalsToBrowser);
+use FS::UID qw(cgisuidsetup getotaker);
+use FS::CGI qw(header popurl);
+use FS::Record qw(qsearch fields);
+use FS::cust_credit;
+use FS::cust_bill;
+
+
+$cgi = new CGI;
+cgisuidsetup($cgi);
+
+if ( $cgi->param('error') ) {
+  #$cust_credit_bill = new FS::cust_credit_bill ( {
+  #  map { $_, scalar($cgi->param($_)) } fields('cust_credit_bill')
+  #} );
+  $crednum = $cgi->param('crednum');
+  $amount = $cgi->param('amount');
+  #$refund = $cgi->param('refund');
+  $invnum = $cgi->param('invnum');
+} else {
+  ($query) = $cgi->keywords;
+  $query =~ /^(\d+)$/;
+  $crednum = $1;
+  $amount = '';
+  #$refund = 'yes';
+  $invnum = '';
+}
+$_date = time;
+
+$otaker = getotaker;
+
+$p1 = popurl(1);
+
+print $cgi->header( '-expires' => 'now' ), header("Apply Credit", '');
+print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
+      "</FONT>"
+  if $cgi->param('error');
+print <<END;
+    <FORM ACTION="${p1}process/cust_credit_bill.cgi" METHOD=POST>
+    <PRE>
+END
+
+die unless $cust_credit = qsearchs('cust_credit', { 'crednum' => $crednum } );
+
+print qq!Credit #<B>!, $crednum, qq!</B><INPUT TYPE="hidden" NAME="crednum" VALUE="$crednum">!;
+
+print qq!\nInvoice # <SELECT NAME="invnum" SIZE=1>!;
+foreach $_ (grep $_->owed, qsearch('cust_bill', { 'custnum' => $cust_credit->custnum } ) ) {
+  print "<OPTION", (($_->invnum eq $invnum) ? " SELECTED" : ""),
+    qq! VALUE="! .$_->invnum. qq!">!. $_->invnum. qq! (! . $_->owed . qq!)!;
+}
+print qq!<OPTION VALUE="Refund">Refund!;
+print "</SELECT>";
+
+print qq!\nDate: <B>!, time2str("%D",$_date), qq!</B><INPUT TYPE="hidden" NAME="_date" VALUE="">!;
+
+print qq!\nAmount \$<INPUT TYPE="text" NAME="amount" VALUE="$amount" SIZE=8 MAXLENGTH=8>!;
+
+#print qq! <INPUT TYPE="checkbox" NAME="refund" VALUE="$refund">Also post refund!;
+
+print <<END;
+</PRE>
+<BR>
+<CENTER><INPUT TYPE="submit" VALUE="Post"></CENTER>
+END
+
+print <<END;
+
+    </FORM>
+  </BODY>
+</HTML>
+END
+
+%>
diff --git a/httemplate/edit/process/cust_credit_bill.cgi b/httemplate/edit/process/cust_credit_bill.cgi
new file mode 100755 (executable)
index 0000000..f838dff
--- /dev/null
@@ -0,0 +1,56 @@
+<%
+#<!-- $Id: cust_credit_bill.cgi,v 1.1 2001-09-01 21:52:20 jeff Exp $ -->
+
+use strict;
+use vars qw( $cgi $custnum $crednum $new $error );
+use CGI;
+use CGI::Carp qw(fatalsToBrowser);
+use FS::UID qw(cgisuidsetup getotaker);
+use FS::CGI qw(popurl);
+use FS::Record qw(qsearchs fields);
+use FS::cust_credit;
+use FS::cust_credit_bill;
+
+$cgi = new CGI;
+cgisuidsetup($cgi);
+
+$cgi->param('crednum') =~ /^(\d*)$/ or die "Illegal crednum!";
+$crednum = $1;
+
+my $cust_credit = qsearchs('cust_credit', { 'crednum' => $crednum } )
+  or die "No such crednum";
+
+my $cust_main = qsearchs('cust_main', { 'custnum' => $cust_credit->custnum } )
+  or die "Bogus credit:  not attached to customer";
+
+my $custnum = $cust_main->custnum;
+
+if ($cgi->param('invnum') =~ /^Refund$/) {
+  $new = new FS::cust_refund ( {
+    'reason'  => $cust_credit->reason,
+    'refund'  => $cgi->param('amount'),
+    'payby'   => 'BILL',
+    '_date'   => $cgi->param('_date'),
+    'payinfo' => 'Cash',
+    'crednum' => $crednum,
+  } );
+}else{
+  $new = new FS::cust_credit_bill ( {
+    map {
+      $_, scalar($cgi->param($_));
+    #} qw(custnum _date amount invnum)
+    } fields('cust_credit_bill')
+  } );
+}
+
+$error=$new->insert;
+
+if ( $error ) {
+  $cgi->param('error', $error);
+  print $cgi->redirect(popurl(2). "cust_credit_bill.cgi?". $cgi->query_string );
+} else {
+  print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
+}
+
+
+%>
index cba7026..492183d 100755 (executable)
@@ -1,5 +1,5 @@
 <%
-#<!-- $Id: cust_main.cgi,v 1.5 2001-08-28 14:34:14 ivan Exp $ -->
+#<!-- $Id: cust_main.cgi,v 1.6 2001-09-01 21:52:20 jeff Exp $ -->
 
 use strict;
 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral 
@@ -357,9 +357,15 @@ foreach $bill (@bills) {
 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
 foreach $credit (@credits) {
   my($cref)=$credit->hashref;
+  my($credited)=$credit->credited;
   push @history,
-    $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
-    $cref->{credited} . ") by " . $cref->{otaker} . " - " .
+    $cref->{_date} . "\t" .
+    ($credited ?
+       (qq!<A HREF="! . popurl(2). qq!edit/cust_credit_bill.cgi?!. $cref->{crednum} . qq!">!) :
+       "") .
+    "Credit #" .
+    $cref->{crednum} . ", (Balance \$" .
+    $credited . ")" . ($credited ? "</A>" : "") .
     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
 
   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );