RT#71563: Add agent_custid to credit import [v3 merge]
authorJonathan Prykop <jonathan@freeside.biz>
Tue, 5 Jul 2016 21:51:34 +0000 (16:51 -0500)
committerJonathan Prykop <jonathan@freeside.biz>
Tue, 5 Jul 2016 22:13:53 +0000 (17:13 -0500)
FS/FS/cust_credit.pm
httemplate/misc/cust_credit-import.html

index 7cb79df..4286d34 100644 (file)
@@ -1103,12 +1103,47 @@ use FS::cust_credit_bill;
 sub process_batch_import {
   my $job = shift;
 
+  # some false laziness with FS::cust_pay::process_batch_import
+  my $hashcb = sub {
+    my %hash = @_;
+    my $custnum = $hash{'custnum'};
+    my $agent_custid = $hash{'agent_custid'};
+    # translate agent_custid into regular custnum
+    if ($custnum && $agent_custid) {
+      die "can't specify both custnum and agent_custid\n";
+    } elsif ($agent_custid) {
+      # here is the agent virtualization
+      my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
+      my %search;
+      $search{'agent_custid'} = $agent_custid
+        if $agent_custid;
+      $search{'custnum'} = $custnum
+        if $custnum;
+      my $cust_main = qsearchs({
+        'table'     => 'cust_main',
+        'hashref'   => \%search,
+        'extra_sql' => $extra_sql,
+      });
+      die "can't find customer with" .
+        ($custnum  ? " custnum $custnum" : '') .
+        ($agent_custid ? " agent_custid $agent_custid" : '') . "\n"
+        unless $cust_main;
+      die "mismatched customer number\n"
+        if $custnum && ($custnum ne $cust_main->custnum);
+      $custnum = $cust_main->custnum;
+    }
+    $hash{'custnum'} = $custnum;
+    delete($hash{'agent_custid'});
+    return %hash;
+  };
+
   my $opt = { 'table'   => 'cust_credit',
               'params'  => [ '_date', 'credbatch' ],
               'formats' => { 'simple' =>
-                               [ 'custnum', 'amount', 'reasonnum', 'invnum' ],
+                               [ 'custnum', 'amount', 'reasonnum', 'invnum', 'agent_custid' ],
                            },
               'default_csv' => 1,
+              'format_hash_callbacks' => { 'simple' => $hashcb },
               'postinsert_callback' => sub {
                 my $cust_credit = shift; #my ($cust_credit, $param ) = @_;
 
index 9a63a04..68c566c 100644 (file)
@@ -67,10 +67,11 @@ Uploaded files can be CSV (comma-separated value) files or Excel spreadsheets.
 
 Field information:
 <ul>
-  <li><i>custnum</i>: Customer number
-  <li><i>amount</i>:
-  <li><i>reasonnum</i>: <A HREF="<%$p%>browse/reason.html?class=R">Credit reason</A>
-  <li><i>invnum</i>: Invoice number
+  <li><i>custnum</i>: This is the freeside customer number. It may be left blank. If specified, agent_custid must be blank.</li></li>
+  <li><i>amount</i>:</li>
+  <li><i>reasonnum</i>: <A HREF="<%$p%>browse/reason.html?class=R">Credit reason</A></li>
+  <li><i>invnum</i>: Invoice number</li>
+  <li><i>agent_custid</i>: This is the reseller's idea of the customer number or identifier. It may be left blank. If specified, custnum must be blank.</li>
 </ul>
 <BR><BR>