2d6da9ed6d7fa7b69beac7bc13ecf173b86e3c70
[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 use FS::Record qw( qsearchs );
7
8 =item API_getinfo FIELD => VALUE, ...
9
10 =cut
11
12 #some false laziness w/ClientAPI::Myaccount customer_info/customer_info_short
13
14 use vars qw(
15   @cust_main_addl_fields @cust_main_editable_fields @location_editable_fields
16 );
17 @cust_main_addl_fields = qw(
18   agentnum salesnum refnum classnum usernum referral_custnum
19 );
20 @cust_main_editable_fields = qw(
21   first last company daytime night fax mobile
22 );
23 #  locale
24 #  ss 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       'tagnum' => [ FS::part_tag->default_tags ],
109
110       map { $_ => $opt{$_} } qw(
111         agentnum salesnum refnum agent_custid referral_custnum
112         last first company 
113         daytime night fax mobile
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 sub API_update {
158
159  my( $class, %opt ) = @_;
160
161   my $conf = new FS::Conf;
162
163   my $custnum = $opt{'custnum'}
164     or return { 'error' => "no customer record" };
165
166   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
167     or return { 'error' => "unknown custnum $custnum" };
168
169   my $new = new FS::cust_main { $cust_main->hash };
170
171   $new->set( $_ => $opt{$_} )
172     foreach grep { exists $opt{$_} } qw(
173         agentnum salesnum refnum agent_custid referral_custnum
174         last first company
175         daytime night fax mobile
176       ),
177
178   my @invoicing_list;
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'};
182   } else {
183     @invoicing_list = $cust_main->invoicing_list;
184   }
185
186   if ( exists( $opt{'address1'} ) ) {
187     my $bill_location = FS::cust_location->new({
188         map { $_ => $opt{$_} } @location_editable_fields
189     });
190     $bill_location->set('custnum' => $custnum);
191     my $error = $bill_location->find_or_insert;
192     die $error if $error;
193
194     # if this is unchanged from before, cust_main::replace will ignore it
195     $new->set('bill_location' => $bill_location);
196   }
197
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
201     });
202
203     $ship_location->set('custnum' => $custnum);
204     my $error = $ship_location->find_or_insert;
205     die $error if $error;
206
207    $new->set('ship_location' => $ship_location);
208
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);
212    }
213
214   my $error = $new->replace( $cust_main, \@invoicing_list );
215   return { 'error'   => $error } if $error;
216
217   return { 'error'   => '',
218          };  
219 }
220
221 1;