taxclass for ipifony downloaded charges, #18333
[freeside.git] / FS / bin / freeside-ipifony-download
index 0384926..64905e1 100644 (file)
@@ -12,7 +12,7 @@ use FS::Conf;
 use Text::CSV;
 
 my %opt;
-getopts('va:', \%opt);
+getopts('va:P:C:T:', \%opt);
 
 #$Net::SFTP::Foreign::debug = -1;
 sub HELP_MESSAGE { '
@@ -20,6 +20,9 @@ sub HELP_MESSAGE { '
       freeside-ipifony-download 
         [ -v ]
         [ -a archivedir ]
+        [ -P port ]
+        [ -C category ]
+        [ -T taxclass ]
         freesideuser sftpuser@hostname[:path]
 ' }
 
@@ -48,6 +51,18 @@ if ( $opt{a} ) {
     unless -w $opt{a};
 }
 
+my $categorynum = '';
+if ( $opt{C} ) {
+  # find this category (don't auto-create it, it should exist already)
+  my $category = qsearchs('pkg_category', { categoryname => $opt{C} });
+  if (!defined($category)) {
+    die "Package category '$opt{C}' does not exist.\n";
+  }
+  $categorynum = $category->categorynum;
+}
+
+my $taxclass = $opt{T} || '';
+
 #my $tmpdir = File::Temp->newdir();
 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
 
@@ -59,12 +74,18 @@ $sftpuser = $1 || $ENV{USER};
 $host =~ s/:(.*)//;
 $path = $1;
 
+my $port = 22;
+if ( $opt{P} =~ /^(\d+)$/ ) {
+  $port = $1;
+}
+
 # for now assume SFTP download as the only method
 print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
 
 my $sftp = Net::SFTP::Foreign->new(
   host      => $host,
   user      => $sftpuser,
+  port      => $port,
   # for now we don't support passwords. use authorized_keys.
   timeout   => 30,
   more      => ($opt{v} ? '-v' : ''),
@@ -87,11 +108,20 @@ FILE: foreach my $filename (@$files) {
     next FILE;
   }
 
+  # make sure server archive dir exists
+  if ( !$sftp->stat('Archive') ) {
+    print STDERR "Creating $path/Archive\n" if $opt{v};
+    $sftp->mkdir('Archive');
+    if($sftp->error) {
+      # something is seriously wrong
+      die "failed to create archive directory on server:\n".$sftp->error."\n";
+    }
+  }
   #move to server archive dir
   $sftp->rename("$filename", "Archive/$filename");
   if($sftp->error) {
-    warn "failed to archive $filename on server\n";
-  } # process it anyway though
+    warn "failed to archive $filename on server:\n".$sftp->error."\n";
+  } # process it anyway, I guess/
 
   #copy to local archive dir
   if ( $opt{a} ) {
@@ -129,16 +159,31 @@ FILE: foreach my $filename (@$files) {
       quantity    => $hash{quantity},
       start_date  => $cust_main->next_bill_date,
       pkg         => $hash{date_desc},
+      taxclass    => $taxclass,
     );
     if (my $classname = $hash{classname}) {
       if (!exists($classnum_of{$classname}) ) {
         # then look it up
-        my $pkg_class = qsearch('pkg_class', { classname => $classname });
-        $classnum_of{$classname} = $pkg_class ? $pkg_class->classnum : '';
+        my $pkg_class = qsearchs('pkg_class', {
+            classname   => $classname,
+            categorynum => $categorynum,
+        });
+        if (!defined($pkg_class)) {
+          # then create it
+          $pkg_class = FS::pkg_class->new({
+              classname   => $classname,
+              categorynum => $categorynum,
+          });
+          my $error = $pkg_class->insert;
+          die "Error creating package class for product code '$classname':\n".
+            "$error\n"
+            if $error;
+        }
+
+        $classnum_of{$classname} = $pkg_class->classnum;
       }
       $opt{classnum} = $classnum_of{$classname};
     }
-    # XXX what's the tax status of these charges?
     print STDERR "  Charging $hash{amount}\n"
       if $opt{v};
     my $error = $cust_main->charge(\%opt);