1 package FS::part_export::artera_turbo;
3 use vars qw(@ISA %info);
5 use FS::Record qw(qsearch);
10 @ISA = qw(FS::part_export);
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',
21 'aid' => { 'label' => 'Artera Agent ID to use if not using agentnum values', },
22 'production' => { 'label' => 'Production mode (leave unchecked for staging)',
25 'debug' => { 'label' => 'Enable debug logging',
28 'enable_edit' => { 'label' => 'Enable local editing of Artera serial numbers and key codes (note that the changes will NOT be exported to Artera)',
34 'svc' => 'svc_external',
35 #'svc' => [qw( svc_acct svc_forward )],
37 'Real-time export to Artera Turbo Reseller API',
38 'options' => \%options,
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:
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>.
53 sub rebless { shift; }
58 my $artera = new Net::Artera (
59 map { $_ => $self->option($_) }
60 qw( rid username password production )
66 my($self, $svc_external) = (shift, shift);
68 # want the ASN (serial) and AKC (key code) right away
70 eval "use Net::Artera;";
72 $Net::Artera::DEBUG = 1 if $self->option('debug');
73 my $artera = $self->_new_Artera;
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' }
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 : '';
84 my $cust_main = $cust_pkg->cust_main;
86 my $result = $artera->newOrder(
87 'pid' => $self->option('pid'),
88 'priceid' => $self->option('priceid'),
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,
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);
111 $result->{'message'} || 'No response from Artera';
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;
126 my( $self, $svc_external ) = (shift, shift);
127 $self->queue_statusChange(17, $svc_external);
130 sub _export_suspend {
131 my( $self, $svc_external ) = (shift, shift);
132 $self->queue_statusChange(16, $svc_external);
135 sub _export_unsuspend {
136 my( $self, $svc_external ) = (shift, shift);
137 $self->queue_statusChange(15, $svc_external);
140 sub queue_statusChange {
141 my( $self, $status, $svc_external ) = @_;
143 my $queue = new FS::queue {
144 'svcnum' => $svc_external->svcnum,
145 'job' => 'FS::part_export::artera_turbo::statusChange',
148 ( map { $self->option($_) }
149 qw( rid username password production ) ),
152 $svc_external->title,
153 $self->option('debug'),
158 my( $rid, $username, $password, $prod, $status, $id, $title, $debug ) = @_;
160 eval "use Net::Artera;";
162 $Net::Artera::DEBUG = 1 if $debug;
164 my $artera = new Net::Artera (
166 'username' => $username,
167 'password' => $password,
168 'production' => $prod,
171 my $result = $artera->statusChange(
172 'asn' => sprintf('%010d', $id),
173 'akc' => substr("0000000000$title", -10),
174 'statusid' => $status,
177 die $result->{'message'} unless $result->{'id'} == 1;