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