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