6 use FS::UID qw(adminsuidsetup);
7 use FS::cust_main_county;
8 use FS::Record qw(qsearch qsearchs dbh);
9 use DateTime::Format::Natural;
13 use FS::cust_main::Search qw(smart_search);
15 use FS::did_order_item;
22 print "started time=".time."\n";
24 #### SET THESE! #################################
25 my $file = '/home/levinse/dids1.csv';
26 my $did_vendor_id = 1;
29 my $internal_diddb_exportnum = 2; # IMPORTANT: set this to the correct exportnum or everything will go in wrong into phone_avail
30 my %custname2num = (); # MyCust => 12345,
31 ################################################
36 # oh yeah this is fun when you can't Ctrl+C me
37 local $SIG{HUP} = 'IGNORE';
38 local $SIG{INT} = 'IGNORE';
39 local $SIG{QUIT} = 'IGNORE';
40 local $SIG{TERM} = 'IGNORE';
41 local $SIG{TSTP} = 'IGNORE';
42 local $SIG{PIPE} = 'IGNORE';
44 my $oldAutoCommit = $FS::UID::AutoCommit;
45 local $FS::UID::AutoCommit = 0;
48 my $min_date = 1262304000; # January 1st 2010
51 my %rate_center_abbrev = ();
54 # YOU CANNOT USE THE STATE/NPA/LATA OF A DID TO TRY TO FIND ITS MSA. IT HAS
55 # NOTHING IN COMMON WITH THE STATE OF THE MSA. THERE IS SIMPLY INSUFFICIENT
56 # DATA IN THE CSV FILE TO DETERMINE CANONICAL MSA WITHOUT THIS:
57 'Washington DC' => 47900,
58 'Fort Lauderdale' => 33100,
63 'Bloomington' => 14020,
70 # more hax upon hax (the above are unique, no issues)
71 'Portland OR' => 38900,
72 'Portland ME' => 38860,
78 # cache LATA and MSA tables in one query for performance
79 my @latas = qsearch('lata', {});
80 my %latas = map { $_->latanum => $_->description } @latas;
82 my @msas = qsearch('msa', {});
83 my %msas = map { $_->msanum => $_->description } @msas;
85 # now add in the brain-dead LATA hacks
86 $latas{636} = 'BRAINERD-FARGO ND';
87 $latas{920} = 'CONNECTICUT';
88 $latas{334} = 'AUBURN-HUNTINGTON IN';
89 $latas{232} = 'NORTHEAST - PA';
90 $latas{460} = 'SOUTHEAST FL GR-EA';
91 $latas{952} = 'TAMPA FLORIDA';
92 $latas{524} = 'KANSAS CITY';
94 my $parser = new DateTime::Format::Natural( 'time_zone' => 'local' );
96 my ($dt,$min,$max) = (shift,shift,shift);
97 my $epoch = $parser->parse_datetime($dt);
99 if ($parser->success && $epoch->epoch >= $min && $epoch->epoch <= $max);
100 fatal("invalid date $dt (min=$min, max=$max)");
104 my ($their,$our) = (shift,shift);
107 return 1 if $a eq $their;
108 return 1 if ($our =~ /^([\w\s]+)-/ && $1 eq $their);
114 $str =~ s/^\s+|\s+$//g;
122 my $did = trim($columns[0]);
123 my $npa = trim($columns[1]);
124 my $state = trim($columns[2]);
125 my $rate_center_abbrev = trim($columns[3]);
126 my $rate_center = trim($columns[4]);
127 my $customer = trim($columns[5]);
128 my $submitted = parsedt(trim($columns[7]),$min_date,$max_date);
130 my $ordernum = trim($columns[8]);
131 return if $ordernum eq 'Unknown';
133 my $confirmed = parsedt(trim($columns[9]),$submitted,$max_date);
135 # sometimes, we're in a non-Y2K-compliant bullshit format, differing from
136 # all the other dates. Other times, we randomly change formats multiple times
137 # in the middle of the file for absolutely no reason...wtf
138 my $received = trim($columns[10]);
139 if ( $received =~ /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/ ) {
140 $received = $2."/".$1."/20".$3;
141 } elsif ( $received !~ /^\d{2}\/\d{2}\/\d{4}$/ ) {
142 fatal("invalid received date $received");
144 if ( $ordernum == 300383 ) { # another hack due to bad data
145 $received = parsedt($received,1,$max_date)
147 $received = parsedt($received,$confirmed,$max_date);
150 my $latanum = trim($columns[12]);
151 my $latadesc = trim($columns[13]);
152 my $msadesc = trim($columns[14]);
154 fatal("invalid DID and/or NPA or NPA doesn't match DID")
155 unless ($did =~ /^(\d{3})\d{7}$/ && $npa == $1);
156 fatal("invalid state, order #, LATA #, or LATA description")
157 unless ($state =~ /^[A-Z]{2}$/ && ($ordernum =~ /^\d+$/ || $ordernum eq 'Test') # more hacks
158 && $latanum =~ /^\d{3}$/
159 && $latadesc =~ /^[\w\s\-]+$/);
164 fatal("no lata found for latanum $latanum") unless exists($latas{$latanum});
166 # unsurprisingly, our idea of a LATA name doesn't always match their idea
167 # of the same. Specifically, they randomly expand the state portion and
168 # abbreviate it arbitrarily
170 my $ourdesc = $latas{$latanum};
172 # strip off the fixed state abbreviation portion in ours
173 $ourdesc =~ s/ ..$//;
175 # strip off the variable state abbreviation (or full name) portion in theirs
176 $latadesc =~ s/\s\w+$// unless uc($ourdesc) eq uc($latadesc); # yeah...long story :(
178 fatal("their LATA description '$latadesc' doesn't match our LATA description '$ourdesc'")
179 unless uc($ourdesc) eq uc($latadesc);
186 # XXX: no idea what the MSA is for Danbury, so discard it for now and deal with it manually/later
187 $msadesc = '' if $msadesc eq 'Danbury';
190 $msadesc = 'Portland OR' if ($msadesc eq 'Portland' && $state eq 'OR');
191 $msadesc = 'Portland ME' if ($msadesc eq 'Portland' && $state eq 'ME');
193 # not everything in their file has a MSA
194 if ( $msadesc =~ /^[\w\s]+$/ ) {
196 # their idea of a MSA differs from our idea of it
197 if ( exists($msamap{$msadesc}) ) {
198 $msanum = $msamap{$msadesc};
201 my @msa = grep { msatest($msadesc,$_->description) } @msas;
202 fatal("multiple MSA matches for '$msadesc'") if(scalar(@msa) > 1);
203 $msanum = $msa[0]->msanum if scalar(@msa) == 1;
204 $msamap{$msadesc} = $msanum if $msanum != -1;
206 fatal("msa $msadesc not found") if $msanum == -1;
207 warn "$msadesc matched msanum $msanum for line $linenum\n" if $debug;
213 if ( exists $rate_center{$rate_center} ) {
214 fatal("rate center abbreviation for '$rate_center' doesn't exist or doesn't match '$rate_center_abbrev'")
215 unless ( exists $rate_center_abbrev{$rate_center} &&
216 $rate_center_abbrev{$rate_center} eq $rate_center_abbrev);
218 print "creating new rate center '$rate_center' '$rate_center_abbrev'\n";
219 my $rc = new FS::rate_center{ description => $rate_center };
220 my $error = $rc->insert;
221 fatal("can't insert rate center '$rate_center' '$rate_center_abbrev': $error")
223 $rate_center{$rate_center} = $rc->ratecenternum;
224 $rate_center_abbrev{$rate_center} = $rate_center_abbrev;
226 my $ratecenternum = $rate_center{$rate_center};
229 my $order = order($ordernum,$submitted,$confirmed,$received,$customer);
230 my $order_item = order_item($order,$npa,$latanum,$state,$msanum,$ratecenternum);
231 my $phone_avail = phone_avail($order,$state,$did,$rate_center,$latanum,$msanum);
232 provision($did,$customer,$phone_avail) if $customer ne 'Stock';
234 warn "Pass $linenum\n" if $debug;
237 print "Done $linenum time=$time\n" if ($linenum % 100 == 0);
241 my ($order,$state,$did,$rate_center,$latanum,$msanum)
242 = (shift,shift,shift,shift,shift,shift);
243 $did =~ /^(\d{3})(\d{3})(\d{4})$/;
248 exportnum => $internal_diddb_exportnum,
254 name => $rate_center,
255 rate_center_abbrev => $rate_center_abbrev{$rate_center},
256 ordernum => $order->ordernum,
259 $hash{'msanum'} = $msanum if $msanum != -1;
261 my $pa = new FS::phone_avail{ %hash };
262 my $error = $pa->insert;
263 fatal("can't insert phone_avail: $error") if $error;
269 my($order,$npa,$latanum,$state,$msanum,$ratecenternum)
270 = (shift,shift,shift,shift,shift,shift);
272 $msa{'msanum'} = $msanum if $msanum != -1;
274 my @order_item = $order->did_order_item;
275 foreach my $order_item ( @order_item ) {
276 if($order_item->npa == $npa
277 && $order_item->latanum == $latanum
278 && $order_item->state eq $state
279 && $order_item->ratecenternum == $ratecenternum
280 && (!$order_item->msanum || $order_item->msanum == $msanum) ) {
281 fatal("Multiple order items") if $oi;
287 $oi->quantity($oi->quantity+1);
288 my $error = $oi->replace;
289 fatal("can't replace order item: $error") if $error;
291 $oi = new FS::did_order_item{ ordernum => $order->ordernum,
296 ratecenternum => $ratecenternum,
298 my $error = $oi->insert;
299 fatal("can't insert order item: $error") if $error;
302 fatal("wtf2") unless $oi;
308 my($vendor_order_id,$submitted,$confirmed,$received,$customer)
309 = (shift,shift,shift,shift,shift);
312 if ( $customer ne 'Stock' ) {
313 if ( exists($custname2num{$customer}) ) {
314 $cust{'custnum'} = $custname2num{$customer};
316 print "new customer case for '$customer'\n";
317 my @cust_main = smart_search('search' => $customer);
318 fatal(scalar(@cust_main) . " customers found for $customer")
319 unless scalar(@cust_main) == 1;
320 my $cust_main = $cust_main[0];
322 $cust{'custnum'} = $cust_main->custnum;
323 $custname2num{$customer} = $cust_main->custnum;
324 $cust2pkg{$cust_main->custnum} = {};
326 my @pkgs = $cust_main->ncancelled_pkgs;
327 fatal("no packages") unless scalar(@pkgs);
329 foreach my $pkg ( @pkgs ) {
330 my @avail_part_svc = $pkg->available_part_svc;
332 foreach my $avail_part_svc ( @avail_part_svc ) {
333 if ($avail_part_svc->svcdb eq 'svc_phone') {
334 push @svcpart, $avail_part_svc->svcpart;
337 fatal("multiple svc_phone services") if scalar(@svcpart) > 1;
338 fatal("multiple packages with svc_phone services")
339 if (exists $cust2pkg{$cust_main->custnum}->{pkgnum}
340 && scalar(@svcpart));
341 if(scalar(@svcpart) == 1) {
342 $cust2pkg{$cust_main->custnum}->{pkgnum} = $pkg->pkgnum;
343 $cust2pkg{$cust_main->custnum}->{svcpart} = $svcpart[0];
348 unless (exists $cust2pkg{$cust_main->custnum}->{pkgnum}
349 && exists $cust2pkg{$cust_main->custnum}->{svcpart});
354 if( exists $did_order{$vendor_order_id} ) {
355 $o = $did_order{$vendor_order_id};
356 fatal("vendor order #$vendor_order_id - order data differs from one item to another")
357 unless ( ($o->submitted == $submitted
358 || $o->vendor_order_id == 293011) # yet another bad data hack
359 && $o->confirmed == $confirmed
360 && $o->received == $received);
361 fatal("customer mismatch for vendor order #$vendor_order_id")
362 unless ( ($o->custnum && $cust{'custnum'}
363 && $o->custnum == $cust{'custnum'})
364 || (!$o->custnum && !exists($cust{'custnum'})) );
366 $o = new FS::did_order{ vendornum => $did_vendor_id,
367 vendor_order_id => $vendor_order_id,
368 submitted => $submitted,
369 confirmed => $confirmed,
370 received => $received,
372 my $error = $o->insert;
373 fatal("can't insert vendor order #$vendor_order_id: $error") if $error;
374 $did_order{$vendor_order_id} = $o;
377 fatal("wtf") unless $o;
382 my($did,$customer,$phone_avail) = (shift,shift,shift);
384 local $FS::svc_Common::noexport_hack = 1;
385 # because of the above, we now need to do the internal did db
386 # export's job ourselves (set the svcnum for the DID in phone_avail)
388 fatal("customer not found") unless exists $cust2pkg{$custname2num{$customer}};
390 my $svc_phone = new FS::svc_phone({
391 pkgnum => $cust2pkg{$custname2num{$customer}}->{pkgnum},
392 svcpart => $cust2pkg{$custname2num{$customer}}->{svcpart},
397 # XXX: THIS LINE CAUSES PERFORMANCE TO DEGRADE AND THE SCRIPT NEVER FINISHES! WHY!?
398 # -unattaching the exports has no effect
399 # -after each successive call, the time taken to complete 100 rows becomes greater
400 # -commenting out this call results in a constant time taken to complete 100 rows
401 # -after 10K rows we get a random error that makes no sense
402 my $error = $svc_phone->insert;
404 fatal("can't insert svc_phone: $error") if $error;
406 #$phone_avail->svcnum($svc_phone->svcnum);
407 #$error = $phone_avail->replace;
408 #fatal("can't replace phone_avail: $error") if $error;
415 $dbh->rollback if $oldAutoCommit;
419 my $csv = new Text::CSV;
420 open (CSV, "<", $file) or die $!;
421 print "Starting main loop time=".time."\n";
423 if ( $linenum == 1 ) { # skip header
428 if( $skipto > $linenum ) { # debug stuff
433 last if $limit > 0 && $limit <= $linenum;
435 # kept getting these errors for many lines:
436 # "EIQ - Binary character inside quoted field, binary off"
437 $_ =~ s/[^[:ascii:]]//g;
439 if ($csv->parse($_)) {
440 my @columns = $csv->fields();
441 suffer($linenum,@columns);
443 my $err = $csv->error_diag . "(" . $csv->error_input . ")";
444 print "WARNING: failed to parse line $linenum: " . $csv->error_diag
445 . " (" . $csv->error_input . ")\n";
451 fatal("COMMIT ABORTED DUE TO DRY RUN BEING ON") if $dry;
452 $dbh->commit or die $dbh->errstr if $oldAutoCommit;