HE:Vorstand/Workflows/csv2dtaus pph.pl

eval 'exec perl -S $0 ${1+"$@"}' # -*-Perl-*-
    if 0;
# File name: csv2dtaus_pph.pl
# Project: Pirate Hessen
#
# Task: CSV im Mitgliederformat nach DTAUS konvertieren, mithilfe des dtaus tools
#
# State: Quick Hack, bitte im Code nach Doku suchen. Sorry :-(
#
# DOC
# - Hier, bzw. Code
#
# Notes
# - Kann - aus mir unbekannten Gründen - nicht mit Quoted Fields umgehen ...
#
# TODO
# -
#
#      created: 2009-08-23
#      Version: $LastChangedRevision$
#  last change: $LastChangedDate$

# *** packes to use ***
use warnings;
use strict;
use Text::CSV;
use IO::File;

# --------------------------------------------------------------------------------

# *** Global settings ***

# Program Name
our $Program_Name="csv2dtaus_pph.pl";

# --------------------------------------------------------------------------------

# *** Global Variables ***

# Program Options
## my %opts = (
## 
##   # *** Settings from Command line
##   # true when no execution is requested (command line)
##   test => 0,
## 
##   # array
##   add_structure_frsw => ['a', 'b' ],
## 
##   # hash
##   add_aux => {'a' => "$ENV{'DT32_CUR_REV'}/verilog_nc/settings/ncvlog.user.args",
## 	      'b' => "$ENV{'DT32_CUR_REV'}/verilog_nc/settings/ncsim.startup" },
## 	     },
## );



# Abbuchungstexte
our $zweck = "Mitgliedsbeitrag Piratenpartei 2009";

# DTAUS control file
our $dtausfile ="dtaus_pph.ctl";
# DTA Import Datei
our $dtafile = "dtaus_pph.txt";
# DTA Import Datei
our $csv_in = "abbuchung.csv";

# --------------------------------------------------------------------------------


# *** subroutine declarations ***
# Main routine without any arguments
sub main ();

# Reads the command line options
# sub read_commandline();

# Print help message
# return: help string
# sub issue_help();

# Return version string from SVN revision & date
# sub versionstring();

# --------------------------------------------------------------------------------

# *** "Body" of the program ***
main();

# --------------------------------------------------------------------------------

# *** Main routine ***
sub main() {


  # *** Variables
  my $csv;
  my $file;
  my ($DOIT, $dummy1, $id, $last, $first, $kto, $blz, $bank, $amount, $dummy2, $dummy3, $dummy4);
  my $count = 0;

  # *** Read CSV
  $csv = Text::CSV->new({binary => 1,
                         allow_loose_quotes => 1,
                         allow_loose_escapes => 1
			});              # create a new object

  # Das CSV hat folgenden Aufbau:
  # "ABBUCH","Bezahlt","External Identifier (match to contact)","Last Name","First Name",\
  # "Kontonummer","Bankleitzahl","Kreditinstitut","Gesamtbeitrag","Einzugsermächtigung",,
  # 1. Spalte: Text "DOIT" heisst Abbuchen
  # Und die erste Zeile (Header) wird ignoriert


  # CSV öffnen und ans CSV-Objekt hängen


  # Und das ziehen wir uns jetzt rein:
  $csv->bind_columns (\$DOIT, \$dummy1, \$id, \$last, \$first, \$kto, \$blz, \$bank, \$amount,
		       \$dummy2,\$dummy3,\$dummy4);


  # Aufbereiten
  $file = new IO::File;
  $file->open("< $csv_in")
    or die "CSV-Eingangsdatei konnte nicht geöffnet werden: $csv_in\n";

  # *** DTAUS Control File schreiben
  die "Ausgabedatei existiert schon: $dtausfile" if -f $dtausfile;
  die "Ausgabedatei existiert schon: $dtafile" if -f $dtafile;
  open FH, ">$dtausfile"
    or die "Konnte ausgabedatei nicht öffnen: $dtausfile\n";

  # Header schreiben
  print FH <<EOF;
   BEGIN {

           Art   LK
           Name  Piratenpartei Hessen
           Konto 6004334400
           BLZ   43060967
   #       Ausfuehrung 30.08.2009
           Euro
         }

EOF


  # 1. Zeile verwerfen
  $csv->getline ($file) or die "Fehler beim CSV-Lesen: " . $csv->error_diag();



  while (1) {
    if (! $csv->getline ($file) ) {
      last if $csv->eof();
      die "Fehler beim CSV-Lesen: " . $csv->error_diag();
    }

    next if $DOIT ne "DOIT";
    $count++;   # Zähler
    print FH <<EOF;
         {
           Transaktion Einzug
           Name   $first $last
           Konto  $kto
           BLZ    $blz
           Betrag $amount
           Zweck  $zweck
           Text   Mitglied $id
         }

EOF
  }


  # ***DTAUS aufrufen
  print "dtaus -dtaus -c $dtausfile -d $dtafile\n";
  system("dtaus",  "-dtaus", "-c", $dtausfile, "-d", $dtafile) == 0
    or die "dtaus Aufruf gescheitert: $?";


  # *** Feddisch
  print "Fertig! \n";
  print "Anzahl Datensätze: $count\n";
  print "Dateien geschrieben:\n";
  print "   DTAUS control file: $dtausfile\n";
  print "     DTA Import Datei: $dtafile\n";
}



# --------------------------------------------------------------------------------