import torrus 1.0.9
[freeside.git] / FS / FS / part_pkg.pm
index 46067d1..f4aacae 100644 (file)
@@ -21,6 +21,7 @@ use FS::part_pkg_taxoverride;
 use FS::part_pkg_taxproduct;
 use FS::part_pkg_link;
 use FS::part_pkg_discount;
+use FS::part_pkg_vendor;
 
 @ISA = qw( FS::m2m_Common FS::option_Common );
 $DEBUG = 0;
@@ -456,7 +457,7 @@ sub replace {
       my($exportnum,$vendor_pkg_id);
       while ( ($exportnum,$vendor_pkg_id) 
                                = each %{$options->{'part_pkg_vendor'}} ) {
-         my $replaced = 0;
+         my $noinsert = 0;
          foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
            if($exportnum == $part_pkg_vendor->exportnum
                && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
@@ -466,11 +467,16 @@ sub replace {
                  $dbh->rollback if $oldAutoCommit;
                  return "Error replacing part_pkg_vendor record: $error";
                }
-               $replaced = 1;
+               $noinsert = 1;
+               last;
+           }
+           elsif($exportnum == $part_pkg_vendor->exportnum
+               && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
+               $noinsert = 1;
                last;
            }
          }
-         unless ( $replaced ) {
+         unless ( $noinsert ) {
            my $ppv = new FS::part_pkg_vendor( {
                    'pkgpart' => $new->pkgpart,
                    'exportnum' => $exportnum,
@@ -1453,6 +1459,25 @@ sub _upgrade_data { # class method
     die $error if $error;
   }
 
+  my @part_pkg_option = qsearch('part_pkg_option',
+    { 'optionname'  => 'unused_credit',
+      'optionvalue' => 1,
+    });
+  foreach my $old_opt (@part_pkg_option) {
+    my $pkgpart = $old_opt->pkgpart;
+    my $error = $old_opt->delete;
+    die $error if $error;
+
+    foreach (qw(unused_credit_cancel unused_credit_change)) {
+      my $new_opt = new FS::part_pkg_option {
+        'pkgpart'     => $pkgpart,
+        'optionname'  => $_,
+        'optionvalue' => 1,
+      };
+      $error = $new_opt->insert;
+      die $error if $error;
+    }
+  }
 }
 
 =item curuser_pkgs_sql
@@ -1539,19 +1564,48 @@ foreach my $INC ( @INC ) {
       next;
     }
     warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
-    if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
-      warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
-      next;
-    }
+    #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
+    #  warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
+    #  next;
+    #}
     $info{$mod} = $info;
+    $info->{'weight'} ||= 0; # quiet warnings
   }
 }
 
+# copy one level deep to allow replacement of fields and fieldorder
 tie %plans, 'Tie::IxHash',
-  map  { $_ => $info{$_} }
+  map  { my %infohash = %{ $info{$_} }; 
+          $_ => \%infohash }
   sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
   keys %info;
 
+# inheritance of plan options
+foreach my $name (keys(%info)) {
+  if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
+    warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
+    delete $plans{$name};
+    next;
+  }
+  my $parents = $info{$name}->{'inherit_fields'} || [];
+  my (%fields, %field_exists, @fieldorder);
+  foreach my $parent ($name, @$parents) {
+    %fields = ( # avoid replacing existing fields
+      %{ $info{$parent}->{'fields'} || {} },
+      %fields
+    );
+    foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
+      # avoid duplicates
+      next if $field_exists{$_};
+      $field_exists{$_} = 1;
+      # allow inheritors to remove inherited fields from the fieldorder
+      push @fieldorder, $_ if !exists($fields{$_}->{'disabled'});
+    }
+  }
+  $plans{$name}->{'fields'} = \%fields;
+  $plans{$name}->{'fieldorder'} = \@fieldorder;
+}
+
 sub plan_info {
   \%plans;
 }