credit sources, RT#28917
[freeside.git] / FS / FS / part_pkg.pm
index 77f70e4..d2f2f86 100644 (file)
@@ -122,6 +122,10 @@ part_pkg, will be equal to pkgpart.
 
 =item delay_start - Number of days to delay package start, by default
 
+=item start_on_hold - 'Y' to suspend this package immediately when it is 
+ordered. The package will not start billing or have a setup fee charged 
+until it is manually unsuspended.
+
 =back
 
 =head1 METHODS
@@ -672,14 +676,15 @@ sub check {
     || $self->ut_textn('comment')
     || $self->ut_textn('promo_code')
     || $self->ut_alphan('plan')
-    || $self->ut_enum('setuptax', [ '', 'Y' ] )
-    || $self->ut_enum('recurtax', [ '', 'Y' ] )
+    || $self->ut_flag('setuptax')
+    || $self->ut_flag('recurtax')
     || $self->ut_textn('taxclass')
-    || $self->ut_enum('disabled', [ '', 'Y' ] )
-    || $self->ut_enum('custom', [ '', 'Y' ] )
-    || $self->ut_enum('no_auto', [ '', 'Y' ])
-    || $self->ut_enum('recur_show_zero', [ '', 'Y' ])
-    || $self->ut_enum('setup_show_zero', [ '', 'Y' ])
+    || $self->ut_flag('disabled')
+    || $self->ut_flag('custom')
+    || $self->ut_flag('no_auto')
+    || $self->ut_flag('recur_show_zero')
+    || $self->ut_flag('setup_show_zero')
+    || $self->ut_flag('start_on_hold')
     #|| $self->ut_moneyn('setup_cost')
     #|| $self->ut_moneyn('recur_cost')
     || $self->ut_floatn('setup_cost')
@@ -1130,7 +1135,10 @@ sub is_free {
 sub can_discount { 0; }
  
 # whether the plan allows changing the start date
-sub can_start_date { 1; }
+sub can_start_date {
+  my $self = shift;
+  $self->start_on_hold ? 0 : 1;
+}
 
 # whether the plan supports part_pkg_usageprice add-ons (a specific kind of
 #  pre-selectable usage pricing, there's others this doesn't refer to)
@@ -1657,7 +1665,7 @@ recur_cost divided by freq (only supported for monthly and longer frequencies)
 sub recur_cost_permonth {
   my($self, $cust_pkg) = @_;
   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
-  sprintf('%.2f', $self->recur_cost / $self->freq );
+  sprintf('%.2f', ($self->recur_cost || 0) / $self->freq );
 }
 
 =item cust_bill_pkg_recur CUST_PKG
@@ -1702,7 +1710,7 @@ unit_setup minus setup_cost
 
 sub setup_margin {
   my $self = shift;
-  $self->unit_setup(@_) - $self->setup_cost;
+  $self->unit_setup(@_) - ($self->setup_cost || 0);
 }
 
 =item recur_margin_permonth
@@ -1763,7 +1771,7 @@ sub parse {
 # Used by FS::Upgrade to migrate to a new database.
 
 sub _upgrade_data { # class method
-  my($class, %opts) = @_;
+   my($class, %opts) = @_;
 
   warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
 
@@ -1783,6 +1791,49 @@ sub _upgrade_data { # class method
     $part_pkg->replace;
 
   }
+
+  # Convert RADIUS accounting usage metrics from megabytes to gigabytes
+  # (FS RT#28105)
+  my $upgrade = 'part_pkg_gigabyte_usage';
+  if (!FS::upgrade_journal->is_done($upgrade)) {
+    foreach my $part_pkg (qsearch('part_pkg',
+                                 { plan => 'sqlradacct_hour' })
+                        ){
+
+      my $pkgpart = $part_pkg->pkgpart;
+
+      foreach my $opt (qsearch('part_pkg_option',
+                              { 'optionname'  => { op => 'LIKE',
+                                                   value => 'recur_included_%',
+                                                 },
+                                pkgpart => $pkgpart,
+                              })){
+
+        next if $opt->optionname eq 'recur_included_hours'; # unfortunately named field
+        next if $opt->optionvalue == 0;
+
+       $opt->optionvalue($opt->optionvalue / 1024);
+
+       my $error = $opt->replace;
+       die $error if $error;
+      }
+
+      foreach my $opt (qsearch('part_pkg_option',
+                              { 'optionname'  => { op => 'LIKE',
+                                                   value => 'recur_%_charge',
+                                                 },
+                                pkgpart => $pkgpart,
+                              })){
+       $opt->optionvalue($opt->optionvalue * 1024);
+
+       my $error = $opt->replace;
+       die $error if $error;
+      }
+
+    }
+    FS::upgrade_journal->set_done($upgrade);
+  }
+
   # the rest can be done asynchronously
 }