[freeside-commits] freeside/FS/FS Misc.pm, 1.31, 1.32 Schema.pm, 1.113, 1.114 cust_tax_location.pm, 1.2, 1.3 part_pkg_taxrate.pm, 1.4, 1.5 tax_class.pm, 1.3, 1.4 tax_rate.pm, 1.10, 1.11
Jeff Finucane,420,,
jeff at wavetail.420.am
Wed Nov 19 06:56:02 PST 2008
Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv1284/FS/FS
Modified Files:
Misc.pm Schema.pm cust_tax_location.pm part_pkg_taxrate.pm
tax_class.pm tax_rate.pm
Log Message:
support for cch fixed format
Index: tax_rate.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/tax_rate.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- tax_rate.pm 30 Aug 2008 02:10:08 -0000 1.10
+++ tax_rate.pm 19 Nov 2008 14:55:59 -0000 1.11
@@ -13,6 +13,7 @@
use FS::cust_tax_location;
use FS::part_pkg_taxrate;
use FS::cust_main;
+use FS::Misc qw( csv_from_fixed );
@ISA = qw( FS::Record );
@@ -503,12 +504,25 @@
my @fields;
my $hook;
+ my @column_lengths = ();
+ my @column_callbacks = ();
+ if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+ $format =~ s/-fixed//;
+ my $date_format = sub { my $r='';
+ /^(\d{4})(\d{2})(\d{2})$/ && ($r="$1/$2/$3");
+ $r;
+ };
+ $column_callbacks[8] = $date_format;
+ push @column_lengths, qw( 10 1 1 8 8 5 8 8 8 1 2 2 30 8 8 10 2 8 2 1 2 2 );
+ push @column_lengths, 1 if $format eq 'cch-update';
+ }
+
my $line;
my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
- if ( $job ) {
- $count++
- while ( defined($line=<$fh>) );
- seek $fh, 0, 0;
+ if ( $job || scalar(@column_callbacks) ) {
+ my $error =
+ csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
+ return $error if $error;
}
$count *=2;
@@ -745,7 +759,7 @@
my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
- if ($format eq 'cch') {
+ if ($format eq 'cch' || $format eq 'cch-fixed') {
my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
@@ -779,7 +793,7 @@
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
}
- }elsif ($format eq 'cch-update') {
+ }elsif ($format eq 'cch-update' || $format eq 'cch-fixed-update') {
my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
Index: Misc.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Misc.pm,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- Misc.pm 4 Oct 2008 22:35:58 -0000 1.31
+++ Misc.pm 19 Nov 2008 14:55:58 -0000 1.32
@@ -7,6 +7,7 @@
use Data::Dumper;
use IPC::Run qw( run timeout ); # for _pslatex
use IPC::Run3; # for do_print... should just use IPC::Run i guess
+use File::Temp;
#do NOT depend on any FS:: modules here, causes weird (sometimes unreproducable
#until on client machine) dependancy loops. put them in FS::Misc::Something
#instead
@@ -16,6 +17,7 @@
states_hash counties state_label
card_types
generate_ps generate_pdf do_print
+ csv_from_fixed
);
$DEBUG = 0;
@@ -774,6 +776,65 @@
}
+=item csv_from_fixed, FILEREF COUNTREF, [ LENGTH_LISTREF, [ CALLBACKS_LISTREF ] ]
+
+Converts the filehandle referenced by FILEREF from fixed length record
+lines to a CSV file according to the lengths specified in LENGTH_LISTREF.
+The CALLBACKS_LISTREF refers to a correpsonding list of coderefs. Each
+should return the value to be substituted in place of its single argument.
+
+Returns false on success or an error if one occurs.
+
+=cut
+
+sub csv_from_fixed {
+ my( $fhref, $countref, $lengths, $callbacks) = @_;
+
+ eval { require Text::CSV_XS; };
+ return $@ if $@;
+
+ my $ofh = $$fhref;
+ my $unpacker = new Text::CSV_XS;
+ my $total = 0;
+ my $template = join('', map {$total += $_; "A$_"} @$lengths) if $lengths;
+
+ my $dir = "%%%FREESIDE_CACHE%%%/cache.$FS::UID::datasrc";
+ my $fh = new File::Temp( TEMPLATE => "CODE.csv.XXXXXXXX",
+ DIR => $dir,
+ UNLINK => 0,
+ ) or return "can't open temp file: $!\n"
+ if $template;
+
+ while ( defined(my $line=<$ofh>) ) {
+ $$countref++;
+ if ( $template ) {
+ my $column = 0;
+
+ chomp $line;
+ return "unexpected input at line $$countref: $line".
+ " -- expected $total but received ". length($line)
+ unless length($line) == $total;
+
+ $unpacker->combine( map { my $i = $column++;
+ defined( $callbacks->[$i] )
+ ? &{ $callbacks->[$i] }( $_ )
+ : $_
+ } unpack( $template, $line )
+ )
+ or return "invalid data for CSV: ". $unpacker->error_input;
+
+ print $fh $unpacker->string(), "\n"
+ or return "can't write temp file: $!\n";
+ }
+ }
+
+ if ( $template ) { close $$fhref; $$fhref = $fh }
+
+ seek $$fhref, 0, 0;
+ '';
+}
+
+
=back
=head1 BUGS
Index: cust_tax_location.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_tax_location.pm,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- cust_tax_location.pm 15 Apr 2008 20:47:59 -0000 1.2
+++ cust_tax_location.pm 19 Nov 2008 14:55:59 -0000 1.3
@@ -3,6 +3,7 @@
use strict;
use vars qw( @ISA );
use FS::Record qw( qsearch qsearchs dbh );
+use FS::Misc qw ( csv_from_fixed );
@ISA = qw(FS::Record);
@@ -135,12 +136,19 @@
my @fields;
my $hook;
+ my @column_lengths = ();
+ my @column_callbacks = ();
+ if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+ $format =~ s/-fixed//;
+ push @column_lengths, qw( 5 2 4 4 10 1 );
+ push @column_lengths, 1 if $format eq 'cch-update';
+ }
+
my $line;
my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
- if ( $job ) {
- $count++
- while ( defined($line=<$fh>) );
- seek $fh, 0, 0;
+ if ( $job || scalar(@column_callbacks) ) {
+ my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
+ return $error if $error;
}
if ( $format eq 'cch' || $format eq 'cch-update' ) {
Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.113
retrieving revision 1.114
diff -u -d -r1.113 -r1.114
--- Schema.pm 19 Nov 2008 01:24:24 -0000 1.113
+++ Schema.pm 19 Nov 2008 14:55:58 -0000 1.114
@@ -1110,7 +1110,7 @@
'taxproductnum', 'serial', '', '', '', '',
'data_vendor', 'varchar', 'NULL', $char_d, '', '',
'taxproduct', 'varchar', '', $char_d, '', '',
- 'description', 'varchar', '', 2*$char_d, '', '',
+ 'description', 'varchar', '', 3*$char_d, '', '',
],
'primary_key' => 'taxproductnum',
'unique' => [ [ 'data_vendor', 'taxproduct' ] ],
Index: tax_class.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/tax_class.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- tax_class.pm 15 Aug 2008 01:09:45 -0000 1.3
+++ tax_class.pm 19 Nov 2008 14:55:59 -0000 1.4
@@ -4,6 +4,7 @@
use vars qw( @ISA );
use FS::UID qw(dbh);
use FS::Record qw( qsearch qsearchs );
+use FS::Misc qw( csv_from_fixed );
@ISA = qw(FS::Record);
@@ -147,12 +148,19 @@
my $imported = 0;
my $dbh = dbh;
+ my @column_lengths = ();
+ my @column_callbacks = ();
+ if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+ $format =~ s/-fixed//;
+ push @column_lengths, qw( 8 10 3 2 2 10 100 );
+ push @column_lengths, 1 if $format eq 'cch-update';
+ }
+
my $line;
my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
- if ( $job ) {
- $count++
- while ( defined($line=<$fh>) );
- seek $fh, 0, 0;
+ if ( $job || scalar(@column_callbacks) ) {
+ my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
+ return $error if $error;
}
if ( $format eq 'cch' || $format eq 'cch-update' ) {
Index: part_pkg_taxrate.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg_taxrate.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- part_pkg_taxrate.pm 23 Aug 2008 03:29:18 -0000 1.4
+++ part_pkg_taxrate.pm 19 Nov 2008 14:55:59 -0000 1.5
@@ -6,6 +6,7 @@
use FS::UID qw(dbh);
use FS::Record qw( qsearch qsearchs );
use FS::part_pkg_taxproduct;
+use FS::Misc qw(csv_from_fixed);
@ISA = qw(FS::Record);
@@ -175,12 +176,25 @@
my @fields;
my $hook;
+ my @column_lengths = ();
+ my @column_callbacks = ();
+ if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+ $format =~ s/-fixed//;
+ my $date_format = sub { my $r='';
+ /^(\d{4})(\d{2})(\d{2})$/ && ($r="$1/$2/$3");
+ $r;
+ };
+ $column_callbacks[16] = $date_format;
+ push @column_lengths, qw( 28 25 2 1 10 4 30 3 100 2 2 2 2 1 2 2 8 1 );
+ push @column_lengths, 1 if $format eq 'cch-update';
+ }
+
my $line;
my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
- if ( $job ) {
- $count++
- while ( defined($line=<$fh>) );
- seek $fh, 0, 0;
+ if ( $job || scalar(@column_callbacks) ) {
+ my $error =
+ csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
+ return $error if $error;
}
if ( $format eq 'cch' || $format eq 'cch-update' ) {
More information about the freeside-commits
mailing list