export host selection per service, RT#17914
[freeside.git] / FS / FS / part_export / indosoft.pm
1 package FS::part_export::indosoft;
2
3 use vars qw(@ISA %info $insert_hack);
4 use Tie::IxHash;
5 use Date::Format;
6 use FS::part_export;
7
8 @ISA = qw(FS::part_export);
9
10 tie my %options, 'Tie::IxHash',
11    'url'        => { label => 'Voicebridge API URL' },
12    'account_id' => { label => 'Voicebridge Account ID' },
13 ;
14
15 %info = (
16   'svc'      => 'svc_phone', #svc_bridge?  svc_confbridge?
17   'desc'     =>
18     'Export conferences to the Indosoft Conference Bridge',
19   'options'  => \%options,
20   'no_machine' => 1,
21   'notes'    => <<'END'
22 Export conferences to the Indosoft conference bridge.
23 Net::Indosoft::Voicebridge is required.
24 END
25 );
26
27 $insert_hack = 0;
28
29 sub rebless { shift; }
30
31 sub _export_insert {
32   my($self, $svc_phone) = (shift, shift);
33
34   my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
35
36   my $address = $cust_main->address1;
37   $address .= ' '.$cust_main->address2 if $cust_main->address2;
38
39   my $phone = $cust_main->daytime || $cust_main->night;
40
41   my @email = $cust_main->invoicing_list_emailonly;
42
43   #svc_phone->location_hash stuff?  well that was for e911.. this shouldn't
44   # even be svc_phone
45
46   #add client
47   my $client_return = eval {
48     indosoft_runcommand( 'addClient',
49       'account_id' => $self->option('account_id'),
50
51       'client_contact_name'     => $cust_main->name, #or just first last?
52       'client_contact_password' => $svc_phone->sip_password, # ?
53
54       'client_contact_addr'     => $address,
55       'client_contact_city'     => $cust_main->city,
56       'client_contact_state'    => $cust_main->state,
57       'client_contact_country'  => $cust_main->country,
58       'client_contact_zip'      => $cust_main->zip,
59
60       'client_contact_phone'    => $phone,
61       'client_contact_fax'      => $cust_main->fax,
62       'client_contact_email'    => $email[0],
63     );
64   };
65   return $@ if $@;
66
67   my $client_id = $client_return->{client_id};
68
69   #add conference
70   my $conf_return = eval {
71     indosoft_runcommand( 'addConference',
72       'client_id'          => $client_id,
73       'conference_name'    => $cust_main->name,
74       'conference_desc'    => $svc_phone->svcnum. ' for '. $cust_main->name,
75       'start_time'         => time2str('%Y-%d-$m %T', time), #now, right??  '2010-20-04 16:20:00',
76       #'moderated_flag'     => 0,
77       #'entry_ann_flag'     => 0
78       #'record_flag'        => 0
79       #'moh_flag'           => 0
80       #'talk_detect_flag'   => 0
81       #'play_user_cnt_flag' => 0
82       #'wait_for_admin'     => 0
83       #'stop_on_admin_exit' => 0
84       #'second_pin'         => 0
85       #'secondary_pin'      => 0,
86       #'allow_sub-conf'     => 0,
87       #'duration'           => 0,
88       #'conference_type' => 'reservation', #'reservationless',
89     );
90   };
91   return $@ if $@;
92
93   my $conference_id = $conf_return->{conference_id};
94
95   #put conference_id in svc_phone.phonenum (and client_id in... phone_name???)
96   local($insert_hack) = 1;
97   $svc_phone->phonenum($conference_id);
98   $svc_phone->phone_name($client_id);
99   #my $error = $svc_phone->replace;
100   #return $error if $error;
101   $svc_phone->replace;
102
103 }
104
105 sub _export_replace {
106   my( $self, $new, $old ) = (shift, shift, shift);
107   return "can't change phone number as conference_id with indosoft"
108     if $old->phonenum ne $new->phonenum && ! $insert_hack;
109   return '';
110
111   #change anything?
112 }
113
114 sub _export_delete {
115   my( $self, $svc_phone ) = (shift, shift);
116
117   #delete conference
118   my $conf_return = eval {
119     indosoft_runcommand( 'deleteConference',
120       'conference_id' => $svc_phone->phonenum,
121     );
122   };
123   return $@ if $@;
124
125   #delete client
126   my $client_return = eval {
127     indosoft_runcommand( 'deleteClient',
128       'client_id' => $svc_phone->phone_name,
129     )
130   };
131   return $@ if $@;
132
133   '';
134
135 }
136
137 # #these three are optional
138 # # fallback for svc_acct will change and restore password
139 # sub _export_suspend {
140 #   my( $self, $svc_phone ) = (shift, shift);
141 #   $err_or_queue = $self->indosoft_queue( $svc_phone->svcnum,
142 #     'suspend', $svc_phone->username );
143 #   ref($err_or_queue) ? '' : $err_or_queue;
144 # }
145
146 # sub _export_unsuspend {
147 #   my( $self, $svc_phone ) = (shift, shift);
148 #   $err_or_queue = $self->indosoft_queue( $svc_phone->svcnum,
149 #     'unsuspend', $svc_phone->username );
150 #   ref($err_or_queue) ? '' : $err_or_queue;
151 # }
152
153 # sub export_links {
154 #   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
155 #   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
156 #   #                 qq!">!. $svc_phone->username. qq!</A>!;
157 #   '';
158 # }
159
160 ###
161
162 sub indosoft_runcommand {
163   my( $self, $method ) = (shift, shift);
164
165   indosoft_command(
166     $self->option('url'),
167     $method,
168     @_,
169   );
170
171 }
172
173 sub indosoft_command {
174   my( $url, $method, @args ) = @_;
175
176   eval 'use Net::Indosoft::Voicebridge;';
177   die $@ if $@;
178
179   my $vb = new Net::Indosoft::Voicebridge( 'url' => $url );
180
181   my $return = $vb->$method( @args );
182
183   die "Indosoft error: ". $return->{'error'} if $return->{'error'};
184
185   $return;
186
187 }
188
189
190 # #a good idea to queue anything that could fail or take any time
191 # sub indosoft_queue {
192 #   my( $self, $svcnum, $method ) = (shift, shift, shift);
193 #   my $queue = new FS::queue {
194 #     'svcnum' => $svcnum,
195 #     'job'    => "FS::part_export::indosoft::indosoft_$method",
196 #   };
197 #   $queue->insert( @_ ) or $queue;
198 # }
199
200 # sub indosoft_insert { #subroutine, not method
201 #   my( $username, $password ) = @_;
202 #   #do things with $username and $password
203 # }
204
205 # sub indosoft_replace { #subroutine, not method
206 # }
207
208 # sub indosoft_delete { #subroutine, not method
209 #   my( $username ) = @_;
210 #   #do things with $username
211 # }
212
213 # sub indosoft_suspend { #subroutine, not method
214 # }
215
216 # sub indosoft_unsuspend { #subroutine, not method
217 # }
218
219
220 1;