RT#29296: API stuff: Add new locations [change_package_location API call]
[freeside.git] / FS / FS / cust_pkg / API.pm
1 package FS::cust_pkg::API;
2
3 use strict;
4
5 use FS::cust_location::API;
6
7 sub API_getinfo {
8   my $self = shift;
9
10   +{ ( map { $_=>$self->$_ } $self->fields ),
11    };
12
13 }
14
15 # currently only handles location change...
16 # eventually have it handle all sorts of package changes
17 sub API_change {
18   my $self = shift;
19   my %opt = @_;
20
21   return { 'error' => 'Cannot change canceled package' }
22     if $self->cancel;
23
24   my %changeopt;
25
26   # update location--accepts raw fields OR location
27   my %location_hash;
28   foreach my $field (FS::cust_location::API::API_editable_fields()) {
29     $location_hash{$field} = $opt{$field} if $opt{$field};
30   }
31   return { 'error' => 'Cannot pass both locationnum and location fields' }
32     if $opt{'locationnum'} && %location_hash;
33
34   if (%location_hash) {
35     my $cust_location = FS::cust_location->new({
36       'custnum' => $self->custnum,
37       %location_hash,
38     });
39     $changeopt{'cust_location'} = $cust_location;
40   } elsif ($opt{'locationnum'}) {
41     $changeopt{'locationnum'} = $opt{'locationnum'};
42   }
43
44   # not quite "nothing changed" because passed changes might be identical to current record,
45   #   we don't currently check for that, don't want to imply that we do...but maybe we should?
46   return { 'error' => 'No changes passed to method' }
47     unless $changeopt{'cust_location'} || $changeopt{'locationnum'};
48
49   $changeopt{'keep_dates'} = 1;
50
51   my $pkg_or_error = $self->change( \%changeopt );
52   my $error = ref($pkg_or_error) ? '' : $pkg_or_error;
53
54   return { 'error' => $error } if $error;
55
56   # return all fields?  we don't yet expose them through FS::API
57   return { map { $_ => $pkg_or_error->get($_) } qw( pkgnum locationnum ) };
58
59 }
60
61 1;