- # for internal use: takes another cust_main_county object, transfers
- # all existing references to this record to that one, and deletes this
- # one.
- my $record = shift;
- my $other = shift or die "record to merge into must be provided";
- my $new_taxnum = $other->taxnum;
- my $old_taxnum = $record->taxnum;
- if ($other->tax != $record->tax or
- $other->exempt_amount != $record->exempt_amount) {
- # don't assume these are the same.
- warn "Found duplicate taxes (#$new_taxnum and #$old_taxnum) but they have different rates and can't be merged.\n";
- } else {
- warn "Merging tax #$old_taxnum into #$new_taxnum\n";
- foreach my $table (qw(
- cust_bill_pkg_tax_location
- cust_bill_pkg_tax_location_void
- cust_tax_exempt_pkg
- cust_tax_exempt_pkg_void
- )) {
- foreach my $row (qsearch($table, { 'taxnum' => $old_taxnum })) {
- $row->set('taxnum' => $new_taxnum);
- my $error = $row->replace;
- die $error if $error;
- }
- }
- my $error = $record->delete;
- die $error if $error;
- }
-}
-
-=item process_edit_import
-
-=cut
-
-use Data::Dumper;
-sub process_edit_import {
- my $job = shift;
-
- my $opt = { 'table' => 'cust_main_county',
- 'params' => [], #required, apparantly
- 'formats' => { 'default' => [
- 'country',
- 'state',
- 'county',
- 'city',
- '', #tax class
- 'taxname',
- 'tax',
- 'old_tax', #old tax
- ] },
- 'format_headers' => { 'default' => 1, },
- 'format_types' => { 'default' => 'xls' },
- };
-
- #false laziness w/
- #FS::Record::process_batch_import( $job, $opt, @_ );
-
- my $table = $opt->{table};
- my @pass_params = @{ $opt->{params} };
- my %formats = %{ $opt->{formats} };
-
- my $param = 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 $error =
- #false laziness w/
- #FS::Record::batch_import( {
- FS::cust_main_county::edit_import( {
- #class-static
- table => $table,
- formats => \%formats,
- format_types => $opt->{format_types},
- format_headers => $opt->{format_headers},
- format_sep_chars => $opt->{format_sep_chars},
- format_fixedlength_formats => $opt->{format_fixedlength_formats},
- #per-import
- job => $job,
- file => $file,
- #type => $type,
- format => $param->{format},
- params => { map { $_ => $param->{$_} } @pass_params },
- #?
- default_csv => $opt->{default_csv},
- } );
-
- unlink $file;
-
- die "$error\n" if $error;
-
-}
-
-=item edit_import
-
-=cut
-
-#false laziness w/ #FS::Record::batch_import, grep "edit_import" for differences
-#could be turned into callbacks or something
-use Text::CSV_XS;
-sub edit_import {
- my $param = shift;
-
- warn "$me edit_import call with params: \n". Dumper($param)
- if $DEBUG;
-
- my $table = $param->{table};
- my $formats = $param->{formats};
-
- my $job = $param->{job};
- my $file = $param->{file};
- my $format = $param->{'format'};
- my $params = $param->{params} || {};
-
- die "unknown format $format" unless exists $formats->{ $format };
-
- my $type = $param->{'format_types'}
- ? $param->{'format_types'}{ $format }
- : $param->{type} || 'csv';
-
- unless ( $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';
- }
- $type = 'csv'
- if $param->{'default_csv'} && $type ne 'xls';
- }
-
- my $header = $param->{'format_headers'}
- ? $param->{'format_headers'}{ $param->{'format'} }
- : 0;
-
- my $sep_char = $param->{'format_sep_chars'}
- ? $param->{'format_sep_chars'}{ $param->{'format'} }
- : ',';
-
- my $fixedlength_format =
- $param->{'format_fixedlength_formats'}
- ? $param->{'format_fixedlength_formats'}{ $param->{'format'} }
- : '';
-
- my @fields = @{ $formats->{ $format } };
-
- my $row = 0;
- my $count;
- my $parser;
- my @buffer = ();
- my @header = (); #edit_import
- if ( $type eq 'csv' || $type eq 'fixedlength' ) {
-
- if ( $type eq 'csv' ) {
-
- my %attr = ();
- $attr{sep_char} = $sep_char if $sep_char;
- $parser = new Text::CSV_XS \%attr;
-
- } elsif ( $type eq 'fixedlength' ) {
-
- eval "use Parse::FixedLength;";
- die $@ if $@;
- $parser = new Parse::FixedLength $fixedlength_format;
-
- } else {
- die "Unknown file type $type\n";
- }
-
- @buffer = split(/\r?\n/, slurp($file) );
- splice(@buffer, 0, ($header || 0) );
- $count = scalar(@buffer);
-
- } elsif ( $type eq 'xls' ) {
-
- eval "use Spreadsheet::ParseExcel;";
- die $@ if $@;
-
- eval "use DateTime::Format::Excel;";
- #for now, just let the error be thrown if it is used, since only CDR
- # formats bill_west and troop use it, not other excel-parsing things
- #die $@ if $@;
-
- my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($file);
-
- $parser = $excel->{Worksheet}[0]; #first sheet
-
- $count = $parser->{MaxRow} || $parser->{MinRow};
- $count++;
-
- $row = $header || 0;
-
- #edit_import - need some magic to parse the header
- if ( $header ) {
- my @header_row = @{ $parser->{Cells}[$0] };
- @header = map $_->{Val}, @header_row;
- }
-
- } else {
- die "Unknown file type $type\n";
+ # For internal use:
+ #
+ # When given two cust_main_county row objects, rewrite all database foreign
+ # key references referring to $row_to_merge->taxnum as references to
+ # $row_to_keep->taxnum, so $row_to_merge can be safely deleted from
+ # cust_main_county
+ #
+ # Usage (class method):
+ # $row_to_merge->_merge_into( $row_to_keep )
+ #
+ # Usage (package function):
+ # FS::cust_main_county::_merge_into( $row_to_merge, $row_to_keep )
+ #
+ # Optionally, allow merge when records don't match
+ # (useful during tax table update routines)
+ # $row_to_merge->_merge_info(
+ # $row_to_keep,
+ # { identical_record_check => 0 }
+ # );
+
+ my $row_to_merge = shift;
+ my $row_to_keep = shift
+ or croak 'record to merge into must be provided';
+
+ my $args = shift || { identical_record_check => 1 };
+ croak 'invalid arguments hashref' unless ref $args;
+
+ my $log = FS::Log->new('FS::cust_main_county');
+
+ my $keep_taxnum = $row_to_keep->taxnum;
+ my $merge_taxnum = $row_to_merge->taxnum;
+
+ if (
+ $args->{identical_record_check}
+ && (
+ $row_to_keep->tax != $row_to_merge->tax
+ || $row_to_keep->exempt_amount != $row_to_merge->exempt_amount
+ )
+ ) {
+ my $msg = "Found duplicate taxes (#$keep_taxnum and #$merge_taxnum) "
+ . "but they have different rates and can't be merged.";
+ $log->warn( $msg );
+ warn "$msg\n";
+ return;