bootstrapping issues
[freeside.git] / FS / FS / part_pkg.pm
index 38fc03e..ef24b53 100644 (file)
@@ -1,7 +1,7 @@
 package FS::part_pkg;
 
 use strict;
-use vars qw( @ISA %plans $DEBUG );
+use vars qw( @ISA %plans $DEBUG $setup_hack );
 use Carp qw(carp cluck confess);
 use Scalar::Util qw( blessed );
 use Time::Local qw( timelocal_nocheck );
@@ -22,6 +22,7 @@ use FS::part_pkg_link;
 
 @ISA = qw( FS::m2m_Common FS::option_Common );
 $DEBUG = 0;
+$setup_hack = 0;
 
 =head1 NAME
 
@@ -432,6 +433,12 @@ sub check {
     $self->freq($1);
   }
 
+  my @null_agentnum_right = ( 'Edit global package definitions' );
+  push @null_agentnum_right, 'One-time charge'
+    if $self->freq =~ /^0/;
+  push @null_agentnum_right, 'Customize customer package'
+    if $self->disabled eq 'Y'; #good enough
+
   my $error = $self->ut_numbern('pkgpart')
     || $self->ut_text('pkg')
     || $self->ut_text('comment')
@@ -448,7 +455,10 @@ sub check {
                               'part_pkg_taxproduct',
                               'taxproductnum'
                              )
-    || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
+    || ( $setup_hack
+           ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
+           : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
+       )
     || $self->SUPER::check
   ;
   return $error if $error;
@@ -554,6 +564,18 @@ packages.
 
 =cut
 
+=item type_pkgs
+
+Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
+definition.
+
+=cut
+
+sub type_pkgs {
+  my $self = shift;
+  qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
+}
+
 sub pkg_svc {
   my $self = shift;
 
@@ -1009,10 +1031,13 @@ sub part_pkg_taxrate {
     ')';
   # much more CCH oddness in m2m -- this is kludgy
   my @tpnums = $self->_expand_cch_taxproductnum($class);
-  $extra_sql .= ' AND ('.
-                          join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
-                     ')'
-     if @tpnums;
+  if (scalar(@tpnums)) {
+    $extra_sql .= ' AND ('.
+                            join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
+                       ')';
+  } else {
+    $extra_sql .= ' AND ( 0 = 1 )';
+  }
 
   my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
   my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
@@ -1091,10 +1116,48 @@ sub calc_remain { 0; }
 sub calc_cancel { 0; }
 sub calc_units  { 0; }
 
+=item format OPTION DATA
+
+Returns data formatted according to the function 'format' described
+in the plan info.  Returns DATA if no such function exists.
+
+=cut
+
+sub format {
+  my ($self, $option, $data) = (shift, shift, shift);
+  if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
+    &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
+  }else{
+    $data;
+  }
+}
+
+=item parse OPTION DATA
+
+Returns data parsed according to the function 'parse' described
+in the plan info.  Returns DATA if no such function exists.
+
+=cut
+
+sub parse {
+  my ($self, $option, $data) = (shift, shift, shift);
+  if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
+    &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
+  }else{
+    $data;
+  }
+}
+
 =back
 
 =cut
 
+=head1 CLASS METHODS
+
+=over 4
+
+=cut
+
 # _upgrade_data
 #
 # Used by FS::Upgrade to migrate to a new database.
@@ -1161,6 +1224,37 @@ sub _upgrade_data { # class method
 
 }
 
+=item curuser_pkgs_sql
+
+Returns an SQL fragment for searching for packages the current user can
+use, either via part_pkg.agentnum directly, or via agent type (see
+L<FS::type_pkgs>).
+
+=cut
+
+sub curuser_pkgs_sql {
+  #my($class) = shift;
+
+  my $agentnums = join(',', $FS::CurrentUser::CurrentUser->agentnums);
+
+  "
+    (
+      agentnum IS NOT NULL
+      OR
+      0 < ( SELECT COUNT(*)
+              FROM type_pkgs
+                LEFT JOIN agent_type USING ( typenum )
+                LEFT JOIN agent AS typeagent USING ( typenum )
+              WHERE type_pkgs.pkgpart = part_pkg.pkgpart
+                AND typeagent.agentnum IN ($agentnums)
+          )
+    )
+  ";
+
+}
+
+=back
+
 =head1 SUBROUTINES
 
 =over 4
@@ -1208,38 +1302,6 @@ sub plan_info {
   \%plans;
 }
 
-=item format OPTION DATA
-
-Returns data formatted according to the function 'format' described
-in the plan info.  Returns DATA if no such function exists.
-
-=cut
-
-sub format {
-  my ($self, $option, $data) = (shift, shift, shift);
-  if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
-    &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
-  }else{
-    $data;
-  }
-}
-
-=item parse OPTION DATA
-
-Returns data parsed according to the function 'parse' described
-in the plan info.  Returns DATA if no such function exists.
-
-=cut
-
-sub parse {
-  my ($self, $option, $data) = (shift, shift, shift);
-  if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
-    &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
-  }else{
-    $data;
-  }
-}
-
 
 =back