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