RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / indosoft_pbx.pm
1 package FS::part_export::indosoft_pbx;
2 use base qw( FS::part_export );
3
4 use vars qw( %info );
5 use Tie::IxHash;
6 use Lingua::EN::NameParse;
7
8 tie my %options, 'Tie::IxHash',
9    'host'        => { label => 'Q-Suite API hostname' },
10    'gateway_key' => { label => 'API gateway key' },
11 ;
12
13 %info = (
14   'svc'      => [qw( svc_pbx svc_phone svc_acct )],
15   'desc'     =>
16     'Export PBX tenants and DIDs to Indosoft Q-Suite',
17   'options'  => \%options,
18   'no_machine' => 1,
19   'notes'    => <<'END'
20 Export PBX tenants and DIDs to Indosoft Q-Suite.
21 Net::Indosoft::QSuite is required.
22 END
23 );
24
25 $insert_hack = 0;
26
27 sub rebless { shift; }
28
29 sub _export_insert {
30   my($self, $svc_x) = (shift, shift);
31
32   my $cust_main = $svc_x->cust_svc->cust_pkg->cust_main;
33
34   if ( $svc_x->isa('FS::svc_pbx') ) {
35
36     my $uuid = $self->indosoft_runcommand( 'editTenant',
37       'mode'          => 'create',
38       'name'          => $svc_x->title || $cust_main->name,
39       'shortname'     => $svc_x->svcnum, #?
40       #'hostname'      => #XXX what goes here?  add to svc_pbx?
41       'callerid_name' => $svc_x->title || $cust_main->name, #separate?  add to svc_pbx?
42       #'callerid_number' => #XXX where tf to get this from?  svc_pbx again?
43     );
44
45     $svc_x->id( $uuid );
46
47     # ?
48     #my $unneeded_trunk_uuid = $self->indosoft_command( 'assignTrunkToTenant,
49     #  'tenant_uuid' => $uuid,
50     #  'trunk_id'    => $self->option('trunk_id'),
51     #);
52
53   } elsif ( $svc_x->isa('FS::svc_phone') ) {
54
55     my $same_number = $self->indosoft_runcommand( 'editDID',
56       'mode'        => 'create',
57       'tenant_uuid' => $svc_x->svc_pbx->id,
58       'number'      => $svc_x->phonenum,
59       # 'auto_attendant_uuid' =>#XXX where to get this from? svc_phone.newfield?
60     );
61
62     #set the auto-attendant name, repeate limit, response limit, extension dialing?
63
64     #XXX ring group?
65
66   } elsif ( $svc_x->isa('FS::svc_acct') ) {
67
68     my($firstname, $lastname);
69     my $NameParse = new Lingua::EN::NameParse;
70     if ( $NameParse->parse( $svc_x->finger ) ) {
71       $firstname = $cust_main->first,
72       $lastname  = $cust_main->get('last'),
73     } else {
74       my %name = $NameParse->components;
75       $firstname = $name{given_name_1} || $name{initials_1}; #wtf NameParse, Ed?
76       $lastname  = $name{surname_1};
77     }
78
79     my $uuid = $self->indosoft_command( 'editEmployee',
80       'mode'        => 'create',
81       'tenant_uuid' => $svc_x->svc_pbx->id,
82       'username'    => $svc_x->username,
83       'password'    => $svc_x->_password,
84       'firstname'   => $firstname,
85       'lastname'    => $firstname,
86     );
87
88   #XXX extensions (pbx_extension export?  look at how svc_phone does its device export stuff
89
90   #XXX devices (links employee to extension (svc_acct to pbx_extension)?  how's that?  seems like they _go_ with extensions.  where to pick user?
91
92   #XXX voicemail pin?
93   #XXX extension forwarding?
94
95   } else {
96     die "guru meditation #five five five five: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
97   }
98
99   local($insert_hack) = 1;
100   #my $error = $svc_phone->replace;
101   #return $error if $error;
102   $svc_x->replace;
103
104 }
105
106 sub _export_replace {
107   my( $self, $new, $old ) = (shift, shift, shift);
108
109   return '' if $insert_hack;
110
111   #change anything?
112 }
113
114 sub _export_delete {
115   my( $self, $svc_phone ) = (shift, shift);
116
117   #delete conference
118
119   '';
120
121 }
122
123 # #these three are optional
124 # # fallback for svc_acct will change and restore password
125 # sub _export_suspend {
126 #   my( $self, $svc_phone ) = (shift, shift);
127 #   $err_or_queue = $self->indosoft_queue( $svc_phone->svcnum,
128 #     'suspend', $svc_phone->username );
129 #   ref($err_or_queue) ? '' : $err_or_queue;
130 # }
131
132 # sub _export_unsuspend {
133 #   my( $self, $svc_phone ) = (shift, shift);
134 #   $err_or_queue = $self->indosoft_queue( $svc_phone->svcnum,
135 #     'unsuspend', $svc_phone->username );
136 #   ref($err_or_queue) ? '' : $err_or_queue;
137 # }
138
139 # sub export_links {
140 #   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
141 #   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
142 #   #                 qq!">!. $svc_phone->username. qq!</A>!;
143 #   '';
144 # }
145
146 ###
147
148 sub indosoft_runcommand {
149   my( $self, $method ) = (shift, shift);
150
151   indosoft_command(
152     $self->option('host'),
153     $self->option('gateway_key'),
154     $method,
155     @_,
156   );
157
158 }
159
160 sub indosoft_command {
161   my( $host, $gateway_key, $method, @args ) = @_;
162
163   eval 'use Net::Indosoft::Qsuite;';
164   die $@ if $@;
165
166   my $qsuite = new Net::Indosoft::Qsuite( 'host'        => $host,
167                                           'gateway_key' => $gateway_key,
168                                         );
169
170   my $return = $qsuite->$method( @args );
171
172   die "Indosoft error: ". $qsuite->errstr if $qsuite->errstr;
173
174   $return;
175
176 }
177
178
179 # #a good idea to queue anything that could fail or take any time
180 # sub indosoft_queue {
181 #   my( $self, $svcnum, $method ) = (shift, shift, shift);
182 #   my $queue = new FS::queue {
183 #     'svcnum' => $svcnum,
184 #     'job'    => "FS::part_export::indosoft::indosoft_$method",
185 #   };
186 #   $queue->insert( @_ ) or $queue;
187 # }
188
189 # sub indosoft_insert { #subroutine, not method
190 #   my( $username, $password ) = @_;
191 #   #do things with $username and $password
192 # }
193
194 # sub indosoft_replace { #subroutine, not method
195 # }
196
197 # sub indosoft_delete { #subroutine, not method
198 #   my( $username ) = @_;
199 #   #do things with $username
200 # }
201
202 # sub indosoft_suspend { #subroutine, not method
203 # }
204
205 # sub indosoft_unsuspend { #subroutine, not method
206 # }
207
208
209 1;
210