show credit balance on invoices, #11564
[freeside.git] / FS / FS / Maestro.pm
1 package FS::Maestro;
2
3 use strict;
4 use Date::Format;
5 use FS::Conf;
6 use FS::Record qw( qsearchs );
7 use FS::cust_main;
8 use FS::cust_pkg;
9 use FS::part_svc;
10
11 #i guess this is kind of deprecated in favor of service_status, but keeping it
12 #around until they say they don't need it.
13 sub customer_status {
14   my( $custnum ) = shift; #@_;
15   my $svcnum = @_ ? shift : '';
16
17   my $curuser = $FS::CurrentUser::CurrentUser;
18
19   my $cust_main = qsearchs({
20     'table'     => 'cust_main',
21     'hashref'   => { 'custnum' => $custnum },
22     'extra_sql' => ' AND '. $curuser->agentnums_sql,
23   })
24     or return { 'status' => 'E',
25                 'error'  => "custnum $custnum not found" };
26
27   return service_status($svcnum) if $svcnum;
28
29   ###
30   # regular customer to maestro (single package)
31   ###
32
33   my %result = ();
34
35   my @cust_pkg = $cust_main->cust_pkg;
36
37   #things specific to the non-reseller scenario
38
39   $result{'status'} = substr($cust_main->ucfirst_status,0,1);
40
41   $result{'products'} =
42     [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ];
43
44   #find svc_pbx
45
46   my @cust_svc = map $_->cust_svc, @cust_pkg;
47
48   my @cust_svc_pbx =
49     grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
50     @cust_svc;
51
52   if ( ! @cust_svc_pbx ) {
53     return { 'status' => 'E',
54              'error'  => "customer $custnum has no conference service" };
55   } elsif ( scalar(@cust_svc_pbx) > 1 ) {
56     return { 'status' => 'E',
57              'error'  =>
58                "customer $custnum has more than one conference".
59                " service (reseller?); specify a svcnum as a second argument",
60            };
61   }
62
63   my $cust_svc_pbx = $cust_svc_pbx[0];
64
65   my $svc_pbx = $cust_svc_pbx->svc_x;
66
67   # find "outbound service" y/n
68
69   my $conf = new FS::Conf;
70   my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
71   $result{'outbound_service'} =
72     scalar( grep { $outbound_pkgs{ $_->pkgpart }
73                      && !$_->get('cancel')
74                  }
75                  @cust_pkg
76           )
77     ? 1 : 0;
78
79   # find "good till" date/time stamp
80
81   my @active_cust_pkg =
82     sort { $a->bill <=> $b->bill }
83     grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
84     @cust_pkg;
85   $result{'good_till'} = time2str('%c', $active_cust_pkg[0]->bill || time );
86
87   return { 
88     'name'    => $cust_main->name,
89     'email'   => $cust_main->invoicing_list_emailonly_scalar,
90     #'agentnum' => $cust_main->agentnum,
91     #'agent'    => $cust_main->agent->agent,
92     'max_lines'        => $svc_pbx ? $svc_pbx->max_extensions : '',
93     'max_simultaneous' => $svc_pbx ? $svc_pbx->max_simultaneous : '',
94     %result,
95   };
96
97 }
98
99 sub service_status {
100   my $svcnum = shift;
101
102   my $svc_pbx = qsearchs({
103     'table'      => 'svc_pbx',
104     'addl_from'  => ' LEFT JOIN cust_svc USING ( svcnum ) '.
105                     ' LEFT JOIN cust_pkg USING ( pkgnum ) ',
106     'hashref'   => { 'svcnum' => $svcnum },
107     #'extra_sql' => " AND custnum = $custnum",
108   })
109     or return { 'status' => 'E',
110                 'error'  => "svcnum $svcnum not found" };
111
112   my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;
113   my $cust_main = $cust_pkg->cust_main;
114
115   my %result = ();
116
117   #status in the reseller scenario
118   $result{'status'} = substr($cust_pkg->ucfirst_status,0,1);
119
120   # find "outbound service" y/n
121   my @cust_pkg = $cust_main->cust_pkg;
122   #XXX what about outbound service per-reseller ?
123   my $conf = new FS::Conf;
124   my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
125   $result{'outbound_service'} =
126     scalar( grep { $outbound_pkgs{ $_->pkgpart }
127                      && !$_->get('cancel')
128                  }
129                  @cust_pkg
130           )
131     ? 1 : 0;
132
133   # find "good till" date/time stamp (this package)
134   $result{'good_till'} = time2str('%c', $cust_pkg->bill || time );
135
136   return { 
137     'custnum' => $cust_main->custnum,
138     'name'    => $cust_main->name,
139     'email'   => $cust_main->invoicing_list_emailonly_scalar,
140     #'agentnum' => $cust_main->agentnum,
141     #'agent'    => $cust_main->agent->agent,
142     'max_lines'        => $svc_pbx->max_extensions,
143     'max_simultaneous' => $svc_pbx->max_simultaneous,
144     %result,
145   };
146
147 }
148
149 #some false laziness w/ MyAccount order_pkg
150 sub order_pkg {
151   my $opt = ref($_[0]) ? shift : { @_ };
152
153   $opt->{'title'} = delete $opt->{'name'}
154     if !exists($opt->{'title'}) && exists($opt->{'name'});
155
156   my $custnum = $opt->{'custnum'};
157
158   my $curuser = $FS::CurrentUser::CurrentUser;
159
160   my $cust_main = qsearchs({
161     'table'     => 'cust_main',
162     'hashref'   => { 'custnum' => $custnum },
163     'extra_sql' => ' AND '. $curuser->agentnums_sql,
164   })
165     or return { 'error'  => "custnum $custnum not found" };
166
167   my $status = $cust_main->status;
168   #false laziness w/ClientAPI/Signup.pm
169
170   my $cust_pkg = new FS::cust_pkg ( {
171     'custnum' => $custnum,
172     'pkgpart' => $opt->{'pkgpart'},
173   } );
174   my $error = $cust_pkg->check;
175   return { 'error' => $error } if $error;
176
177   my @svc = ();
178   unless ( $opt->{'svcpart'} eq 'none' ) {
179
180     my $svcpart = '';
181     if ( $opt->{'svcpart'} =~ /^(\d+)$/ ) {
182       $svcpart = $1;
183     } else {
184       $svcpart = $cust_pkg->part_pkg->svcpart; #($svcdb);
185     }
186
187     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
188     return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
189
190     my $svcdb = $part_svc->svcdb;
191
192     my %fields = (
193       'svc_acct'     => [ qw( username domsvc _password sec_phrase popnum ) ],
194       'svc_domain'   => [ qw( domain ) ],
195       'svc_phone'    => [ qw( phonenum pin sip_password phone_name ) ],
196       'svc_external' => [ qw( id title ) ],
197       'svc_pbx'      => [ qw( id title ) ],
198     );
199   
200     my $svc_x = "FS::$svcdb"->new( {
201       'svcpart'   => $svcpart,
202       map { $_ => $opt->{$_} } @{$fields{$svcdb}}
203     } );
204     
205     #snarf processing not necessary here (or probably at all, anymore)
206     
207     my $y = $svc_x->setdefault; # arguably should be in new method
208     return { 'error' => $y } if $y && !ref($y);
209   
210     $error = $svc_x->check;
211     return { 'error' => $error } if $error;
212
213     push @svc, $svc_x;
214
215   }
216
217   use Tie::RefHash;
218   tie my %hash, 'Tie::RefHash';
219   %hash = ( $cust_pkg => \@svc );
220   #msgcat
221   $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
222   return { 'error' => $error } if $error;
223
224 # currently they're using this in the reseller scenario, so don't
225 # bill the package immediately
226 #  my $conf = new FS::Conf;
227 #  if ( $conf->exists('signup_server-realtime') ) {
228 #
229 #    my $bill_error = _do_bop_realtime( $cust_main, $status );
230 #
231 #    if ($bill_error) {
232 #      $cust_pkg->cancel('quiet'=>1);
233 #      return $bill_error;
234 #    } else {
235 #      $cust_pkg->reexport;
236 #    }
237 #
238 #  } else {
239     $cust_pkg->reexport;
240 #  }
241
242   my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
243
244   return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
245
246 }
247
248 1;