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