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");
267 if ($href->{'slave_account_id'} =~ /^\s*(\S[\S ]*?)\s*$/) {
270 my $l = $href->{cbilling_cycle_login};
271 $l =~ /^\s*(\S[\S ]*?)\s*$/ && ($l = $1);
277 my ( $field, $hash ) = ( shift, shift );
278 $field = 'billing_'. $field
279 if $hash->{'billing_use'} eq 'Billing Address';
284 my ( $field, $hash ) = ( shift, shift );
285 $field = 'billing_'. $field
286 if $hash->{'billing_use'} eq 'Billing Address';
287 my $ac = ( $hash->{$field. '_area_code'}
288 && $hash->{$field. '_area_code'} =~ /^\d{3}$/ )
289 ? $hash->{$field. '_area_code'}. '-'
292 ( $hash->{$field} && $hash->{$field} =~ /^\d{3}-\d{4}$/)
293 ? $ac. $hash->{$field}
298 my ( $field, $hash ) = ( shift, shift );
299 $hash->{'billing_use'} eq 'Billing Address' ? $hash->{$field} : '';
303 my ( $field, $hash ) = ( shift, shift );
304 $hash->{'billing_use'} eq 'Billing Address' && $hash->{$field} =~ /^\d{3}-\d{4}$/
305 ? ( $hash->{$field. '_area_code'} =~ /^\d{3}$/
306 ? $hash->{$field. '_area_code'}. '-'
312 my %payby_map = ( '' => 'BILL',
314 'Credit Card' => 'CARD',
315 'Bank Debit' => 'CHEK',
316 'Virtual Check' => 'CHEK',
319 $payby_map{ shift->{billing_type} };
324 my $payby = payby($hash);
327 $hash->{'credit_card_number_1'}.
328 $hash->{'credit_card_number_2'}.
329 $hash->{'credit_card_number_3'}.
330 $hash->{'credit_card_number_4'};
332 $hash->{'bank_account_number'}.
334 $hash->{'bank_transit_number'};
335 if ($payby eq 'CARD') {
337 }elsif ($payby eq 'CHEK') {
339 }elsif ($payby eq 'BILL') {
340 $info = $hash->{'blanket_purchase_order_number'};
341 $bank =~ s/[^\d\@]//g;
343 if ( $bank =~ /^\d+\@\d{9}/) {
347 if ( $cc =~ /^\d{13,16}/ ) {
352 die "unexpected payby";
358 my ($object, $field) = (shift, shift);
359 my $value = $object->getfield($field);
360 $value =~ s/[^\w \,\.\-\']/ /g;
361 $object->setfield($field, $value);
365 my ($object, $field) = (shift, shift);
366 my $value = $object->getfield($field);
367 $value =~ s/[^\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]/ /g;
368 $object->setfield($field, $value);
372 my ($object, $field) = (shift, shift);
373 my $value = $object->getfield($field);
374 $value = 'TX' if $value eq 'TTX';
375 $object->setfield($field, $value);
379 my ($object, $field) = (shift, shift);
380 my $value = $object->getfield($field);
381 $value =~ s/[^-\d]//g;
382 $object->setfield($field, $value);
386 part_pkg => { 'stable' => 'product',
387 #part_pkg => { 'stable' => 'billcycle',
389 { 'pkg' => sub { my $href = shift;
390 $href->{'description'}
391 ? $href->{'description'}
392 : $href->{'product_id'};
394 'comment' => 'product_id',
395 'freq' => sub { pkg_freq(shift) },
396 'recur_fee'=> sub { my $href = shift;
397 my $price = ( pkg_freq($href)
398 ? $href->{'unit_price'}
401 $price =~ s/[^\d.]//g;
402 $price = 0 unless $price;
403 sprintf("%.2f", $price);
405 'setuptax' => sub { my $href = shift;
406 $href->{'taxable'} ? '' : 'Y';
408 'recurtax' => sub { my $href = shift;
409 $href->{'taxable'} ? '' : 'Y';
411 'plan' => sub { 'flat' },
412 'disabled' => sub { 'Y' },
413 'pkg_svc' => sub { my $href = shift;
415 if (pkg_freq($href)){
416 $result->{$legacy_ppp_svcpart} = 1;
417 $result->{$legacy_email_svcpart} =
418 $href->{emails_allowed}
419 if $href->{emails_allowed};
422 'primary_svc'=> sub { pkg_freq(shift)
423 ? $legacy_ppp_svcpart
428 'fixup' => sub { my $part_pkg = shift;
430 unless ($part_pkg->pkg =~ /^\s*(\S[\S ]*?)\s*$/) {
431 warn "no pkg: ". $part_pkg->pkg. " for ". $row->{product_id};
435 unless ($part_pkg->comment =~ /^\s*(\S[\S ]*?)\s*$/) {
436 warn "no comment: ". $part_pkg->comment. " for ". $row->{product_id};
440 return 1 if exists($package_cache{$1});
441 $package_cache{$1} = $part_pkg;
444 'wrapup' => sub { foreach (keys %package_cache) {
445 my $part_pkg = $package_cache{$_};
447 { map { my $v = $part_pkg->$_;
451 qw (setup_fee recur_fee)
454 $part_pkg->insert(options=>$options);
455 die "Error inserting package: $error"
457 $count++ unless $error;
461 part_referral => { 'stable' => 'cust',
463 { 'agentnum' => sub { $agentnum },
464 'referral' => sub { my $r = shift->{'referred_from'};
468 'fixup' => sub { 1 },
469 'wrapup' => sub { foreach (keys %referrals) {
471 new FS::part_referral( {
472 'agentnum' => $agentnum,
473 'referral' => $referrals{$_},
475 my $error = $part_referral->insert;
476 die "Error inserting referral: $error"
478 $count++ unless $error;
479 $referrals{$_} = $part_referral->refnum;
483 #svc_acct => { 'stable' => 'cust',
485 # { 'username' => 'login',
486 # '_password' => 'password',
487 # 'svcpart' => sub{ $legacy_ppp_svcpart },
488 # 'domsvc' => sub{ $legacy_domain_svcnum },
489 # 'status' => 'status',
491 # 'fixup' => sub { my $svc_acct = shift;
493 # my $id = $row->{'master_account'}
494 # ? 'slave:'. $row->{'customer_id'}
496 # my $status = $svc_acct->status;
497 # if ( $status ne 'Current'
498 # && $status ne 'On Hold' )
501 # str2time($row->{termination_date});
502 # warn "not creating (cancelled) svc_acct for " .
503 # $svc_acct->username. "\n";
506 # $susp{$id} = str2time($row->{hold_date})
507 # if $status eq 'On Hold';
508 # $adjo{$id} = str2time($row->{hold_date})
509 # if ( $status eq 'Current' &&
510 # $row->{hold_date} );
512 # str2time($row->{expiration_date});
515 # 'skey' => sub { my $svc_acct = shift;
517 # my $id = $row->{'master_account'}
518 # ? 'slave:'. $row->{'customer_id'}
522 cust_main => { 'stable' => 'cust',
524 { 'agentnum' => sub { $agentnum },
525 'agent_custid' => sub { my $id = shift->{'customer_number'};
526 if (exists($custid{$id})) {
528 $id. chr(64 + $custid{$id});
534 'last' => sub { b_or('last_name', shift) || ' ' },
535 'first' => sub { b_or('first_name', shift) || ' ' },
536 'stateid' => 'drivers_license_number',
537 'signupdate' => sub { str2time(shift->{'creation_date'}) },
538 'company' => sub { b_or('company_name', shift) },
539 'address1' => sub { b_or('address', shift) || ' ' },
540 'city' => sub { b_or('city', shift) || 'Paris' },
541 'state' => sub { uc(b_or('state', shift)) || 'TX' },
542 'zip' => sub { b_or('zip_code', shift) || '75460' },
543 'country' => sub { 'US' },
544 'daytime' => sub { p_or('phone', shift) },
545 'night' => sub { p_or('phone_alternate_1', shift) },
546 'fax' => sub { p_or('fax', shift) },
547 'ship_last' => sub { or_b('last_name', shift) },
548 'ship_first' => sub { or_b('first_name', shift) },
549 'ship_company' => sub { or_b('company_name', shift) },
550 'ship_address1'=> sub { or_b('address', shift) },
551 'ship_city' => sub { or_b('city', shift) },
552 'ship_state' => sub { uc(or_b('state', shift)) },
553 'ship_zip' => sub { or_b('zip_code', shift) },
554 'ship_daytime' => sub { or_p('phone', shift) },
555 'ship_fax' => sub { or_p('fax', shift) },
556 'tax' => sub { shift->{taxable} eq '' ? 'Y' : '' },
557 'refnum' => sub { $referrals{shift->{'referred_from'}}
561 'fixup' => sub { my $cust_main = shift;
564 my ($master_account, $customer_id, $login) =
566 $row->{'master_account'} =~ /^\s*(\S[\S ]*?)\s*$/
567 && ($master_account = $1);
568 $row->{'customer_id'} =~ /^\s*(\S[\S ]*?)\s*$/
569 && ($customer_id = $1);
570 $row->{'login'} =~ /^\s*(\S[\S ]*?)\s*$/
573 my ($first, $last, $company) =
575 $cust_main->first =~ /^\s*(\S[\S ]*?)\s*$/
577 $cust_main->last =~ /^\s*(\S[\S ]*?)\s*$/
579 $cust_main->company =~ /^\s*(\S[\S ]*?)\s*$/
582 unless ($first || $last || $company) {
583 warn "bogus entry: ". $row->{'login'};
587 my $id = $master_account
588 ? 'slave:'. $customer_id
591 my $status = $row->{status};
594 if ( $status ne 'Current'
595 && $status ne 'current'
596 && $status ne 'On Hold' )
600 str2time($row->{termination_date});
602 $susp{$id} = str2time($row->{hold_date})
603 if ($status eq 'On Hold' && !$cancelled);
604 $adjo{$id} = str2time($row->{hold_date})
605 if ( $status eq 'Current' && !$cancelled &&
608 str2time($row->{expiration_date})
612 $object_map{legacy_ppp}{$row->{'login'} };
613 unless( $cancelled || $svcnum || $status eq 'Pn Hold' ) {
614 warn "can't find svc_acct for legacy ppp ".
615 $row->{'login'}, "\n";
618 $object_map{svc_acct}{$id} = $svcnum
621 $master_map{$login} = $master_account
623 return 1 if $master_account;
624 $cust_main->ship_country('US')
625 if $cust_main->has_ship_address;
626 ut_name_fixup($cust_main, 'first');
627 ut_name_fixup($cust_main, 'company');
628 ut_name_fixup($cust_main, 'last');
630 my ($info, $payby) = payinfo($row);
631 $cust_main->payby($payby);
632 $cust_main->payinfo($info);
635 $row->{'credit_card_cvv_number'}
637 if ($payby eq 'CARD' or $payby eq 'DCRD');
639 $cust_main->paydate('20'.
640 $row->{'credit_card_exp_date_2'}. '-'.
642 $row->{'credit_card_exp_date_1'},
648 if ($payby eq 'CARD' or $payby eq 'DCRD');
651 $payname = $row->{'credit_card_name'}
652 if ($payby eq 'CARD' or $payby eq 'DCRD');
653 $payname = $row->{'bank_name'}
654 if ($payby eq 'CHEK' or $payby eq 'DCHK');
655 $cust_main->payname($payname);
658 $row->{'bank_account_to_debit'}
660 $row->{bank_account_to_debit}
663 if ($payby eq 'CHEK' or $payby eq 'DCHK');
665 $cust_main->payby('BILL')
666 if ($cust_main->payby eq 'CHEK' &&
667 $cust_main->payinfo !~ /^\d+\@\d{9}$/);
668 $cust_main->payby('BILL')
669 if ($cust_main->payby eq 'CARD' &&
670 $cust_main->payinfo =~ /^\s*$/);
671 $cust_main->paydate('2037-12-01')
672 if ($cust_main->payby eq 'BILL');
673 ut_text_fixup($cust_main, 'address1');
674 ut_state_fixup($cust_main, 'state');
675 ut_zip_fixup($cust_main, 'zip');
680 'skey' => sub { my $object = shift;
682 my $balance = sprintf("%.2f",
683 $href->{balance_due});
685 my $cust_credit = new FS::cust_credit({
686 'custnum' => $object->custnum,
687 'amount' => sprintf("%.2f", -$balance),
688 'reasonnum' => $previous_credit_reasonnum,
690 my $error = $cust_credit->insert;
691 warn "Error inserting credit for ",
692 $href->{'login'}, " : $error\n"
695 }elsif($balance > 0) {
696 my $error = $object->charge(
697 $balance, "Prior balance",
699 warn "Error inserting balance charge for ",
700 $href->{'login'}, " : $error\n"
707 #cust_main => { 'stable' => 'cust',
709 # { 'referred_by' => sub { my $href = shift;
710 # my $u = shift->{'login'};
711 # my $cn = $href->{'customer_number'};
713 # my $c = qsearch( 'cust_main',
714 # { 'custnum' => $cn }
715 # ) or die "can't fine customer $cn";
717 # my $s = qsearch( 'svc_acct',
718 # { 'username' => $u }
721 # my $n = $s->cust_svc
726 # $c->referral_custnum($n);
727 # my $error = $c->replace;
728 # die "error setting referral: $error"
733 # 'fixup' => sub { 1 },
735 cust_pkg => { 'stable' => 'billcycle',
737 { 'custnum' => sub { my $l = shift->{cbilling_cycle_login};
738 $l =~ /^\s*(\S[\S ]*?)\s*$/ && ($l = $1);
739 my $r = $object_map{'cust_main'}{$l};
741 my $m = $master_map{$l};
742 $r = $object_map{'cust_main'}{$m}
747 'pkgpart' => sub { my $href = shift;
748 my $p = $href->{product_id};
749 $p =~ /^\s*(\S[\S ]*?)\s*$/ && ($p = $1);
750 my $pkg = $package_cache{$p}
751 if $package_cache{$p};
754 $href->{month_credit} =~ /\s*(\S[\S ]*?)\s*$/ && ($month = $1);
755 $month = int(eval "$month + 0");
758 $href->{unit_price} =~ /\s*(\S[\S ]*?)\s*$/ && ($price = $1);
759 $price = eval "$price + 0";
763 unless $pkg->freq + 0 == $month;
765 if ($pkg && ($pkg->freq + 0)) {
767 $pkg->recur_fee =~ /\s*(\S[\S ]*?)\s*$/ && ($recur = $1);
768 $recur = eval "$recur + 0";
770 unless $recur == $price;
775 unless $pkg->setuptax
776 eq ($href->{taxable} ? '' : 'Y');
782 my $pkghref = { 'pkg' => ($href->{description} ? $href->{description} : $href->{product_id} ),
783 'comment' => $href->{product_id},
785 'setuptax' => ($href->{'taxable'} ? '' : 'Y'),
786 'recurtax' => ($href->{'taxable'} ? '' : 'Y'),
791 my @pkgs = qsearch('part_pkg', $pkghref);
792 my $recur = sprintf("%.2f", ($month ? $price : 0));
794 my %options = $_->options;
795 if ($options{recur_fee} eq $recur) {
801 $pkghref->{recur_fee} = $recur
807 $pkg_svc->{$legacy_ppp_svcpart} = 1;
808 $pkg_svc->{$legacy_email_svcpart} =
809 $href->{emails_allowed}
810 if $href->{emails_allowed};
812 $pkghref->{pkg_svc} = $pkg_svc;
813 $pkghref->{primary_svc}
815 ? $legacy_ppp_svcpart
818 $pkg = new FS::part_pkg $pkghref;
820 { map { my $v = $pkg->$_;
824 qw (setup_fee recur_fee)
827 $pkg->insert(options=>$options);
829 warn "Error inserting pkg ".
830 join(", ", map{"$_ => ". $pkg->get($_)} fields $pkg).
836 $pkg ? $pkg->pkgpart : '';
838 'setup' => sub { str2time(shift->{creation_date}) },
839 'bill' => sub { $bill{account_id(shift)}
840 #$bill{$href->{cbilling_cycle_login}};
842 'susp' => sub { $susp{account_id(shift)}
843 #$susp{$href->{cbilling_cycle_login}};
845 'adjo' => sub { $adjo{account_id(shift)}
846 #$adjo{$href->{cbilling_cycle_login}};
848 'cancel' => sub { $cancel{account_id(shift)}
849 #$cancel{$href->{cbilling_cycle_login}};
852 'fixup' => sub { my ($object, $row) = (shift,shift);
853 unless ($object->custnum) {
854 warn "can't find customer for ".
855 $row->{cbilling_cycle_login}. "\n";
858 unless ($object->pkgpart) {
859 warn "can't find package for ".
860 $row->{product_id}. "\n";
865 'skey' => sub { my $object = shift;
867 my $id = $href->{'billing_cycle_item_id'};
868 $id =~ /^\s*(\S[\S ]*?)\s*$/ && ($id = $1);
869 $cust_pkg_map{$id} = $object->pkgnum;
872 'wrapup' => sub { for my $id (keys %{$object_map{'cust_pkg'}}){
874 qsearchs( 'cust_svc', { 'svcnum' =>
875 $object_map{'svc_acct'}{$id} }
878 warn "can't find legacy ppp $id\n";
882 pkgnum($object_map{'cust_pkg'}{$id});
883 my $error = $cust_svc->replace;
884 warn "error linking legacy ppp $id: $error\n"
889 svc_acct => { 'stable' => 'email',
891 { 'username' => 'email_name',
892 '_password' => 'password',
893 'svcpart' => sub{ $legacy_email_svcpart },
894 'domsvc' => sub{ $legacy_domain_svcnum },
896 # 'fixup' => sub { my ($object, $row) = (shift,shift);
897 # my ($sd,$sm,$sy) = split '/',
898 # $row->{shut_off_date}
899 # if $row->{shut_off_date};
900 # if ($sd && $sm && $sy) {
901 # my ($cd, $cm, $cy) = (localtime)[3,4,5];
902 # $cy += 1900; $cm++;
903 # return 1 if $sy < $cy;
904 # return 1 if ($sy == $cy && $sm < $cm);
905 # return 1 if ($sy == $cy && $sm == $cm && $sd <= $cd);
907 # return 1 if $object_map{'cust_main'}{$object->username};
910 'fixup' => sub { my ($object, $row) = (shift,shift);
911 my ($sd,$sm,$sy) = split '/',
912 $row->{shut_off_date}
913 if $row->{shut_off_date};
914 if ($sd && $sm && $sy) {
915 my ($cd, $cm, $cy) = (localtime)[3,4,5];
917 return 1 if $sy < $cy;
918 return 1 if ($sy == $cy && $sm < $cm);
919 return 1 if ($sy == $cy && $sm == $cm && $sd <= $cd);
921 #return 1 if $object_map{'cust_main'}{$object->username};
924 $row->{email_name} =~ /^\s*(\S[\S ]*?)\s*$/
925 && ($email_name = $1);
928 $object_map{legacy_email}{$email_name}
931 warn "can't find svc_acct for legacy email ".
932 $row->{'email_name'}, "\n";
936 $object_map{svc_acct}{'email:'.$row->{'email_customer_id'}} = $svcnum;
939 # 'skey' => sub { my $object = shift;
941 # 'email:'. $href->{'email_customer_id'};
943 'wrapup' => sub { for my $id (keys %{$object_map{'svc_acct'}}){
944 next unless $id =~ /^email:(\d+)/;
947 qsearchs( 'cust_svc', { 'svcnum' =>
948 $object_map{'svc_acct'}{$id} }
951 warn "can't find legacy email $id\n";
955 if ($cust_svc->pkgnum) {
956 warn "service already linked for $id\n";
961 pkgnum($cust_pkg_map{$custid});
962 if ($cust_svc->pkgnum){
963 my $error = $cust_svc->replace;
964 warn "error linking legacy email $id: $error\n"
967 warn "can't find package for $id\n"
974 #my $s_dbh = DBI->connect($s_datasrc, $s_dbuser, $s_dbpass) or die $DBI::errstr;
977 my ($table, $href) = (shift @tables, shift @tables);
978 my $stable = $href->{'stable'} or die "No source table"; # good enough for now
979 my (%mapping) = %{$href->{'mapping'}};
980 my ($fixup) = $href->{'fixup'};
981 my ($wrapup) = $href->{'wrapup'};
982 my ($id) = $href->{'id'};
983 my ($skey) = $href->{'skey'};
985 #$d_dbh->do("delete from $table");
987 my $s_sth = $s_dbh->prepare("select count(*) from $stable");
988 $s_sth->execute or die $s_sth->errstr;
989 my $rowcount = $s_sth->fetchrow_arrayref->[0];
991 $s_sth = $s_dbh->prepare("select * from $stable");
992 $s_sth->execute or die $s_sth->errstr;
996 while ( $row = $s_sth->fetchrow_hashref ) {
997 my $class = "FS::$table";
999 warn sprintf("%.2f", 100*$count/$rowcount). "% of $table processed\n"
1000 unless( !$count || $count % 100 );
1002 my $object = new $class ( {
1003 map { $_ => ( ref($mapping{$_}) eq 'CODE'
1004 ? &{$mapping{$_}}($row)
1005 : $row->{$mapping{$_}}
1010 my $skip = &{$fixup}($object, $row)
1014 my $error = $object->insert;
1016 warn "Error inserting $table ".
1017 join(", ", map{"$_ => ". $object->get($_)} fields $object).
1022 my $key = (ref($skey) eq 'CODE') ? &{$skey}($object, $row)
1024 $object_map{$table}{$key} = $object->get($object->primary_key)
1033 print "$count/$rowcount of $table SUCCESSFULLY processed\n";
1037 # link to any uncancelled package on customer
1038 foreach my $username ( keys %{$object_map{'legacy_email'}} ) {
1039 my $cust_svc = qsearchs( 'cust_svc',
1040 { 'svcnum' => $object_map{legacy_email}{$username} }
1042 next unless $cust_svc;
1043 next if $cust_svc->pkgnum;
1045 my $custnum = $object_map{cust_main}{$username};
1047 my $master = $master_map{$username};
1048 $custnum = $object_map{'cust_main'}{$master}
1050 next unless $custnum;
1053 #my $extra_sql = ' AND 0 != (select freq from part_pkg where '.
1054 # 'cust_pkg.pkgpart = part_pkg.pkgpart )';
1055 my $extra_sql = " AND 'Prior balance' != (select pkg from part_pkg where ".
1056 "cust_pkg.pkgpart = part_pkg.pkgpart )";
1058 my @cust_pkg = qsearch( {
1059 'table' => 'cust_pkg',
1060 'hashref' => { 'custnum' => $custnum,
1063 'extra_sql' => $extra_sql,
1065 next unless scalar(@cust_pkg);
1067 $cust_svc->pkgnum($cust_pkg[0]->pkgnum);
1075 $d_dbh->commit or die $d_dbh->errstr;