5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir);
7 use Net::SFTP::Foreign;
8 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch qsearchs);
12 use File::Copy qw(copy);
16 getopts('va:P:C:e:', \%opt);
18 # Product codes that are subject to flat rate E911 charges. For these
19 # products, the'quantity' field represents the number of lines.
20 my @E911_CODES = ( 'V-HPBX', 'V-TRUNK' );
22 # Map TAXNONVOICE/TAXVOICE to Freeside taxclass names
24 'TAXNONVOICE' => 'Other',
29 #$Net::SFTP::Foreign::debug = -1;
32 freeside-ipifony-download
38 freesideuser sftpuser@hostname[:path]
50 my $user = shift or die &HELP_MESSAGE;
51 my $dbh = adminsuidsetup $user;
52 $FS::UID::AutoCommit = 0;
62 die "no such directory: $opt{a}\n"
64 die "archive directory $opt{a} is not writable by the freeside user\n"
70 $e911_part_pkg = FS::part_pkg->by_key($opt{e})
71 or die "E911 pkgpart $opt{e} not found.\n";
73 if ( $e911_part_pkg->base_recur > 0 or $e911_part_pkg->freq ) {
74 die "E911 pkgpart $opt{e} must be a one-time charge.\n";
80 # find this category (don't auto-create it, it should exist already)
81 my $category = qsearchs('pkg_category', { categoryname => $opt{C} });
82 if (!defined($category)) {
83 die "Package category '$opt{C}' does not exist.\n";
85 $categorynum = $category->categorynum;
88 #my $tmpdir = File::Temp->newdir();
89 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
93 my ($sftpuser, $path);
95 $sftpuser = $1 || $ENV{USER};
100 if ( $opt{P} =~ /^(\d+)$/ ) {
104 # for now assume SFTP download as the only method
105 print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
107 my $sftp = Net::SFTP::Foreign->new(
111 # for now we don't support passwords. use authorized_keys.
113 #more => ($opt{v} ? '-v' : ''),
115 die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
118 $sftp->setcwd($path) if $path;
120 my $files = $sftp->ls('ready', wanted => qr/\.csv$/, names_only => 1);
122 print STDERR "No charge files found.\n" if $opt{v};
126 my %cust_main; # cache
127 my %e911_qty; # custnum => sum of E911-subject quantity
129 my %is_e911 = map {$_ => 1} @E911_CODES;
131 FILE: foreach my $filename (@$files) {
132 print STDERR "Retrieving $filename\n" if $opt{v};
133 $sftp->get("ready/$filename", "$tmpdir/$filename");
135 warn "failed to download $filename\n";
139 # make sure server archive dir exists
140 if ( !$sftp->stat('done') ) {
141 print STDERR "Creating $path/done\n" if $opt{v};
142 $sftp->mkdir('done');
144 # something is seriously wrong
145 die "failed to create archive directory on server:\n".$sftp->error."\n";
148 #move to server archive dir
149 $sftp->rename("ready/$filename", "done/$filename");
151 warn "failed to archive $filename on server:\n".$sftp->error."\n";
152 } # process it anyway, I guess/
154 #copy to local archive dir
156 print STDERR "Copying $tmpdir/$filename to archive dir $opt{a}\n"
158 copy("$tmpdir/$filename", $opt{a});
159 warn "failed to copy $tmpdir/$filename to $opt{a}: $!" if $!;
162 open my $fh, "<$tmpdir/$filename";
163 my $csv = Text::CSV->new; # orthodox CSV
165 while (my $line = <$fh>) {
166 $csv->parse($line) or do {
167 warn "can't parse $filename: ".$csv->error_input."\n";
170 @hash{@fields} = $csv->fields();
171 if ( $hash{custnum} =~ /^cust/ ) {
172 # there appears to be a header row
173 print STDERR "skipping header row\n" if $opt{v};
177 $cust_main{$hash{custnum}} ||= FS::cust_main->by_key($hash{custnum});
179 warn "customer #$hash{custnum} not found\n";
182 print STDERR "Found customer #$hash{custnum}: ".$cust_main->name."\n"
185 my $amount = sprintf('%.2f',$hash{quantity} * $hash{unit_price});
186 # construct arguments for $cust_main->charge
188 amount => $hash{unit_price},
189 quantity => $hash{quantity},
190 start_date => $cust_main->next_bill_date,
191 pkg => $hash{date_desc} .
192 ' (' . $hash{quantity} . ' @ $' . $hash{unit_price} . ' ea)',
193 taxclass => $TAXCLASSES{ $hash{taxclass} },
195 if (my $classname = $hash{classname}) {
196 if (!exists($classnum_of{$classname}) ) {
198 my $pkg_class = qsearchs('pkg_class', {
199 classname => $classname,
200 categorynum => $categorynum,
202 if (!defined($pkg_class)) {
204 $pkg_class = FS::pkg_class->new({
205 classname => $classname,
206 categorynum => $categorynum,
208 my $error = $pkg_class->insert;
209 die "Error creating package class for product code '$classname':\n".
214 $classnum_of{$classname} = $pkg_class->classnum;
216 $charge_opt{classnum} = $classnum_of{$classname};
218 print STDERR " Charging $hash{unit_price} * $hash{quantity}\n"
220 my $error = $cust_main->charge(\%charge_opt);
222 warn "Error creating charge: $error" if $error;
226 $sum_charges += $amount;
229 if ( $opt{e} and $is_e911{$hash{classname}} ) {
230 $e911_qty{$hash{custnum}} ||= 0;
231 $e911_qty{$hash{custnum}} += $hash{quantity};
237 # Order E911 packages
240 foreach my $custnum ( keys (%e911_qty) ) {
241 my $cust_main = $cust_main{$custnum};
242 my $quantity = $e911_qty{$custnum};
243 next if $quantity == 0;
244 my $cust_pkg = FS::cust_pkg->new({
247 start_date => $cust_main->next_bill_date,
248 quantity => $quantity,
250 my $error = $cust_main->order_pkg({ cust_pkg => $cust_pkg });
252 warn "Error creating e911 charge for customer $custnum: $error\n";
256 $num_lines += $quantity;
265 Processed files: @$files
266 Created charges: $num_charges
267 Sum of charges: \$".sprintf('%0.2f', $sum_charges)."
268 E911 charges: $num_e911
269 E911 lines: $num_lines
276 freeside-ipifony-download - Download and import invoice items from IPifony.
280 freeside-ipifony-download
287 freesideuser sftpuser@hostname[:path]
289 =head1 REQUIRED PARAMETERS
291 I<freesideuser>: the Freeside user to run as.
293 I<sftpuser>: the SFTP user to connect as. The 'freeside' system user should
294 have an authorization key to connect as that user.
296 I<hostname>: the SFTP server.
298 =head1 OPTIONAL PARAMETERS
302 -a I<archivedir>: Save a copy of the downloaded file to I<archivedir>.
304 -P I<port>: Connect to that TCP port.
306 -C I<category>: The name of a package category to use when creating package
309 -e I<pkgpart>: The pkgpart (L<FS::part_pkg>) to use for E911 charges. A
310 package of this type will be ordered for each invoice that has E911-subject
311 line items. The 'quantity' field on this package will be set to the total
312 quantity of those line items.
314 The E911 package must be a one-time package (flat rate, no frequency, no
315 recurring fee) with setup fee equal to the fee per line.