summaryrefslogtreecommitdiff
path: root/httemplate/search/svc_acct.cgi
blob: daf025a38de8a6ece81e520b5e64eb1a29b1916d (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<%

my $mydomain = '';

my $conf = new FS::Conf;
my $maxrecords = $conf->config('maxsearchrecordsperpage');

my $orderby = ''; #removeme

my $limit = '';
$limit .= "LIMIT $maxrecords" if $maxrecords;

my $offset = $cgi->param('offset') || 0;
$limit .= " OFFSET $offset" if $offset;

my $total;

my($query)=$cgi->keywords;
$query ||= ''; #to avoid use of unitialized value errors

my $unlinked = '';
if ( $query =~ /^UN_(.*)$/ ) {
  $query = $1;
  $unlinked = '
    WHERE 0 <
      ( SELECT count(*) FROM cust_svc
          WHERE cust_svc.svcnum = svc_acct.svcnum
            AND pkgnum IS NULL
      )
  ';
}

my(@svc_acct, $sortby);
if ( $query eq 'svcnum' ) {
  $sortby=\*svcnum_sort;
  $orderby = 'ORDER BY svcnum';
} elsif ( $query eq 'username' ) {
  $sortby=\*username_sort;
  $orderby = 'ORDER BY username';
} elsif ( $query eq 'uid' ) {
  $sortby=\*uid_sort;
  $orderby = ( $unlinked ? 'AND' : 'WHERE' ). ' uid IS NOT NULL ORDER BY uid';
} else {
  $sortby=\*uid_sort;
  @svc_acct = @{&usernamesearch};
}

if ( $query eq 'svcnum' || $query eq 'username' || $query eq 'uid' ) {

  my $statement = "SELECT COUNT(*) FROM svc_acct $unlinked";
  my $sth = dbh->prepare($statement)
    or die dbh->errstr. " doing $statement";
  $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;

  $total = $sth->fetchrow_arrayref->[0];

  @svc_acct = qsearch('svc_acct', {}, '', "$unlinked $orderby $limit");

}

if ( scalar(@svc_acct) == 1 ) {
  my($svcnum)=$svc_acct[0]->svcnum;
  print $cgi->redirect(popurl(2). "view/svc_acct.cgi?$svcnum");  #redirect
  #exit;
} elsif ( scalar(@svc_acct) == 0 ) { #error
%>
<!-- mason kludge -->
<%
  idiot("Account not found");
} else {
%>
<!-- mason kludge -->
<%
  $total ||= scalar(@svc_acct);

  #begin pager
  my $pager = '';
  if ( $total != scalar(@svc_acct) && $maxrecords ) {
    unless ( $offset == 0 ) {
      $cgi->param('offset', $offset - $maxrecords);
      $pager .= '<A HREF="'. $cgi->self_url.
                '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
    }
    my $poff;
    my $page;
    for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
      $page++;
      if ( $offset == $poff ) {
        $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
      } else {
        $cgi->param('offset', $poff);
        $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
      }
    }
    unless ( $offset + $maxrecords > $total ) {
      $cgi->param('offset', $offset + $maxrecords);
      $pager .= '<A HREF="'. $cgi->self_url.
                '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
    }
  }
  #end pager

  print header("Account Search Results",menubar('Main Menu'=>popurl(2))),
        "$total matching accounts found<BR><BR>$pager",
        &table(), <<END;
      <TR>
        <TH><FONT SIZE=-1>#</FONT></TH>
        <TH><FONT SIZE=-1>Username</FONT></TH>
        <TH><FONT SIZE=-1>Domain</FONT></TH>
        <TH><FONT SIZE=-1>UID</FONT></TH>
        <TH><FONT SIZE=-1>Service</FONT></TH>
        <TH><FONT SIZE=-1>Cust#</FONT></TH>
        <TH><FONT SIZE=-1>(bill) name</FONT></TH>
        <TH><FONT SIZE=-1>company</FONT></TH>
END
  if ( defined dbdef->table('cust_main')->column('ship_last') ) {
    print <<END;
        <TH><FONT SIZE=-1>(service) name</FONT></TH>
        <TH><FONT SIZE=-1>company</FONT></TH>
END
  }
  print "</TR>";

  my(%saw,$svc_acct);
  my $p = popurl(2);
  foreach $svc_acct (
    sort $sortby grep(!$saw{$_->svcnum}++, @svc_acct)
  ) {
    my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_acct->svcnum })
      or die "No cust_svc record for svcnum ". $svc_acct->svcnum;
    my $part_svc = qsearchs('part_svc', { 'svcpart' => $cust_svc->svcpart })
      or die "No part_svc record for svcpart ". $cust_svc->svcpart;

    my $domain;
    my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $svc_acct->domsvc });
    if ( $svc_domain ) {
      $domain = "<A HREF=\"${p}view/svc_domain.cgi?". $svc_domain->svcnum.
                "\">". $svc_domain->domain. "</A>";
    } else {
      unless ( $mydomain ) {
        my $conf = new FS::Conf;
        unless ( $mydomain = $conf->config('domain') ) {
          die "No legacy domain config file and no svc_domain.svcnum record ".
              "for svc_acct.domsvc: ". $svc_acct->domsvc;
        }
      }
      $domain = "<i>$mydomain</i><FONT COLOR=\"#FF0000\">*</FONT>";
    }
    my($cust_pkg,$cust_main);
    if ( $cust_svc->pkgnum ) {
      $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_svc->pkgnum })
        or die "No cust_pkg record for pkgnum ". $cust_svc->pkgnum;
      $cust_main = qsearchs('cust_main', { 'custnum' => $cust_pkg->custnum })
        or die "No cust_main record for custnum ". $cust_pkg->custnum;
    }
    my($svcnum, $username, $uid, $svc, $custnum, $last, $first, $company) = (
      $svc_acct->svcnum,
      $svc_acct->getfield('username'),
      $svc_acct->getfield('uid'),
      $part_svc->svc,
      $cust_svc->pkgnum ? $cust_main->custnum : '',
      $cust_svc->pkgnum ? $cust_main->getfield('last') : '',
      $cust_svc->pkgnum ? $cust_main->getfield('first') : '',
      $cust_svc->pkgnum ? $cust_main->company : '',
    );
    my($pcustnum) = $custnum
      ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\"><FONT SIZE=-1>$custnum</FONT></A>"
      : "<I>(unlinked)</I>"
    ;
    my $pname = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$last, $first</A>" : '';
    my $pcompany = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$company</A>" : '';
    my($pship_name, $pship_company);
    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
      my($ship_last, $ship_first, $ship_company) = (
        $cust_svc->pkgnum ? ( $cust_main->ship_last || $last ) : '',
        $cust_svc->pkgnum ? ( $cust_main->ship_last
                              ? $cust_main->ship_first
                              : $first
                            ) : '',
        $cust_svc->pkgnum ? ( $cust_main->ship_last
                              ? $cust_main->ship_company
                              : $company
                            ) : '',
      );
      $pship_name = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_last, $ship_first</A>" : '';
      $pship_company = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_company</A>" : '';
    }
    print <<END;
    <TR>
      <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$svcnum</FONT></A></TD>
      <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$username</FONT></A></TD>
      <TD><FONT SIZE=-1>$domain</FONT></TD>
      <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$uid</FONT></A></TD>
      <TD><FONT SIZE=-1>$svc</FONT></TH>
      <TD><FONT SIZE=-1>$pcustnum</FONT></TH>
      <TD><FONT SIZE=-1>$pname<FONT></TH>
      <TD><FONT SIZE=-1>$pcompany</FONT></TH>
END
    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
      print <<END;
      <TD><FONT SIZE=-1>$pship_name<FONT></TH>
      <TD><FONT SIZE=-1>$pship_company</FONT></TH>
END
    }
    print "</TR>";

  }
 
  print "</TABLE>$pager<BR>";

  if ( $mydomain ) {
    print "<BR><FONT COLOR=\"#FF0000\">*</FONT> The <I>$mydomain</I> domain ".
          "is contained in your legacy <CODE>domain</CODE> ".
          "<A HREF=\"${p}docs/config.html#domain\">configuration file</A>.  ".
          "You should run the <CODE>bin/fs-migrate-svc_acct_sm</CODE> script ".
          "to create a proper svc_domain record for this domain.";
  }

  print '</BODY></HTML>';

}

sub svcnum_sort {
  $a->getfield('svcnum') <=> $b->getfield('svcnum');
}

sub username_sort {
  $a->getfield('username') cmp $b->getfield('username');
}

sub uid_sort {
  $a->getfield('uid') <=> $b->getfield('uid');
}

sub usernamesearch {

  $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
  my($username)=$1;

  [ qsearch('svc_acct',{'username'=>$username}) ];

}

%>