Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / edit / process / change-cust_pkg.html
index 77f261d..046a979 100644 (file)
@@ -27,21 +27,57 @@ my $cust_pkg = qsearchs({
 die 'unknown pkgnum' unless $cust_pkg;
 
 my %change = map { $_ => scalar($cgi->param($_)) }
-                 qw( locationnum pkgpart );
+                 qw( locationnum pkgpart quantity );
 
 $change{'keep_dates'} = 1;
 
 if ( $cgi->param('locationnum') == -1 ) {
-  my $cust_location = FS::cust_location->new_or_existing({
+  my $cust_location = FS::cust_location->new({
     'custnum' => $cust_pkg->custnum,
     map { $_ => scalar($cgi->param($_)) }
-        qw( address1 address2 city county state zip country )
+        FS::cust_main->location_fields
   });
   $change{'cust_location'} = $cust_location;
 }
 
-my $pkg_or_error = $cust_pkg->change( \%change );
+my $error;
+my $now = time;
+if (defined($cgi->param('contract_end'))) {
+  $change{'contract_end'} = parse_datetime($cgi->param('contract_end'));
+}
 
-my $error = ref($pkg_or_error) ? '' : $pkg_or_error;
+unless ($error) {
+  if ( $cgi->param('delay') ) {
+    my $date = parse_datetime($cgi->param('start_date'));
+    if (!$date) {
+      $error = "Invalid change date '".$cgi->param('start_date')."'.";
+    } elsif ( $date < $now ) {
+      $error = "Change date ".$cgi->param('start_date')." is in the past.";
+    } else {
+      # schedule the change
+      $change{'start_date'} = $date;
+      $error = $cust_pkg->change_later(\%change);
+    }
+  } else {
+    # special case: if there's a package change scheduled, and it matches
+    # the parameters the user requested this time, then change to the existing
+    # future package.
+    if ( $cust_pkg->change_to_pkgnum ) {
+      my $change_to = FS::cust_pkg->by_key($cust_pkg->change_to_pkgnum);
+      if (
+        $change_to->pkgpart      == $change{'pkgpart'} and
+        $change_to->locationnum  == $change{'locationnum'} and
+        $change_to->quantity     == $change{'quantity'} and
+        $change_to->contract_end == $change{'contract_end'}
+      ) {
+        %change = ( 'cust_pkg' => $change_to );
+      }
+    }
+    
+    # do a package change right now
+    my $pkg_or_error = $cust_pkg->change( \%change );
+    $error = ref($pkg_or_error) ? '' : $pkg_or_error;
+  }
+}
 
 </%init>