REST API, RT#28181
[freeside.git] / FS / FS / cust_main / API.pm
1 package FS::cust_main::API;
2
3 use strict;
4 use FS::Conf;
5
6 =item API_getinfo FIELD => VALUE, ...
7
8 =cut
9
10 #some false laziness w/ClientAPI::Myaccount customer_info/customer_info_short
11
12 use vars qw(
13   @cust_main_addl_fields @cust_main_editable_fields @location_editable_fields
14 );
15 @cust_main_addl_fields = qw(
16   agentnum salesnum refnum classnum usernum referral_custnum
17 );
18 @cust_main_editable_fields = qw(
19   first last company daytime night fax mobile
20 );
21 #  locale
22 #  payby payinfo payname paystart_month paystart_year payissue payip
23 #  ss paytype paystate stateid stateid_state
24 @location_editable_fields = qw(
25   address1 address2 city county state zip country
26 );
27
28 sub API_getinfo {
29   my( $self, %opt ) = @_;
30
31   my %return = (
32     'error'           => '',
33     'display_custnum' => $self->display_custnum,
34     'name'            => $self->first. ' '. $self->get('last'),
35     'balance'         => $self->balance,
36     'status'          => $self->status,
37     'statuscolor'     => $self->statuscolor,
38   );
39
40   $return{$_} = $self->get($_)
41     foreach @cust_main_editable_fields;
42
43   unless ( $opt{'selfservice'} ) {
44     $return{$_} = $self->get($_)
45       foreach @cust_main_addl_fields;
46   }
47
48   for (@location_editable_fields) {
49     $return{$_} = $self->bill_location->get($_)
50       if $self->bill_locationnum;
51     $return{'ship_'.$_} = $self->ship_location->get($_)
52       if $self->ship_locationnum;
53   }
54
55   my @invoicing_list = $self->invoicing_list;
56   $return{'invoicing_list'} =
57     join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
58   $return{'postal_invoicing'} =
59     0 < ( grep { $_ eq 'POST' } @invoicing_list );
60
61   #generally, the more useful data from the cust_main record the better.
62   # well, tell me what you want
63
64   return \%return;
65
66 }
67
68
69 #or maybe all docs go in FS::API ?  argh
70
71 =item API_insert
72
73 Class method (constructor).
74
75 Example:
76
77   use FS::cust_main;
78   FS::cust_main->API_insert(
79     'agentnum' => 1,
80     'refnum'   => 1,
81     'first'    => 'Harvey',
82     'last'     => 'Black',
83     'address1' => '5354 Pink Rabbit Lane',
84     'city'     => 'Farscape',
85     'state'    => 'CA',
86     'zip'      => '54144',
87
88     'invoicing_list' => 'harvey2@example.com',
89   );
90
91 =cut
92
93 #certainly false laziness w/ClientAPI::Signup new_customer/new_customer_minimal
94 # but approaching this from a clean start / back-office perspective
95 #  i.e. no package/service, no immediate credit card run, etc.
96
97 sub API_insert {
98   my( $class, %opt ) = @_;
99
100   my $conf = new FS::Conf;
101
102   #default agentnum like signup_server-default_agentnum?
103  
104   #same for refnum like signup_server-default_refnum?
105
106   my $cust_main = new FS::cust_main ( { # $class->new( {
107       'payby'         => 'BILL',
108
109       map { $_ => $opt{$_} } qw(
110         agentnum refnum agent_custid referral_custnum
111         last first company 
112         daytime night fax mobile
113         payby payinfo paydate paycvv payname
114       ),
115
116   } );
117
118   my @invoicing_list = $opt{'invoicing_list'}
119                          ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
120                          : ();
121   push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
122
123   my ($bill_hash, $ship_hash);
124   foreach my $f (FS::cust_main->location_fields) {
125     # avoid having to change this in front-end code
126     $bill_hash->{$f} = $opt{"bill_$f"} || $opt{$f};
127     $ship_hash->{$f} = $opt{"ship_$f"};
128   }
129
130   my $bill_location = FS::cust_location->new($bill_hash);
131   my $ship_location;
132   # we don't have an equivalent of the "same" checkbox in selfservice^Wthis API
133   # so is there a ship address, and if so, is it different from the billing 
134   # address?
135   if ( length($ship_hash->{address1}) > 0 and
136           grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
137          ) {
138
139     $ship_location = FS::cust_location->new( $ship_hash );
140   
141   } else {
142     $ship_location = $bill_location;
143   }
144
145   $cust_main->set('bill_location' => $bill_location);
146   $cust_main->set('ship_location' => $ship_location);
147
148   my $error = $cust_main->insert( {}, \@invoicing_list );
149   return { 'error'   => $error } if $error;
150   
151   return { 'error'   => '',
152            'custnum' => $cust_main->custnum,
153          };
154
155 }
156
157 1;