summaryrefslogtreecommitdiff
path: root/httemplate/search/cust_pkg.cgi
blob: dabcf46fbe1c2188f9846647fc84239b96bc62ba (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
245
246
247
248
249
250
251
252
253
254
255
256
257
<%
# <!-- $Id: cust_pkg.cgi,v 1.12 2001-12-18 07:12:00 ivan Exp $ -->

use strict;
use vars qw ( $cgi @cust_pkg $sortby $query %part_pkg
              $conf $maxrecords $limit $offset );
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use FS::UID qw(dbh cgisuidsetup);
use FS::Conf;
use FS::Record qw(qsearch qsearchs dbdef);
use FS::CGI qw(header eidiot popurl table);
use FS::cust_pkg;
use FS::pkg_svc;
use FS::cust_svc;
use FS::cust_main;

$cgi = new CGI;
&cgisuidsetup($cgi);

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

my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});

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

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

my $total;

my $unconf = '';
($query) = $cgi->keywords;
if ( $query eq 'pkgnum' ) {
  $sortby=\*pkgnum_sort;

} elsif ( $query eq 'APKG_pkgnum' ) {

  $sortby=\*pkgnum_sort;

  $unconf = "
    WHERE 0 <
      ( SELECT count(*) FROM pkg_svc
          WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
            AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
                                       WHERE cust_svc.pkgnum = cust_pkg.pkgnum
                                         AND cust_svc.svcpart = pkg_svc.svcpart
                                   )
      )
  ";

  #@cust_pkg=();
  ##perhaps this should go in cust_pkg as a qsearch-like constructor?
  #my($cust_pkg);
  #foreach $cust_pkg (
  #  qsearch('cust_pkg',{}, '', "ORDER BY pkgnum $limit" )
  #) {
  #  my($flag)=0;
  #  my($pkg_svc);
  #  PKG_SVC: 
  #  foreach $pkg_svc (qsearch('pkg_svc',{ 'pkgpart' => $cust_pkg->pkgpart })) {
  #    if ( $pkg_svc->quantity 
  #         > scalar(qsearch('cust_svc',{
  #             'pkgnum' => $cust_pkg->pkgnum,
  #             'svcpart' => $pkg_svc->svcpart,
  #           }))
  #       )
  #    {
  #      $flag=1;
  #      last PKG_SVC;
  #    }
  #  }
  #  push @cust_pkg, $cust_pkg if $flag;
  #}
  
} else {
  die "Empty QUERY_STRING!";
}

my $statement = "SELECT COUNT(*) FROM cust_pkg $unconf";
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];

@cust_pkg = qsearch('cust_pkg',{}, '', "$unconf ORDER BY pkgnum $limit" );


if ( scalar(@cust_pkg) == 1 ) {
  my($pkgnum)=$cust_pkg[0]->pkgnum;
  print $cgi->redirect(popurl(2). "view/cust_pkg.cgi?$pkgnum");
  exit;
} elsif ( scalar(@cust_pkg) == 0 ) { #error
  eidiot("No packages found");
} else {
  $total ||= scalar(@cust_pkg);

  #begin pager
  my $pager = '';
  if ( $total != scalar(@cust_pkg) && $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('Package Search Results',''),
        "$total matching packages found<BR><BR>$pager", &table(), <<END;
      <TR>
        <TH>Package</TH>
        <TH><FONT SIZE=-1>Setup</FONT></TH>
        <TH><FONT SIZE=-1>Next<BR>bill</FONT></TH>
        <TH><FONT SIZE=-1>Susp.</FONT></TH>
        <TH><FONT SIZE=-1>Expire</FONT></TH>
        <TH><FONT SIZE=-1>Cancel</FONT></TH>
        <TH><FONT SIZE=-1>Cust#</FONT></TH>
        <TH>(bill) name</TH>
        <TH>company</TH>
END

if ( defined dbdef->table('cust_main')->column('ship_last') ) {
  print <<END;
      <TH>(service) name</TH>
      <TH>company</TH>
END
}

print <<END;
        <TH COLSPAN=2>Services</TH>
      </TR>
END

  my $n1 = '<TR>';
  my(%saw,$cust_pkg);
  foreach $cust_pkg (
    sort $sortby grep(!$saw{$_->pkgnum}++, @cust_pkg)
  ) {
    my($cust_main)=qsearchs('cust_main',{'custnum'=>$cust_pkg->custnum});
    my($pkgnum, $setup, $bill, $susp, $expire, $cancel,
       $custnum, $last, $first, $company ) = (
      $cust_pkg->pkgnum,
      $cust_pkg->getfield('setup')
        ? time2str("%D", $cust_pkg->getfield('setup') )
        : '',
      $cust_pkg->getfield('bill')
        ? time2str("%D", $cust_pkg->getfield('bill') )
        : '',
      $cust_pkg->getfield('susp')
        ? time2str("%D", $cust_pkg->getfield('susp') )
        : '',
      $cust_pkg->getfield('expire')
        ? time2str("%D", $cust_pkg->getfield('expire') )
        : '',
      $cust_pkg->getfield('cancel')
        ? time2str("%D", $cust_pkg->getfield('cancel') )
        : '',
      $cust_pkg->custnum,
      $cust_main ? $cust_main->last : '',
      $cust_main ? $cust_main->first : '',
      $cust_main ? $cust_main->company : '',
    );
    my($ship_last, $ship_first, $ship_company);
    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
      ($ship_last, $ship_first, $ship_company) = (
        $cust_main
          ? ( $cust_main->ship_last || $cust_main->getfield('last') )
          : '',
        $cust_main 
          ? ( $cust_main->ship_last
              ? $cust_main->ship_first
              : $cust_main->first )
          : '',
        $cust_main 
          ? ( $cust_main->ship_last
              ? $cust_main->ship_company
              : $cust_main->company )
          : '',
      );
    }
    my $pkg = $part_pkg{$cust_pkg->pkgpart}->pkg;
    #$pkg .= ' - '. $part_pkg{$cust_pkg->pkgpart}->comment;
    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
    my $rowspan = scalar(@cust_svc) || 1;
    my $p = popurl(2);
    print $n1, <<END;
      <TD ROWSPAN=$rowspan><A HREF="${p}view/cust_pkg.cgi?$pkgnum"><FONT SIZE=-1>$pkgnum - $pkg</FONT></A></TD>
      <TD>$setup</TD>
      <TD>$bill</TD>
      <TD>$susp</TD>
      <TD>$expire</TD>
      <TD>$cancel</TD>
END
    if ( $cust_main ) {
      print <<END;
      <TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$custnum</A></FONT></TD>
      <TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$last, $first</A></FONT></TD>
      <TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$company</A></FONT></TD>
END
      if ( defined dbdef->table('cust_main')->column('ship_last') ) {
        print <<END;
      <TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$ship_last, $ship_first</A></FONT></TD>
      <TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$ship_company</A></FONT></TD>
END
      }
    } else {
      my $colspan = defined dbdef->table('cust_main')->column('ship_last')
                    ? 5 : 3;
      print <<END;
      <TD ROWSPAN=$rowspan COLSPAN=$colspan>WARNING: couldn't find cust_main.custnum $custnum (cust_pkg.pkgnum $pkgnum)</TD>
END
    }

    my $n2 = '';
    foreach my $cust_svc ( @cust_svc ) {
      my($label, $value, $svcdb) = $cust_svc->label;
      my $svcnum = $cust_svc->svcnum;
      my $sview = $p. "view";
      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
      $n2="</TR><TR>";
    }

    $n1 = "</TR><TR>";

  }
    print '</TR>';
 
  print "</TABLE>$pager</BODY></HTML>";

}

sub pkgnum_sort {
  $a->getfield('pkgnum') <=> $b->getfield('pkgnum');
}

%>