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 # ss stateid stateid_state
25 @location_editable_fields = qw(
26 address1 address2 city county state zip country
30 my( $self, %opt ) = @_;
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,
41 $return{$_} = $self->get($_)
42 foreach @cust_main_editable_fields;
44 unless ( $opt{'selfservice'} ) {
45 $return{$_} = $self->get($_)
46 foreach @cust_main_addl_fields;
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;
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 );
62 #generally, the more useful data from the cust_main record the better.
63 # well, tell me what you want
70 #or maybe all docs go in FS::API ? argh
74 Class method (constructor).
79 FS::cust_main->API_insert(
84 'address1' => '5354 Pink Rabbit Lane',
89 'invoicing_list' => 'harvey2@example.com',
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.
99 my( $class, %opt ) = @_;
101 my $conf = new FS::Conf;
103 #default agentnum like signup_server-default_agentnum?
105 #same for refnum like signup_server-default_refnum?
107 my $cust_main = new FS::cust_main ( { # $class->new( {
108 'tagnum' => [ FS::part_tag->default_tags ],
110 map { $_ => $opt{$_} } qw(
111 agentnum salesnum refnum agent_custid referral_custnum
113 daytime night fax mobile
118 my @invoicing_list = $opt{'invoicing_list'}
119 ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
121 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
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"};
130 my $bill_location = FS::cust_location->new($bill_hash);
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
135 if ( length($ship_hash->{address1}) > 0 and
136 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
139 $ship_location = FS::cust_location->new( $ship_hash );
142 $ship_location = $bill_location;
145 $cust_main->set('bill_location' => $bill_location);
146 $cust_main->set('ship_location' => $ship_location);
148 my $error = $cust_main->insert( {}, \@invoicing_list );
149 return { 'error' => $error } if $error;
151 return { 'error' => '',
152 'custnum' => $cust_main->custnum,
159 my( $class, %opt ) = @_;
161 my $conf = new FS::Conf;
163 my $custnum = $opt{'custnum'}
164 or return { 'error' => "no customer record" };
166 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
167 or return { 'error' => "unknown custnum $custnum" };
169 my $new = new FS::cust_main { $cust_main->hash };
171 $new->set( $_ => $opt{$_} )
172 foreach grep { exists $opt{$_} } qw(
173 agentnum salesnum refnum agent_custid referral_custnum
175 daytime night fax mobile
179 if ( exists $opt{'invoicing_list'} || exists $opt{'postal_invoicing'} ) {
180 @invoicing_list = split( /\s*\,\s*/, $opt{'invoicing_list'} );
181 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
183 @invoicing_list = $cust_main->invoicing_list;
186 if ( exists( $opt{'address1'} ) ) {
187 my $bill_location = FS::cust_location->new({
188 map { $_ => $opt{$_} } @location_editable_fields
190 $bill_location->set('custnum' => $custnum);
191 my $error = $bill_location->find_or_insert;
192 die $error if $error;
194 # if this is unchanged from before, cust_main::replace will ignore it
195 $new->set('bill_location' => $bill_location);
198 if ( exists($opt{'ship_address1'}) && length($opt{"ship_address1"}) > 0 ) {
199 my $ship_location = FS::cust_location->new({
200 map { $_ => $opt{"ship_$_"} } @location_editable_fields
203 $ship_location->set('custnum' => $custnum);
204 my $error = $ship_location->find_or_insert;
205 die $error if $error;
207 $new->set('ship_location' => $ship_location);
209 } elsif (exists($opt{'ship_address1'} ) && !grep { length($opt{"ship_$_"}) } @location_editable_fields ) {
210 my $ship_location = $new->bill_location;
211 $new->set('ship_location' => $ship_location);
214 my $error = $new->replace( $cust_main, \@invoicing_list );
215 return { 'error' => $error } if $error;
217 return { 'error' => '',