fix linking to account list per access number
[freeside.git] / httemplate / browse / part_pkg.cgi
index b6e47b1..2a167e8 100755 (executable)
+<!-- mason kludge -->
 <%
-#<!-- $Id: part_pkg.cgi,v 1.3 2001-08-21 09:34:13 ivan Exp $ -->
 
-use strict;
-use vars qw( $cgi $p $part_pkg );
-use CGI;
-use CGI::Carp qw(fatalsToBrowser);
-use FS::UID qw(cgisuidsetup);
-use FS::Record qw(qsearch qsearchs);
-use FS::CGI qw(header menubar popurl table);
-use FS::part_pkg;
-use FS::pkg_svc;
-use FS::part_svc;
+my %search;
+if ( $cgi->param('showdisabled') ) {
+  %search = ();
+} else {
+  %search = ( 'disabled' => '' );
+}
 
-$cgi = new CGI;
+my @part_pkg = qsearch('part_pkg', \%search );
+my $total = scalar(@part_pkg);
 
-&cgisuidsetup($cgi);
+my $sortby;
+my %num_active_cust_pkg;
+if ( $cgi->param('active') ) {
+  my $active_sth = dbh->prepare(
+    'SELECT COUNT(*) FROM cust_pkg WHERE pkgpart = ?'.
+    ' AND ( cancel IS NULL OR cancel = 0 )'.
+    ' AND ( susp IS NULL OR susp = 0 )'
+  ) or die dbh->errstr;
+  foreach my $part_pkg ( @part_pkg ) {
+    $active_sth->execute($part_pkg->pkgpart) or die $active_sth->errstr;
+    $num_active_cust_pkg{$part_pkg->pkgpart} =
+      $active_sth->fetchrow_arrayref->[0];
+  }
+  $sortby = sub {
+    $num_active_cust_pkg{$b->pkgpart} <=> $num_active_cust_pkg{$a->pkgpart};
+  };
+} else {
+  $sortby = \*pkgpart_sort;
+}
 
-$p = popurl(2);
+%>
+<%= header("Package Definition Listing",menubar( 'Main Menu' => $p )) %>
+<% unless ( $cgi->param('active') ) { %>
+  One or more service definitions are grouped together into a package 
+  definition and given pricing information.  Customers purchase packages
+  rather than purchase services directly.<BR><BR>
+  <A HREF="<%= $p %>edit/part_pkg.cgi"><I>Add a new package definition</I></A>
+  <BR><BR>
+<% } %>
 
-print $cgi->header( '-expires' => 'now' ), header("Package Part Listing",menubar(
-  'Main Menu' => $p,
-)), "One or more services are grouped together into a package and given",
-  " pricing information. Customers purchase packages, not services.<BR><BR>", 
-  &table(), <<END;
-    <TABLE BORDER>
+<%= $total %> package definitions
+<%
+if ( $cgi->param('showdisabled') ) {
+  $cgi->param('showdisabled', 0);
+  print qq!( <a href="!. $cgi->self_url. qq!">hide disabled packages</a> )!;
+} else {
+  $cgi->param('showdisabled', 1);
+  print qq!( <a href="!. $cgi->self_url. qq!">show disabled packages</a> )!;
+}
+
+my $colspan = $cgi->param('showdisabled') ? 2 : 3;
+print &table(), <<END;
       <TR>
-        <TH COLSPAN=2>Package</TH>
+        <TH COLSPAN=$colspan>Package</TH>
         <TH>Comment</TH>
-        <TH><FONT SIZE=-1>Setup Fee</FONT></TH>
+END
+print '        <TH><FONT SIZE=-1>Customer<BR>packages</FONT></TH>'
+  if $cgi->param('active');
+print <<END;
         <TH><FONT SIZE=-1>Freq.</FONT></TH>
-        <TH><FONT SIZE=-1>Recur. Fee</FONT></TH>
+        <TH><FONT SIZE=-1>Plan</FONT></TH>
+        <TH><FONT SIZE=-1>Data</FONT></TH>
         <TH>Service</TH>
         <TH><FONT SIZE=-1>Quan.</FONT></TH>
       </TR>
 END
 
-foreach $part_pkg ( sort { 
-  $a->getfield('pkgpart') <=> $b->getfield('pkgpart')
-} qsearch('part_pkg',{}) ) {
+foreach my $part_pkg ( sort $sortby @part_pkg ) {
   my($hashref)=$part_pkg->hashref;
   my(@pkg_svc)=grep $_->getfield('quantity'),
     qsearch('pkg_svc',{'pkgpart'=> $hashref->{pkgpart} });
   my($rowspan)=scalar(@pkg_svc);
+  my $plandata;
+  if ( $hashref->{plan} ) {
+    $plandata = $hashref->{plandata};
+    $plandata =~ s/^(\w+)=/$1&nbsp;/mg;
+    $plandata =~ s/\n/<BR>/g;
+  } else {
+    $hashref->{plan} = "(legacy)";
+    $plandata = "Setup&nbsp;". $hashref->{setup}.
+                "<BR>Recur&nbsp;". $hashref->{recur};
+  }
   print <<END;
       <TR>
-        <TD ROWSPAN=$rowspan><A HREF="${p}edit/part_pkg.cgi?$hashref->{pkgpart}">
-          $hashref->{pkgpart}
-        </A></TD>
+        <TD ROWSPAN=$rowspan><A HREF="${p}edit/part_pkg.cgi?$hashref->{pkgpart}">$hashref->{pkgpart}</A></TD>
+END
+
+  unless ( $cgi->param('showdisabled') ) {
+    print "<TD ROWSPAN=$rowspan>";
+    print "DISABLED" if $hashref->{disabled};
+    print '</TD>';
+  }
+
+  print <<END;
         <TD ROWSPAN=$rowspan><A HREF="${p}edit/part_pkg.cgi?$hashref->{pkgpart}">$hashref->{pkg}</A></TD>
         <TD ROWSPAN=$rowspan>$hashref->{comment}</TD>
-        <TD ROWSPAN=$rowspan>$hashref->{setup}</TD>
+END
+  if ( $cgi->param('active') ) {
+    print "        <TD ROWSPAN=$rowspan>";
+    print '<FONT COLOR="#00CC00"><B>'.
+          $num_active_cust_pkg{$hashref->{'pkgpart'}}.
+          qq!</B></FONT>&nbsp;<A HREF="${p}search/cust_pkg.cgi?magic=active;pkgpart=$hashref->{pkgpart}">active</A>!;
+    # suspended/cancelled
+    print '</TD>';
+  }
+  print <<END;
         <TD ROWSPAN=$rowspan>$hashref->{freq}</TD>
-        <TD ROWSPAN=$rowspan>$hashref->{recur}</TD>
+        <TD ROWSPAN=$rowspan>$hashref->{plan}</TD>
+        <TD ROWSPAN=$rowspan>$plandata</TD>
 END
 
   my($pkg_svc);
@@ -68,10 +126,16 @@ END
   print "</TR>";
 }
 
+$colspan = $cgi->param('showdisabled') ? 8 : 9;
 print <<END;
-   <TR><TD COLSPAN=2><I><A HREF="${p}edit/part_pkg.cgi">Add new package</A></I></TD></TR>
+
     </TABLE>
   </BODY>
 </HTML>
 END
+
+sub pkgpart_sort {
+  $a->pkgpart <=> $b->pkgpart;
+}
+
 %>