RT#29296: API stuff: Add new locations [change_package_location API call]
[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 use FS::cust_location::API;
8
9 =item API_getinfo FIELD => VALUE, ...
10
11 =cut
12
13 #some false laziness w/ClientAPI::Myaccount customer_info/customer_info_short
14
15 use vars qw(
16   @cust_main_addl_fields @cust_main_editable_fields @location_editable_fields
17 );
18 @cust_main_addl_fields = qw(
19   agentnum salesnum refnum classnum usernum referral_custnum
20 );
21 @cust_main_editable_fields = qw(
22   first last company daytime night fax mobile
23 );
24 #  locale
25 #  ss stateid stateid_state
26 @location_editable_fields = FS::cust_location::API::API_editable_fields();
27
28 sub API_getinfo {
29   my( $self, %opt ) = @_;
30
31   my %return = (
32     'error'           => '',
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,
38   );
39
40   $return{$_} = $self->get($_)
41     foreach @cust_main_editable_fields;
42
43   unless ( $opt{'selfservice'} ) {
44     $return{$_} = $self->get($_)
45       foreach @cust_main_addl_fields;
46   }
47
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;
53   }
54
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 );
60
61   #generally, the more useful data from the cust_main record the better.
62   # well, tell me what you want
63
64   return \%return;
65
66 }
67
68
69 #or maybe all docs go in FS::API ?  argh
70
71 =item API_insert
72
73 Class method (constructor).
74
75 Example:
76
77   use FS::cust_main;
78   FS::cust_main->API_insert(
79     'agentnum' => 1,
80     'refnum'   => 1,
81     'first'    => 'Harvey',
82     'last'     => 'Black',
83     'address1' => '5354 Pink Rabbit Lane',
84     'city'     => 'Farscape',
85     'state'    => 'CA',
86     'zip'      => '54144',
87
88     'invoicing_list' => 'harvey2@example.com',
89   );
90
91 =cut
92
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.
96
97 sub API_insert {
98   my( $class, %opt ) = @_;
99
100   my $conf = new FS::Conf;
101
102   #default agentnum like signup_server-default_agentnum?
103  
104   #same for refnum like signup_server-default_refnum?
105
106   my $cust_main = new FS::cust_main ( { # $class->new( {
107       'tagnum' => [ FS::part_tag->default_tags ],
108
109       map { $_ => $opt{$_} } qw(
110         agentnum salesnum refnum agent_custid referral_custnum
111         last first company 
112         daytime night fax mobile
113       ),
114
115   } );
116
117   my @invoicing_list = $opt{'invoicing_list'}
118                          ? split( /\s*\,\s*/, $opt{'invoicing_list'} )
119                          : ();
120   push @invoicing_list, 'POST' if $opt{'postal_invoicing'};
121
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"};
127   }
128
129   my $bill_location = FS::cust_location->new($bill_hash);
130   my $ship_location;
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 
133   # address?
134   if ( length($ship_hash->{address1}) > 0 and
135           grep { $bill_hash->{$_} ne $ship_hash->{$_} } keys(%$ship_hash)
136          ) {
137
138     $ship_location = FS::cust_location->new( $ship_hash );
139   
140   } else {
141     $ship_location = $bill_location;
142   }
143
144   $cust_main->set('bill_location' => $bill_location);
145   $cust_main->set('ship_location' => $ship_location);
146
147   my $error = $cust_main->insert( {}, \@invoicing_list );
148   return { 'error'   => $error } if $error;
149   
150   return { 'error'   => '',
151            'custnum' => $cust_main->custnum,
152          };
153
154 }
155
156 sub API_update {
157
158  my( $class, %opt ) = @_;
159
160   my $conf = new FS::Conf;
161
162   my $custnum = $opt{'custnum'}
163     or return { 'error' => "no customer record" };
164
165   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
166     or return { 'error' => "unknown custnum $custnum" };
167
168   my $new = new FS::cust_main { $cust_main->hash };
169
170   $new->set( $_ => $opt{$_} )
171     foreach grep { exists $opt{$_} } qw(
172         agentnum salesnum refnum agent_custid referral_custnum
173         last first company
174         daytime night fax mobile
175       ),
176
177   my @invoicing_list;
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'};
181   } else {
182     @invoicing_list = $cust_main->invoicing_list;
183   }
184
185   if ( exists( $opt{'address1'} ) ) {
186     my $bill_location = FS::cust_location->new({
187         map { $_ => $opt{$_} } @location_editable_fields
188     });
189     $bill_location->set('custnum' => $custnum);
190     my $error = $bill_location->find_or_insert;
191     die $error if $error;
192
193     # if this is unchanged from before, cust_main::replace will ignore it
194     $new->set('bill_location' => $bill_location);
195   }
196
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
200     });
201
202     $ship_location->set('custnum' => $custnum);
203     my $error = $ship_location->find_or_insert;
204     die $error if $error;
205
206    $new->set('ship_location' => $ship_location);
207
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);
211    }
212
213   my $error = $new->replace( $cust_main, \@invoicing_list );
214   return { 'error'   => $error } if $error;
215
216   return { 'error'   => '',
217          };  
218 }
219
220 1;