SAP 02 XML Message Generator
#!/usr/bin/perl
#
# Skrypcik tworzy xml'e do SAP'a 02, plik wejsciowy w formacie .csv pola oddzielane znakiem ;
# $inputfile - plik wejsciowy
# $outputfile - naglowek nazwy pliku wyjsciowego, potem dodajemy date oraz numerek czesci
# $docnum - numer DOCNUM w SAP02 bez ostatniej cyfry, ktora nadaje kolejno dla plikow
# ktore tworzymy, w zaleznosci od ilosci rekordow
# $numrec - ilosc rekordow jakie ujmiemy w pojedynczym pliku SAP02
# %payment - wartosci oplaty za rozne rodzaje kart
#
############ Maciej Stopa
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
############
use strict;
my $inputfile = "input001.txt";
my $date = `date "+%Y%m%d%H%M%S"`; chomp($date);
my $outputfile = "PZKE02_SAP_".$date;
my $docnum = "99900000319001"; # do docnum dodaj 2 cyfry
my $numrec = 49;
my $rekord = 1;
my $filenum = 0;
my $tail = "</IDOC></ZIFSAP02>";
my %payment = ('DriverCard','0000018300','WorkshopCard','0000030500','Company','0000030500','Control','0000025620');
print "Plik wejsciowy: $inputfile\n";
print "Plik wyjsciowy: $outputfile\n\n";
open (INPUT, "< $inputfile") or die "Nie mozna otworzyc pliku $inputfile: $!\n";
my $header="<?xml version=\"1.0\" encoding=\"UTF-8\"?><ZIFSAP02><IDOC BEGIN=\"1\"><EDI_DC40 SEGMENT=\"1\"><TABNAM>EDI_DC40</TABNAM><MANDT>400</MANDT><DOCNUM>".$docnum."00"."</DOCNUM><DOCREL>620</DOCREL><STATUS>30</STATUS><DIRECT>1</DIRECT><OUTMOD>2</OUTMOD><IDOCTYP>ZIFSAP02</IDOCTYP><MESTYP>ZLIFSAP02</MESTYP><SNDPOR>SAPRA1</SNDPOR><SNDPRT>LS</SNDPRT><SNDPRN>RA1400</SNDPRN><RCVPOR>STC_DO</RCVPOR><RCVPRT>LS</RCVPRT><RCVPRN>STCLOGSYS</RCVPRN><CREDAT>20060813</CREDAT><CRETIM>142851</CRETIM><SERIAL>".$date."</SERIAL></EDI_DC40>";
open (OUTPUT, "> $outputfile.xml") or die "Nie mozna otworzyc pliku $outputfile.xml : $!\n";
print OUTPUT $header;
sleep(1);
while (<INPUT>) {
my @linia=split(/;|\n|\r/);
my $imie = @linia[3];
my $nazwisko = @linia[4];
my $cin = @linia[0];
my $lastupdt = "";
my $crn = "";
my $product = @linia[2];
my $appstatus = "";
my $nip = @linia[5];
print "Rekord $rekord:\nImie: ".$imie." Nazwisko: ".$nazwisko." NIP: ".$nip."\nCIN: ".$cin." CRN: ".$crn."\n";
print "typ: ".$product." status: ".$appstatus." ostatnia zmiana: ".$lastupdt."\n";
print OUTPUT "<ZSTCS02 SEGMENT=\"1\"><NIP>".$nip."</NIP><APPLNR>".$cin."</APPLNR><AMOUNT>".$payment{$product}."</AMOUNT></ZSTCS02>";
if ($rekord eq $numrec) {
$filenum++;
$rekord=0;
print OUTPUT $tail;
close (OUTPUT);
$date = `date "+%Y%m%d%H%M%S"`; chomp($date);
$outputfile = "PZKE02_SAP_".$date;
open (OUTPUT, "> $outputfile.xml") or die "Nie mozna otworzyc pliku $outputfile.xml : $!\n";
if ($filenum<10)
{
$header="<?xml version=\"1.0\" encoding=\"UTF-8\"?><ZIFSAP02><IDOC BEGIN=\"1\"><EDI_DC40 SEGMENT=\"1\"><TABNAM>EDI_DC40</TABNAM><MANDT>400</MANDT><DOCNUM>".$docnum."0".$filenum."</DOCNUM><DOCREL>620</DOCREL><STATUS>30</STATUS><DIRECT>1</DIRECT><OUTMOD>2</OUTMOD><IDOCTYP>ZIFSAP02</IDOCTYP><MESTYP>ZLIFSAP02</MESTYP><SNDPOR>SAPRA1</SNDPOR><SNDPRT>LS</SNDPRT><SNDPRN>RA1400</SNDPRN><RCVPOR>STC_DO</RCVPOR><RCVPRT>LS</RCVPRT><RCVPRN>STCLOGSYS</RCVPRN><CREDAT>20060813</CREDAT><CRETIM>142851</CRETIM><SERIAL>".$date."</SERIAL></EDI_DC40>";
}
else
{
$header="<?xml version=\"1.0\" encoding=\"UTF-8\"?><ZIFSAP02><IDOC BEGIN=\"1\"><EDI_DC40 SEGMENT=\"1\"><TABNAM>EDI_DC40</TABNAM><MANDT>400</MANDT><DOCNUM>".$docnum.$filenum."</DOCNUM><DOCREL>620</DOCREL><STATUS>30</STATUS><DIRECT>1</DIRECT><OUTMOD>2</OUTMOD><IDOCTYP>ZIFSAP02</IDOCTYP><MESTYP>ZLIFSAP02</MESTYP><SNDPOR>SAPRA1</SNDPOR><SNDPRT>LS</SNDPRT><SNDPRN>RA1400</SNDPRN><RCVPOR>STC_DO</RCVPOR><RCVPRT>LS</RCVPRT><RCVPRN>STCLOGSYS</RCVPRN><CREDAT>20060813</CREDAT><CRETIM>142851</CRETIM><SERIAL>".$date."</SERIAL></EDI_DC40>";
}
print OUTPUT $header;
sleep(1);
}
$rekord++;
};
print OUTPUT $tail;
close (OUTPUT);
close (INPUT);