-
Notifications
You must be signed in to change notification settings - Fork 0
/
import
368 lines (343 loc) · 10.4 KB
/
import
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/pkg/bin/perl
#
use DBI;
use DBD::mysql;
use Text::CSV;
use textfix;
#
$dbserver = '192.168.1.1';
$user = 'firstdirect';
$pass = 'FirstDirect01234';
$replace_records = 'YOUBETCHA';
open (TEXT,">output.txt");
open (COMMAND,">command.sql");
if ($#ARGV == -1) {
print "This is the Import Script for First Direct.\n\n";
print "Usage: ./import 'Name of Campaign' [import file name] [subcode]\n\n";
print "The campaign ID (IN QUOATATION MARKS) is required and will be used for all of the \n";
print "records imported into this campaign set.\n";
print "The import file name is optional and defaults to 'import.csv'.\n";
print "The subcode is optional and should only be used if the subcode ";
print "is not included in the original data.\n";
print "Exiting.\n";
print TEXT "This is the Import Script for ETS.\n\n";
print TEXT "Usage: ./import 'Name of Campaign' [import file name] [subcode] ['KEEPOLD|DELETEOLD'] ['PREPROC'] ['REFUSED']\n\n";
print TEXT "Exiting.\n";
exit(1);
}
if (length($cid = $ARGV[0]) == 0) {
$cid = 'TEST';
}
if (length($impfile = $ARGV[1]) == 0) {
$impfile = 'import.csv';
}
$keepold = 'N';
$clearcontactold = 'N';
if (length($subcode = $ARGV[2]) == 0) {
$subcode = 'T99';
}
$data_source = "DBI:mysql:database=services;host=$dbserver";
$dbh = DBI->connect($data_source, $user, $pass);
$statement = "SELECT * FROM import_headers";
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
while ($fieldhash = $sth->fetchrow_hashref()) {
$label = $fieldhash->{'label'};
$field = $fieldhash->{'field'};
$dblabel{$label} = $field;
# print "Label = $label: Field = $dblabel{$label}\n";
}
$statement = "SELECT clientnum FROM contact WHERE cid = 'X' and fname = 'RANDOM' and lname = 'CLIENTNUMBER'";
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
$clnthash = $sth->fetchrow_hashref();
$nxtclient = $clnthash->{clientnum};
$haveclient = 'N';
$data_source = "DBI:mysql:database=services;host=$dbserver";
$dbh = DBI->connect($data_source, $user, $pass);
$statement = "SELECT qid,rlabel FROM resp2 WHERE cid = '$cid' AND rother = 'N' ORDER BY rlabel";
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
while ($resphash = $sth->fetchrow_hashref()) {
$quest = "q_" . $resphash->{'qid'};
$resp = $resphash->{'rlabel'};
$valid_resp{$quest} .= "$resp,";
# print TEXT "Question = $quest\n" if ($debug);
# print TEXT "Response = $resp\n" if ($debug);
# print "Valid Responses for $quest are $valid_resp{$quest}\n" if ($debug);
}
print TEXT "Filename: $impfile\n";
open (INFILE,"<$impfile");
my $csv = Text::CSV->new;
$debug = 1;
$rec = 0;
$reccount = 0;
$haveclnt = 'N';
#
# Process the actual file.
#
while (<INFILE>) {
#
# We need to remove apostrophes and backslashes from all data
#
$inline = fixfield($_);
print TEXT "$inline\n" if ($debug);
if ($csv->parse($inline)) {
my @field = $csv->fields;
my $count = 0;
print TEXT "Processing CSV Fields\n" if ($debug);
#
# First, we process the line with the Field names on it. We know this is the
# first record since the fldname array entry for this column (count) is empty.
#
# If we have a "Q_..." field, we need to preload that field with with a
# questionnaire response.
#
# We use the field positions to set the outrec positions. These aren't
# really positions, so much as they are a hash based on the output fields.
#
for $column (@field) {
if (length($fldname[$count]) == 0) {
$column = lc(fixfield($column));
$fldname[$count] = $column;
if (substr($column,0,2) eq 'q_') {
$qfld_list{$column} = $count;
$redir[$count] = $column;
} else {
$redir[$count] = $dblabel{$column};
if ($redir[$count] eq 'clientnum') {
$haveclnt = 'Y';
}
}
print TEXT "Field: $redir[$count]\n" ;
} else {
#
# Other post process special cases
#
# No subcode was supplied on the program call, so we have to use the one out of
# the original records.
#
if (length($subcode) == 0 && $redir[$count] eq 'promo') {
$subcode = $column;
}
#
# Set the outrec field to the column data. The only exception is that the
# subcode is overridden by whatever we put in the subcode field.
#
$outrec{$redir[$count]} = $column;
}
$count++;
}
#
# All fields have now been set into the outrec variables (if that's
# appropriate).
#
#
print TEXT "Processing promo\n" if ($debug);
if (length($outrec{'promo'}) == 0) {
$outrec{'promo'} = $subcode;
}
#
# If we didn't find a clientnumber field, we need to make one up.
# Since they are our clientnumbers, they will always be 'N0' records.
# We store the clientnumber in the database in an easily manageable
# way (no N9 on the end) so that we don't have to jerk around with the
# it here before we use it.
#
# All client numbers in the system are at least nine digits long, zero
# filled. If there is a client number, we need to make sure its long
# enough.
#
if ($haveclnt eq 'N') {
$clientnum = $nxtclient++ . 'N0';
} elsif (length($outrec{'clientnum'}) < 9) {
$outrec{'clientnum'} = fillzero($outrec{'clientnum'},9);
$clientnum = $outrec{'clientnum'};
} else {
$clientnum = $outrec{'clientnum'};
}
#
# Let's clean up the US ZIP codes (they are either 5 or 9 digits).
#
print TEXT "Processing ZIP\n" if ($debug);
if (length($outrec{'country'}) == 0) {
$zip = numfield($outrec{'zip'});
while (length($zip) > 5 && length($zip) < 9) {
$zip = fillzero($zip,9);
}
while (length($zip) < 5) {
$zip = fillzero($zip,5);
}
} else {
$zip = $outrec{'zip'};
}
#
# Let's process presets so that they match up with responses from the
# questionnaire manager.
#
print TEXT "Processing Q_ Codes\n" if ($debug);
while (($key,$value) = each %qfld_list) {
print TEXT "Importing $key in column $value\n" if ($debug);
print TEXT " Valid_Resp is ",$valid_resp{$key},"\n" if ($debug);
my @values = split(',', $valid_resp{$key});
$field_ok = 0;
foreach $val (@values) {
print TEXT "Valid Value is $val\n";
print TEXT " Compared to ",$outrec{$key},"\n";
if (index($val,"$outrec{$key}") >= 0) {
print "Transformed " . $outrec{$key} . " to $val\n";
$outrec{$key} = $val;
$field_ok = 1;
last;
}
}
if ($field_ok == 0) {
print TEXT "Match not found for $key was $val\n";
$outrec{$key} = '';
}
}
if (length($outrec{'country'}) == 0) {
$zip = numfield($outrec{'zip'});
while (length($zip) > 5 && length($zip) < 9) {
$zip = fillzero($zip,9);
}
while (length($zip) < 5) {
$zip = fillzero($zip,5);
}
} else {
$zip = $outrec{'zip'};
}
#
# We always import a fullname.
#
if (length($fullname) < 2) {
$fullname = $fname . ' ' . $lname;
}
if ($fullname eq ' ') {
$fullname = '';
}
#
# If there's no name, there shouldn't be a phone number.
#
if (length($fullname) > 1) {
$phone = numfield($outrec{'phone'});
$fax = numfield($outrec{'fax'});
}
#
# If we are loading up an old CSV file as a list, we need to make sure the
# goes in as a 'CALL' status record. If we are loading a list that actually
# has 'reason' codes, then use those. Either way, 'reason' cannot be blank.
#
if (length($outrec{'reason'}) < 1) {
$outrec{'reason'} = "CALL";
}
#
#
#
$statement = "INSERT IGNORE INTO contact VALUES ('";
if ($replace_records eq 'YOUBETCHA') {
$statement = "REPLACE INTO contact VALUES ('";
}
$statement .= $clientnum;
$statement .= "','".$phone;
$statement .= "','".$fax;
$statement .= "','".$outrec{'cid'};
$statement .= "','".$outrec{'promo'};
$statement .= "','".$fname;
$statement .= "','".$lname;
$statement .= "','".$fullname;
$statement .= "','".$outrec{'title'};
$statement .= "','".$outrec{'orgname'};
$statement .= "','".$outrec{'department'};
$statement .= "','".$outrec{'address'};
$statement .= "','".$outrec{'city'};
$statement .= "','".$outrec{'st'};
$statement .= "','".$outrec{'country'};
$statement .= "','".$zip;
$statement .= "',''";
$statement .= "',':.$outrec{'reason'};
$statement .= ",'".$outrec{'email'};
$statement .= ")";
if ($cid eq 'TEST') {
print COMMAND "$statement;\n";
$phone = '';
}
if ($phone ne '') {
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
$reccount += 1;
print TEXT "$statement\n";
print COMMAND "$statement;\n";
if (keys(%qfld_list) > 0) {
print TEXT "\nUploading new questrep data \n";
while (($key,$value) = each %qfld_list) {
$qid = substr($key,2);
print TEXT "Processing Q_$qid\n";
if ($outrec{$key} ne '') {
$statement = "REPLACE INTO questrep SET ";
$statement .= "cid = '$cid', ";
$statement .= "qid = '$qid', ";
$statement .= "sid = '', ";
$statement .= "qresp = '$outrec{$key}', ";
$statement .= "qother = '', ";
$statement .= "phone = '$phone', ";
$statement .= "clientnum = '$clientnum' ";
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
print COMMAND "$statement;\n";
print TEXT "$statement\n";
}
}
}
} else {
print TEXT "$clientnum has no phone\n";
}
#
# Clear all of the variables for the next record
#
$outrec{'clientnum'} = '';
$clientnum = '';
$outrec{'promo'} = '';
$outrec{'fname'} = '';
$outrec{'lname'} = '';
$outrec{'fullname'} = '';
$fname = '';
$lname = '';
$fullname = '';
$outrec{'title'} = '';
$outrec{'orgname'} = '';
$outrec{'department'} = '';
$outrec{'address'} = '';
$outrec{'city'} = '';
$outrec{'st'} = '';
$outrec{'country'} = '';
$outrec{'email'} = '';
$outrec{'completed'} = '';
$outrec{'reason'} = 'CALL';
$phone = '';
$fax = '';
$zip = '';
if (keys(%qfld_list) > 0) {
while (($key,$value) = each %qfld_list) {
$outrec{$key} = '';
}
}
} else {
my $err = $csv->error_input;
print TEXT "parse() failed on argument: ", $err, "\n";
}
}
print TEXT "$reccount records processed $dncremoved do-not-calls removed.\n";
print "$reccount processed, $dncremoved + $refremovedremoved.\n";
if ($haveclnt eq 'N') {
$dbh->disconnect;
$data_source = "DBI:mysql:database=teleservices;host=$dbserver";
$dbh = DBI->connect($data_source, $user, $pass);
$nxtclient = $nxtclient + 1000 - ($nxtclient % 1000);
$statement = "UPDATE contact SET clientnum = '$nxtclient' WHERE cid = 'X'";
$sth = $dbh->prepare($statement);
$retvals = $sth->execute;
}
close(INFILE);
close(COMMAND);
$dbh->disconnect();