summaryrefslogtreecommitdiff
path: root/FS/FS/Maestro.pm
blob: 2061f28f15439ad286fc04d2c0f134e860625df7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package FS::Maestro;

use Date::Format;
use FS::Conf;
use FS::Record qw( qsearchs );
use FS::cust_main;

sub customer_status {
  my( $custnum ) = shift; #@_;
  my $svcnum = @_ ? shift : '';

  my $curuser = $FS::CurrentUser::CurrentUser;

  my $cust_main = qsearchs({
    'table'     => 'cust_main',
    'hashref'   => { 'custnum' => $custnum },
    'extra_sql' => ' AND '. $curuser->agentnums_sql,
  })
    or return { 'status' => 'E',
                'error'  => "custnum $custnum not found" };

  my( $svc_pbx, $good_till, $outbound_service ) = ( '', '', '' );
  my %result = ();
  if ( $svcnum ) {
   
    ###
    # reseller scenario to maestro (customer w/ multiple packages)
    ###

    # find $svc_pbx

    $svc_pbx = qsearchs({
      'table'      => 'svc_pbx',
      'addl_from'  => ' LEFT JOIN cust_svc USING ( svcnum ) '.
                      ' LEFT JOIN cust_pkg USING ( pkgnum ) ',
      'hashref'   => { 'svcnum' => $svcnum },
      'extra_sql' => " AND custnum = $custnum",
    })
      or return { 'status' => 'E',
                  'error'  => "svcnum $svcnum not found" };

    #status in the reseller scenario

    my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;

    $result{'status'} = substr($cust_pkg->ucfirst_status,0,1);

    # find "outbound service" y/n

    #XXX outbound service per-reseller ?
    #my @cust_pkg = $cust_main->cust_pkg;
    #
    #my $conf = new FS::Conf;
    #my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
    #my $outbound_service =
    #  scalar( grep { $outbound_pkgs{ $_->pkgpart }
    #                   && !$_->get('cancel')
    #               }
    #               @cust_pkg
    #        )
    #  ? 1 : 0;

    # find "good till" date/time stamp (this package)

    $good_till  = time2str('%c', $cust_pkg->bill || time );

  } else {

    ###
    # regular customer to maestro (single package)
    ###

    my @cust_pkg = $cust_main->cust_pkg;

    #things specific to the non-reseller scenario

    $result{'status'} = substr($cust_main->ucfirst_status,0,1);

    $result{'products'} =
      [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ];

    #find svc_pbx

    my @cust_svc = map $_->cust_svc, @cust_pkg;

    my @cust_svc_pbx =
      grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
      @cust_svc;

    if ( ! @cust_svc_pbx ) {
      return { 'status' => 'E',
               'error'  => "customer $custnum has no conference service" };
    } elsif ( scalar(@cust_svc_pbx) > 1 ) {
      return { 'status' => 'E',
               'error'  =>
                 "customer $custnum has more than one conference".
                 " service (reseller?); specify a svcnum as a second argument",
             };
    }

    my $cust_svc_pbx = $cust_svc_pbx[0];

    $svc_pbx = $cust_svc_pbx->svc_x;

    # find "outbound service" y/n

    my $conf = new FS::Conf;
    my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
    my $outbound_service =
      scalar( grep { $outbound_pkgs{ $_->pkgpart }
                       && !$_->get('cancel')
                   }
                   @cust_pkg
            )
      ? 1 : 0;

    # find "good till" date/time stamp

    my @active_cust_pkg =
      sort { $a->bill <=> $b->bill }
      grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
      @cust_pkg;
    my $good_till = time2str('%c', $active_cust_pkg[0]->bill || time );

  }

  return { 
    'name'   => $cust_main->name,
    'email'  => $cust_main->invoicing_list_emailonly_scalar,
    'max_lines'        => $svc_pbx ? $svc_pbx->max_extensions : '',
    'max_simultaneous' => $svc_pbx ? $svc_pbx->max_simultaneous : '',
    'outbound_service' => $outbound_service,
    'good_till' => $good_till,
    %result,
  };

}

1;