1 package FS::cust_main::API;
6 use FS::Record qw( qsearchs );
8 =item API_getinfo FIELD => VALUE, ...
12 #some false laziness w/ClientAPI::Myaccount customer_info/customer_info_short
15 @cust_main_addl_fields @cust_main_editable_fields @location_editable_fields
17 @cust_main_addl_fields = qw(
18 agentnum salesnum refnum classnum usernum referral_custnum
20 @cust_main_editable_fields = qw(
21 first last company daytime night fax mobile
24 # payby payinfo payname paystart_month paystart_year payissue payip
25 # ss paytype paystate stateid stateid_state
26 @location_editable_fields = qw(
27 address1 address2 city county state zip country
31 my( $self, %opt ) = @_;
35 'display_custnum' => $self->display_custnum,
36 'name' => $self->first. ' '. $self->get('last'),
37 'balance' => $self->balance,
38 'status' => $self->status,
39 'statuscolor' => $self->statuscolor,
42 $return{$_} = $self->get($_)
43 foreach @cust_main_editable_fields;
45 unless ( $opt{'selfservice'} ) {
46 $return{$_} = $self->get($_)
47 foreach @cust_main_addl_fields;
50 for (@location_editable_fields) {
51 $return{$_} = $self->bill_location->get($_)
52 if $self->bill_locationnum;
53 $return{'ship_'.$_} = $self->ship_location->get($_)
54 if $self->ship_locationnum;
57 my @invoicing_list = $self->invoicing_list;
58 $return{'invoicing_list'} =
59 join(', ', grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list );
60 $return{'postal_invoicing'} =
61 0 < ( grep { $_ eq 'POST' } @invoicing_list );
63 #generally, the more useful data from the cust_main record the better.
64 # well, tell me what you want
71 #or maybe all docs go in FS::API ? argh
75 Class method (constructor).
80 FS::cust_main->API_insert(
85 'address1' => '5354 Pink Rabbit Lane',
90 'invoicing_list' => 'harvey2@example.com',
95 #certainly false laziness w/ClientAPI::Signup new_customer/new_customer_minimal
96 # but approaching this from a clean start / back-office perspective
97 # i.e. no package/service, no immediate credit card run, etc.
100 my( $class, %opt ) = @_;
102 my $conf = new FS::Conf;
104 #default agentnum like signup_server-default_agentnum?
106 #same for refnum like signup_server-default_refnum?
108 my $cust_main = new FS::cust_main ( { # $class->new( {
110 'tagnum' => [ FS::part_tag->default_tags ],
112 map { $_ => $opt{$_} } qw(
113 agentnum salesnum refnum agent_custid referral_custnum
115 daytime night fax mobile
116 payby payinfo paydate paycvv payname
121 my @invoicing_list = $opt{'invoicing_list'}
122 ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
124 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
126 my ($bill_hash, $ship_hash);
127 foreach my $f (FS::cust_main->location_fields) {
128 # avoid having to change this in front-end code
129 $bill_hash->{$f} = $opt{"bill_$f"} || $opt{$f};
130 $ship_hash->{$f} = $opt{"ship_$f"};
133 my $bill_location = FS::cust_location->new($bill_hash);
135 # we don't have an equivalent of the "same" checkbox in selfservice^Wthis API
136 # so is there a ship address, and if so, is it different from the billing
138 if ( length($ship_hash->{address1}) > 0 and
139 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
142 $ship_location = FS::cust_location->new( $ship_hash );
145 $ship_location = $bill_location;
148 $cust_main->set('bill_location' => $bill_location);
149 $cust_main->set('ship_location' => $ship_location);
151 my $error = $cust_main->insert( {}, \@invoicing_list );
152 return { 'error' => $error } if $error;
154 return { 'error' => '',
155 'custnum' => $cust_main->custnum,
162 my( $class, %opt ) = @_;
164 my $conf = new FS::Conf;
167 my $custnum = $opt{'custnum'}
168 or return { 'error' => "no customer record" };
170 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
171 or return { 'error' => "unknown custnum $custnum" };
173 my $new = new FS::cust_main { $cust_main->hash };
175 $new->set( $_ => $opt{$_} )
176 foreach grep { exists $opt{$_} } qw(
177 agentnum salesnum refnum agent_custid referral_custnum
179 daytime night fax mobile
180 payby payinfo paydate paycvv payname
183 my @invoicing_list = $opt{'invoicing_list'}
184 ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
185 : $cust_main->invoicing_list;
186 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
188 if ( exists( $opt{'address1'} ) ) {
189 my $bill_location = FS::cust_location->new({
190 map { $_ => $opt{$_} } @location_editable_fields
192 $bill_location->set('custnum' => $custnum);
193 my $error = $bill_location->find_or_insert;
194 die $error if $error;
196 # if this is unchanged from before, cust_main::replace will ignore it
197 $new->set('bill_location' => $bill_location);
200 if ( exists($opt{'ship_address1'}) ) {
201 my $ship_location = FS::cust_location->new({
202 map { $_ => $opt{"ship_$_"} } @location_editable_fields
205 $ship_location->set('custnum' => $custnum);
206 my $error = $ship_location->find_or_insert;
207 die $error if $error;
211 if ( !grep { length($opt{"ship_$_"}) } @location_editable_fields ) {
212 # Selfservice unfortunately tries to indicate "same as billing
213 # address" by sending all fields empty. Did this ever work?
215 my $ship_location = $cust_main->bill_location;
216 $new->set('ship_location' => $ship_location);
219 my $error = $new->replace( $cust_main, \@invoicing_list );
220 return { 'error' => $error } if $error;
222 return { 'error' => '',