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