From 1ac4a177bd93ad7a97a45aacb66aa0bc9c23726b Mon Sep 17 00:00:00 2001 From: Jonathan Prykop Date: Sat, 28 Feb 2015 00:04:43 -0600 Subject: [PATCH] RT#30825: Modernize Bulk payment importing --- FS/FS/Record.pm | 14 ++++- FS/FS/cust_pay.pm | 87 +++++++++++++++++++++++++++++ FS/FS/payby.pm | 22 ++++++++ httemplate/elements/select-payby.html | 2 +- httemplate/misc/cust_pay-import.cgi | 52 ++++++++++++----- httemplate/misc/process/cust_pay-import.cgi | 24 +------- 6 files changed, 163 insertions(+), 38 deletions(-) diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 35ed6f7a8..0f3685bfa 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -1803,6 +1803,7 @@ sub process_batch_import { format_xml_formats => $opt->{format_xml_formats}, format_asn_formats => $opt->{format_asn_formats}, format_row_callbacks => $opt->{format_row_callbacks}, + format_hash_callbacks => $opt->{format_hash_callbacks}, #per-import job => $job, file => $file, @@ -1811,6 +1812,7 @@ sub process_batch_import { params => { map { $_ => $param->{$_} } @pass_params }, #? default_csv => $opt->{default_csv}, + preinsert_callback => $opt->{preinsert_callback}, postinsert_callback => $opt->{postinsert_callback}, insert_args_callback => $opt->{insert_args_callback}, ); @@ -1849,6 +1851,8 @@ Class method for batch imports. Available params: =item format_row_callbacks +=item format_hash_callbacks - After parsing, before object creation + =item fields - Alternate way to specify import, specifying import fields directly as a listref =item preinsert_callback @@ -1891,7 +1895,7 @@ sub batch_import { my( $type, $header, $sep_char, $fixedlength_format, $xml_format, $asn_format, - $parser_opt, $row_callback, @fields ); + $parser_opt, $row_callback, $hash_callback, @fields ); my $postinsert_callback = ''; $postinsert_callback = $param->{'postinsert_callback'} @@ -1947,6 +1951,11 @@ sub batch_import { ? $param->{'format_row_callbacks'}{ $param->{'format'} } : ''; + $hash_callback = + $param->{'format_hash_callbacks'} + ? $param->{'format_hash_callbacks'}{ $param->{'format'} } + : ''; + @fields = @{ $formats->{ $format } }; } elsif ( $param->{'fields'} ) { @@ -1956,6 +1965,7 @@ sub batch_import { $sep_char = ','; $fixedlength_format = ''; $row_callback = ''; + $hash_callback = ''; @fields = @{ $param->{'fields'} }; } else { @@ -2181,6 +2191,8 @@ sub batch_import { $hash{custnum} = $2; } + %hash = &{$hash_callback}(%hash) if $hash_callback; + #my $table = $param->{table}; my $class = "FS::$table"; diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm index 8274b3d7a..0a36aca5d 100644 --- a/FS/FS/cust_pay.pm +++ b/FS/FS/cust_pay.pm @@ -239,6 +239,12 @@ sub insert { $dbh->rollback if $oldAutoCommit; return "Unknown cust_bill.invnum: ". $self->invnum; }; + if ($self->custnum && ($cust_bill->custnum ne $self->custnum)) { + $dbh->rollback if $oldAutoCommit; + return "Invoice custnum ".$cust_bill->custnum + ." does not match specified custnum ".$self->custnum + ." for invoice ".$self->invnum; + } $self->custnum($cust_bill->custnum ); } @@ -1157,6 +1163,87 @@ sub process_upgrade_paybatch { =over 4 +=item process_batch_import + +=cut + +sub process_batch_import { + my $job = shift; + + #agent_custid isn't a cust_pay field, see hash callback + my $format = [ qw(custnum agent_custid paid payinfo invnum) ]; + my $hashcb = sub { + my %hash = @_; + my $custnum = $hash{'custnum'}; + my $agent_custid = $hash{'agent_custid'}; + #standardize date + $hash{'_date'} = parse_datetime($hash{'_date'}) + if $hash{'_date'} && $hash{'_date'} =~ /\D/; + # translate agent_custid into regular custnum + if ($custnum && $agent_custid) { + die "can't specify both custnum and agent_custid\n"; + } elsif ($agent_custid) { + # here is the agent virtualization + my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql; + my $agentnum = $hash{'agentnum'}; + my %search = ( + 'agent_custid' => $agent_custid, + 'agentnum' => $agentnum, + ); + my $cust_main = qsearchs({ + 'table' => 'cust_main', + 'hashref' => \%search, + 'extra_sql' => $extra_sql, + }); + die "can't find customer with agent_custid $agent_custid\n" + unless $cust_main; + $custnum = $cust_main->custnum; + } + #remove custnum_prefix + my $custnum_prefix = $conf->config('cust_main-custnum-display_prefix'); + my $custnum_length = $conf->config('cust_main-custnum-display_length') || 8; + if ( + $custnum_prefix + && $custnum =~ /^$custnum_prefix(0*([1-9]\d*))$/ + && length($1) == $custnum_length + ) { + $custnum = $2; + } + $hash{'custnum'} = $custnum; + delete($hash{'agent_custid'}); + return %hash; + }; + + my $opt = { 'table' => 'cust_pay', + 'params' => [ '_date', 'agentnum', 'payby', 'paybatch' ], + 'formats' => { + 'simple-csv' => $format, + 'simple-xls' => $format, + }, + 'format_types' => { + 'simple-csv' => 'csv', + 'simple-xls' => 'xls', + }, + 'default_csv' => 1, + 'format_hash_callbacks' => { + 'simple-csv' => $hashcb, + 'simple-xls' => $hashcb, + }, + 'postinsert_callback' => sub { + my $cust_pay = shift; + my $cust_main = $cust_pay->cust_main || + return "can't find customer to which payments apply"; + my $error = $cust_main->apply_payments_and_credits; + return $error + ? "can't apply payments to customer ".$cust_pay->custnum."$error" + : ''; + }, + }; + + FS::Record::process_batch_import( $job, $opt, @_ ); + +} + =item batch_import HASHREF Inserts new payments. diff --git a/FS/FS/payby.pm b/FS/FS/payby.pm index 13423c48f..5b4559d99 100644 --- a/FS/FS/payby.pm +++ b/FS/FS/payby.pm @@ -224,6 +224,28 @@ sub cust_payby2longname { map { $_ => $hash{$_}->{longname} } $self->cust_payby; } +=item payment_payby + +Returns all values of payby that can be used by payments. + +=cut + +sub payment_payby { + my $self = shift; + grep { ! exists $hash{$_}->{cust_pay} } $self->payby; +} + +=item payment_payby2longname + +Returns hash, keys are L types, values are payby longname. + +=cut + +sub payment_payby2longname { + my $self = shift; + map { $_ => $hash{$_}->{longname} } $self->payment_payby; +} + =back =head1 BUGS diff --git a/httemplate/elements/select-payby.html b/httemplate/elements/select-payby.html index b2d542190..2018874e7 100644 --- a/httemplate/elements/select-payby.html +++ b/httemplate/elements/select-payby.html @@ -3,7 +3,7 @@ <% $onchange %> > -% unless ( $opt{'multiple'} ) { +% unless ( $opt{'multiple'} || $opt{'no_all'} ) {