#!/usr/bin/perl -w my $RCS_Id = '$Id$ '; # Author : Johan Vromans # Created On : Fri Jun 17 21:31:52 2005 # Last Modified By: Johan Vromans # Last Modified On: Mon Jan 30 14:39:57 2006 # Update Count : 101 # Status : Unknown, Use with caution! ################ Common stuff ################ use strict; # Package or program libraries, if appropriate. # $LIBDIR = $ENV{'LIBDIR'} || '/usr/local/lib/sample'; # use lib qw($LIBDIR); # require 'common.pl'; # Package name. my $my_package = 'EekBoek'; # Program name and version. my ($my_name, $my_version) = $RCS_Id =~ /: (.+).pl,v ([\d.]+)/; # Tack '*' if it is not checked in into RCS. $my_version .= '*' if length('$Locker$ ') > 12; ################ Command line parameters ################ use Getopt::Long 2.13; # Command line options. my $all = 0; # all my $verbose = 0; # verbose processing my $ac5 = 0; # DaviDOS compatibility # Development options (not shown with -help). my $debug = 0; # debugging my $trace = 0; # trace (show process) my $test = 0; # test mode. # Process command line options. app_options(); # Post-processing. $trace |= ($debug || $test); ################ Presets ################ my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP} || '/usr/tmp'; ################ The Process ################ use Text::CSV_XS; use EB::Config qw(EekBoek); use EB::Globals; unless ( @ARGV ) { foreach ( "DEBITR.CSV", "CREDIT.CSV" ) { if ( -s $_ ) { push(@ARGV, $_); } elsif ( -s lc($_) ) { push(@ARGV, lc($_)); } } } # Load field names from __DATA__. my @debfieldnames; my @crdfieldnames; my $fieldnames = \@debfieldnames; while ( ) { next if /^#/; $fieldnames = \@crdfieldnames, next unless /\S/; my @a = split(/\t/); push(@$fieldnames, $a[1]); } # Load maps, if provided. my $crdmap = -s "crdmap.pl" ? require "crdmap.pl" : {}; my $debmap = -s "debmap.pl" ? require "debmap.pl" : {} ; # Find out which codes are actually used. my %used; my $csv = new Text::CSV_XS ({binary => 1}); my $db; if ( open($db, "fmuta6.csv") || open($db, "FMUTA6.CSV") ) { my $mut; while ( <$db> ) { s/0""/0,""/g; $csv->parse($_); my @a = $csv->fields(); $used{uc($a[9]||$a[10])}++; } close($db); } $csv = new Text::CSV_XS ({binary => 1}); while ( <> ) { s/0""/0,""/g; unless ( $csv->parse($_) ) { warn("Geen geldige invoer op regel $., file $ARGV\n"); next; } my %a; my @a = $csv->fields(); if ( @a == @debfieldnames ) { # debiteur @a{@debfieldnames} = @a; $a{debzk} ||= $a{debnr} if $ac5; if ( $a{debzk} ) { next unless $all || $used{$a{debzk}}; } elsif ( $debmap->{$a{naam}} ) { $a{debzk} = $debmap->{$a{naam}}; next unless $all || $used{$a{debzk}}; } else { warn("Geen relatiecode voor debiteur $a{naam} -- overgeslagen\n"); next; } $a{debzk} = "R_".$a{debzk} if $a{debzk} =~ /^\d+$/; print("relatie ", ($a{btw_nummer} ne "" && $a{btw_nummer} eq "0") ? "--btw=extra " : "", '"', $a{debzk}, '"', " ", '"', $a{naam}, '"', " ", "8000C", "\n"); next; } if ( @a == @crdfieldnames ) { # crediteur @a{@crdfieldnames} = @a; $a{crdzk} ||= $a{crdnr} if $ac5; if ( $a{crdzk} ) { next unless $all || $used{$a{crdzk}}; } elsif ( $crdmap->{$a{naam}} ) { $a{crdzk} = $crdmap->{$a{naam}}; next unless $all || $used{$a{crdzk}}; } else { warn("Geen relatiecode voor crediteur $a{naam} -- overgeslagen\n"); next; } $a{crdzk} = "R_".$a{crdzk} if $a{crdzk} =~ /^\d+$/; print("relatie ", ($a{btw_nummer} ne "" && $a{btw_nummer} eq "0") ? "--btw=extra " : "", '"', $a{crdzk}, '"', " ", '"', $a{naam}, '"', " ", "4990D", "\n"); next; } warn("Geen geldige debiteur/crediteur op regel $., file $ARGV\n"); } continue { close(ARGV) if eof; } exit 0; ################ Subroutines ################ sub app_options { my $help = 0; # handled locally my $ident = 0; # handled locally # Process options, if any. # Make sure defaults are set before returning! return unless @ARGV > 0; if ( !GetOptions( 'ac5' => \$ac5, 'all' => \$all, 'ident' => \$ident, 'verbose' => \$verbose, 'trace' => \$trace, 'help|?' => \$help, 'debug' => \$debug, ) or $help ) { app_usage(2); } app_ident() if $ident; } sub app_ident { print STDERR ("This is $my_package [$my_name $my_version]\n"); } sub app_usage { my ($exit) = @_; app_ident(); print STDERR <