bulk credit import, RT#26319
authorIvan Kohler <ivan@freeside.biz>
Wed, 15 Jan 2014 22:41:16 +0000 (14:41 -0800)
committerIvan Kohler <ivan@freeside.biz>
Wed, 15 Jan 2014 22:41:16 +0000 (14:41 -0800)
FS/FS/cust_credit.pm
httemplate/search/cust_credit.html

index df59950..3f8d57c 100644 (file)
@@ -1103,6 +1103,58 @@ sub credit_lineitems {
 
 =back
 
+=head1 SUBROUTINES
+
+=over 4
+
+=item process_batch_import
+
+=cut
+
+use List::Util qw( min );
+use FS::cust_bill;
+use FS::cust_credit_bill;
+sub process_batch_import {
+  my $job = shift;
+
+  my $opt = { 'table'   => 'cust_credit',
+              'params'  => [ 'credbatch' ],
+              'formats' => { 'simple' =>
+                               [ 'custnum', 'amount', 'reasonnum', 'invnum' ],
+                           },
+              'default_csv' => 1,
+              'postinsert_callback' => sub {
+                my $cust_credit = shift; #my ($cust_credit, $param ) = @_;
+
+                if ( $cust_credit->invnum ) {
+
+                  my $cust_bill = qsearchs('cust_bill', { invnum=>$cust_credit->invnum } );
+                  my $amount = min( $cust_credit->credited, $cust_bill->owed );
+    
+                  my $cust_credit_bill = new FS::cust_credit_bill ( {
+                    'crednum' => $cust_credit->crednum,
+                    'invnum'  => $cust_bill->invnum,
+                    'amount'  => $amount,
+                  } );
+                  my $error = $cust_credit_bill->insert;
+                  return '' unless $error;
+
+                }
+
+                #apply_payments_and_credits ?
+                $cust_credit->cust_main->apply_credits;
+
+                return '';
+
+              },
+            };
+
+  FS::Record::process_batch_import( $job, $opt, @_ );
+
+}
+
+=back
+
 =head1 BUGS
 
 The delete method.  The replace method.
index fb6ef83..4526171 100755 (executable)
@@ -97,6 +97,10 @@ if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   $title = $agent->agent. " $title";
 }
 
+if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
+  push @search, "cust_credit.credbatch = '$1'";
+}
+
 # commission_salesnum
 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
   push @search, "commission_salesnum = $1";