import torrus 1.0.9
[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   'debug'      => { 'label' => 'Enable debug logging',
26                     'type'  => 'checkbox',
27                   },
28   'enable_edit' => { 'label' => 'Enable local editing of Artera serial numbers and key codes (note that the changes will NOT be exported to Artera)',
29                      'type'  => 'checkbox',
30                    },
31 ;
32
33 %info = (
34   'svc'      => 'svc_external',
35   #'svc'      => [qw( svc_acct svc_forward )],
36   'desc'     =>
37     'Real-time export to Artera Turbo Reseller API',
38   'options'  => \%options,
39   #'nodomain' => 'Y',
40   'notes'    => <<'END'
41 Real-time export to <a href="http://www.arteraturbo.com/">Artera Turbo</a>
42 Reseller API.  Requires installation of
43 <a href="http://search.cpan.org/dist/Net-Artera">Net::Artera</a>
44 from CPAN.  You probably also want to:
45 <UL>
46   <LI>In the configuration UI section: set the <B>svc_external-skip_manual</B> and <B>svc_external-display_type</B> configuration values.
47   <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>.
48 </UL>
49 END
50 );
51
52 sub rebless { shift; }
53
54 sub _new_Artera {
55   my $self = shift;
56
57   my $artera = new Net::Artera (
58     map { $_ => $self->option($_) }
59         qw( rid username password production )
60   );
61 }
62
63
64 sub _export_insert {
65   my($self, $svc_external) = (shift, shift);
66
67   # want the ASN (serial) and AKC (key code) right away
68
69   eval "use Net::Artera;";
70   return $@ if $@;
71   $Net::Artera::DEBUG = 1 if $self->option('debug');
72   my $artera = $self->_new_Artera;
73
74   my $cust_pkg = $svc_external->cust_svc->cust_pkg;
75   my $part_pkg = $cust_pkg->part_pkg;
76   my @svc_acct = grep { $_->table eq 'svc_acct' }
77                  map { $_->svc_x }
78                  sort { my $svcpart = $part_pkg->svcpart('svc_acct');
79                         ($b->svcpart==$svcpart) cmp ($a->svcpart==$svcpart); }
80                  qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
81   my $email = scalar(@svc_acct) ? $svc_acct[0]->email : '';
82   
83   my $cust_main = $cust_pkg->cust_main;
84
85   my $result = $artera->newOrder(
86     'pid'     => $self->option('pid'),
87     'priceid' => $self->option('priceid'),
88     'email'   => $email,
89     'cname'   => $cust_main->name,
90     'ref'     => $svc_external->svcnum,
91     'aid'     => ( $self->option('agent_aid')
92                      ? $cust_main->agentnum
93                      : $self->option('aid')   ),
94     'add1'    => $cust_main->address1,
95     'add2'    => $cust_main->address2,
96     'add3'    => $cust_main->city,
97     'add4'    => $cust_main->state,
98     'zip'     => $cust_main->zip,
99     'cid'     => $cust_main->country,
100     'phone'   => $cust_main->daytime || $cust_main->night,
101     'fax'     => $cust_main->fax,
102   );
103
104   if ( $result->{'id'} == 1 ) {
105     my $new = new FS::svc_external { $svc_external->hash };
106     $new->id(sprintf('%010d', $result->{'ASN'}));
107     $new->title( substr('0000000000'.uc($result->{'AKC'}), -10) );
108     $new->replace($svc_external);
109   } else {
110     $result->{'message'} || 'No response from Artera';
111   }
112 }
113
114 sub _export_replace {
115   my( $self, $new, $old ) = (shift, shift, shift);
116   return '' if $self->option('enable_edit');
117   return "can't change serial number with Artera"
118     if $old->id != $new->id && $old->id;
119   return "can't change key code with Artera"
120     if $old->title ne $new->title && $old->title;
121   '';
122 }
123
124 sub _export_delete {
125   my( $self, $svc_external ) = (shift, shift);
126   $self->queue_statusChange(17, $svc_external);
127 }
128
129 sub _export_suspend {
130   my( $self, $svc_external ) = (shift, shift);
131   $self->queue_statusChange(16, $svc_external);
132 }
133
134 sub _export_unsuspend {
135   my( $self, $svc_external ) = (shift, shift);
136   $self->queue_statusChange(15, $svc_external);
137 }
138
139 sub queue_statusChange {
140   my( $self, $status, $svc_external ) = @_;
141
142   my $queue = new FS::queue {
143     'svcnum' => $svc_external->svcnum,
144     'job'    => 'FS::part_export::artera_turbo::statusChange',
145   };
146   $queue->insert(
147     ( map { $self->option($_) }
148           qw( rid username password production ) ),
149     $status,
150     $svc_external->id,
151     $svc_external->title,
152     $self->option('debug'),
153   );
154 }
155
156 sub statusChange {
157   my( $rid, $username, $password, $prod, $status, $id, $title, $debug ) = @_;
158
159   eval "use Net::Artera;";
160   return $@ if $@;
161   $Net::Artera::DEBUG = 1 if $debug;
162
163   my $artera = new Net::Artera (
164     'rid'        => $rid,
165     'username'   => $username,
166     'password'   => $password,
167     'production' => $prod,
168   );
169
170   my $result = $artera->statusChange(
171     'asn'      => sprintf('%010d', $id),
172     'akc'      => substr("0000000000$title", -10),
173     'statusid' => $status,
174   );
175
176   die $result->{'message'} unless $result->{'id'} == 1;
177
178 }
179
180 1;
181