8 use FS::Record qw(qsearch qsearchs);
14 use FS::part_referral;
16 use FS::UID qw(adminsuidsetup);
22 my $s_dbname = 'DBI:Pg:dbname=optigoldimport';
23 my $s_dbuser = 'freeside';
25 my $extension = '.htm';
27 #my $d_dbuser = 'freeside';
28 my $d_dbuser = 'enet';
29 #my $d_dbuser = 'ivan';
30 #my $d_dbuser = 'freesideimport';
32 my $radius_file = 'radius.csv';
33 my $email_file = 'email.csv';
37 my $legacy_domain_svcnum = 1;
38 my $legacy_ppp_svcpart = 2;
39 my $legacy_email_svcpart = 3;
40 #my $legacy_broadband_svcpart = 4;
41 #my $legacy_broadband_svcpart = 14;
42 #my $previous_credit_reasonnum = 1;
43 my $previous_credit_reasonnum = 1220;
46 my $state = ''; #statemachine-ish
63 $s_dbh = DBI->connect($s_dbname, $s_dbuser, $s_dbpass,
66 'ShowErrorStatement' => 1
70 foreach ( qw ( billcycle cust email product ) ) {
73 print "parsing $sourcefile\n";
75 die "bad file name" unless $sourcefile =~ /^\w+$/;
81 if ( $c_sth = $s_dbh->prepare("SELECT COUNT(*) FROM $sourcefile") ) {
82 if ( $c_sth->execute ) {
83 if ( $c_sth->fetchrow_arrayref->[0] ) {
84 warn "already have data in $sourcefile table; skipping";
90 my $tp = new HTML::TableParser( \@args, { Decode => 1, Trim => 1, Chomp => 1 });
91 $tp->parse_file($sourcefile.$extension) or die "failed";
92 $s_dbh->commit or die $s_dbh->errstr;
98 warn "start\n" if $DEBUG;
100 die "unexpected state change" unless $state eq '';
101 die "unexpected table" unless $table_id eq '1';
106 warn "end\n" if $DEBUG;
107 my ($tbl_id, $line, $udata) = @_;
108 die "unexpected state change in header" unless $state eq 'rows';
109 die "unexpected table" unless $tbl_id eq '1';
114 warn "header\n" if $DEBUG;
115 my ($tbl_id, $line, $cols, $udata) = @_;
116 die "unexpected state change in header" unless $state eq 'table';
117 die "unexpected table" unless $tbl_id eq '1';
120 die "invalid column ". join (', ', grep { !/^[ \w\r]+$/ } @$cols)
121 if scalar(grep { !/^[ \w\r]+$/ } @$cols);
123 my $sql = "CREATE TABLE $sourcefile ( ".
124 join(', ', map { s/[ \r]/_/g; "$_ varchar NULL" } @$cols). " )";
125 $s_dbh->do($sql) or die "create table failed: ". $s_dbh->errstr;
126 $columncount = scalar( @$cols );
130 warn "row\n" if $DEBUG;
131 my ($tbl_id, $line, $cols, $udata) = @_;
132 die "unexpected state change in row" unless $state eq 'rows';
133 die "unexpected table" unless $tbl_id eq '1';
135 die "invalid number of columns: ". join(', ', @$cols)
136 unless (scalar(@$cols) == $columncount);
138 my $sql = "INSERT INTO $sourcefile VALUES(".
139 join(', ', map { s/\s*(\S[\S ]*?)\s*$/$1/; $s_dbh->quote($_) } @$cols). ")";
140 $s_dbh->do($sql) or die "insert failed: ". $s_dbh->errstr;
142 warn "row $rowcount\n" unless ($rowcount % 1000);
145 ## now svc_acct from CSV files
147 $FS::cust_main::import=1;
148 $FS::cust_pkg::disable_agentcheck = 1;
149 $FS::cust_svc::ignore_quantity = 1;
151 my (%master_map) = ();
152 my (%referrals) = ();
158 my (%cust_pkg_map) = ();
159 my (%object_map) = ();
160 my (%package_cache) = ();
163 my $d_dbh = adminsuidsetup $d_dbuser;
164 local $FS::UID::AutoCommit = 0;
166 my @import = ( { 'file' => $radius_file,
168 'fields' => [ qw( garbage1 username garbage2 garbage3 _password ) ],
172 foreach qw (garbage1 garbage2 garbage3);
173 $hash->{'svcpart'} = $legacy_ppp_svcpart;
174 $hash->{'domsvc'} = $legacy_domain_svcnum;
177 'mapkey' => 'legacy_ppp',
178 'skey' => 'username',
180 { 'file' => $email_file,
182 'fields' => [ qw( username null finger _password status garbage ) ],
186 # if $object_map{'legacy_ppp'}{$hash->{'username'}};
188 foreach qw (null status garbage);
189 $hash->{'svcpart'} = $legacy_email_svcpart;
190 $hash->{'domsvc'} = $legacy_domain_svcnum;
193 'mapkey' => 'legacy_email',
194 'skey' => 'username',
199 my $href = shift @import;
200 my $file = $href->{'file'} or die "No file specified";
201 my (@fields) = @{$href->{'fields'}};
202 my ($sep_char) = $href->{'sep_char'} || ';';
203 my ($fixup) = $href->{'fixup'};
204 my ($mapkey) = $href->{'mapkey'};
205 my ($skey) = $href->{'skey'};
208 my $csv = new Text::CSV_XS({'sep_char' => $sep_char});
209 open(FH, $file) or die "cannot open $file: $!";
212 while ( defined($line=<FH>) ) {
215 $line &= "\177" x length($line); # i hope this isn't really necessary
217 or die "cannot parse: " . $csv->error_input();
219 my @values = $csv->fields();
221 foreach my $field (@fields) {
222 $hash{$field} = shift @values;
226 warn "skipping malformed line: $line\n";
230 my $skip = &{$fixup}(\%hash)
234 my $svc_acct = new FS::svc_acct { %hash };
235 my $error = $svc_acct->insert;
241 if ($skey && $mapkey) {
242 my $key = (ref($skey) eq 'CODE') ? &{$skey}($svc_acct) : $hash{$skey};
243 $object_map{$mapkey}{$key} = $svc_acct->svcnum;
249 print "Imported $count service records\n";
256 my ( $href ) = ( shift );
258 $href->{'one_time_list'} =~ /^\s*(\S[\S ]*?)\s*$/ && ($once = $1);
261 : int(eval "$href->{'months_credit'} + 0");
262 # int(eval "$href->{'month_credit'} + 0");
266 my ( $field, $hash ) = ( shift, shift );
267 $field = 'billing_'. $field
268 if $hash->{'billing_use'} eq 'Billing Address';
273 my ( $field, $hash ) = ( shift, shift );
274 $field = 'billing_'. $field
275 if $hash->{'billing_use'} eq 'Billing Address';
276 my $ac = ( $hash->{$field. '_area_code'}
277 && $hash->{$field. '_area_code'} =~ /^\d{3}$/ )
278 ? $hash->{$field. '_area_code'}. '-'
281 ( $hash->{$field} && $hash->{$field} =~ /^\d{3}-\d{4}$/)
282 ? $ac. $hash->{$field}
287 my ( $field, $hash ) = ( shift, shift );
288 $hash->{'billing_use'} eq 'Billing Address' ? $hash->{$field} : '';
292 my ( $field, $hash ) = ( shift, shift );
293 $hash->{'billing_use'} eq 'Billing Address' && $hash->{$field} =~ /^\d{3}-\d{4}$/
294 ? ( $hash->{$field. '_area_code'} =~ /^\d{3}$/
295 ? $hash->{$field. '_area_code'}. '-'
301 my %payby_map = ( '' => 'BILL',
303 'Credit Card' => 'CARD',
304 'Bank Debit' => 'CHEK',
305 'Virtual Check' => 'CHEK',
308 $payby_map{ shift->{billing_type} };
313 my $payby = payby($hash);
316 $hash->{'credit_card_number_1'}.
317 $hash->{'credit_card_number_2'}.
318 $hash->{'credit_card_number_3'}.
319 $hash->{'credit_card_number_4'};
321 $hash->{'bank_account_number'}.
323 $hash->{'bank_transit_number'};
324 if ($payby eq 'CARD') {
326 }elsif ($payby eq 'CHEK') {
328 }elsif ($payby eq 'BILL') {
329 $info = $hash->{'blanket_purchase_order_number'};
330 $bank =~ s/[^\d\@]//g;
332 if ( $bank =~ /^\d+\@\d{9}/) {
336 if ( $cc =~ /^\d{13,16}/ ) {
341 die "unexpected payby";
347 my ($object, $field) = (shift, shift);
348 my $value = $object->getfield($field);
349 $value =~ s/[^\w \,\.\-\']/ /g;
350 $object->setfield($field, $value);
354 my ($object, $field) = (shift, shift);
355 my $value = $object->getfield($field);
356 $value =~ s/[^\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]/ /g;
357 $object->setfield($field, $value);
361 my ($object, $field) = (shift, shift);
362 my $value = $object->getfield($field);
363 $value = 'TX' if $value eq 'TTX';
364 $object->setfield($field, $value);
368 my ($object, $field) = (shift, shift);
369 my $value = $object->getfield($field);
370 $value =~ s/[^-\d]//g;
371 $object->setfield($field, $value);
375 part_pkg => { 'stable' => 'product',
376 #part_pkg => { 'stable' => 'billcycle',
378 { 'pkg' => sub { my $href = shift;
379 $href->{'description'}
380 ? $href->{'description'}
381 : $href->{'product_id'};
383 'comment' => 'product_id',
384 'freq' => sub { pkg_freq(shift) },
385 'recur' => sub { my $href = shift;
386 my $price = ( pkg_freq($href)
387 ? $href->{'unit_price'}
390 $price =~ s/[^\d.]//g;
391 $price = 0 unless $price;
392 sprintf("%.2f", $price);
394 'setuptax' => sub { my $href = shift;
395 $href->{'taxable'} ? '' : 'Y';
397 'recurtax' => sub { my $href = shift;
398 $href->{'taxable'} ? '' : 'Y';
400 'plan' => sub { 'flat' },
401 'disabled' => sub { 'Y' },
402 'pkg_svc' => sub { my $href = shift;
404 if (pkg_freq($href)){
405 $result->{$legacy_ppp_svcpart} = 1;
406 $result->{$legacy_email_svcpart} =
407 $href->{emails_allowed}
408 if $href->{emails_allowed};
411 'primary_svc'=> sub { pkg_freq(shift)
412 ? $legacy_ppp_svcpart
417 'fixup' => sub { my $part_pkg = shift;
419 unless ($part_pkg->pkg =~ /^\s*(\S[\S ]*?)\s*$/) {
420 warn "no pkg: ". $part_pkg->pkg. " for ". $row->{product_id};
424 unless ($part_pkg->comment =~ /^\s*(\S[\S ]*?)\s*$/) {
425 warn "no comment: ". $part_pkg->comment. " for ". $row->{product_id};
429 return 1 if exists($package_cache{$1});
430 $package_cache{$1} = $part_pkg;
433 'wrapup' => sub { foreach (keys %package_cache) {
434 my $part_pkg = $package_cache{$_};
436 { map { my $v = $part_pkg->$_;
443 $part_pkg->insert(options=>$options);
444 die "Error inserting package: $error"
446 $count++ unless $error;
450 part_referral => { 'stable' => 'cust',
452 { 'agentnum' => sub { $agentnum },
453 'referral' => sub { my $r = shift->{'referred_from'};
457 'fixup' => sub { 1 },
458 'wrapup' => sub { foreach (keys %referrals) {
460 new FS::part_referral( {
461 'agentnum' => $agentnum,
462 'referral' => $referrals{$_},
464 my $error = $part_referral->insert;
465 die "Error inserting referral: $error"
467 $count++ unless $error;
468 $referrals{$_} = $part_referral->refnum;
472 #svc_acct => { 'stable' => 'cust',
474 # { 'username' => 'login',
475 # '_password' => 'password',
476 # 'svcpart' => sub{ $legacy_ppp_svcpart },
477 # 'domsvc' => sub{ $legacy_domain_svcnum },
478 # 'status' => 'status',
480 # 'fixup' => sub { my $svc_acct = shift;
482 # my $id = $row->{'master_account'}
483 # ? 'slave:'. $row->{'customer_id'}
485 # my $status = $svc_acct->status;
486 # if ( $status ne 'Current'
487 # && $status ne 'On Hold' )
490 # str2time($row->{termination_date});
491 # warn "not creating (cancelled) svc_acct for " .
492 # $svc_acct->username. "\n";
495 # $susp{$id} = str2time($row->{hold_date})
496 # if $status eq 'On Hold';
497 # $adjo{$id} = str2time($row->{hold_date})
498 # if ( $status eq 'Current' &&
499 # $row->{hold_date} );
501 # str2time($row->{expiration_date});
504 # 'skey' => sub { my $svc_acct = shift;
506 # my $id = $row->{'master_account'}
507 # ? 'slave:'. $row->{'customer_id'}
511 cust_main => { 'stable' => 'cust',
513 { 'agentnum' => sub { $agentnum },
514 'agent_custid' => sub { my $id = shift->{'customer_number'};
515 if (exists($custid{$id})) {
517 $id. chr(64 + $custid{$id});
523 'last' => sub { b_or('last_name', shift) || ' ' },
524 'first' => sub { b_or('first_name', shift) || ' ' },
525 'stateid' => 'drivers_license_number',
526 'signupdate' => sub { str2time(shift->{'creation_date'}) },
527 'company' => sub { b_or('company_name', shift) },
528 'address1' => sub { b_or('address', shift) || ' ' },
529 'city' => sub { b_or('city', shift) || 'Paris' },
530 'state' => sub { uc(b_or('state', shift)) || 'TX' },
531 'zip' => sub { b_or('zip_code', shift) || '75460' },
532 'country' => sub { 'US' },
533 'daytime' => sub { p_or('phone', shift) },
534 'night' => sub { p_or('phone_alternate_1', shift) },
535 'fax' => sub { p_or('fax', shift) },
536 'ship_last' => sub { or_b('last_name', shift) },
537 'ship_first' => sub { or_b('first_name', shift) },
538 'ship_company' => sub { or_b('company_name', shift) },
539 'ship_address1'=> sub { or_b('address', shift) },
540 'ship_city' => sub { or_b('city', shift) },
541 'ship_state' => sub { uc(or_b('state', shift)) },
542 'ship_zip' => sub { or_b('zip_code', shift) },
543 'ship_daytime' => sub { or_p('phone', shift) },
544 'ship_fax' => sub { or_p('fax', shift) },
545 'tax' => sub { shift->{taxable} eq '' ? 'Y' : '' },
546 'refnum' => sub { $referrals{shift->{'referred_from'}}
550 'fixup' => sub { my $cust_main = shift;
553 my ($master_account, $customer_id, $login) =
555 $row->{'master_account'} =~ /^\s*(\S[\S ]*?)\s*$/
556 && ($master_account = $1);
557 $row->{'customer_id'} =~ /^\s*(\S[\S ]*?)\s*$/
558 && ($customer_id = $1);
559 $row->{'login'} =~ /^\s*(\S[\S ]*?)\s*$/
562 my $id = $master_account
563 ? 'slave:'. $customer_id
566 my $status = $row->{status};
569 if ( $status ne 'Current'
570 && $status ne 'On Hold' )
574 str2time($row->{termination_date});
576 $susp{$id} = str2time($row->{hold_date})
577 if ($status eq 'On Hold' && !$cancelled);
578 $adjo{$id} = str2time($row->{hold_date})
579 if ( $status eq 'Current' && !$cancelled &&
582 str2time($row->{expiration_date})
586 $object_map{legacy_ppp}{$row->{'login'} };
587 unless( $cancelled || $svcnum || $status eq 'Pn Hold' ) {
588 warn "can't find svc_acct for legacy ppp ".
589 $row->{'login'}, "\n";
592 $object_map{svc_acct}{$id} = $svcnum
595 $master_map{$login} = $master_account
597 return 1 if $master_account;
598 $cust_main->ship_country('US')
599 if $cust_main->has_ship_address;
600 ut_name_fixup($cust_main, 'first');
601 ut_name_fixup($cust_main, 'company');
602 ut_name_fixup($cust_main, 'last');
604 my ($info, $payby) = payinfo($row);
605 $cust_main->payby($payby);
606 $cust_main->payinfo($info);
609 $row->{'credit_card_cvv_number'}
611 if ($payby eq 'CARD' or $payby eq 'DCRD');
613 $cust_main->paydate('20'.
614 $row->{'credit_card_exp_date_2'}. '-'.
616 $row->{'credit_card_exp_date_1'},
622 if ($payby eq 'CARD' or $payby eq 'DCRD');
625 $payname = $row->{'credit_card_name'}
626 if ($payby eq 'CARD' or $payby eq 'DCRD');
627 $payname = $row->{'bank_name'}
628 if ($payby eq 'CHEK' or $payby eq 'DCHK');
629 $cust_main->payname($payname);
632 $row->{'bank_account_to_debit'}
634 $row->{bank_account_to_debit}
637 if ($payby eq 'CHEK' or $payby eq 'DCHK');
639 $cust_main->payby('BILL')
640 if ($cust_main->payby eq 'CHEK' &&
641 $cust_main->payinfo !~ /^\d+\@\d{9}$/);
642 $cust_main->payby('BILL')
643 if ($cust_main->payby eq 'CARD' &&
644 $cust_main->payinfo =~ /^\s*$/);
645 $cust_main->paydate('2037-12-01')
646 if ($cust_main->payby eq 'BILL');
647 ut_text_fixup($cust_main, 'address1');
648 ut_state_fixup($cust_main, 'state');
649 ut_zip_fixup($cust_main, 'zip');
654 'skey' => sub { my $object = shift;
656 my $balance = sprintf("%.2f",
657 $href->{balance_due});
659 my $cust_credit = new FS::cust_credit({
660 'custnum' => $object->custnum,
661 'amount' => sprintf("%.2f", -$balance),
662 'reasonnum' => $previous_credit_reasonnum,
664 my $error = $cust_credit->insert;
665 warn "Error inserting credit for ",
666 $href->{'login'}, " : $error\n"
669 }elsif($balance > 0) {
670 my $error = $object->charge(
671 $balance, "Prior balance",
673 warn "Error inserting balance charge for ",
674 $href->{'login'}, " : $error\n"
681 #cust_main => { 'stable' => 'cust',
683 # { 'referred_by' => sub { my $href = shift;
684 # my $u = shift->{'login'};
685 # my $cn = $href->{'customer_number'};
687 # my $c = qsearch( 'cust_main',
688 # { 'custnum' => $cn }
689 # ) or die "can't fine customer $cn";
691 # my $s = qsearch( 'svc_acct',
692 # { 'username' => $u }
695 # my $n = $s->cust_svc
700 # $c->referral_custnum($n);
701 # my $error = $c->replace;
702 # die "error setting referral: $error"
707 # 'fixup' => sub { 1 },
709 cust_pkg => { 'stable' => 'billcycle',
711 { 'custnum' => sub { my $l = shift->{cbilling_cycle_login};
712 $l =~ /^\s*(\S[\S ]*?)\s*$/ && ($l = $1);
713 my $r = $object_map{'cust_main'}{$l};
715 my $m = $master_map{$l};
716 $r = $object_map{'cust_main'}{$m}
721 'pkgpart' => sub { my $href = shift;
722 my $p = $href->{product_id};
723 $p =~ /^\s*(\S[\S ]*?)\s*$/ && ($p = $1);
724 my $pkg = $package_cache{$p}
725 if $package_cache{$p};
728 $href->{month_credit} =~ /\s*(\S[\S ]*?)\s*$/ && ($month = $1);
729 $month = int(eval "$month + 0");
732 $href->{unit_price} =~ /\s*(\S[\S ]*?)\s*$/ && ($price = $1);
733 $price = eval "$price + 0";
737 unless $pkg->freq + 0 == $month;
739 if ($pkg && ($pkg->freq + 0)) {
741 $pkg->recur =~ /\s*(\S[\S ]*?)\s*$/ && ($recur = $1);
742 $recur = eval "$recur + 0";
744 unless $recur == $price;
749 unless $pkg->setuptax
750 eq ($href->{taxable} ? '' : 'Y');
756 my $pkghref = { 'pkg' => ($href->{description} ? $href->{description} : $href->{product_id} ),
757 'comment' => $href->{product_id},
759 'setuptax' => ($href->{'taxable'} ? '' : 'Y'),
760 'recurtax' => ($href->{'taxable'} ? '' : 'Y'),
765 my @pkgs = qsearch('part_pkg', $pkghref);
766 my $recur = sprintf("%.2f", ($month ? $price : 0));
768 my %options = $_->options;
769 if ($options{recur} eq $recur) {
775 $pkghref->{recur} = $recur
781 $pkg_svc->{$legacy_ppp_svcpart} = 1;
782 $pkg_svc->{$legacy_email_svcpart} =
783 $href->{emails_allowed}
784 if $href->{emails_allowed};
786 $pkghref->{pkg_svc} = $pkg_svc;
787 $pkghref->{primary_svc}
789 ? $legacy_ppp_svcpart
792 $pkg = new FS::part_pkg $pkghref;
794 { map { my $v = $pkg->$_;
801 $pkg->insert(options=>$options);
803 warn "Error inserting pkg ".
804 join(", ", map{"$_ => ". $pkg->get($_)} fields $pkg).
810 $pkg ? $pkg->pkgpart : '';
812 'setup' => sub { str2time(shift->{creation_date}) },
813 'bill' => sub { my $href = shift;
814 my $id = $href->{'slave_account_id'}
815 ? 'slave:'. $href->{'slave_account_id'}
816 : $href->{'cbilling_cycle_login'};
818 #$bill{$href->{cbilling_cycle_login}};
820 'susp' => sub { my $href = shift;
821 my $id = $href->{'slave_account_id'}
822 ? 'slave:'. $href->{'slave_account_id'}
823 : $href->{'cbilling_cycle_login'};
825 #$susp{$href->{cbilling_cycle_login}};
827 'adjo' => sub { my $href = shift;
828 my $id = $href->{'slave_account_id'}
829 ? 'slave:'. $href->{'slave_account_id'}
830 : $href->{'cbilling_cycle_login'};
832 #$adjo{$href->{cbilling_cycle_login}};
834 'cancel' => sub { my $href = shift;
835 my $id = $href->{'slave_account_id'}
836 ? 'slave:'. $href->{'slave_account_id'}
837 : $href->{'cbilling_cycle_login'};
839 #$cancel{$href->{cbilling_cycle_login}};
842 'fixup' => sub { my ($object, $row) = (shift,shift);
843 unless ($object->custnum) {
844 warn "can't find customer for ".
845 $row->{cbilling_cycle_login}. "\n";
848 unless ($object->pkgpart) {
849 warn "can't find package for ".
850 $row->{product_id}. "\n";
855 'skey' => sub { my $object = shift;
857 my $id = $href->{'billing_cycle_item_id'};
858 $id =~ /^\s*(\S[\S ]*?)\s*$/ && ($id = $1);
859 $cust_pkg_map{$id} = $object->pkgnum;
860 if ($href->{'slave_account_id'} =~ /^\s*(\S[\S ]*?)\s*$/) {
863 my $l = $href->{cbilling_cycle_login};
864 $l =~ /^\s*(\S[\S ]*?)\s*$/ && ($l = $1);
868 'wrapup' => sub { for my $id (keys %{$object_map{'cust_pkg'}}){
870 qsearchs( 'cust_svc', { 'svcnum' =>
871 $object_map{'svc_acct'}{$id} }
874 warn "can't find legacy ppp $id\n";
878 pkgnum($object_map{'cust_pkg'}{$id});
879 my $error = $cust_svc->replace;
880 warn "error linking legacy ppp $id: $error\n"
885 svc_acct => { 'stable' => 'email',
887 { 'username' => 'email_name',
888 '_password' => 'password',
889 'svcpart' => sub{ $legacy_email_svcpart },
890 'domsvc' => sub{ $legacy_domain_svcnum },
892 # 'fixup' => sub { my ($object, $row) = (shift,shift);
893 # my ($sd,$sm,$sy) = split '/',
894 # $row->{shut_off_date}
895 # if $row->{shut_off_date};
896 # if ($sd && $sm && $sy) {
897 # my ($cd, $cm, $cy) = (localtime)[3,4,5];
898 # $cy += 1900; $cm++;
899 # return 1 if $sy < $cy;
900 # return 1 if ($sy == $cy && $sm < $cm);
901 # return 1 if ($sy == $cy && $sm == $cm && $sd <= $cd);
903 # return 1 if $object_map{'cust_main'}{$object->username};
906 'fixup' => sub { my ($object, $row) = (shift,shift);
907 my ($sd,$sm,$sy) = split '/',
908 $row->{shut_off_date}
909 if $row->{shut_off_date};
910 if ($sd && $sm && $sy) {
911 my ($cd, $cm, $cy) = (localtime)[3,4,5];
913 return 1 if $sy < $cy;
914 return 1 if ($sy == $cy && $sm < $cm);
915 return 1 if ($sy == $cy && $sm == $cm && $sd <= $cd);
917 #return 1 if $object_map{'cust_main'}{$object->username};
920 $row->{email_name} =~ /^\s*(\S[\S ]*?)\s*$/
921 && ($email_name = $1);
924 $object_map{legacy_email}{$email_name}
927 warn "can't find svc_acct for legacy email ".
928 $row->{'email_name'}, "\n";
932 $object_map{svc_acct}{'email:'.$row->{'email_customer_id'}} = $svcnum;
935 # 'skey' => sub { my $object = shift;
937 # 'email:'. $href->{'email_customer_id'};
939 'wrapup' => sub { for my $id (keys %{$object_map{'svc_acct'}}){
940 next unless $id =~ /^email:(\d+)/;
943 qsearchs( 'cust_svc', { 'svcnum' =>
944 $object_map{'svc_acct'}{$id} }
947 warn "can't find legacy email $id\n";
951 if ($cust_svc->pkgnum) {
952 warn "service already linked for $id\n";
957 pkgnum($cust_pkg_map{$custid});
958 if ($cust_svc->pkgnum){
959 my $error = $cust_svc->replace;
960 warn "error linking legacy email $id: $error\n"
963 warn "can't find package for $id\n"
970 #my $s_dbh = DBI->connect($s_datasrc, $s_dbuser, $s_dbpass) or die $DBI::errstr;
973 my ($table, $href) = (shift @tables, shift @tables);
974 my $stable = $href->{'stable'} or die "No source table"; # good enough for now
975 my (%mapping) = %{$href->{'mapping'}};
976 my ($fixup) = $href->{'fixup'};
977 my ($wrapup) = $href->{'wrapup'};
978 my ($id) = $href->{'id'};
979 my ($skey) = $href->{'skey'};
981 #$d_dbh->do("delete from $table");
983 my $s_sth = $s_dbh->prepare("select count(*) from $stable");
984 $s_sth->execute or die $s_sth->errstr;
985 my $rowcount = $s_sth->fetchrow_arrayref->[0];
987 $s_sth = $s_dbh->prepare("select * from $stable");
988 $s_sth->execute or die $s_sth->errstr;
992 while ( $row = $s_sth->fetchrow_hashref ) {
993 my $class = "FS::$table";
995 warn sprintf("%.2f", 100*$count/$rowcount). "% of $table processed\n"
996 unless( !$count || $count % 100 );
998 my $object = new $class ( {
999 map { $_ => ( ref($mapping{$_}) eq 'CODE'
1000 ? &{$mapping{$_}}($row)
1001 : $row->{$mapping{$_}}
1006 my $skip = &{$fixup}($object, $row)
1010 my $error = $object->insert;
1012 warn "Error inserting $table ".
1013 join(", ", map{"$_ => ". $object->get($_)} fields $object).
1018 my $key = (ref($skey) eq 'CODE') ? &{$skey}($object, $row)
1020 $object_map{$table}{$key} = $object->get($object->primary_key)
1029 print "$count/$rowcount of $table SUCCESSFULLY processed\n";
1033 # link to any uncancelled package on customer
1034 foreach my $username ( keys %{$object_map{'legacy_email'}} ) {
1035 my $cust_svc = qsearchs( 'cust_svc',
1036 { 'svcnum' => $object_map{legacy_email}{$username} }
1038 next unless $cust_svc;
1039 next if $cust_svc->pkgnum;
1041 my $custnum = $object_map{cust_main}{$username};
1043 my $master = $master_map{$username};
1044 $custnum = $object_map{'cust_main'}{$master}
1046 next unless $custnum;
1049 #my $extra_sql = ' AND 0 != (select freq from part_pkg where '.
1050 # 'cust_pkg.pkgpart = part_pkg.pkgpart )';
1051 my $extra_sql = " AND 'Prior balance' != (select pkg from part_pkg where ".
1052 "cust_pkg.pkgpart = part_pkg.pkgpart )";
1054 my @cust_pkg = qsearch( {
1055 'table' => 'cust_pkg',
1056 'hashref' => { 'custnum' => $custnum,
1059 'extra_sql' => $extra_sql,
1061 next unless scalar(@cust_pkg);
1063 $cust_svc->pkgnum($cust_pkg[0]->pkgnum);
1071 $d_dbh->commit or die $d_dbh->errstr;