1 package FS::cust_main::API;
6 use FS::Record qw( qsearchs );
7 use FS::cust_location::API;
9 =item API_getinfo FIELD => VALUE, ...
13 #some false laziness w/ClientAPI::Myaccount customer_info/customer_info_short
16 @cust_main_addl_fields @cust_main_editable_fields @location_editable_fields
18 @cust_main_addl_fields = qw(
19 agentnum salesnum refnum classnum usernum referral_custnum
21 @cust_main_editable_fields = qw(
22 first last company daytime night fax mobile
25 # ss stateid stateid_state
26 @location_editable_fields = FS::cust_location::API::API_editable_fields();
29 my( $self, %opt ) = @_;
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,
40 $return{$_} = $self->get($_)
41 foreach @cust_main_editable_fields;
43 unless ( $opt{'selfservice'} ) {
44 $return{$_} = $self->get($_)
45 foreach @cust_main_addl_fields;
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;
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 );
61 #generally, the more useful data from the cust_main record the better.
62 # well, tell me what you want
69 #or maybe all docs go in FS::API ? argh
73 Class method (constructor).
78 FS::cust_main->API_insert(
83 'address1' => '5354 Pink Rabbit Lane',
88 'invoicing_list' => 'harvey2@example.com',
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.
98 my( $class, %opt ) = @_;
100 my $conf = new FS::Conf;
102 #default agentnum like signup_server-default_agentnum?
104 #same for refnum like signup_server-default_refnum?
106 my $cust_main = new FS::cust_main ( { # $class->new( {
107 'tagnum' => [ FS::part_tag->default_tags ],
109 map { $_ => $opt{$_} } qw(
110 agentnum salesnum refnum agent_custid referral_custnum
112 daytime night fax mobile
117 my @invoicing_list = $opt{'invoicing_list'}
118 ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
120 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
122 my ($bill_hash, $ship_hash);
123 foreach my $f (FS::cust_main->location_fields) {
124 # avoid having to change this in front-end code
125 $bill_hash->{$f} = $opt{"bill_$f"} || $opt{$f};
126 $ship_hash->{$f} = $opt{"ship_$f"};
129 my $bill_location = FS::cust_location->new($bill_hash);
131 # we don't have an equivalent of the "same" checkbox in selfservice^Wthis API
132 # so is there a ship address, and if so, is it different from the billing
134 if ( length($ship_hash->{address1}) > 0 and
135 grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
138 $ship_location = FS::cust_location->new( $ship_hash );
141 $ship_location = $bill_location;
144 $cust_main->set('bill_location' => $bill_location);
145 $cust_main->set('ship_location' => $ship_location);
147 my $error = $cust_main->insert( {}, \@invoicing_list );
148 return { 'error' => $error } if $error;
150 return { 'error' => '',
151 'custnum' => $cust_main->custnum,
158 my( $class, %opt ) = @_;
160 my $conf = new FS::Conf;
162 my $custnum = $opt{'custnum'}
163 or return { 'error' => "no customer record" };
165 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
166 or return { 'error' => "unknown custnum $custnum" };
168 my $new = new FS::cust_main { $cust_main->hash };
170 $new->set( $_ => $opt{$_} )
171 foreach grep { exists $opt{$_} } qw(
172 agentnum salesnum refnum agent_custid referral_custnum
174 daytime night fax mobile
178 if ( exists $opt{'invoicing_list'} || exists $opt{'postal_invoicing'} ) {
179 @invoicing_list = split( /\s*\,\s*/, $opt{'invoicing_list'} );
180 push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
182 @invoicing_list = $cust_main->invoicing_list;
185 if ( exists( $opt{'address1'} ) ) {
186 my $bill_location = FS::cust_location->new({
187 map { $_ => $opt{$_} } @location_editable_fields
189 $bill_location->set('custnum' => $custnum);
190 my $error = $bill_location->find_or_insert;
191 die $error if $error;
193 # if this is unchanged from before, cust_main::replace will ignore it
194 $new->set('bill_location' => $bill_location);
197 if ( exists($opt{'ship_address1'}) && length($opt{"ship_address1"}) > 0 ) {
198 my $ship_location = FS::cust_location->new({
199 map { $_ => $opt{"ship_$_"} } @location_editable_fields
202 $ship_location->set('custnum' => $custnum);
203 my $error = $ship_location->find_or_insert;
204 die $error if $error;
206 $new->set('ship_location' => $ship_location);
208 } elsif (exists($opt{'ship_address1'} ) && !grep { length($opt{"ship_$_"}) } @location_editable_fields ) {
209 my $ship_location = $new->bill_location;
210 $new->set('ship_location' => $ship_location);
213 my $error = $new->replace( $cust_main, \@invoicing_list );
214 return { 'error' => $error } if $error;
216 return { 'error' => '',