add some more customer output formats that include service address, RT#4583
[freeside.git] / FS / FS / part_pkg.pm
index 3eb3bc1..3308ead 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw( @ISA %plans $DEBUG );
 use Carp qw(carp cluck confess);
 use Scalar::Util qw( blessed );
+use Time::Local qw( timelocal_nocheck );
 use Tie::IxHash;
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs dbh dbdef );
@@ -431,6 +432,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')
@@ -447,7 +454,7 @@ sub check {
                               'part_pkg_taxproduct',
                               'taxproductnum'
                              )
-    || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
+    || $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
     || $self->SUPER::check
   ;
   return $error if $error;
@@ -599,15 +606,29 @@ sub svcpart {
   my $svcdb = scalar(@_) ? shift : '';
   my @svcdb_pkg_svc =
     grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
-  my @pkg_svc = ();
-  @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
-    if dbdef->table('pkg_svc')->column('primary_svc');
+  my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
   @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
     unless @pkg_svc;
   return '' if scalar(@pkg_svc) != 1;
   $pkg_svc[0]->svcpart;
 }
 
+=item svcpart_unique_svcdb SVCDB
+
+Returns the svcpart of the a service definition (see L<FS::part_svc>) matching
+SVCDB associated with this package definition (see L<FS::pkg_svc>).  Returns
+false if there not a primary service definition for SVCDB or there are multiple
+service definitions for SVCDB.
+
+=cut
+
+sub svcpart_unique_svcdb {
+  my( $self, $svcdb ) = @_;
+  my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
+  return '' if scalar(@svcdb_pkg_svc) != 1;
+  $svcdb_pkg_svc[0]->svcpart;
+}
+
 =item payby
 
 Returns a list of the acceptable payment types for this package.  Eventually
@@ -714,6 +735,41 @@ sub freq_pretty {
   }
 }
 
+=item add_freq TIMESTAMP
+
+Adds the frequency of this package to the provided timestamp and returns
+the resulting timestamp, or -1 if the frequency of this package could not be
+parsed (shouldn't happen).
+
+=cut
+
+sub add_freq {
+  my( $self, $date ) = @_;
+  my $freq = $self->freq;
+
+  #change this bit to use Date::Manip? CAREFUL with timezones (see
+  # mailing list archive)
+  my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
+
+  if ( $self->freq =~ /^\d+$/ ) {
+    $mon += $self->freq;
+    until ( $mon < 12 ) { $mon -= 12; $year++; }
+  } elsif ( $self->freq =~ /^(\d+)w$/ ) {
+    my $weeks = $1;
+    $mday += $weeks * 7;
+  } elsif ( $self->freq =~ /^(\d+)d$/ ) {
+    my $days = $1;
+    $mday += $days;
+  } elsif ( $self->freq =~ /^(\d+)h$/ ) {
+    my $hours = $1;
+    $hour += $hours;
+  } else {
+    return -1;
+  }
+
+  timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
+}
+
 =item plandata
 
 For backwards compatibility, returns the plandata field as well as all options
@@ -858,7 +914,9 @@ sub has_taxproduct {
   my $self = shift;
 
   $self->taxproductnum ||
-  scalar(grep { $_ =~/^usage_taxproductnum_/ } keys %{ {$self->options} } )
+  scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) } 
+          keys %{ {$self->options} }
+  )
 
 }
 
@@ -929,6 +987,7 @@ sub _expand_cch_taxproductnum {
                          ? ( split ':', $part_pkg_taxproduct->taxproduct )
                          : ()
                      );
+  $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
   my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
                       OR taxproduct = '$a:$b:$c:'
                       OR taxproduct = '$a:$b:".":$d'
@@ -955,11 +1014,14 @@ sub part_pkg_taxrate {
         ).
     ')';
   # much more CCH oddness in m2m -- this is kludgy
-  $extra_sql .= ' AND ('.
-    join(' OR ', map{ "taxproductnum = $_" }
-                 $self->_expand_cch_taxproductnum($class)
-        ).
-    ')';
+  my @tpnums = $self->_expand_cch_taxproductnum($class);
+  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';
@@ -1038,10 +1100,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.
@@ -1108,6 +1208,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
@@ -1155,38 +1286,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