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);
15 getopts('va:P:C:e:', \%opt);
17 # Product codes that are subject to flat rate E911 charges. For these
18 # products, the'quantity' field represents the number of lines.
19 my @E911_CODES = ( 'V-HPBX', 'V-TRUNK' );
21 # Map TAXNONVOICE/TAXVOICE to Freeside taxclass names
23 'TAXNONVOICE' => 'Other',
28 #$Net::SFTP::Foreign::debug = -1;
31 freeside-ipifony-download
37 freesideuser sftpuser@hostname[:path]
49 my $user = shift or die &HELP_MESSAGE;
50 my $dbh = adminsuidsetup $user;
51 $FS::UID::AutoCommit = 0;
61 die "no such directory: $opt{a}\n"
63 die "archive directory $opt{a} is not writable by the freeside user\n"
69 $e911_part_pkg = FS::part_pkg->by_key($opt{e})
70 or die "E911 pkgpart $opt{e} not found.\n";
72 if ( $e911_part_pkg->base_recur > 0 or $e911_part_pkg->freq ) {
73 die "E911 pkgpart $opt{e} must be a one-time charge.\n";
79 # find this category (don't auto-create it, it should exist already)
80 my $category = qsearchs('pkg_category', { categoryname => $opt{C} });
81 if (!defined($category)) {
82 die "Package category '$opt{C}' does not exist.\n";
84 $categorynum = $category->categorynum;
87 #my $tmpdir = File::Temp->newdir();
88 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
92 my ($sftpuser, $path);
94 $sftpuser = $1 || $ENV{USER};
99 if ( $opt{P} =~ /^(\d+)$/ ) {
103 # for now assume SFTP download as the only method
104 print STDERR "Connecting to $sftpuser\@$host...\n" if $opt{v};
106 my $sftp = Net::SFTP::Foreign->new(
110 # for now we don't support passwords. use authorized_keys.
112 #more => ($opt{v} ? '-v' : ''),
114 die "failed to connect to '$sftpuser\@$host'\n(".$sftp->error.")\n"
117 $sftp->setcwd($path) if $path;
119 my $files = $sftp->ls('.', wanted => qr/\.csv$/, names_only => 1);
121 print STDERR "No charge files found.\n" if $opt{v};
125 my %cust_main; # cache
126 my %e911_qty; # custnum => sum of E911-subject quantity
128 my %is_e911 = map {$_ => 1} @E911_CODES;
130 FILE: foreach my $filename (@$files) {
131 print STDERR "Retrieving $filename\n" if $opt{v};
132 $sftp->get("$filename", "$tmpdir/$filename");
134 warn "failed to download $filename\n";
138 # make sure server archive dir exists
139 if ( !$sftp->stat('Archive') ) {
140 print STDERR "Creating $path/Archive\n" if $opt{v};
141 $sftp->mkdir('Archive');
143 # something is seriously wrong
144 die "failed to create archive directory on server:\n".$sftp->error."\n";
147 #move to server archive dir
148 $sftp->rename("$filename", "Archive/$filename");
150 warn "failed to archive $filename on server:\n".$sftp->error."\n";
151 } # process it anyway, I guess/
153 #copy to local archive dir
155 print STDERR "Copying $tmpdir/$filename to archive dir $opt{a}\n"
157 copy("$tmpdir/$filename", $opt{a});
158 warn "failed to copy $tmpdir/$filename to $opt{a}: $!" if $!;
161 open my $fh, "<$tmpdir/$filename";
163 if ($header !~ /^"cust_id"/) {
164 warn "warning: $filename has incorrect header row:\n$header\n";
167 my $csv = Text::CSV->new; # orthodox CSV
169 while (my $line = <$fh>) {
170 $csv->parse($line) or do {
171 warn "can't parse $filename: ".$csv->error_input."\n";
174 @hash{@fields} = $csv->fields();
176 $cust_main{$hash{custnum}} ||= FS::cust_main->by_key($hash{custnum});
178 warn "customer #$hash{custnum} not found\n";
181 print STDERR "Found customer #$hash{custnum}: ".$cust_main->name."\n"
184 my $amount = sprintf('%.2f',$hash{quantity} * $hash{unit_price});
185 # construct arguments for $cust_main->charge
188 quantity => $hash{quantity},
189 start_date => $cust_main->next_bill_date,
190 pkg => $hash{date_desc},
191 taxclass => $TAXCLASSES{ $hash{taxclass} },
193 if (my $classname = $hash{classname}) {
194 if (!exists($classnum_of{$classname}) ) {
196 my $pkg_class = qsearchs('pkg_class', {
197 classname => $classname,
198 categorynum => $categorynum,
200 if (!defined($pkg_class)) {
202 $pkg_class = FS::pkg_class->new({
203 classname => $classname,
204 categorynum => $categorynum,
206 my $error = $pkg_class->insert;
207 die "Error creating package class for product code '$classname':\n".
212 $classnum_of{$classname} = $pkg_class->classnum;
214 $charge_opt{classnum} = $classnum_of{$classname};
216 print STDERR " Charging $hash{unit_price} * $hash{quantity}\n"
218 my $error = $cust_main->charge(\%charge_opt);
220 warn "Error creating charge: $error" if $error;
224 $sum_charges += $hash{amount};
227 if ( $opt{e} and $is_e911{$hash{classname}} ) {
228 $e911_qty{$hash{custnum}} ||= 0;
229 $e911_qty{$hash{custnum}} += $hash{quantity};
235 # Order E911 packages
238 foreach my $custnum ( keys (%e911_qty) ) {
239 my $cust_main = $cust_main{$custnum};
240 my $quantity = $e911_qty{$custnum};
241 next if $quantity == 0;
242 my $cust_pkg = FS::cust_pkg->new({
245 start_date => $cust_main->next_bill_date,
246 quantity => $quantity,
248 my $error = $cust_main->order_pkg({ cust_pkg => $cust_pkg });
250 warn "Error creating e911 charge for customer $custnum: $error\n";
254 $num_lines += $quantity;
263 Processed files: @$files
264 Created charges: $num_charges
265 Sum of charges: \$".sprintf('%0.2f', $sum_charges)."
266 E911 charges: $num_e911
267 E911 lines: $num_lines
274 freeside-ipifony-download - Download and import invoice items from IPifony.
278 freeside-ipifony-download
285 freesideuser sftpuser@hostname[:path]
287 =head1 REQUIRED PARAMETERS
289 I<freesideuser>: the Freeside user to run as.
291 I<sftpuser>: the SFTP user to connect as. The 'freeside' system user should
292 have an authorization key to connect as that user.
294 I<hostname>: the SFTP server.
296 =head1 OPTIONAL PARAMETERS
300 -a I<archivedir>: Save a copy of the downloaded file to I<archivedir>.
302 -P I<port>: Connect to that TCP port.
304 -C I<category>: The name of a package category to use when creating package
307 -e I<pkgpart>: The pkgpart (L<FS::part_pkg>) to use for E911 charges. A
308 package of this type will be ordered for each invoice that has E911-subject
309 line items. The 'quantity' field on this package will be set to the total
310 quantity of those line items.
312 The E911 package must be a one-time package (flat rate, no frequency, no
313 recurring fee) with setup fee equal to the fee per line.