Ticket #33252 API customer update
authorJeremy Davis <jeremyd@freeside.biz>
Thu, 29 Jan 2015 04:25:23 +0000 (22:25 -0600)
committerJeremy Davis <jeremyd@freeside.biz>
Thu, 29 Jan 2015 04:25:23 +0000 (22:25 -0600)
FS/FS/API.pm
FS/FS/cust_main/API.pm

index 62a97d8..629463c 100644 (file)
@@ -427,6 +427,119 @@ sub new_customer {
 
 =back 
 
+=item update_customer
+
+Updates an existing customer. Takes a hash reference as parameter with the foll$
+
+=over 4
+
+=item secret
+
+API Secret
+
+=item first
+
+first name (required)
+
+=item last
+
+last name (required)
+
+=item company
+
+Company name
+
+=item address1 (required)
+
+Address line one
+
+=item city (required)
+
+City
+
+=item county
+
+County
+
+=item state (required)
+
+State
+
+=item zip (required)
+
+Zip or postal code
+
+=item country
+
+2 Digit Country Code
+
+=item daytime
+
+Daytime phone number
+
+=item night
+
+Evening phone number
+
+=item fax
+
+Fax number
+
+=item mobile
+
+Mobile number
+
+=item invoicing_list
+
+comma-separated list of email addresses for email invoices. The special value '$
+postal_invoicing
+Set to 1 to enable postal invoicing
+
+=item payby
+
+CARD, DCRD, CHEK, DCHK, LECB, BILL, COMP or PREPAY
+
+=item payinfo
+
+Card number for CARD/DCRD, account_number@aba_number for CHEK/DCHK, prepaid "pi$
+
+=item paycvv
+
+Credit card CVV2 number (1.5+ or 1.4.2 with CVV schema patch)
+
+=item paydate
+
+Expiration date for CARD/DCRD
+
+=item payname
+
+Exact name on credit card for CARD/DCRD, bank name for CHEK/DCHK
+
+=item referral_custnum
+
+Referring customer number
+
+=item salesnum
+Sales person number
+
+=item agentnum
+
+Agent number
+
+=cut
+sub update_customer {
+  my( $class, %opt ) = @_;
+
+  my $conf = new FS::Conf;
+  return { 'error' => 'Incorrect shared secret' }
+    unless $opt{secret} eq $conf->config('api_shared_secret');
+
+  FS::cust_main->API_update( %opt );
+}
+
+=back
+
+
 =item customer_info
 
 Returns general customer information. Takes a hash reference as parameter with the following keys: custnum and API secret 
index 283683b..4a09b93 100644 (file)
@@ -3,6 +3,7 @@ package FS::cust_main::API;
 use strict;
 use FS::Conf;
 use FS::part_tag;
+use FS::Record qw( qsearchs );
 
 =item API_getinfo FIELD => VALUE, ...
 
@@ -156,4 +157,66 @@ sub API_insert {
 
 }
 
+sub API_update {
+
+  my( $class, %opt ) = @_;
+
+  my $conf = new FS::Conf;
+
+
+  my $custnum = $opt{'custnum'}
+    or return { 'error' => "no customer record" };
+
+  my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
+    or return { 'error' => "unknown custnum $custnum" };
+
+  my $new = new FS::cust_main { $cust_main->hash };
+
+  $new->set( $_ => $opt{$_} )
+    foreach grep { exists $opt{$_} } qw(
+        agentnum salesnum refnum agent_custid referral_custnum
+        last first company
+        daytime night fax mobile
+        payby payinfo paydate paycvv payname
+      ),
+
+  
+  my @invoicing_list = $opt{'invoicing_list'}
+                         ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
+                         : ();
+  push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
+
+  my ($bill_hash, $ship_hash);
+  foreach my $f (FS::cust_main->location_fields) {
+    # avoid having to change this in front-end code
+    $bill_hash->{$f} = $opt{"bill_$f"} || $opt{$f};
+    $ship_hash->{$f} = $opt{"ship_$f"};
+  }
+
+  my $bill_location = FS::cust_location->new($bill_hash);
+  my $ship_location;
+  # we don't have an equivalent of the "same" checkbox in selfservice^Wthis API
+  # so is there a ship address, and if so, is it different from the billing 
+  # address?
+  if ( length($ship_hash->{address1}) > 0 and
+          grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
+         ) {
+
+    $ship_location = FS::cust_location->new( $ship_hash );
+  
+  } else {
+    $ship_location = $bill_location;
+  }
+
+  $new->set('bill_location' => $bill_location);
+  $new->set('ship_location' => $ship_location);
+
+  my $error = $new->replace( $cust_main, \@invoicing_list );
+  return { 'error'   => $error } if $error;
+  
+  return { 'error'   => '',
+         };
+  
+}
+
 1;