-
Notifications
You must be signed in to change notification settings - Fork 0
/
traitementCsv.pm
171 lines (139 loc) · 3.43 KB
/
traitementCsv.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use strict;
use utf8;
use Text::CSV; # sudo apt-get install libtext-csv-perl
use open qw( :encoding(utf8) :std );
use IO::File;
use formation;
use personne;
use MyLogger;
package Traitement;
my %code2file;
my $csv = Text::CSV->new({ sep_char => ',', binary => 1, auto_diag => 1, always_quote => 1});
my $univ ;
my $path ;
my $dateFile;
my $annee;
my $type;
my $tmp;
my %fileName2file;
sub parseFile {
$type = shift;
$univ = shift;
$dateFile = shift;
$annee = shift;
$tmp = shift; #le repertoire temporaire de travail;
%fileName2file =();
my $fileName = sprintf("%s_%s_%s.csv", $univ->prefix, $dateFile, $type);
$path = $univ->path;
my $fileNameLog = "${path}.log";
#$csv->sep_char($univ->sepChar());
DEBUG! "open $path/$fileName \n";
open (CSV, "<$path/$fileName") || FATAL! "$path/$fileName " . $!;
open (LOG, ">>$fileNameLog");
unless ( -d $tmp) {
mkdir $tmp, 0775;
}
my $isEtu = $type eq 'ETU';
$_ = <CSV>;
my $nbligne = 1;
while (<CSV>) {
$nbligne ++;
s/\"\;\"/\"\,\"/g; # on force les ,
if ($csv->parse($_) ){
# "eppn";"nomFamilleEtudiant";"prenomEtudiant";"courrielEtudiant";"matriculeEtudiant";"codesEtape"...
my $person;
if ($isEtu) {
$person = new Etudiant($univ, $csv->fields());
} else {
$person = new Staff($univ, $csv->fields());
}
if ($person) {
if (traitement($person)) {
print LOG "dans $fileName ligne $nbligne\n";
}
} else {
print LOG "$fileName rejet : $_\n";
}
} else {
DEBUG! "csv no parse line : ", $nbligne, " $fileName: $_\n";
}
}
foreach my $file (values %fileName2file) {
close $file;
}
close LOG;
return keys %fileName2file;
}
sub traitement {
my $personne = shift;
my $nberr = 0;
foreach my $codeEtap (@{$personne->codesEtape()}) {
my $etape = Etape::getByCodeEtap($codeEtap);
if ($etape) {
printInFormationFile($etape, $personne);
} else {
WARN! ("pas d'etape pour ce codeEtap : $codeEtap !");
print LOG "codeEtape erreur: $codeEtap !\n";
$nberr++;
}
}
return $nberr;
}
sub printInFormationFile {
my $etape = shift;
my $personne = shift;
my $file = getFile($etape, $personne->type);
if ($file) {
unless ($personne->inFile($file) ) {
$csv->print($file, $personne->info());
print $file "\n";
}
}
}
sub openFile {
my $typeFile = shift; # pour les staff ce sera formation_code
my $etape = shift;
my $type = shift;
if ($etape && $type) {
my $fileName = sprintf("%s_%s_%s_%s_%s.csv", $univ->id , $type, $typeFile, $annee, $dateFile);
my $file = $fileName2file{$fileName};
if ($file) {
return $file;
} else {
$file = new IO::File;
}
DEBUG! "write file $fileName\n";
open ($file , ">$tmp/$fileName") || FATAL! "$tmp/$fileName " . $!;
foreach my $entete (Personne->getEntete($type, $univ->id, $annee, $etape, $typeFile)) {
$csv->print($file, $entete);
print $file "\n";
}
$fileName2file{$fileName} = $file;
return $file;
}
return 0;
}
sub getFile {
my $etape = shift;
my $type = shift;
my $file;
my $haveFiles;
my $typeFile; # contient site_cohorte/formation .
my $formation = $etape->formation;
if ($type eq 'ETU') {
$haveFiles = $etape;
$typeFile = $etape->site . "_" . $etape->cohorte;
} else {
$haveFiles = $formation;
$typeFile = $etape->site . "_" . $formation->code;
}
$haveFiles->getFile($type);
unless ($file) {
$file = openFile($typeFile, $etape, $type);
if ($file) {
$haveFiles->setFile($file, $type);
}
}
return $file;
}
1;