add option to specify a static aid
[freeside.git] / FS / FS / part_export / artera_turbo.pm
1 package FS::part_export::artera_turbo;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::Record qw(qsearch);
6 use FS::part_export;
7 use FS::cust_svc;
8 use FS::svc_external;
9
10 @ISA = qw(FS::part_export);
11
12 tie my %options, 'Tie::IxHash',
13   'rid'        => { 'label' => 'Reseller ID (RID)' },
14   'username'   => { 'label' => 'Reseller username', },
15   'password'   => { 'label' => 'Reseller password', },
16   'pid'        => { 'label' => 'Artera Product ID', },
17   'priceid'    => { 'label' => 'Artera Price ID', },
18   'agent_aid'  => { 'label' => 'Export agentnum values to Artera AID',
19                     'type'  => 'checkbox',
20                   },
21   'aid'        => { 'label' => 'Artera Agent ID to use if not using agentnum values', },
22   'production' => { 'label' => 'Production mode (leave unchecked for staging)',
23                     'type'  => 'checkbox',
24                   },
25 ;
26
27 %info = (
28   'svc'      => 'svc_external',
29   #'svc'      => [qw( svc_acct svc_forward )],
30   'desc'     =>
31     'Real-time export to Artera Turbo Reseller API',
32   'options'  => \%options,
33   #'nodomain' => 'Y',
34   'notes'    => <<'END'
35 Real-time export to <a href="http://www.arteraturbo.com/">Artera Turbo</a>
36 Reseller API.  Requires installation of
37 <a href="http://search.cpan.org/dist/Net-Artera">Net::Artera</a>
38 from CPAN.  You probably also want to:
39 <UL>
40   <LI>In the configuraiton UI section: set the <B>svc_external-skip_manual</B> and <B>svc_external-display_type</B> configuration values.
41   <LI>In the message catalog: set <B>svc_external-id</B> to <I>Artera Serial Number</I> and set <B>svc_external-title</B> to <I>Artera Key Code</I>.
42 </UL>
43 END
44 );
45
46 sub rebless { shift; }
47
48 sub _new_Artera {
49   my $self = shift;
50
51   my $artera = new Net::Artera (
52     map { $_ => $self->option($_) }
53         qw( rid username password production )
54   );
55 }
56
57
58 sub _export_insert {
59   my($self, $svc_external) = (shift, shift);
60
61   # want the ASN (serial) and AKC (key code) right away
62
63   eval "use Net::Artera;";
64   return $@ if $@;
65   my $artera = $self->_new_Artera;
66
67   my $cust_pkg = $svc_external->cust_svc->cust_pkg;
68   my $part_pkg = $cust_pkg->part_pkg;
69   my @svc_acct = grep { $_->table eq 'svc_acct' }
70                  map { $_->svc_x }
71                  sort { my $svcpart = $part_pkg->svcpart('svc_acct');
72                         ($b->svcpart==$svcpart) cmp ($a->svcpart==$svcpart); }
73                  qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
74   my $email = scalar(@svc_acct) ? $svc_acct[0]->email : '';
75   
76   my $cust_main = $cust_pkg->cust_main;
77
78   my $result = $artera->newOrder(
79     'pid'     => $self->option('pid'),
80     'priceid' => $self->option('priceid'),
81     'email'   => $email,
82     'cname'   => $cust_main->name,
83     'ref'     => $svc_external->svcnum,
84     'aid'     => ( $self->option('agent_aid')
85                      ? $cust_main->agentnum
86                      : $self->option('aid')   ),
87     'add1'    => $cust_main->address1,
88     'add2'    => $cust_main->address2,
89     'add3'    => $cust_main->city,
90     'add4'    => $cust_main->state,
91     'zip'     => $cust_main->zip,
92     'cid'     => $cust_main->country,
93     'phone'   => $cust_main->daytime || $cust_main->night,
94     'fax'     => $cust_main->fax,
95   );
96
97   if ( $result->{'id'} == 1 ) {
98     my $new = new FS::svc_external { $svc_external->hash };
99     $new->id($result->{'ASN'});
100     $new->title($result->{'AKC'});
101     $new->replace($svc_external);
102   } else {
103     $result->{'message'} || 'No response from Artera';
104   }
105 }
106
107 sub _export_replace {
108   my( $self, $new, $old ) = (shift, shift, shift);
109   return "can't change serial number with Artera"
110     if $old->id != $new->id && $old->id;
111   return "can't change key code with Artera"
112     if $old->title ne $new->title && $old->title;
113   '';
114 }
115
116 sub _export_delete {
117   my( $self, $svc_external ) = (shift, shift);
118   $self->statusChange(17, $svc_external);
119 }
120
121 sub _export_suspend {
122   my( $self, $svc_external ) = (shift, shift);
123   $self->statusChange(16, $svc_external);
124 }
125
126 sub _export_unsuspend {
127   my( $self, $svc_external ) = (shift, shift);
128   $self->statusChange(15, $svc_external);
129 }
130
131 sub statusChange {
132   my( $self, $status, $svc_external ) = @_;
133
134   eval "use Net::Artera;";
135   return $@ if $@;
136   my $artera = $self->_new_Artera;
137
138   my $result = $artera->statusChange(
139     'asn'      => sprintf('%010d', $svc_external->id),
140     'akc'      => $svc_external->title,
141     'statusid' => $status,
142   );
143
144   $result->{'id'} == 1
145     ? ''
146     : $result->{'message'};
147 }
148
149 1;
150