1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use SAP::Rfc; |
4 |
use Data::Dumper; |
5 |
use XML::Simple; |
6 |
|
7 |
my $config_file; |
8 |
|
9 |
if ($#ARGV < 0) { |
10 |
print "usage: $0 config.xml\n"; |
11 |
exit 1; |
12 |
} else { |
13 |
$config_file = $ARGV[0]; |
14 |
} |
15 |
|
16 |
$|++; |
17 |
|
18 |
my $config = XMLin($config_file); |
19 |
|
20 |
my $log = $config->{log} || die "config: no <log> defined"; |
21 |
|
22 |
# open log and redirect die to it... |
23 |
open(LOG,">> $log") || warn "open log $log: $!"; |
24 |
local $SIG{__DIE__} = sub { print LOG $_[0] ; die $_[0] }; |
25 |
|
26 |
# directory in which files will be left |
27 |
my $outdir = $config->{outdir} || die "config: on <outdir> defined"; |
28 |
|
29 |
# create output filename |
30 |
my ($ss,$mm,$hh,$dd,$mm,$yy) = localtime(time); |
31 |
$yy += 1900; |
32 |
$mm++; |
33 |
my $outfile = $config_file; |
34 |
$outfile =~ s/\.xml//i; |
35 |
$outfile =~ s/^.*\/([^\/]+)/$1/i; # basename |
36 |
$outfile .= "_${yy}-${mm}-${dd}_${hh}:${mm}:${ss}_$$"; |
37 |
|
38 |
my $rfc = new SAP::Rfc( |
39 |
ASHOST => $config->{sap}->{ashost}, |
40 |
USER => $config->{sap}->{user}, |
41 |
PASSWD => $config->{sap}->{passwd}, |
42 |
LANG => $config->{sap}->{lang}, |
43 |
CLIENT => $config->{sap}->{client}, |
44 |
SYSNR => $config->{sap}->{sysnr}, |
45 |
TRACE => $config->{sap}->{trace} |
46 |
) || die "new: $!"; |
47 |
|
48 |
$rfc->is_connected || die "FATAL: SAP not connected"; |
49 |
|
50 |
my $it = $rfc->discover($config->{sap}->{discover}) || die "FATAL: SAP discover: $!"; |
51 |
|
52 |
foreach my $p ($config->{sap}->{params}) { |
53 |
foreach my $p_name (keys %{$p}) { |
54 |
# print "$p_name --> ",$p->{$p_name},"\n"; |
55 |
$it->$p_name($p->{$p_name}); |
56 |
} |
57 |
} |
58 |
|
59 |
# override FILENAME with my name |
60 |
$it->FILENAME($outfile); |
61 |
print "Using file '$outfile'\n"; |
62 |
|
63 |
my @data; # array for order |
64 |
my $lines; # number of lines in order |
65 |
my $nr; # order number |
66 |
|
67 |
my $first = <STDIN>; |
68 |
push @data,$first; |
69 |
chomp $first; |
70 |
$first=~s/ |
71 |
//g; |
72 |
if ($first =~ m/\s+(\d+)\s+(\d+)\w+\s*$/) { |
73 |
($nr,$lines) = ($1,$2); |
74 |
print "Processing $lines lines order no. $nr\n"; |
75 |
} else { |
76 |
die "FATAL: can't find number of lines in first line ($first) of input file."; |
77 |
} |
78 |
|
79 |
# suck file in array |
80 |
while(<STDIN>) { |
81 |
push @data,$_; |
82 |
} |
83 |
|
84 |
# check integrity of file |
85 |
if ($#data != $lines) { |
86 |
print "FATAL: file integrity check failed: "; |
87 |
if ($#data > $lines) { |
88 |
print "too many ($#data > $lines)"; |
89 |
} else { |
90 |
print "too few ($#data < $lines)"; |
91 |
} |
92 |
print " lines...\n"; |
93 |
} else { |
94 |
print "File integrity checked, saving file...\n"; |
95 |
open(OUT, "> $outdir/$outfile") || die "FATAL: can't open '$outdir/$outfile': $!"; |
96 |
|
97 |
# dump order to file |
98 |
print OUT $first,@data; |
99 |
close(OUT); |
100 |
|
101 |
# cludge filename to nark.0004 |
102 |
rename "$outdir/$outfile", "$outdir/nark.0004"; |
103 |
$it->FILENAME("nark.0004"); |
104 |
|
105 |
# call SAP R/3 |
106 |
$rfc->callrfc( $it ); |
107 |
} |
108 |
|
109 |
$rfc->close(); |
110 |
|