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