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,
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:
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>.
52 sub rebless { shift; }
57 my $artera = new Net::Artera (
58 map { $_ => $self->option($_) }
59 qw( rid username password production )
65 my($self, $svc_external) = (shift, shift);
67 # want the ASN (serial) and AKC (key code) right away
69 eval "use Net::Artera;";
71 $Net::Artera::DEBUG = 1 if $self->option('debug');
72 my $artera = $self->_new_Artera;
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' }
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 : '';
83 my $cust_main = $cust_pkg->cust_main;
85 my $result = $artera->newOrder(
86 'pid' => $self->option('pid'),
87 'priceid' => $self->option('priceid'),
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,
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);
110 $result->{'message'} || 'No response from Artera';
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;
125 my( $self, $svc_external ) = (shift, shift);
126 $self->queue_statusChange(17, $svc_external);
129 sub _export_suspend {
130 my( $self, $svc_external ) = (shift, shift);
131 $self->queue_statusChange(16, $svc_external);
134 sub _export_unsuspend {
135 my( $self, $svc_external ) = (shift, shift);
136 $self->queue_statusChange(15, $svc_external);
139 sub queue_statusChange {
140 my( $self, $status, $svc_external ) = @_;
142 my $queue = new FS::queue {
143 'svcnum' => $svc_external->svcnum,
144 'job' => 'FS::part_export::artera_turbo::statusChange',
147 ( map { $self->option($_) }
148 qw( rid username password production ) ),
151 $svc_external->title,
152 $self->option('debug'),
157 my( $rid, $username, $password, $prod, $status, $id, $title, $debug ) = @_;
159 eval "use Net::Artera;";
161 $Net::Artera::DEBUG = 1 if $debug;
163 my $artera = new Net::Artera (
165 'username' => $username,
166 'password' => $password,
167 'production' => $prod,
170 my $result = $artera->statusChange(
171 'asn' => sprintf('%010d', $id),
172 'akc' => substr("0000000000$title", -10),
173 'statusid' => $status,
176 die $result->{'message'} unless $result->{'id'} == 1;