tax engine refactoring for Avalara and Billsoft tax vendors, #25718
[freeside.git] / httemplate / edit / process / part_pkg.cgi
index db4fcb9..0343cc0 100755 (executable)
@@ -50,8 +50,7 @@ my $precheck_callback = sub {
   }
   return "At least one agent type must be specified."
     unless scalar(@agents)
-           || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
-           || ( !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg') )
+           #wtf? || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
            || $cgi->param('disabled')
            || $cgi->param('agentnum');
 
@@ -97,11 +96,31 @@ my $args_callback = sub {
         grep { $_ !~ /^report_option_/ }
         @options;
 
-  foreach ( split(',', $cgi->param('taxproductnums') ) ) {
-    my $value = $cgi->param("taxproductnum_$_");
-    $error ||= "Illegal taxproductnum_$_: $value"
+  foreach my $class ( '', split(',', $cgi->param('taxproductnums') ) ) {
+    my $param = 'taxproductnum';
+    $param .= "_$class" if length($class); # gah, "_$class"?
+    my $value = $cgi->param($param);
+
+    if ( $value == -1 ) {
+      my $desc = $cgi->param($param.'_description');
+      # insert a new part_pkg_taxproduct
+      my $engine = FS::TaxEngine->new;
+      my $obj_or_error = $engine->add_taxproduct($desc);
+      if (ref $obj_or_error) {
+        $value = $obj_or_error->taxproductnum;
+        $cgi->param($param, $value); # for error handling
+      } else {
+        die "$obj_or_error (adding tax product)";
+      }
+    }
+
+    $error ||= "Illegal $param: $value"
       unless ( $value =~ /^\d*$/  );
-    $options{"usage_taxproductnum_$_"} = $value;
+    if (length($class)) {
+      $options{"usage_taxproductnum_$_"} = $value;
+    } else {
+      $new->set('taxproductnum', $value);
+    }
   }
 
   foreach ( grep $_, $cgi->param('report_option') ) {
@@ -128,6 +147,14 @@ my $args_callback = sub {
   push @args, 'part_pkg_currency' => \%part_pkg_currency;
 
   ###
+  # fcc options
+  ###
+  my $fcc_options_string = $cgi->param('fcc_options_string');
+  if ($fcc_options_string) {
+    push @args, 'fcc_options' => decode_json($fcc_options_string);
+  }
+
+  ###
   #pkg_svc
   ###
 
@@ -251,17 +278,30 @@ foreach my $override_class ($cgi->param) {
 
 my $conf = new FS::Conf;
 
-if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
-  my @agents = ();
-  foreach ($cgi->param('agent_type')) {
-    /^(\d+)$/;
-    push @agents, $1 if $1;
-  }
-  push @process_m2m, {
-    'link_table'   => 'type_pkgs',
-    'target_table' => 'agent_type',
-    'params'       => \@agents,
-  };
+my @agents = ();
+foreach ($cgi->param('agent_type')) {
+  /^(\d+)$/;
+  push @agents, $1 if $1;
+}
+push @process_m2m, {
+  'link_table'   => 'type_pkgs',
+  'target_table' => 'agent_type',
+  'params'       => \@agents,
+};
+
+my $targets = FS::part_pkg_usageprice->targets;
+foreach my $amount_param ( grep /^usagepricepart(\d+)_amount$/, $cgi->param ) {
+  $amount_param =~ /^usagepricepart(\d+)_amount$/ or die 'unpossible';
+  my $num = $1;
+  my $amount = $cgi->param($amount_param);
+  if ( ! $amount && ! $cgi->param("usagepricepart${num}_price") ) {
+    #don't add empty rows just because the dropdowns have a value
+    $cgi->param("usagepricepart${num}_$_", '') for qw( currency action target );
+    next;
+  } 
+  my $target = $cgi->param("usagepricepart${num}_target");
+  $amount *= $targets->{$target}{multiplier} if $targets->{$target}{multiplier};
+  $cgi->param($amount_param, $amount);
 }
 
 my @process_o2m = (
@@ -269,6 +309,11 @@ my @process_o2m = (
     'table'  => 'part_pkg_msgcat',
     'fields' => [qw( locale pkg )],
   },
+  {
+    'table'  => 'part_pkg_usageprice',
+    'fields' => [qw( price currency action target amount )],
+
+  }
 );
 
 </%init>