RT#41866: Punctuation prevented package from billing
authorJonathan Prykop <jonathan@freeside.biz>
Mon, 25 Apr 2016 21:18:10 +0000 (16:18 -0500)
committerJonathan Prykop <jonathan@freeside.biz>
Mon, 25 Apr 2016 21:18:10 +0000 (16:18 -0500)
FS/FS/part_pkg.pm
FS/FS/part_pkg/global_Mixin.pm

index 9ed623f..709e137 100644 (file)
@@ -770,6 +770,32 @@ sub check {
   '';
 }
 
   '';
 }
 
+=item check_options
+
+For a passed I<$options> hashref, validates any options that
+have 'validate' subroutines defined (I<$options> values might
+be altered.)  Returns error message, or empty string if valid.
+
+Invoked by L</insert> and L</replace> via the equivalent
+methods in L<FS::option_Common>.
+
+=cut
+
+sub check_options {
+  my ($self,$options) = @_;
+  foreach my $option (keys %$options) {
+    if (exists $plans{ $self->plan }->{fields}->{$option}) {
+      if (exists($plans{$self->plan}->{fields}->{$option}->{'validate'})) {
+        # pass option name for use in error message
+        # pass a reference to the $options value, so it can be cleaned up
+        my $error = &{$plans{$self->plan}->{fields}->{$option}->{'validate'}}($option,\($options->{$option}));
+        return $error if $error;
+      }
+    } # else "option does not exist" error?
+  }
+  return '';
+}
+
 =item check_pkg_svc
 
 Checks pkg_svc records as a whole (for part_svc_link dependencies).
 =item check_pkg_svc
 
 Checks pkg_svc records as a whole (for part_svc_link dependencies).
index 9faddf0..31d178d 100644 (file)
@@ -14,16 +14,35 @@ tie my %a2billing_simultaccess, 'Tie::IxHash', (
   1 => 'Enabled',
 );
 
   1 => 'Enabled',
 );
 
+# much false laziness with FS::Record::ut_money
+sub validate_moneyn {
+  my ($option, $valref) = @_;
+  if ( $$valref eq '' ) {
+    return '';
+  } elsif ( $$valref =~ /^\s*(\-)?\s*(\d*)(\.\d{1})\s*$/ ) {
+    #handle one decimal place without barfing out
+    $$valref = ( ($1||''). ($2||''). ($3.'0') ) || 0;
+  } elsif ( $$valref =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/ ) {
+    $$valref = ( ($1||''). ($2||''). ($3||'') ) || 0;
+  } else {
+    return "Illegal (money) $option: ". $$valref;
+  }
+  return '';
+}
+
+
 %info = (
   'disabled' => 1,
   'fields' => {
     'setup_fee' => { 
       'name' => 'Setup fee for this package',
       'default' => 0,
 %info = (
   'disabled' => 1,
   'fields' => {
     'setup_fee' => { 
       'name' => 'Setup fee for this package',
       'default' => 0,
+      'validate' => \&validate_moneyn,
     },
     'recur_fee' => { 
       'name' => 'Recurring fee for this package',
       'default' => 0,
     },
     'recur_fee' => { 
       'name' => 'Recurring fee for this package',
       'default' => 0,
+      'validate' => \&validate_moneyn,
     },
     'unused_credit_cancel' => {
       'name' => 'Credit the customer for the unused portion of service at '.
     },
     'unused_credit_cancel' => {
       'name' => 'Credit the customer for the unused portion of service at '.