use Date::Format;
use Date::Parse;
#use Date::Manip;
+use File::Slurp qw( slurp );
+use File::Temp qw( tempfile );
use String::Approx qw(amatch);
use Business::CreditCard 0.28;
use Locale::Country;
use FS::cust_refund;
use FS::part_referral;
use FS::cust_main_county;
+use FS::cust_tax_location;
use FS::agent;
use FS::cust_main_invoice;
use FS::cust_credit_bill;
=item spool_cdr - Enable individual CDR spooling, empty or `Y'
+=item squelch_cdr - Discourage individual CDR printing, empty or `Y'
+
=back
=head1 METHODS
|| $self->ut_number('agentnum')
|| $self->ut_textn('agent_custid')
|| $self->ut_number('refnum')
+ || $self->ut_textn('custbatch')
|| $self->ut_name('last')
|| $self->ut_name('first')
|| $self->ut_snumbern('birthdate')
$self->payname($1);
}
- foreach my $flag (qw( tax spool_cdr )) {
+ foreach my $flag (qw( tax spool_cdr squelch_cdr )) {
$self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
$self->$flag($1);
}
sub bill {
my( $self, %options ) = @_;
return '' if $self->payby eq 'COMP';
+ local $DEBUG = 1;
warn "$me bill customer ". $self->custnum. "\n"
if $DEBUG;
$self->select_for_update; #mutex
my @cust_bill_pkg = ();
- my @appended_cust_bill_pkg = ();
###
# find the packages which are due for billing, find out how much they are
my $real_pkgpart = $cust_pkg->pkgpart;
my %hash = $cust_pkg->hash;
- my $old_cust_pkg = new FS::cust_pkg \%hash;
foreach my $part_pkg ( $cust_pkg->part_pkg->self_and_bill_linked ) {
+
+ $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
+
my $error =
$self->_make_lines( 'part_pkg' => $part_pkg,
'cust_pkg' => $cust_pkg,
'precommit_hooks' => \@precommit_hooks,
'line_items' => \@cust_bill_pkg,
- 'appended_line_items' => \@appended_cust_bill_pkg,
'setup' => \$total_setup,
'recur' => \$total_recur,
'tax_matrix' => \%taxlisthash,
} #foreach my $cust_pkg
- push @cust_bill_pkg, @appended_cust_bill_pkg;
-
unless ( @cust_bill_pkg ) { #don't create an invoice w/o line items
#but do commit any package date cycling that happened
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
'cust_pkg' => $postal_pkg,
'precommit_hooks' => \@precommit_hooks,
'line_items' => \@cust_bill_pkg,
- 'appended_line_items' => \@appended_cust_bill_pkg,
'setup' => \$total_setup,
'recur' => \$total_recur,
'tax_matrix' => \%taxlisthash,
my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
- my $appended_cust_bill_pkg = $params{appended_line_items}
- or die "no appended line buffer specified";
my $total_setup = $params{setup} or die "no setup accumulator specified";
my $total_recur = $params{recur} or die "no recur accumulator specified";
my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
my %hash = $cust_pkg->hash;
my $old_cust_pkg = new FS::cust_pkg \%hash;
- $cust_pkg->pkgpart($part_pkg->pkgpart);
- $cust_pkg->set($_, $hash{$_}) foreach qw( setup last_bill bill );
-
my @details = ();
my $lineitems = 0;
+ $cust_pkg->pkgpart($part_pkg->pkgpart);
+
###
# bill setup
###
warn " charges (setup=$setup, recur=$recur); adding line items\n"
if $DEBUG > 1;
+
my $cust_bill_pkg = new FS::cust_bill_pkg {
'pkgnum' => $cust_pkg->pkgnum,
'setup' => $setup,
};
$cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
unless $part_pkg->pkgpart == $real_pkgpart;
- push @$cust_bill_pkgs, $cust_bill_pkg;
$$total_setup += $setup;
$$total_recur += $recur;
# handle taxes
###
- unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
+ my $err_or_cust_bill_pkg =
+ $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg);
- $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
+ return $err_or_cust_bill_pkg
+ unless ( ref($err_or_cust_bill_pkg) );
- } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
+ push @$cust_bill_pkgs, @$err_or_cust_bill_pkg;
} #if $setup != 0 || $recur != 0
} #if $line_items
- if ( $part_pkg->can('append_cust_bill_pkgs') ) {
- my %param = ( 'precommit_hooks' => $precommit_hooks, );
- my ($more_cust_bill_pkgs) =
- eval { $part_pkg->append_cust_bill_pkgs( $cust_pkg, \$sdate, \%param ) };
-
- return "$@ running append_cust_bill_pkgs for $cust_pkg\n"
- if ( $@ );
- return "$more_cust_bill_pkgs"
- unless ( ref($more_cust_bill_pkgs) );
-
- foreach my $cust_bill_pkg ( @{$more_cust_bill_pkgs} ) {
-
- $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
- unless $part_pkg->pkgpart == $real_pkgpart;
- push @$appended_cust_bill_pkg, $cust_bill_pkg;
-
- $$total_setup += $cust_bill_pkg->setup;
- $$total_recur += $cust_bill_pkg->recur;
-
- ###
- # handle taxes
- ###
-
- unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
-
- $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
-
- } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
- }
- }
+ '';
}
my $part_pkg = shift;
my $taxlisthash = shift;
my $cust_bill_pkg = shift;
+ my $cust_pkg = shift;
- my @taxes = ();
- my @taxoverrides = $part_pkg->part_pkg_taxoverride;
+ my %cust_bill_pkg = ();
+ my %taxes = ();
my $prefix =
( $conf->exists('tax-ship_address') && length($self->ship_last) )
? 'ship_'
: '';
+ my @classes;
+ #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
+ push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
+ push @classes, 'setup' if $cust_bill_pkg->setup;
+ push @classes, 'recur' if $cust_bill_pkg->recur;
+
if ( $conf->exists('enable_taxproducts')
- && (scalar(@taxoverrides) || $part_pkg->taxproductnum )
+ && (scalar($part_pkg->part_pkg_taxoverride) || $part_pkg->has_taxproduct)
+ && ( $self->tax !~ /Y/i && $self->payby ne 'COMP' )
)
{
- my @taxclassnums = ();
- my $geocode = $self->geocode('cch');
-
- if ( scalar( @taxoverrides ) ) {
- @taxclassnums = map { $_->taxclassnum } @taxoverrides;
- }elsif ( $part_pkg->taxproductnum ) {
- @taxclassnums = map { $_->taxclassnum }
- $part_pkg->part_pkg_taxrate('cch', $geocode);
+ foreach my $class (@classes) {
+ my $err_or_ref = $self->_gather_taxes( $part_pkg, $class, $prefix );
+ return $err_or_ref unless ref($err_or_ref);
+ $taxes{$class} = $err_or_ref;
}
- my $extra_sql =
- "AND (".
- join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
-
- @taxes = qsearch({ 'table' => 'tax_rate',
- 'hashref' => { 'geocode' => $geocode, },
- 'extra_sql' => $extra_sql,
- })
- if scalar(@taxclassnums);
-
+ unless (exists $taxes{''}) {
+ my $err_or_ref = $self->_gather_taxes( $part_pkg, '', $prefix );
+ return $err_or_ref unless ref($err_or_ref);
+ $taxes{''} = $err_or_ref;
+ }
- }else{
+ }elsif ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
my %taxhash = map { $_ => $self->get("$prefix$_") }
qw( state county country );
$taxhash{'taxclass'} = $part_pkg->taxclass;
- @taxes = qsearch( 'cust_main_county', \%taxhash );
+ my @taxes = qsearch( 'cust_main_county', \%taxhash );
unless ( @taxes ) {
$taxhash{'taxclass'} = '';
@taxes = qsearch( 'cust_main_county', \%taxhash );
}
- } #if $conf->exists('enable_taxproducts')
+ $taxes{''} = [ @taxes ];
+ $taxes{'setup'} = [ @taxes ];
+ $taxes{'recur'} = [ @taxes ];
+ $taxes{$_} = [ @taxes ] foreach (@classes);
- # maybe eliminate this entirely, along with all the 0% records
- unless ( @taxes ) {
- my $error;
- if ( $conf->exists('enable_taxproducts') ) {
- $error =
- "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
- join('/', ( map $self->get("$prefix$_"),
- qw(zip)
- ),
- $part_pkg->taxproduct_description,
- $part_pkg->pkgpart ). "\n";
- } else {
- $error =
+ # maybe eliminate this entirely, along with all the 0% records
+ unless ( @taxes ) {
+ return
"fatal: can't find tax rate for state/county/country/taxclass ".
join('/', ( map $self->get("$prefix$_"),
qw(state county country)
),
$part_pkg->taxclass ). "\n";
}
- return $error;
+
+ } #if $conf->exists('enable_taxproducts') ...
+
+ my $section = $cust_pkg->part_pkg->option('usage_section', 'Hush!')
+ if $cust_pkg->part_pkg->option('separate_usage');
+ my $want_duplicate =
+ $cust_pkg->part_pkg->option('summarize_usage', 'Hush!') &&
+ $cust_pkg->part_pkg->option('usage_section', 'Hush!');
+
+ # XXX this mostly goes away with cust_bill_pkg refactor
+
+ $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
+ $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
+
+ #split setup and recur
+ if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
+ my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
+ $cust_bill_pkg->set('details', []);
+ $cust_bill_pkg->recur(0);
+ $cust_bill_pkg->unitrecur(0);
+ $cust_bill_pkg->type('');
+ $cust_bill_pkg_recur->setup(0);
+ $cust_bill_pkg_recur->unitsetup(0);
+ $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
+ }
+
+ #split usage from recur
+ my $usage = sprintf( "%.2f", $cust_bill_pkg{recur}->usage );
+ warn "usage is $usage\n" if $DEBUG;
+ if ($usage) {
+ my $cust_bill_pkg_usage =
+ new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
+ $cust_bill_pkg_usage->recur( $usage );
+ $cust_bill_pkg_usage->type( 'U' );
+ $cust_bill_pkg_usage->duplicate( $want_duplicate ? 'Y' : '' );
+ $cust_bill_pkg_usage->section( $section );
+ $cust_bill_pkg_usage->post_total( $want_duplicate ? 'Y' : '' );
+ my $recur = sprintf( "%.2f", $cust_bill_pkg{recur}->recur - $usage );
+ $cust_bill_pkg{recur}->recur( $recur );
+ $cust_bill_pkg{recur}->type( '' );
+ $cust_bill_pkg{recur}->set('details', []);
+ $cust_bill_pkg{''} = $cust_bill_pkg_usage;
+ }
+
+ #subdivide usage by usage_class
+ if (exists($cust_bill_pkg{''})) {
+ foreach my $class (grep {$_ && $_ ne 'setup' && $_ ne 'recur' } @classes) {
+ my $usage = sprintf( "%.2f", $cust_bill_pkg{''}->usage($class) );
+ my $cust_bill_pkg_usage =
+ new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
+ $cust_bill_pkg_usage->recur( $usage );
+ $cust_bill_pkg_usage->set('details', []);
+ my $classless = sprintf( "%.2f", $cust_bill_pkg{''}->recur - $usage );
+ $cust_bill_pkg{''}->recur( $classless );
+ $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
+ }
+ delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
}
- foreach my $tax ( @taxes ) {
- my $taxname = ref( $tax ). ' '. $tax->taxnum;
- if ( exists( $taxlisthash->{ $taxname } ) ) {
- push @{ $taxlisthash->{ $taxname } }, $cust_bill_pkg;
- }else{
- $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
+ foreach my $key (keys %cust_bill_pkg) {
+ my @taxes = @{ $taxes{$key} };
+ my $cust_bill_pkg = $cust_bill_pkg{$key};
+
+ foreach my $tax ( @taxes ) {
+ my $taxname = ref( $tax ). ' '. $tax->taxnum;
+ if ( exists( $taxlisthash->{ $taxname } ) ) {
+ push @{ $taxlisthash->{ $taxname } }, $cust_bill_pkg;
+ }else{
+ $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
+ }
}
}
+ # sort setup,recur,'', and the rest numeric && return
+ my @result = map { $cust_bill_pkg{$_} }
+ sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
+ ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
+ }
+ keys %cust_bill_pkg;
+
+ \@result;
+}
+
+sub _gather_taxes {
+ my $self = shift;
+ my $part_pkg = shift;
+ my $class = shift;
+ my $prefix = shift;
+
+ my @taxes = ();
+ my $geocode = $self->geocode('cch');
+
+ my @taxclassnums = map { $_->taxclassnum }
+ $part_pkg->part_pkg_taxoverride($class);
+
+ unless (@taxclassnums) {
+ @taxclassnums = map { $_->taxclassnum }
+ $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
+ }
+ warn "Found taxclassnum values of ". join(',', @taxclassnums)
+ if $DEBUG;
+
+ my $extra_sql =
+ "AND (".
+ join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
+
+ @taxes = qsearch({ 'table' => 'tax_rate',
+ 'hashref' => { 'geocode' => $geocode, },
+ 'extra_sql' => $extra_sql,
+ })
+ if scalar(@taxclassnums);
+
+ # maybe eliminate this entirely, along with all the 0% records
+ unless ( @taxes ) {
+ return
+ "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
+ join('/', ( map $self->get("$prefix$_"),
+ qw(zip)
+ ),
+ $part_pkg->taxproduct_description,
+ $part_pkg->pkgpart ). "\n";
+ }
+
+ warn "Found taxes ".
+ join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n"
+ if $DEBUG;
+
+ [ @taxes ];
+
}
=item collect OPTIONS
sub charge {
my $self = shift;
my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
+ my ( $taxproduct, $override );
if ( ref( $_[0] ) ) {
$amount = $_[0]->{amount};
$quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
$taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
$classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
$additional = $_[0]->{additional};
+ $taxproduct = $_[0]->{taxproductnum};
+ $override = { '' => $_[0]->{tax_override} };
}else{
$amount = shift;
$quantity = 1;
my $dbh = dbh;
my $part_pkg = new FS::part_pkg ( {
- 'pkg' => $pkg,
- 'comment' => $comment,
- 'plan' => 'flat',
- 'freq' => 0,
- 'disabled' => 'Y',
- 'classnum' => $classnum ? $classnum : '',
- 'taxclass' => $taxclass,
+ 'pkg' => $pkg,
+ 'comment' => $comment,
+ 'plan' => 'flat',
+ 'freq' => 0,
+ 'disabled' => 'Y',
+ 'classnum' => $classnum ? $classnum : '',
+ 'taxclass' => $taxclass,
+ 'taxproductnum' => $taxproduct,
} );
my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
'setup_fee' => $amount,
);
- my $error = $part_pkg->insert( options => \%options );
+ my $error = $part_pkg->insert( options => \%options,
+ tax_overrides => $override,
+ );
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;
@{ $params->{'current_balance'} };
##
+ # custbatch
+ ##
+
+ if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
+ push @where,
+ "cust_main.custbatch = '$1'";
+ }
+
+ ##
# setup queries, subs, etc. for the search
##
1;
}
+=item process_batch_import
+
+Load a batch import as a queued JSRPC job
+
+=cut
+
+use Storable qw(thaw);
+use Data::Dumper;
+use MIME::Base64;
+sub process_batch_import {
+ my $job = shift;
+
+ my $param = thaw(decode_base64(shift));
+ warn Dumper($param) if $DEBUG;
+
+ my $files = $param->{'uploaded_files'}
+ or die "No files provided.\n";
+
+ my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
+
+ my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
+ my $file = $dir. $files{'file'};
+
+ my $type;
+ if ( $file =~ /\.(\w+)$/i ) {
+ $type = lc($1);
+ } else {
+ #or error out???
+ warn "can't parse file type from filename $file; defaulting to CSV";
+ $type = 'csv';
+ }
+
+ my $error =
+ FS::cust_main::batch_import( {
+ job => $job,
+ file => $file,
+ type => $type,
+ custbatch => $param->{custbatch},
+ agentnum => $param->{'agentnum'},
+ refnum => $param->{'refnum'},
+ pkgpart => $param->{'pkgpart'},
+ #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
+ # city state zip comments )],
+ 'format' => $param->{'format'},
+ } );
+
+ unlink $file;
+
+ die "$error\n" if $error;
+
+}
+
=item batch_import
=cut
+#some false laziness w/cdr.pm now
sub batch_import {
my $param = shift;
- #warn join('-',keys %$param);
- my $fh = $param->{filehandle};
- my $agentnum = $param->{agentnum};
- my $refnum = $param->{refnum};
- my $pkgpart = $param->{pkgpart};
+ my $job = $param->{job};
+
+ my $filename = $param->{file};
+ my $type = $param->{type} || 'csv';
+
+ my $custbatch = $param->{custbatch};
+
+ my $agentnum = $param->{agentnum};
+ my $refnum = $param->{refnum};
+ my $pkgpart = $param->{pkgpart};
+
+ my $format = $param->{'format'};
- #my @fields = @{$param->{fields}};
- my $format = $param->{'format'};
my @fields;
my $payby;
if ( $format eq 'simple' ) {
die "unknown format $format";
}
- eval "use Text::CSV_XS;";
- die $@ if $@;
+ my $count;
+ my $parser;
+ my @buffer = ();
+ if ( $type eq 'csv' ) {
- my $csv = new Text::CSV_XS;
- #warn $csv;
- #warn $fh;
+ eval "use Text::CSV_XS;";
+ die $@ if $@;
+
+ $parser = new Text::CSV_XS;
+
+ @buffer = split(/\r?\n/, slurp($filename) );
+ $count = scalar(@buffer);
+
+ } elsif ( $type eq 'xls' ) {
+
+ eval "use Spreadsheet::ParseExcel;";
+ die $@ if $@;
+
+ my $excel = new Spreadsheet::ParseExcel::Workbook->Parse($filename);
+ $parser = $excel->{Worksheet}[0]; #first sheet
+
+ $count = $parser->{MaxRow} || $parser->{MinRow};
+ $count++;
+
+ } else {
+ die "Unknown file type $type\n";
+ }
- my $imported = 0;
#my $columns;
local $SIG{HUP} = 'IGNORE';
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
- #while ( $columns = $csv->getline($fh) ) {
my $line;
- while ( defined($line=<$fh>) ) {
+ my $row = 0;
+ my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
+ while (1) {
- $csv->parse($line) or do {
- $dbh->rollback if $oldAutoCommit;
- return "can't parse: ". $csv->error_input();
- };
+ my @columns = ();
+ if ( $type eq 'csv' ) {
+
+ last unless scalar(@buffer);
+ $line = shift(@buffer);
+
+ $parser->parse($line) or do {
+ $dbh->rollback if $oldAutoCommit;
+ return "can't parse: ". $parser->error_input();
+ };
+ @columns = $parser->fields();
+
+ } elsif ( $type eq 'xls' ) {
+
+ last if $row > ($parser->{MaxRow} || $parser->{MinRow});
+
+ my @row = @{ $parser->{Cells}[$row] };
+ @columns = map $_->{Val}, @row;
+
+ #my $z = 'A';
+ #warn $z++. ": $_\n" for @columns;
+
+ } else {
+ die "Unknown file type $type\n";
+ }
- my @columns = $csv->fields();
#warn join('-',@columns);
my %cust_main = (
- agentnum => $agentnum,
- refnum => $refnum,
- country => $conf->config('countrydefault') || 'US',
- payby => $payby, #default
- paydate => '12/2037', #default
+ custbatch => $custbatch,
+ agentnum => $agentnum,
+ refnum => $refnum,
+ country => $conf->config('countrydefault') || 'US',
+ payby => $payby, #default
+ paydate => '12/2037', #default
);
my $billtime = time;
my %cust_pkg = ( pkgpart => $pkgpart );
$columns[0] = $part_referral->refnum;
}
- #$cust_main{$field} = shift @$columns;
- $cust_main{$field} = shift @columns;
+ my $value = shift @columns;
+ $cust_main{$field} = $value if length($value);
}
}
- $cust_main{'payby'} = 'CARD' if length($cust_main{'payinfo'});
+ $cust_main{'payby'} = 'CARD'
+ if defined $cust_main{'payinfo'}
+ && length $cust_main{'payinfo'};
my $invoicing_list = $cust_main{'invoicing_list'}
? [ delete $cust_main{'invoicing_list'} ]
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
- return "can't insert customer for $line: $error";
+ return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
}
if ( $format eq 'simple' ) {
}
- $imported++;
+ $row++;
+
+ if ( $job && time - $min_sec > $last ) { #progress bar
+ $job->update_statustext( int(100 * $row / $count) );
+ $last = time;
+ }
+
}
- $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
- return "Empty file!" unless $imported;
+ return "Empty file!" unless $row;
''; #no error