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